Skip to content

Commit e054884

Browse files
authored
Merge pull request #55 from arangodb/external-crd-creation
External crd creation
2 parents 27f571d + 2d2d0f0 commit e054884

File tree

9 files changed

+45
-58
lines changed

9 files changed

+45
-58
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ ifneq ($(DEPLOYMENTNAMESPACE), default)
242242
$(ROOTDIR)/scripts/kube_delete_namespace.sh $(DEPLOYMENTNAMESPACE)
243243
kubectl create namespace $(DEPLOYMENTNAMESPACE)
244244
endif
245+
kubectl apply -f manifests/crd.yaml
245246
kubectl apply -f $(MANIFESTPATHSTORAGE)
246247
kubectl apply -f $(MANIFESTPATHDEPLOYMENT)
247248
$(ROOTDIR)/scripts/kube_create_storage.sh $(DEPLOYMENTNAMESPACE)
@@ -310,6 +311,7 @@ delete-operator:
310311

311312
.PHONY: redeploy-operator
312313
redeploy-operator: delete-operator manifests
314+
kubectl apply -f manifests/crd.yaml
313315
kubectl apply -f $(MANIFESTPATHSTORAGE)
314316
kubectl apply -f $(MANIFESTPATHDEPLOYMENT)
315317
kubectl get pods

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ State: In heavy development. DO NOT USE FOR ANY PRODUCTION LIKE PURPOSE! THINGS
1111

1212
```bash
1313
DOCKERNAMESPACE=<your dockerhub account> make
14+
kubectl apply -f manifests/crd.yaml
1415
kubectl apply -f manifests/arango-deployment-dev.yaml
1516
# To use `ArangoLocalStorage`, also run
1617
kubectl apply -f manifests/arango-storage-dev.yaml

docs/user/usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The ArangoDB operator needs to be installed in your Kubernetes
66
cluster first. To do so, clone this repository and run:
77

88
```bash
9+
kubectl apply -f manifests/crd.yaml
910
kubectl apply -f manifests/arango-deployment.yaml
1011
```
1112

manifests/crd.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: arangodeployments.database.arangodb.com
5+
spec:
6+
group: database.arangodb.com
7+
names:
8+
kind: ArangoDeployment
9+
listKind: ArangoDeploymentList
10+
plural: arangodeployments
11+
shortNames:
12+
- arangodb
13+
- arango
14+
singular: arangodeployment
15+
scope: Namespaced
16+
version: v1alpha
17+
18+
---
19+
20+
apiVersion: apiextensions.k8s.io/v1beta1
21+
kind: CustomResourceDefinition
22+
metadata:
23+
name: arangolocalstorages.storage.arangodb.com
24+
spec:
25+
group: storage.arangodb.com
26+
names:
27+
kind: ArangoLocalStorage
28+
listKind: ArangoLocalStorageList
29+
plural: arangolocalstorages
30+
shortNames:
31+
- arangostorage
32+
singular: arangolocalstorage
33+
scope: Namespaced
34+
version: v1alpha

manifests/templates/deployment/rbac.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rules:
1515
resources:
1616
- customresourcedefinitions
1717
verbs:
18-
- "*"
18+
- get
1919
- apiGroups:
2020
- ""
2121
resources:

manifests/templates/storage/rbac.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rules:
1616
resources:
1717
- customresourcedefinitions
1818
verbs:
19-
- "*"
19+
- get
2020
- apiGroups:
2121
- ""
2222
resources:

pkg/operator/crd.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,24 @@
2323
package operator
2424

2525
import (
26-
"fmt"
27-
28-
"github.com/pkg/errors"
29-
3026
deplapi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
3127
lsapi "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
3228
"github.com/arangodb/kube-arangodb/pkg/util/crd"
3329
)
3430

35-
// initResourceIfNeeded initializes the custom resource definition when
36-
// instructed to do so by the config.
37-
func (o *Operator) initResourceIfNeeded(enableDeployment, enableStorage bool) error {
38-
if o.Config.CreateCRD {
39-
if err := o.initCRD(enableDeployment, enableStorage); err != nil {
40-
return maskAny(fmt.Errorf("Failed to initialize Custom Resource Definition: %v", err))
41-
}
42-
}
43-
return nil
44-
}
45-
46-
// initCRD creates the CustomResourceDefinition and waits for it to be ready.
47-
func (o *Operator) initCRD(enableDeployment, enableStorage bool) error {
31+
// waitForCRD waits for the CustomResourceDefinition (created externally)
32+
// to be ready.
33+
func (o *Operator) waitForCRD(enableDeployment, enableStorage bool) error {
4834
log := o.Dependencies.Log
4935

5036
if enableDeployment {
51-
log.Debug().Msg("Creating ArangoDeployment CRD")
52-
if err := crd.CreateCRD(o.KubeExtCli, deplapi.SchemeGroupVersion, deplapi.ArangoDeploymentCRDName, deplapi.ArangoDeploymentResourceKind, deplapi.ArangoDeploymentResourcePlural, deplapi.ArangoDeploymentShortNames...); err != nil {
53-
return maskAny(errors.Wrapf(err, "failed to create CRD: %v", err))
54-
}
5537
log.Debug().Msg("Waiting for ArangoDeployment CRD to be ready")
5638
if err := crd.WaitCRDReady(o.KubeExtCli, deplapi.ArangoDeploymentCRDName); err != nil {
5739
return maskAny(err)
5840
}
5941
}
6042

6143
if enableStorage {
62-
log.Debug().Msg("Creating ArangoLocalStorage CRD")
63-
if err := crd.CreateCRD(o.KubeExtCli, lsapi.SchemeGroupVersion, lsapi.ArangoLocalStorageCRDName, lsapi.ArangoLocalStorageResourceKind, lsapi.ArangoLocalStorageResourcePlural, lsapi.ArangoLocalStorageShortNames...); err != nil {
64-
return maskAny(errors.Wrapf(err, "failed to create CRD: %v", err))
65-
}
6644
log.Debug().Msg("Waiting for ArangoLocalStorage CRD to be ready")
6745
if err := crd.WaitCRDReady(o.KubeExtCli, lsapi.ArangoLocalStorageCRDName); err != nil {
6846
return maskAny(err)

pkg/operator/operator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (o *Operator) Run() {
102102
// onStartDeployment starts the deployment operator and run till given channel is closed.
103103
func (o *Operator) onStartDeployment(stop <-chan struct{}) {
104104
for {
105-
if err := o.initResourceIfNeeded(true, false); err == nil {
105+
if err := o.waitForCRD(true, false); err == nil {
106106
break
107107
} else {
108108
log.Error().Err(err).Msg("Resource initialization failed")
@@ -116,7 +116,7 @@ func (o *Operator) onStartDeployment(stop <-chan struct{}) {
116116
// onStartStorage starts the storage operator and run till given channel is closed.
117117
func (o *Operator) onStartStorage(stop <-chan struct{}) {
118118
for {
119-
if err := o.initResourceIfNeeded(false, true); err == nil {
119+
if err := o.waitForCRD(false, true); err == nil {
120120
break
121121
} else {
122122
log.Error().Err(err).Msg("Resource initialization failed")

pkg/util/crd/crd.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,13 @@ import (
2626
"fmt"
2727
"time"
2828

29-
"k8s.io/apimachinery/pkg/runtime/schema"
30-
3129
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
3230
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3331
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3432

35-
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
3633
"github.com/arangodb/kube-arangodb/pkg/util/retry"
3734
)
3835

39-
// CreateCRD creates a custom resouce definition.
40-
func CreateCRD(clientset apiextensionsclient.Interface, groupVersion schema.GroupVersion, crdName, rkind, rplural string, shortName ...string) error {
41-
crd := &apiextensionsv1beta1.CustomResourceDefinition{
42-
ObjectMeta: metav1.ObjectMeta{
43-
Name: crdName,
44-
},
45-
Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
46-
Group: groupVersion.Group,
47-
Version: groupVersion.Version,
48-
Scope: apiextensionsv1beta1.NamespaceScoped,
49-
Names: apiextensionsv1beta1.CustomResourceDefinitionNames{
50-
Plural: rplural,
51-
Kind: rkind,
52-
},
53-
},
54-
}
55-
if len(shortName) != 0 {
56-
crd.Spec.Names.ShortNames = shortName
57-
}
58-
_, err := clientset.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
59-
if err != nil && !k8sutil.IsAlreadyExists(err) {
60-
return err
61-
}
62-
return nil
63-
}
64-
6536
// WaitCRDReady waits for a custom resource definition with given name to be ready.
6637
func WaitCRDReady(clientset apiextensionsclient.Interface, crdName string) error {
6738
op := func() error {

0 commit comments

Comments
 (0)