Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External crd creation #55

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ State: In heavy development. DO NOT USE FOR ANY PRODUCTION LIKE PURPOSE! THINGS

```bash
DOCKERNAMESPACE=<your dockerhub account> 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
Expand Down
1 change: 1 addition & 0 deletions docs/user/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
34 changes: 34 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion manifests/templates/deployment/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rules:
resources:
- customresourcedefinitions
verbs:
- "*"
- get
- apiGroups:
- ""
resources:
Expand Down
2 changes: 1 addition & 1 deletion manifests/templates/storage/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rules:
resources:
- customresourcedefinitions
verbs:
- "*"
- get
- apiGroups:
- ""
resources:
Expand Down
28 changes: 3 additions & 25 deletions pkg/operator/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,24 @@
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)
}
}

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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
29 changes: 0 additions & 29 deletions pkg/util/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down