Skip to content

Commit 27f571d

Browse files
authored
Merge pull request #52 from arangodb/operator-split
Splitting operator in two parts
2 parents be34b5a + 8462a7b commit 27f571d

16 files changed

+355
-283
lines changed

Makefile

+11-6
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ else
4040
IMAGESUFFIX := :dev
4141
endif
4242

43-
ifndef MANIFESTPATH
44-
MANIFESTPATH := manifests/arango-operator-dev.yaml
43+
ifndef MANIFESTSUFFIX
44+
MANIFESTSUFFIX := -dev
4545
endif
46+
MANIFESTPATHDEPLOYMENT := manifests/arango-deployment$(MANIFESTSUFFIX).yaml
47+
MANIFESTPATHSTORAGE := manifests/arango-storage$(MANIFESTSUFFIX).yaml
4648
ifndef DEPLOYMENTNAMESPACE
4749
DEPLOYMENTNAMESPACE := default
4850
endif
@@ -189,7 +191,7 @@ endif
189191
.PHONY: manifests
190192
manifests: $(GOBUILDDIR)
191193
GOPATH=$(GOBUILDDIR) go run $(ROOTDIR)/tools/manifests/manifest_builder.go \
192-
--output=$(MANIFESTPATH) \
194+
--output-suffix=$(MANIFESTSUFFIX) \
193195
--image=$(OPERATORIMAGE) \
194196
--image-sha256=$(IMAGESHA256) \
195197
--namespace=$(DEPLOYMENTNAMESPACE)
@@ -240,7 +242,8 @@ ifneq ($(DEPLOYMENTNAMESPACE), default)
240242
$(ROOTDIR)/scripts/kube_delete_namespace.sh $(DEPLOYMENTNAMESPACE)
241243
kubectl create namespace $(DEPLOYMENTNAMESPACE)
242244
endif
243-
kubectl apply -f $(MANIFESTPATH)
245+
kubectl apply -f $(MANIFESTPATHSTORAGE)
246+
kubectl apply -f $(MANIFESTPATHDEPLOYMENT)
244247
$(ROOTDIR)/scripts/kube_create_storage.sh $(DEPLOYMENTNAMESPACE)
245248
$(ROOTDIR)/scripts/kube_run_tests.sh $(DEPLOYMENTNAMESPACE) $(TESTIMAGE) "$(ENTERPRISEIMAGE)" $(TESTTIMEOUT) $(TESTLENGTHOPTIONS)
246249
ifneq ($(DEPLOYMENTNAMESPACE), default)
@@ -302,9 +305,11 @@ minikube-start:
302305

303306
.PHONY: delete-operator
304307
delete-operator:
305-
kubectl delete -f $(MANIFESTPATH) --ignore-not-found
308+
kubectl delete -f $(MANIFESTPATHDEPLOYMENT) --ignore-not-found
309+
kubectl delete -f $(MANIFESTPATHSTORAGE) --ignore-not-found
306310

307311
.PHONY: redeploy-operator
308312
redeploy-operator: delete-operator manifests
309-
kubectl apply -f $(MANIFESTPATH)
313+
kubectl apply -f $(MANIFESTPATHSTORAGE)
314+
kubectl apply -f $(MANIFESTPATHDEPLOYMENT)
310315
kubectl get pods

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +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/arango-operator-dev.yaml
14+
kubectl apply -f manifests/arango-deployment-dev.yaml
15+
# To use `ArangoLocalStorage`, also run
16+
kubectl apply -f manifests/arango-storage-dev.yaml
1517
```

docs/user/usage.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ 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/arango-operator.yaml
9+
kubectl apply -f manifests/arango-deployment.yaml
10+
```
11+
12+
To use `ArangoLocalStorage`, also run:
13+
14+
```bash
15+
kubectl apply -f manifests/arango-storage.yaml
1016
```
1117

1218
## Cluster creation
@@ -37,5 +43,7 @@ To remove the entire ArangoDB operator, remove all
3743
clusters first and then remove the operator by running:
3844

3945
```bash
40-
kubectl delete -f manifests/arango-operator.yaml
46+
kubectl delete -f manifests/arango-deployment.yaml
47+
# If `ArangoLocalStorage` is installed
48+
kubectl delete -f manifests/arango-storage.yaml
4149
```

main.go

+29-53
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ import (
4141
"k8s.io/client-go/kubernetes"
4242
"k8s.io/client-go/kubernetes/scheme"
4343
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
44-
"k8s.io/client-go/tools/leaderelection"
45-
"k8s.io/client-go/tools/leaderelection/resourcelock"
4644
"k8s.io/client-go/tools/record"
4745

4846
"github.com/arangodb/kube-arangodb/pkg/client"
@@ -77,15 +75,21 @@ var (
7775
host string
7876
port int
7977
}
80-
createCRD bool
78+
operatorOptions struct {
79+
enableDeployment bool // Run deployment operator
80+
enableStorage bool // Run deployment operator
81+
createCRD bool
82+
}
8183
)
8284

8385
func init() {
8486
f := cmdMain.Flags()
8587
f.StringVar(&server.host, "server.host", defaultServerHost, "Host to listen on")
8688
f.IntVar(&server.port, "server.port", defaultServerPort, "Port to listen on")
8789
f.StringVar(&logLevel, "log.level", defaultLogLevel, "Set initial log level")
88-
f.BoolVar(&createCRD, "operator.create-crd", true, "Disable to avoid create the custom resource definition")
90+
f.BoolVar(&operatorOptions.enableDeployment, "operator.deployment", false, "Enable to run the ArangoDeployment operator")
91+
f.BoolVar(&operatorOptions.enableStorage, "operator.storage", false, "Enable to run the ArangoLocalStorage operator")
92+
f.BoolVar(&operatorOptions.createCRD, "operator.create-crd", true, "Disable to avoid create the custom resource definition")
8993
}
9094

9195
func main() {
@@ -107,6 +111,11 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
107111
cliLog.Fatal().Err(err).Msg("Failed to initialize log service")
108112
}
109113

114+
// Check operating mode
115+
if !operatorOptions.enableDeployment && !operatorOptions.enableStorage {
116+
cliLog.Fatal().Err(err).Msg("Turn on --operator.deployment or --operator.storage or both")
117+
}
118+
110119
// Log version
111120
cliLog.Info().Msgf("Starting arangodb-operator, version %s build %s", projectVersion, projectBuild)
112121

@@ -126,48 +135,12 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
126135
cliLog.Fatal().Err(err).Msg("Failed to get hostname")
127136
}
128137

129-
// Create k8s client
130-
kubecli, err := k8sutil.NewKubeClient()
131-
if err != nil {
132-
cliLog.Fatal().Err(err).Msg("Failed to create kubernetes client")
133-
}
134-
135138
//http.HandleFunc(probe.HTTPReadyzEndpoint, probe.ReadyzHandler)
136139
http.Handle("/metrics", prometheus.Handler())
137140
listenAddr := net.JoinHostPort(server.host, strconv.Itoa(server.port))
138141
go http.ListenAndServe(listenAddr, nil)
139142

140-
rl, err := resourcelock.New(resourcelock.EndpointsResourceLock,
141-
namespace,
142-
"arangodb-operator",
143-
kubecli.CoreV1(),
144-
resourcelock.ResourceLockConfig{
145-
Identity: id,
146-
EventRecorder: createRecorder(cliLog, kubecli, name, namespace),
147-
})
148-
if err != nil {
149-
cliLog.Fatal().Err(err).Msg("Failed to create resource lock")
150-
}
151-
152-
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
153-
Lock: rl,
154-
LeaseDuration: 15 * time.Second,
155-
RenewDeadline: 10 * time.Second,
156-
RetryPeriod: 2 * time.Second,
157-
Callbacks: leaderelection.LeaderCallbacks{
158-
OnStartedLeading: func(stop <-chan struct{}) {
159-
run(stop, namespace, name)
160-
},
161-
OnStoppedLeading: func() {
162-
cliLog.Fatal().Msg("Leader election lost")
163-
},
164-
},
165-
})
166-
}
167-
168-
// run the operator
169-
func run(stop <-chan struct{}, namespace, name string) {
170-
cfg, deps, err := newOperatorConfigAndDeps(namespace, name)
143+
cfg, deps, err := newOperatorConfigAndDeps(id+"-"+name, namespace, name)
171144
if err != nil {
172145
cliLog.Fatal().Err(err).Msg("Failed to create operator config & deps")
173146
}
@@ -178,13 +151,11 @@ func run(stop <-chan struct{}, namespace, name string) {
178151
if err != nil {
179152
cliLog.Fatal().Err(err).Msg("Failed to create operator")
180153
}
181-
if err := o.Start(); err != nil {
182-
cliLog.Fatal().Err(err).Msg("Failed to start operator")
183-
}
154+
o.Run()
184155
}
185156

186157
// newOperatorConfigAndDeps creates operator config & dependencies.
187-
func newOperatorConfigAndDeps(namespace, name string) (operator.Config, operator.Dependencies, error) {
158+
func newOperatorConfigAndDeps(id, namespace, name string) (operator.Config, operator.Dependencies, error) {
188159
kubecli, err := k8sutil.NewKubeClient()
189160
if err != nil {
190161
return operator.Config{}, operator.Dependencies{}, maskAny(err)
@@ -203,18 +174,23 @@ func newOperatorConfigAndDeps(namespace, name string) (operator.Config, operator
203174
if err != nil {
204175
return operator.Config{}, operator.Dependencies{}, maskAny(fmt.Errorf("Failed to created versioned client: %s", err))
205176
}
177+
eventRecorder := createRecorder(cliLog, kubecli, name, namespace)
206178

207179
cfg := operator.Config{
208-
Namespace: namespace,
209-
PodName: name,
210-
ServiceAccount: serviceAccount,
211-
CreateCRD: createCRD,
180+
ID: id,
181+
Namespace: namespace,
182+
PodName: name,
183+
ServiceAccount: serviceAccount,
184+
EnableDeployment: operatorOptions.enableDeployment,
185+
EnableStorage: operatorOptions.enableStorage,
186+
CreateCRD: operatorOptions.createCRD,
212187
}
213188
deps := operator.Dependencies{
214-
Log: logService.MustGetLogger("operator"),
215-
KubeCli: kubecli,
216-
KubeExtCli: kubeExtCli,
217-
CRCli: crCli,
189+
Log: logService.MustGetLogger("operator"),
190+
KubeCli: kubecli,
191+
KubeExtCli: kubeExtCli,
192+
CRCli: crCli,
193+
EventRecorder: eventRecorder,
218194
}
219195

220196
return cfg, deps, nil

manifests/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
arango-operator-dev.yaml
1+
arango-deployment-dev.yaml
2+
arango-storage-dev.yaml

manifests/arango-operator.yaml

-95
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
apiVersion: extensions/v1beta1
3+
kind: Deployment
4+
metadata:
5+
name: {{ .Deployment.OperatorName }}
6+
namespace: {{ .Deployment.Namespace }}
7+
spec:
8+
replicas: 1
9+
template:
10+
metadata:
11+
labels:
12+
name: {{ .Deployment.OperatorName }}
13+
spec:
14+
containers:
15+
- name: operator
16+
imagePullPolicy: {{ .ImagePullPolicy }}
17+
image: {{ .Image }}
18+
args:
19+
- --operator.deployment
20+
env:
21+
- name: MY_POD_NAMESPACE
22+
valueFrom:
23+
fieldRef:
24+
fieldPath: metadata.namespace
25+
- name: MY_POD_NAME
26+
valueFrom:
27+
fieldRef:
28+
fieldPath: metadata.name

0 commit comments

Comments
 (0)