diff --git a/Makefile b/Makefile index 48a6f69fc..9d7827b95 100644 --- a/Makefile +++ b/Makefile @@ -242,6 +242,7 @@ ifneq ($(DEPLOYMENTNAMESPACE), default) $(ROOTDIR)/scripts/kube_delete_namespace.sh $(DEPLOYMENTNAMESPACE) kubectl create namespace $(DEPLOYMENTNAMESPACE) endif + kubectl apply -f manifests/crd.yaml kubectl apply -f $(MANIFESTPATHSTORAGE) kubectl apply -f $(MANIFESTPATHDEPLOYMENT) $(ROOTDIR)/scripts/kube_create_storage.sh $(DEPLOYMENTNAMESPACE) @@ -310,6 +311,7 @@ delete-operator: .PHONY: redeploy-operator redeploy-operator: delete-operator manifests + kubectl apply -f manifests/crd.yaml kubectl apply -f $(MANIFESTPATHSTORAGE) kubectl apply -f $(MANIFESTPATHDEPLOYMENT) kubectl get pods diff --git a/README.md b/README.md index f5ff5cf91..029930e3b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ State: In heavy development. DO NOT USE FOR ANY PRODUCTION LIKE PURPOSE! THINGS ```bash DOCKERNAMESPACE= make +kubectl apply -f manifests/crd.yaml kubectl apply -f manifests/arango-deployment-dev.yaml # To use `ArangoLocalStorage`, also run kubectl apply -f manifests/arango-storage-dev.yaml diff --git a/docs/user/usage.md b/docs/user/usage.md index 469efb0d1..8d3e78752 100644 --- a/docs/user/usage.md +++ b/docs/user/usage.md @@ -6,6 +6,7 @@ The ArangoDB operator needs to be installed in your Kubernetes cluster first. To do so, clone this repository and run: ```bash +kubectl apply -f manifests/crd.yaml kubectl apply -f manifests/arango-deployment.yaml ``` diff --git a/manifests/crd.yaml b/manifests/crd.yaml new file mode 100644 index 000000000..e708946d7 --- /dev/null +++ b/manifests/crd.yaml @@ -0,0 +1,34 @@ +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: arangodeployments.database.arangodb.com +spec: + group: database.arangodb.com + names: + kind: ArangoDeployment + listKind: ArangoDeploymentList + plural: arangodeployments + shortNames: + - arangodb + - arango + singular: arangodeployment + scope: Namespaced + version: v1alpha + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: arangolocalstorages.storage.arangodb.com +spec: + group: storage.arangodb.com + names: + kind: ArangoLocalStorage + listKind: ArangoLocalStorageList + plural: arangolocalstorages + shortNames: + - arangostorage + singular: arangolocalstorage + scope: Namespaced + version: v1alpha diff --git a/manifests/templates/deployment/rbac.yaml b/manifests/templates/deployment/rbac.yaml index 9d9590bfd..291aeeb71 100644 --- a/manifests/templates/deployment/rbac.yaml +++ b/manifests/templates/deployment/rbac.yaml @@ -15,7 +15,7 @@ rules: resources: - customresourcedefinitions verbs: - - "*" + - get - apiGroups: - "" resources: diff --git a/manifests/templates/storage/rbac.yaml b/manifests/templates/storage/rbac.yaml index aea2a509e..2feee0b4e 100644 --- a/manifests/templates/storage/rbac.yaml +++ b/manifests/templates/storage/rbac.yaml @@ -3,7 +3,7 @@ apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRole metadata: - name: {{ .Deployment.ClusterRoleName }} + name: {{ .Storage.ClusterRoleName }} rules: - apiGroups: - storage.arangodb.com @@ -16,7 +16,7 @@ rules: resources: - customresourcedefinitions verbs: - - "*" + - get - apiGroups: - "" resources: @@ -43,14 +43,14 @@ rules: apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: - name: {{ .Deployment.ClusterRoleBindingName }} + name: {{ .Storage.ClusterRoleBindingName }} roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole - name: {{ .Deployment.ClusterRoleName }} + name: {{ .Storage.ClusterRoleName }} subjects: - kind: ServiceAccount name: default - namespace: {{ .Deployment.Namespace }} + namespace: {{ .Storage.Namespace }} {{- end -}} \ No newline at end of file diff --git a/pkg/operator/crd.go b/pkg/operator/crd.go index 965ff0132..d325dbaab 100644 --- a/pkg/operator/crd.go +++ b/pkg/operator/crd.go @@ -23,35 +23,17 @@ package operator import ( - "fmt" - - "github.com/pkg/errors" - deplapi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha" lsapi "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha" "github.com/arangodb/kube-arangodb/pkg/util/crd" ) -// initResourceIfNeeded initializes the custom resource definition when -// instructed to do so by the config. -func (o *Operator) initResourceIfNeeded(enableDeployment, enableStorage bool) error { - if o.Config.CreateCRD { - if err := o.initCRD(enableDeployment, enableStorage); err != nil { - return maskAny(fmt.Errorf("Failed to initialize Custom Resource Definition: %v", err)) - } - } - return nil -} - -// initCRD creates the CustomResourceDefinition and waits for it to be ready. -func (o *Operator) initCRD(enableDeployment, enableStorage bool) error { +// waitForCRD waits for the CustomResourceDefinition (created externally) +// to be ready. +func (o *Operator) waitForCRD(enableDeployment, enableStorage bool) error { log := o.Dependencies.Log if enableDeployment { - log.Debug().Msg("Creating ArangoDeployment CRD") - if err := crd.CreateCRD(o.KubeExtCli, deplapi.SchemeGroupVersion, deplapi.ArangoDeploymentCRDName, deplapi.ArangoDeploymentResourceKind, deplapi.ArangoDeploymentResourcePlural, deplapi.ArangoDeploymentShortNames...); err != nil { - return maskAny(errors.Wrapf(err, "failed to create CRD: %v", err)) - } log.Debug().Msg("Waiting for ArangoDeployment CRD to be ready") if err := crd.WaitCRDReady(o.KubeExtCli, deplapi.ArangoDeploymentCRDName); err != nil { return maskAny(err) @@ -59,10 +41,6 @@ func (o *Operator) initCRD(enableDeployment, enableStorage bool) error { } if enableStorage { - log.Debug().Msg("Creating ArangoLocalStorage CRD") - if err := crd.CreateCRD(o.KubeExtCli, lsapi.SchemeGroupVersion, lsapi.ArangoLocalStorageCRDName, lsapi.ArangoLocalStorageResourceKind, lsapi.ArangoLocalStorageResourcePlural, lsapi.ArangoLocalStorageShortNames...); err != nil { - return maskAny(errors.Wrapf(err, "failed to create CRD: %v", err)) - } log.Debug().Msg("Waiting for ArangoLocalStorage CRD to be ready") if err := crd.WaitCRDReady(o.KubeExtCli, lsapi.ArangoLocalStorageCRDName); err != nil { return maskAny(err) diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 0ab8d78cc..90f9051be 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -102,7 +102,7 @@ func (o *Operator) Run() { // onStartDeployment starts the deployment operator and run till given channel is closed. func (o *Operator) onStartDeployment(stop <-chan struct{}) { for { - if err := o.initResourceIfNeeded(true, false); err == nil { + if err := o.waitForCRD(true, false); err == nil { break } else { log.Error().Err(err).Msg("Resource initialization failed") @@ -116,7 +116,7 @@ func (o *Operator) onStartDeployment(stop <-chan struct{}) { // onStartStorage starts the storage operator and run till given channel is closed. func (o *Operator) onStartStorage(stop <-chan struct{}) { for { - if err := o.initResourceIfNeeded(false, true); err == nil { + if err := o.waitForCRD(false, true); err == nil { break } else { log.Error().Err(err).Msg("Resource initialization failed") diff --git a/pkg/util/crd/crd.go b/pkg/util/crd/crd.go index 4113d0b35..966a1b847 100644 --- a/pkg/util/crd/crd.go +++ b/pkg/util/crd/crd.go @@ -26,42 +26,13 @@ import ( "fmt" "time" - "k8s.io/apimachinery/pkg/runtime/schema" - apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/arangodb/kube-arangodb/pkg/util/retry" ) -// CreateCRD creates a custom resouce definition. -func CreateCRD(clientset apiextensionsclient.Interface, groupVersion schema.GroupVersion, crdName, rkind, rplural string, shortName ...string) error { - crd := &apiextensionsv1beta1.CustomResourceDefinition{ - ObjectMeta: metav1.ObjectMeta{ - Name: crdName, - }, - Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{ - Group: groupVersion.Group, - Version: groupVersion.Version, - Scope: apiextensionsv1beta1.NamespaceScoped, - Names: apiextensionsv1beta1.CustomResourceDefinitionNames{ - Plural: rplural, - Kind: rkind, - }, - }, - } - if len(shortName) != 0 { - crd.Spec.Names.ShortNames = shortName - } - _, err := clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd) - if err != nil && !k8sutil.IsAlreadyExists(err) { - return err - } - return nil -} - // WaitCRDReady waits for a custom resource definition with given name to be ready. func WaitCRDReady(clientset apiextensionsclient.Interface, crdName string) error { op := func() error {