diff --git a/operators/constellation-node-operator/PROJECT b/operators/constellation-node-operator/PROJECT index bd3fad03f..8e01baef5 100644 --- a/operators/constellation-node-operator/PROJECT +++ b/operators/constellation-node-operator/PROJECT @@ -6,4 +6,13 @@ plugins: scorecard.sdk.operatorframework.io/v2: {} projectName: constellation-node-operator repo: github.com/edgelesssys/constellation/operators/constellation-node-operator +resources: +- api: + crdVersion: v1 + controller: true + domain: edgeless.systems + group: update + kind: NodeImage + path: github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1 + version: v1alpha1 version: "3" diff --git a/operators/constellation-node-operator/api/v1alpha1/groupversion_info.go b/operators/constellation-node-operator/api/v1alpha1/groupversion_info.go new file mode 100644 index 000000000..53940e31f --- /dev/null +++ b/operators/constellation-node-operator/api/v1alpha1/groupversion_info.go @@ -0,0 +1,21 @@ + +// Package v1alpha1 contains API Schema definitions for the update v1alpha1 API group +//+kubebuilder:object:generate=true +//+groupName=update.edgeless.systems +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/scheme" +) + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: "update.edgeless.systems", Version: "v1alpha1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/operators/constellation-node-operator/api/v1alpha1/nodeimage_types.go b/operators/constellation-node-operator/api/v1alpha1/nodeimage_types.go new file mode 100644 index 000000000..935b9d864 --- /dev/null +++ b/operators/constellation-node-operator/api/v1alpha1/nodeimage_types.go @@ -0,0 +1,50 @@ + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// NodeImageSpec defines the desired state of NodeImage +type NodeImageSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "make" to regenerate code after modifying this file + + // Foo is an example field of NodeImage. Edit nodeimage_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// NodeImageStatus defines the observed state of NodeImage +type NodeImageStatus struct { + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + // Important: Run "make" to regenerate code after modifying this file +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status +//+kubebuilder:resource:scope=Cluster + +// NodeImage is the Schema for the nodeimages API +type NodeImage struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec NodeImageSpec `json:"spec,omitempty"` + Status NodeImageStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// NodeImageList contains a list of NodeImage +type NodeImageList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []NodeImage `json:"items"` +} + +func init() { + SchemeBuilder.Register(&NodeImage{}, &NodeImageList{}) +} diff --git a/operators/constellation-node-operator/api/v1alpha1/zz_generated.deepcopy.go b/operators/constellation-node-operator/api/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 000000000..7b024b1c9 --- /dev/null +++ b/operators/constellation-node-operator/api/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,100 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NodeImage) DeepCopyInto(out *NodeImage) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeImage. +func (in *NodeImage) DeepCopy() *NodeImage { + if in == nil { + return nil + } + out := new(NodeImage) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *NodeImage) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NodeImageList) DeepCopyInto(out *NodeImageList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]NodeImage, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeImageList. +func (in *NodeImageList) DeepCopy() *NodeImageList { + if in == nil { + return nil + } + out := new(NodeImageList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *NodeImageList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NodeImageSpec) DeepCopyInto(out *NodeImageSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeImageSpec. +func (in *NodeImageSpec) DeepCopy() *NodeImageSpec { + if in == nil { + return nil + } + out := new(NodeImageSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NodeImageStatus) DeepCopyInto(out *NodeImageStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeImageStatus. +func (in *NodeImageStatus) DeepCopy() *NodeImageStatus { + if in == nil { + return nil + } + out := new(NodeImageStatus) + in.DeepCopyInto(out) + return out +} diff --git a/operators/constellation-node-operator/config/crd/kustomization.yaml b/operators/constellation-node-operator/config/crd/kustomization.yaml new file mode 100644 index 000000000..911b6fd58 --- /dev/null +++ b/operators/constellation-node-operator/config/crd/kustomization.yaml @@ -0,0 +1,21 @@ +# This kustomization.yaml is not intended to be run by itself, +# since it depends on service name and namespace that are out of this kustomize package. +# It should be run by config/default +resources: +- bases/update.edgeless.systems_nodeimages.yaml +#+kubebuilder:scaffold:crdkustomizeresource + +patchesStrategicMerge: +# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix. +# patches here are for enabling the conversion webhook for each CRD +#- patches/webhook_in_nodeimages.yaml +#+kubebuilder:scaffold:crdkustomizewebhookpatch + +# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. +# patches here are for enabling the CA injection for each CRD +#- patches/cainjection_in_nodeimages.yaml +#+kubebuilder:scaffold:crdkustomizecainjectionpatch + +# the following config is for teaching kustomize how to do kustomization for CRDs. +configurations: +- kustomizeconfig.yaml diff --git a/operators/constellation-node-operator/config/crd/kustomizeconfig.yaml b/operators/constellation-node-operator/config/crd/kustomizeconfig.yaml new file mode 100644 index 000000000..ec5c150a9 --- /dev/null +++ b/operators/constellation-node-operator/config/crd/kustomizeconfig.yaml @@ -0,0 +1,19 @@ +# This file is for teaching kustomize how to substitute name and namespace reference in CRD +nameReference: +- kind: Service + version: v1 + fieldSpecs: + - kind: CustomResourceDefinition + version: v1 + group: apiextensions.k8s.io + path: spec/conversion/webhook/clientConfig/service/name + +namespace: +- kind: CustomResourceDefinition + version: v1 + group: apiextensions.k8s.io + path: spec/conversion/webhook/clientConfig/service/namespace + create: false + +varReference: +- path: metadata/annotations diff --git a/operators/constellation-node-operator/config/crd/patches/cainjection_in_nodeimages.yaml b/operators/constellation-node-operator/config/crd/patches/cainjection_in_nodeimages.yaml new file mode 100644 index 000000000..8747c1369 --- /dev/null +++ b/operators/constellation-node-operator/config/crd/patches/cainjection_in_nodeimages.yaml @@ -0,0 +1,7 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME) + name: nodeimages.update.edgeless.systems diff --git a/operators/constellation-node-operator/config/crd/patches/webhook_in_nodeimages.yaml b/operators/constellation-node-operator/config/crd/patches/webhook_in_nodeimages.yaml new file mode 100644 index 000000000..9cb348786 --- /dev/null +++ b/operators/constellation-node-operator/config/crd/patches/webhook_in_nodeimages.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: nodeimages.update.edgeless.systems +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert + conversionReviewVersions: + - v1 diff --git a/operators/constellation-node-operator/config/rbac/nodeimage_editor_role.yaml b/operators/constellation-node-operator/config/rbac/nodeimage_editor_role.yaml new file mode 100644 index 000000000..46f91c005 --- /dev/null +++ b/operators/constellation-node-operator/config/rbac/nodeimage_editor_role.yaml @@ -0,0 +1,24 @@ +# permissions for end users to edit nodeimages. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: nodeimage-editor-role +rules: +- apiGroups: + - update.edgeless.systems + resources: + - nodeimages + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - update.edgeless.systems + resources: + - nodeimages/status + verbs: + - get diff --git a/operators/constellation-node-operator/config/rbac/nodeimage_viewer_role.yaml b/operators/constellation-node-operator/config/rbac/nodeimage_viewer_role.yaml new file mode 100644 index 000000000..85b1d5a6d --- /dev/null +++ b/operators/constellation-node-operator/config/rbac/nodeimage_viewer_role.yaml @@ -0,0 +1,20 @@ +# permissions for end users to view nodeimages. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: nodeimage-viewer-role +rules: +- apiGroups: + - update.edgeless.systems + resources: + - nodeimages + verbs: + - get + - list + - watch +- apiGroups: + - update.edgeless.systems + resources: + - nodeimages/status + verbs: + - get diff --git a/operators/constellation-node-operator/config/samples/kustomization.yaml b/operators/constellation-node-operator/config/samples/kustomization.yaml new file mode 100644 index 000000000..9fb855b33 --- /dev/null +++ b/operators/constellation-node-operator/config/samples/kustomization.yaml @@ -0,0 +1,4 @@ +## Append samples you want in your CSV to this file as resources ## +resources: +- update_v1alpha1_nodeimage.yaml +#+kubebuilder:scaffold:manifestskustomizesamples diff --git a/operators/constellation-node-operator/config/samples/update_v1alpha1_nodeimage.yaml b/operators/constellation-node-operator/config/samples/update_v1alpha1_nodeimage.yaml new file mode 100644 index 000000000..7a3b5b6d1 --- /dev/null +++ b/operators/constellation-node-operator/config/samples/update_v1alpha1_nodeimage.yaml @@ -0,0 +1,6 @@ +apiVersion: update.edgeless.systems/v1alpha1 +kind: NodeImage +metadata: + name: nodeimage-sample +spec: + # TODO(user): Add fields here diff --git a/operators/constellation-node-operator/controllers/nodeimage_controller.go b/operators/constellation-node-operator/controllers/nodeimage_controller.go new file mode 100644 index 000000000..7733001fb --- /dev/null +++ b/operators/constellation-node-operator/controllers/nodeimage_controller.go @@ -0,0 +1,47 @@ + +package controllers + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + updatev1alpha1 "github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1" +) + +// NodeImageReconciler reconciles a NodeImage object +type NodeImageReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeimages,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeimages/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=update.edgeless.systems,resources=nodeimages/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the NodeImage object against the actual cluster state, and then +// perform operations to make the cluster state reflect the state specified by +// the user. +// +// For more details, check Reconcile and its Result here: +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.2/pkg/reconcile +func (r *NodeImageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *NodeImageReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&updatev1alpha1.NodeImage{}). + Complete(r) +} diff --git a/operators/constellation-node-operator/controllers/suite_test.go b/operators/constellation-node-operator/controllers/suite_test.go new file mode 100644 index 000000000..ebf67559b --- /dev/null +++ b/operators/constellation-node-operator/controllers/suite_test.go @@ -0,0 +1,67 @@ + +package controllers + +import ( + "path/filepath" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/envtest/printer" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + updatev1alpha1 "github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1" + //+kubebuilder:scaffold:imports +) + +// These tests use Ginkgo (BDD-style Go testing framework). Refer to +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. + +var cfg *rest.Config +var k8sClient client.Client +var testEnv *envtest.Environment + +func TestAPIs(t *testing.T) { + RegisterFailHandler(Fail) + + RunSpecsWithDefaultAndCustomReporters(t, + "Controller Suite", + []Reporter{printer.NewlineReporter{}}) +} + +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + + By("bootstrapping test environment") + testEnv = &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, + ErrorIfCRDPathMissing: true, + } + + var err error + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + err = updatev1alpha1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + + //+kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) + +}, 60) + +var _ = AfterSuite(func() { + By("tearing down the test environment") + err := testEnv.Stop() + Expect(err).NotTo(HaveOccurred()) +}) diff --git a/operators/constellation-node-operator/go.mod b/operators/constellation-node-operator/go.mod index 144a497eb..3cfafca0d 100644 --- a/operators/constellation-node-operator/go.mod +++ b/operators/constellation-node-operator/go.mod @@ -3,6 +3,8 @@ module github.com/edgelesssys/constellation/operators/constellation-node-operato go 1.17 require ( + github.com/onsi/ginkgo v1.16.5 + github.com/onsi/gomega v1.17.0 k8s.io/apimachinery v0.23.5 k8s.io/client-go v0.23.5 sigs.k8s.io/controller-runtime v0.11.2 @@ -36,6 +38,7 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/nxadm/tail v1.4.8 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.11.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect @@ -56,6 +59,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect k8s.io/api v0.23.5 // indirect diff --git a/operators/constellation-node-operator/main.go b/operators/constellation-node-operator/main.go index 146bd1b60..5243dee02 100644 --- a/operators/constellation-node-operator/main.go +++ b/operators/constellation-node-operator/main.go @@ -15,6 +15,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" + + updatev1alpha1 "github.com/edgelesssys/constellation/operators/constellation-node-operator/api/v1alpha1" + "github.com/edgelesssys/constellation/operators/constellation-node-operator/controllers" //+kubebuilder:scaffold:imports ) @@ -26,6 +29,7 @@ var ( func init() { utilruntime.Must(clientgoscheme.AddToScheme(scheme)) + utilruntime.Must(updatev1alpha1.AddToScheme(scheme)) //+kubebuilder:scaffold:scheme } @@ -59,6 +63,13 @@ func main() { os.Exit(1) } + if err = (&controllers.NodeImageReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "NodeImage") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {