-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* replaces deployment annotation encoding/decoding
- Loading branch information
Showing
44 changed files
with
2,219 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// +k8s:deepcopy-gen=package | ||
|
||
// Package v1 is the v1 version of the API. | ||
// +groupName=akash.network | ||
package v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package v1 | ||
|
||
import ( | ||
"reflect" | ||
|
||
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" | ||
apiextcs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
const ( | ||
CRDPlural string = "manifests" | ||
CRDGroup string = "akash.network" | ||
CRDVersion string = "v1" | ||
FullCRDName string = CRDPlural + "." + CRDGroup | ||
) | ||
|
||
var ( | ||
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) | ||
AddToScheme = SchemeBuilder.AddToScheme | ||
) | ||
|
||
// Create a Rest client with the new CRD Schema | ||
var SchemeGroupVersion = schema.GroupVersion{Group: CRDGroup, Version: CRDVersion} | ||
|
||
func addKnownTypes(scheme *runtime.Scheme) error { | ||
scheme.AddKnownTypes(SchemeGroupVersion, | ||
&Manifest{}, | ||
&ManifestList{}, | ||
) | ||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion) | ||
return nil | ||
} | ||
|
||
// Kind takes an unqualified kind and returns back a Group qualified GroupKind | ||
func Kind(kind string) schema.GroupKind { | ||
return SchemeGroupVersion.WithKind(kind).GroupKind() | ||
} | ||
|
||
// Resource takes an unqualified resource and returns a Group qualified GroupResource | ||
func Resource(resource string) schema.GroupResource { | ||
return SchemeGroupVersion.WithResource(resource).GroupResource() | ||
} | ||
|
||
// Create the CRD resource, ignore error if it already exists | ||
func CreateCRD(clientset apiextcs.Interface) error { | ||
crd := &apiextv1beta1.CustomResourceDefinition{ | ||
ObjectMeta: metav1.ObjectMeta{Name: FullCRDName}, | ||
Spec: apiextv1beta1.CustomResourceDefinitionSpec{ | ||
Group: CRDGroup, | ||
Version: CRDVersion, | ||
Scope: apiextv1beta1.NamespaceScoped, | ||
Names: apiextv1beta1.CustomResourceDefinitionNames{ | ||
Plural: CRDPlural, | ||
Kind: reflect.TypeOf(Manifest{}).Name(), | ||
}, | ||
}, | ||
} | ||
|
||
_, err := clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd) | ||
if err != nil && apierrors.IsAlreadyExists(err) { | ||
return nil | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package v1 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"github.com/gogo/protobuf/jsonpb" | ||
"github.com/ovrclk/akash/types" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// +genclient | ||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
type Manifest struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata"` | ||
Spec ManifestSpec | ||
Status ManifestStatus | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
type ManifestGroup struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// Placement profile name | ||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` | ||
// Service definitions | ||
Services []*ManifestService `protobuf:"bytes,2,rep,name=services" json:"services,omitempty"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
type LeaseID struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// deployment address | ||
Deployment []byte `protobuf:"bytes,1,opt,name=deployment,proto3,customtype=github.com/ovrclk/akash/types/base.Bytes" json:"deployment"` | ||
// deployment group sequence | ||
Group uint64 `protobuf:"varint,2,opt,name=group,proto3" json:"group,omitempty"` | ||
// order sequence | ||
Order uint64 `protobuf:"varint,3,opt,name=order,proto3" json:"order,omitempty"` | ||
// provider address | ||
Provider []byte `protobuf:"bytes,4,opt,name=provider,proto3,customtype=github.com/ovrclk/akash/types/base.Bytes" json:"provider"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
type ManifestSpec struct { | ||
metav1.TypeMeta `json:",inline"` | ||
LeaseID LeaseID `json:"lease_id"` | ||
ManifestGroup ManifestGroup `json:"manifest_group"` | ||
} | ||
|
||
type ManifestService struct { | ||
// Service name | ||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` | ||
// Docker image | ||
Image string `protobuf:"bytes,2,opt,name=image,proto3" json:"image,omitempty"` | ||
Args []string `protobuf:"bytes,3,rep,name=args" json:"args,omitempty"` | ||
Env []string `protobuf:"bytes,4,rep,name=env" json:"env,omitempty"` | ||
// Resource requirements | ||
Unit ResourceUnit `protobuf:"bytes,5,opt,name=unit" json:"unit"` | ||
// Number of instances | ||
Count uint32 `protobuf:"varint,6,opt,name=count,proto3" json:"count,omitempty"` | ||
// Overlay Network Links | ||
Expose []*ManifestServiceExpose `protobuf:"bytes,7,rep,name=expose" json:"expose,omitempty"` | ||
} | ||
|
||
type ManifestServiceExpose struct { | ||
Port uint32 `protobuf:"varint,1,opt,name=port,proto3" json:"port,omitempty"` | ||
ExternalPort uint32 `protobuf:"varint,2,opt,name=externalPort,proto3" json:"externalPort,omitempty"` | ||
Proto string `protobuf:"bytes,3,opt,name=proto,proto3" json:"proto,omitempty"` | ||
Service string `protobuf:"bytes,4,opt,name=service,proto3" json:"service,omitempty"` | ||
Global bool `protobuf:"varint,5,opt,name=global,proto3" json:"global,omitempty"` | ||
// accepted hostnames | ||
Hosts []string `protobuf:"bytes,6,rep,name=hosts" json:"hosts,omitempty"` | ||
} | ||
|
||
type ResourceUnit struct { | ||
CPU uint32 `protobuf:"varint,1,opt,name=CPU,proto3" json:"CPU,omitempty"` | ||
Memory uint32 `protobuf:"varint,2,opt,name=memory,proto3" json:"memory,omitempty"` | ||
Disk uint64 `protobuf:"varint,3,opt,name=disk,proto3" json:"disk,omitempty"` | ||
} | ||
|
||
type ManifestStatus struct { | ||
State string `json:"state,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
type ManifestList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata"` | ||
Items []Manifest `json:"items"` | ||
} | ||
|
||
func (m Manifest) ManifestGroup() *types.ManifestGroup { | ||
group, err := m.manifestGroup() | ||
if err != nil { | ||
panic("kube manifest manifestGroup error: " + err.Error()) | ||
} | ||
return group | ||
} | ||
|
||
func (m *Manifest) manifestGroup() (*types.ManifestGroup, error) { | ||
group := &types.ManifestGroup{} | ||
unmarshaler := &jsonpb.Unmarshaler{} | ||
buf, err := json.Marshal(m.Spec.ManifestGroup) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = unmarshaler.Unmarshal(bytes.NewReader(buf), group) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return group, nil | ||
} | ||
|
||
func (m Manifest) LeaseID() types.LeaseID { | ||
leaseID, err := m.leaseID() | ||
if err != nil { | ||
panic("kube manifest leaseID error: " + err.Error()) | ||
} | ||
return leaseID | ||
} | ||
|
||
func (m *Manifest) leaseID() (types.LeaseID, error) { | ||
leaseID := types.LeaseID{} | ||
buf, err := json.Marshal(m.Spec.LeaseID) | ||
if err != nil { | ||
return leaseID, err | ||
} | ||
unmarshaler := &jsonpb.Unmarshaler{} | ||
err = unmarshaler.Unmarshal(bytes.NewReader(buf), &leaseID) | ||
if err != nil { | ||
return leaseID, err | ||
} | ||
return leaseID, nil | ||
} | ||
|
||
func NewManifest(name string, lid *types.LeaseID, mgroup *types.ManifestGroup) (*Manifest, error) { | ||
buf := bytes.NewBuffer(nil) | ||
marshaler := &jsonpb.Marshaler{} | ||
manifestGroup := &ManifestGroup{} | ||
leaseID := &LeaseID{} | ||
|
||
err := marshaler.Marshal(buf, mgroup) | ||
if err != nil { | ||
return nil, err | ||
} | ||
json.Unmarshal(buf.Bytes(), manifestGroup) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buf.Reset() | ||
err = marshaler.Marshal(buf, lid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
json.Unmarshal(buf.Bytes(), leaseID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Manifest{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Manifest", | ||
APIVersion: "akash.network/v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
Spec: ManifestSpec{ | ||
ManifestGroup: *manifestGroup, | ||
LeaseID: *leaseID, | ||
}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ovrclk/akash/sdl" | ||
"github.com/ovrclk/akash/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestToProto(t *testing.T) { | ||
lease := testutil.Lease(testutil.Address(t), testutil.Address(t), 1, 2, 3) | ||
|
||
sdl, err := sdl.ReadFile("../../../../_run/kube/deployment.yml") | ||
require.NoError(t, err) | ||
|
||
mani, err := sdl.Manifest() | ||
require.NoError(t, err) | ||
|
||
_, err = NewManifest("name", &lease.LeaseID, mani.Groups[0]) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestFromProto(t *testing.T) { | ||
lease := testutil.Lease(testutil.Address(t), testutil.Address(t), 1, 2, 3) | ||
sdl, err := sdl.ReadFile("../../../../_run/kube/deployment.yml") | ||
require.NoError(t, err) | ||
|
||
mani, err := sdl.Manifest() | ||
require.NoError(t, err) | ||
|
||
kubeManifest, err := NewManifest("name", &lease.LeaseID, mani.Groups[0]) | ||
assert.NoError(t, err) | ||
|
||
fromKube := kubeManifest.ManifestGroup() | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, mani.Groups[0].Name, fromKube.Name) | ||
} |
Oops, something went wrong.