diff --git a/CHANGELOG.md b/CHANGELOG.md index 70dd650eb..c1b638d74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - License V2 for ArangoDB 3.9.0+ - Add ArangoClusterSynchronization v1 API - Add core containers names to follow their terminations +- Add ArangoJob and Apps Operator ## [1.2.6](https://github.com/arangodb/kube-arangodb/tree/1.2.6) (2021-12-15) - Add ArangoBackup backoff functionality diff --git a/Makefile b/Makefile index 4ef48bb44..5f9bb5a09 100644 --- a/Makefile +++ b/Makefile @@ -96,6 +96,7 @@ MANIFESTPATHCRD := manifests/arango-crd$(MANIFESTSUFFIX).yaml MANIFESTPATHDEPLOYMENT := manifests/arango-deployment$(MANIFESTSUFFIX).yaml MANIFESTPATHDEPLOYMENTREPLICATION := manifests/arango-deployment-replication$(MANIFESTSUFFIX).yaml MANIFESTPATHBACKUP := manifests/arango-backup$(MANIFESTSUFFIX).yaml +MANIFESTPATHAPPS := manifests/arango-apps$(MANIFESTSUFFIX).yaml MANIFESTPATHSTORAGE := manifests/arango-storage$(MANIFESTSUFFIX).yaml MANIFESTPATHALL := manifests/arango-all$(MANIFESTSUFFIX).yaml MANIFESTPATHTEST := manifests/arango-test$(MANIFESTSUFFIX).yaml @@ -103,6 +104,7 @@ KUSTOMIZEPATHCRD := manifests/kustomize/crd/arango-crd$(MANIFESTSUFFIX).yaml KUSTOMIZEPATHDEPLOYMENT := manifests/kustomize/deployment/arango-deployment$(MANIFESTSUFFIX).yaml KUSTOMIZEPATHDEPLOYMENTREPLICATION := manifests/kustomize/deployment-replication/arango-deployment-replication$(MANIFESTSUFFIX).yaml KUSTOMIZEPATHBACKUP := manifests/kustomize/backup/arango-backup$(MANIFESTSUFFIX).yaml +KUSTOMIZEPATHAPPS := manifests/kustomize/apps/arango-apps$(MANIFESTSUFFIX).yaml KUSTOMIZEPATHSTORAGE := manifests/kustomize/storage/arango-storage$(MANIFESTSUFFIX).yaml KUSTOMIZEPATHALL := manifests/kustomize/all/arango-all$(MANIFESTSUFFIX).yaml KUSTOMIZEPATHTEST := manifests/kustomize/test/arango-test$(MANIFESTSUFFIX).yaml @@ -325,30 +327,42 @@ $(eval $(call manifest-generator, deployment, kube-arangodb, \ --set "operator.features.deployment=true" \ --set "operator.features.deploymentReplications=false" \ --set "operator.features.storage=false" \ + --set "operator.features.apps=false" \ --set "operator.features.backup=false")) $(eval $(call manifest-generator, deployment-replication, kube-arangodb, \ --set "operator.features.deployment=false" \ --set "operator.features.deploymentReplications=true" \ --set "operator.features.storage=false" \ + --set "operator.features.apps=false" \ --set "operator.features.backup=false")) $(eval $(call manifest-generator, storage, kube-arangodb, \ --set "operator.features.deployment=false" \ --set "operator.features.deploymentReplications=false" \ --set "operator.features.storage=true" \ + --set "operator.features.apps=false" \ --set "operator.features.backup=false")) $(eval $(call manifest-generator, backup, kube-arangodb, \ --set "operator.features.deployment=false" \ --set "operator.features.deploymentReplications=false" \ --set "operator.features.storage=false" \ + --set "operator.features.apps=false" \ --set "operator.features.backup=true")) +$(eval $(call manifest-generator, apps, kube-arangodb, \ + --set "operator.features.deployment=false" \ + --set "operator.features.deploymentReplications=false" \ + --set "operator.features.storage=false" \ + --set "operator.features.apps=true" \ + --set "operator.features.backup=false")) + $(eval $(call manifest-generator, all, kube-arangodb, \ --set "operator.features.deployment=true" \ --set "operator.features.deploymentReplications=true" \ --set "operator.features.storage=true" \ + --set "operator.features.apps=true" \ --set "operator.features.backup=true")) .PHONY: chart-crd diff --git a/chart/kube-arangodb-crd/templates/job.yaml b/chart/kube-arangodb-crd/templates/job.yaml new file mode 100644 index 000000000..cfcd00488 --- /dev/null +++ b/chart/kube-arangodb-crd/templates/job.yaml @@ -0,0 +1,35 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: arangojobs.apps.arangodb.com + labels: + app.kubernetes.io/name: {{ template "kube-arangodb-crd.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +spec: + group: apps.arangodb.com + names: + kind: ArangoJob + listKind: ArangoJobList + plural: arangojobs + singular: arangojob + shortNames: + - arangojob + scope: Namespaced + versions: + - name: v1 + schema: + openAPIV3Schema: + type: object + x-kubernetes-preserve-unknown-fields: true + served: true + storage: true + additionalPrinterColumns: + - jsonPath: .spec.arangoDeploymentName + description: Deployment name + name: ArangoDeploymentName + type: string + subresources: + status: {} diff --git a/chart/kube-arangodb/templates/apps-operator/cluster-role-binding.yaml b/chart/kube-arangodb/templates/apps-operator/cluster-role-binding.yaml new file mode 100644 index 000000000..785c6e886 --- /dev/null +++ b/chart/kube-arangodb/templates/apps-operator/cluster-role-binding.yaml @@ -0,0 +1,26 @@ +{{ if .Values.rbac.enabled -}} +{{ if not (eq .Values.operator.scope "namespaced") -}} +{{ if .Values.operator.features.apps -}} + +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: {{ template "kube-arangodb.rbac-cluster" . }}-apps + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: {{ template "kube-arangodb.rbac-cluster" . }}-apps +subjects: + - kind: ServiceAccount + name: {{ template "kube-arangodb.operatorName" . }} + namespace: {{ .Release.Namespace }} + +{{- end }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/chart/kube-arangodb/templates/apps-operator/cluster-role.yaml b/chart/kube-arangodb/templates/apps-operator/cluster-role.yaml new file mode 100644 index 000000000..4789de945 --- /dev/null +++ b/chart/kube-arangodb/templates/apps-operator/cluster-role.yaml @@ -0,0 +1,22 @@ +{{ if .Values.rbac.enabled -}} +{{ if not (eq .Values.operator.scope "namespaced") -}} +{{ if .Values.operator.features.apps -}} + +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: {{ template "kube-arangodb.rbac-cluster" . }}-apps + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +rules: + - apiGroups: ["apiextensions.k8s.io"] + resources: ["customresourcedefinitions"] + verbs: ["get", "list", "watch"] + +{{- end }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/chart/kube-arangodb/templates/apps-operator/role-binding.yaml b/chart/kube-arangodb/templates/apps-operator/role-binding.yaml new file mode 100644 index 000000000..dbd9fc636 --- /dev/null +++ b/chart/kube-arangodb/templates/apps-operator/role-binding.yaml @@ -0,0 +1,47 @@ +{{ if .Values.rbac.enabled -}} +{{ if .Values.operator.features.apps -}} + +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: {{ template "kube-arangodb.rbac" . }}-apps + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ template "kube-arangodb.rbac" . }}-apps +subjects: + - kind: ServiceAccount + name: {{ template "kube-arangodb.operatorName" . }} + namespace: {{ .Release.Namespace }} + +--- + +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: {{ template "kube-arangodb.rbac" . }}-job + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ template "kube-arangodb.rbac" . }}-job +subjects: + - kind: ServiceAccount + name: {{ template "kube-arangodb.operatorName" . }}-job + namespace: {{ .Release.Namespace }} + +{{- end }} +{{- end }} \ No newline at end of file diff --git a/chart/kube-arangodb/templates/apps-operator/role.yaml b/chart/kube-arangodb/templates/apps-operator/role.yaml new file mode 100644 index 000000000..63df49055 --- /dev/null +++ b/chart/kube-arangodb/templates/apps-operator/role.yaml @@ -0,0 +1,54 @@ +{{ if .Values.rbac.enabled -}} +{{ if .Values.operator.features.apps -}} + +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ template "kube-arangodb.rbac" . }}-apps + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +rules: + - apiGroups: [""] + resources: ["pods", "services", "endpoints"] + verbs: ["get", "update"] + - apiGroups: [""] + resources: ["events"] + verbs: ["*"] + - apiGroups: [""] + resources: ["secrets"] + verbs: ["get"] + - apiGroups: ["apps"] + resources: ["deployments", "replicasets"] + verbs: ["get"] + - apiGroups: ["batch"] + resources: ["jobs"] + verbs: ["*"] + - apiGroups: ["database.arangodb.com"] + resources: ["arangodeployments"] + verbs: ["get", "list", "watch"] + - apiGroups: ["apps.arangodb.com"] + resources: ["arangojobs","arangojobs/status"] + verbs: ["*"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: {{ template "kube-arangodb.rbac" . }}-job + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +rules: + - apiGroups: ["database.arangodb.com"] + resources: ["arangodeployments"] + verbs: ["get", "list", "watch"] +{{- end }} +{{- end }} \ No newline at end of file diff --git a/chart/kube-arangodb/templates/apps-operator/service-account-job.yaml b/chart/kube-arangodb/templates/apps-operator/service-account-job.yaml new file mode 100644 index 000000000..07ae08095 --- /dev/null +++ b/chart/kube-arangodb/templates/apps-operator/service-account-job.yaml @@ -0,0 +1,19 @@ +{{ if .Values.operator.features.apps -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ template "kube-arangodb.operatorName" . }}-job + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: {{ template "kube-arangodb.name" . }} + helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + app.kubernetes.io/instance: {{ .Release.Name }} + release: {{ .Release.Name }} +{{- if .Values.operator.imagePullSecrets }} +imagePullSecrets: +{{- range .Values.operator.imagePullSecrets }} + - name: {{ . }} +{{- end }} +{{- end }} +{{- end }} diff --git a/chart/kube-arangodb/templates/backup-operator/role-binding.yaml b/chart/kube-arangodb/templates/backup-operator/role-binding.yaml index dc3ddf6ca..679902669 100644 --- a/chart/kube-arangodb/templates/backup-operator/role-binding.yaml +++ b/chart/kube-arangodb/templates/backup-operator/role-binding.yaml @@ -21,6 +21,5 @@ subjects: name: {{ template "kube-arangodb.operatorName" . }} namespace: {{ .Release.Namespace }} - {{- end }} {{- end }} \ No newline at end of file diff --git a/chart/kube-arangodb/templates/deployment.yaml b/chart/kube-arangodb/templates/deployment.yaml index 0b42cffb6..4321c1336 100644 --- a/chart/kube-arangodb/templates/deployment.yaml +++ b/chart/kube-arangodb/templates/deployment.yaml @@ -102,6 +102,9 @@ spec: {{- end }} {{ if .Values.operator.features.backup }} - --operator.backup +{{- end }} +{{ if .Values.operator.features.apps }} + - --operator.apps {{- end }} - --chaos.allowed={{ .Values.operator.allowChaos }} {{- if .Values.operator.args }} @@ -128,6 +131,10 @@ spec: value: "{{ .Values.operator.images.metricsExporter }}" - name: RELATED_IMAGE_DATABASE value: "{{ .Values.operator.images.arango }}" +{{- if .Values.operator.features.apps }} + - name: ARANGOJOB_SA_NAME + value: "{{ template "kube-arangodb.operatorName" . }}-job" +{{- end }} ports: - name: metrics containerPort: 8528 diff --git a/chart/kube-arangodb/values.yaml b/chart/kube-arangodb/values.yaml index 0accfd8ea..0bc52823f 100644 --- a/chart/kube-arangodb/values.yaml +++ b/chart/kube-arangodb/values.yaml @@ -38,6 +38,7 @@ operator: deploymentReplications: true storage: false backup: false + apps: false images: base: alpine:3.11 diff --git a/examples/arangojob.yaml b/examples/arangojob.yaml new file mode 100644 index 000000000..bea5093a1 --- /dev/null +++ b/examples/arangojob.yaml @@ -0,0 +1,15 @@ +apiVersion: apps.arangodb.com/v1 +kind: ArangoJob +metadata: + name: arangojob-sample +spec: + arangoDeploymentName: deployment + jobTemplate: + template: + spec: + containers: + - name: pi + image: perl + command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] + restartPolicy: Never + backoffLimit: 4 \ No newline at end of file diff --git a/go.mod b/go.mod index f436d3cc7..1fd393c12 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( //github.com/arangodb/rebalancer v0.1.1 github.com/cenkalti/backoff v2.2.1+incompatible github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 - github.com/evanphx/json-patch v4.9.0+incompatible + github.com/evanphx/json-patch v4.9.0+incompatible // indirect github.com/ghodss/yaml v1.0.0 github.com/gin-gonic/gin v1.7.2 github.com/go-playground/validator/v10 v10.8.0 // indirect diff --git a/lifecycle.go b/lifecycle.go index 37cbb4308..42f297b13 100644 --- a/lifecycle.go +++ b/lifecycle.go @@ -31,7 +31,7 @@ import ( "strconv" "time" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util/retry" diff --git a/main.go b/main.go index a22c2bdfd..c27dad3e5 100644 --- a/main.go +++ b/main.go @@ -114,6 +114,7 @@ var ( enableDeploymentReplication bool // Run deployment-replication operator enableStorage bool // Run local-storage operator enableBackup bool // Run backup operator + enableApps bool // Run apps operator versionOnly bool // Run only version endpoint, explicitly disabled with other scalingIntegrationEnabled bool @@ -142,6 +143,7 @@ var ( deploymentReplicationProbe probe.ReadyProbe storageProbe probe.ReadyProbe backupProbe probe.ReadyProbe + appsProbe probe.ReadyProbe ) func init() { @@ -157,6 +159,7 @@ func init() { f.BoolVar(&operatorOptions.enableDeploymentReplication, "operator.deployment-replication", false, "Enable to run the ArangoDeploymentReplication operator") f.BoolVar(&operatorOptions.enableStorage, "operator.storage", false, "Enable to run the ArangoLocalStorage operator") f.BoolVar(&operatorOptions.enableBackup, "operator.backup", false, "Enable to run the ArangoBackup operator") + f.BoolVar(&operatorOptions.enableApps, "operator.apps", false, "Enable to run the ArangoApps operator") f.BoolVar(&operatorOptions.versionOnly, "operator.version", false, "Enable only version endpoint in Operator") f.StringVar(&operatorOptions.alpineImage, "operator.alpine-image", UBIImageEnv.GetOrDefault(defaultAlpineImage), "Docker image used for alpine containers") f.MarkDeprecated("operator.alpine-image", "Value is not used anymore") @@ -220,12 +223,12 @@ func cmdMainRun(cmd *cobra.Command, args []string) { klog.Flush() // Check operating mode - if !operatorOptions.enableDeployment && !operatorOptions.enableDeploymentReplication && !operatorOptions.enableStorage && !operatorOptions.enableBackup { + if !operatorOptions.enableDeployment && !operatorOptions.enableDeploymentReplication && !operatorOptions.enableStorage && !operatorOptions.enableBackup && !operatorOptions.enableApps { if !operatorOptions.versionOnly { - cliLog.Fatal().Err(err).Msg("Turn on --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup or any combination of these") + cliLog.Fatal().Err(err).Msg("Turn on --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps or any combination of these") } } else if operatorOptions.versionOnly { - cliLog.Fatal().Err(err).Msg("Options --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup cannot be enabled together with --operator.version") + cliLog.Fatal().Err(err).Msg("Options --operator.deployment, --operator.deployment-replication, --operator.storage, --operator.backup, --operator.apps cannot be enabled together with --operator.version") } // Log version @@ -298,6 +301,10 @@ func cmdMainRun(cmd *cobra.Command, args []string) { Enabled: cfg.EnableBackup, Probe: &backupProbe, }, + Apps: server.OperatorDependency{ + Enabled: cfg.EnableApps, + Probe: &appsProbe, + }, Operators: o, Secrets: secrets, @@ -384,6 +391,7 @@ func newOperatorConfigAndDeps(id, namespace, name string) (operator.Config, oper EnableDeploymentReplication: operatorOptions.enableDeploymentReplication, EnableStorage: operatorOptions.enableStorage, EnableBackup: operatorOptions.enableBackup, + EnableApps: operatorOptions.enableApps, AllowChaos: chaosOptions.allowed, ScalingIntegrationEnabled: operatorOptions.scalingIntegrationEnabled, ArangoImage: operatorOptions.arangoImage, @@ -402,6 +410,7 @@ func newOperatorConfigAndDeps(id, namespace, name string) (operator.Config, oper DeploymentReplicationProbe: &deploymentReplicationProbe, StorageProbe: &storageProbe, BackupProbe: &backupProbe, + AppsProbe: &appsProbe, } return cfg, deps, nil diff --git a/manifests/kustomize/apps/kustomization.yaml b/manifests/kustomize/apps/kustomization.yaml new file mode 100644 index 000000000..0f44f4fad --- /dev/null +++ b/manifests/kustomize/apps/kustomization.yaml @@ -0,0 +1,5 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +resources: + - arango-apps.yaml \ No newline at end of file diff --git a/pkg/apis/apps/definitions.go b/pkg/apis/apps/definitions.go index e291cad61..6cc403326 100644 --- a/pkg/apis/apps/definitions.go +++ b/pkg/apis/apps/definitions.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package apps diff --git a/pkg/apis/apps/v1/doc.go b/pkg/apis/apps/v1/doc.go index 2bb38f0d5..75ebae94c 100644 --- a/pkg/apis/apps/v1/doc.go +++ b/pkg/apis/apps/v1/doc.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// // +k8s:deepcopy-gen=package // +groupName=apps.arangodb.com diff --git a/pkg/apis/apps/v1/job.go b/pkg/apis/apps/v1/job.go index 20c0d2bb3..38b344180 100644 --- a/pkg/apis/apps/v1/job.go +++ b/pkg/apis/apps/v1/job.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package v1 diff --git a/pkg/apis/apps/v1/job_spec.go b/pkg/apis/apps/v1/job_spec.go index 9e28149f2..21e3fc48d 100644 --- a/pkg/apis/apps/v1/job_spec.go +++ b/pkg/apis/apps/v1/job_spec.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package v1 diff --git a/pkg/apis/apps/v1/job_validate.go b/pkg/apis/apps/v1/job_validate.go index 9c12ededb..7e611d2d1 100644 --- a/pkg/apis/apps/v1/job_validate.go +++ b/pkg/apis/apps/v1/job_validate.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package v1 diff --git a/pkg/apis/apps/v1/register.go b/pkg/apis/apps/v1/register.go index 600b67978..87b438364 100644 --- a/pkg/apis/apps/v1/register.go +++ b/pkg/apis/apps/v1/register.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package v1 diff --git a/pkg/apis/apps/v1/zz_generated.deepcopy.go b/pkg/apis/apps/v1/zz_generated.deepcopy.go index fe7f8fd3c..26e151e15 100644 --- a/pkg/apis/apps/v1/zz_generated.deepcopy.go +++ b/pkg/apis/apps/v1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/backup/v1/backup_policy.go b/pkg/apis/backup/v1/backup_policy.go index 6a250d143..51d499cca 100644 --- a/pkg/apis/backup/v1/backup_policy.go +++ b/pkg/apis/backup/v1/backup_policy.go @@ -25,7 +25,7 @@ import ( deployment "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/pkg/apis/backup/v1/backup_state.go b/pkg/apis/backup/v1/backup_state.go index 6ce9082ed..dfa05203d 100644 --- a/pkg/apis/backup/v1/backup_state.go +++ b/pkg/apis/backup/v1/backup_state.go @@ -21,7 +21,7 @@ package v1 import ( - "github.com/arangodb/kube-arangodb/pkg/backup/state" + "github.com/arangodb/kube-arangodb/pkg/handlers/backup/state" meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/pkg/apis/backup/v1/zz_generated.deepcopy.go b/pkg/apis/backup/v1/zz_generated.deepcopy.go index 877893dc6..7cf01e477 100644 --- a/pkg/apis/backup/v1/zz_generated.deepcopy.go +++ b/pkg/apis/backup/v1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/deployment/v1/deployment_spec.go b/pkg/apis/deployment/v1/deployment_spec.go index b5fdbce01..64a4a0d57 100644 --- a/pkg/apis/deployment/v1/deployment_spec.go +++ b/pkg/apis/deployment/v1/deployment_spec.go @@ -28,7 +28,7 @@ import ( core "k8s.io/api/core/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" diff --git a/pkg/apis/deployment/v1/deployment_spec_test.go b/pkg/apis/deployment/v1/deployment_spec_test.go index c6b80548a..1929b43e6 100644 --- a/pkg/apis/deployment/v1/deployment_spec_test.go +++ b/pkg/apis/deployment/v1/deployment_spec_test.go @@ -26,7 +26,7 @@ import ( "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util" ) diff --git a/pkg/apis/deployment/v1/server_group_init_containers.go b/pkg/apis/deployment/v1/server_group_init_containers.go index bbb118cdd..077c60660 100644 --- a/pkg/apis/deployment/v1/server_group_init_containers.go +++ b/pkg/apis/deployment/v1/server_group_init_containers.go @@ -33,6 +33,7 @@ import ( const ( ServerGroupReservedInitContainerNameLifecycle = "init-lifecycle" ServerGroupReservedInitContainerNameUUID = "uuid" + ServerGroupReservedInitContainerNameWait = "wait" ServerGroupReservedInitContainerNameUpgrade = "upgrade" ServerGroupReservedInitContainerNameVersionCheck = "version-check" ) diff --git a/pkg/apis/deployment/v1/zz_generated.deepcopy.go b/pkg/apis/deployment/v1/zz_generated.deepcopy.go index 4b0fb0020..facf71d05 100644 --- a/pkg/apis/deployment/v1/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/deployment/v2alpha1/deployment_spec.go b/pkg/apis/deployment/v2alpha1/deployment_spec.go index 5325227cf..f9a498060 100644 --- a/pkg/apis/deployment/v2alpha1/deployment_spec.go +++ b/pkg/apis/deployment/v2alpha1/deployment_spec.go @@ -28,7 +28,7 @@ import ( core "k8s.io/api/core/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" diff --git a/pkg/apis/deployment/v2alpha1/deployment_spec_test.go b/pkg/apis/deployment/v2alpha1/deployment_spec_test.go index 51f991d5c..6f6b02a27 100644 --- a/pkg/apis/deployment/v2alpha1/deployment_spec_test.go +++ b/pkg/apis/deployment/v2alpha1/deployment_spec_test.go @@ -26,7 +26,7 @@ import ( "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util" ) diff --git a/pkg/apis/deployment/v2alpha1/server_group_init_containers.go b/pkg/apis/deployment/v2alpha1/server_group_init_containers.go index d5cb1eb1c..7b724ef11 100644 --- a/pkg/apis/deployment/v2alpha1/server_group_init_containers.go +++ b/pkg/apis/deployment/v2alpha1/server_group_init_containers.go @@ -33,6 +33,7 @@ import ( const ( ServerGroupReservedInitContainerNameLifecycle = "init-lifecycle" ServerGroupReservedInitContainerNameUUID = "uuid" + ServerGroupReservedInitContainerNameWait = "wait" ServerGroupReservedInitContainerNameUpgrade = "upgrade" ServerGroupReservedInitContainerNameVersionCheck = "version-check" ) diff --git a/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go b/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go index 9aa4530c6..15b1840e1 100644 --- a/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v2alpha1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/replication/v1/zz_generated.deepcopy.go b/pkg/apis/replication/v1/zz_generated.deepcopy.go index 7ed7b0485..285092124 100644 --- a/pkg/apis/replication/v1/zz_generated.deepcopy.go +++ b/pkg/apis/replication/v1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/replication/v2alpha1/zz_generated.deepcopy.go b/pkg/apis/replication/v2alpha1/zz_generated.deepcopy.go index 40795762c..03a2e802a 100644 --- a/pkg/apis/replication/v2alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/replication/v2alpha1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/shared/v1/zz_generated.deepcopy.go b/pkg/apis/shared/v1/zz_generated.deepcopy.go index 8d42b5fbc..8a865a817 100644 --- a/pkg/apis/shared/v1/zz_generated.deepcopy.go +++ b/pkg/apis/shared/v1/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/apis/storage/v1alpha/zz_generated.deepcopy.go b/pkg/apis/storage/v1alpha/zz_generated.deepcopy.go index 50b7e46ba..d2c6e7641 100644 --- a/pkg/apis/storage/v1alpha/zz_generated.deepcopy.go +++ b/pkg/apis/storage/v1alpha/zz_generated.deepcopy.go @@ -1,3 +1,4 @@ +//go:build !ignore_autogenerated // +build !ignore_autogenerated // diff --git a/pkg/deployment/images.go b/pkg/deployment/images.go index 6bc5b3238..57599976c 100644 --- a/pkg/deployment/images.go +++ b/pkg/deployment/images.go @@ -32,9 +32,10 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/deployment/pod" "github.com/arangodb/kube-arangodb/pkg/deployment/resources" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util/arangod" "github.com/arangodb/kube-arangodb/pkg/util/constants" "github.com/arangodb/kube-arangodb/pkg/util/errors" diff --git a/pkg/deployment/reconcile/action_kill_member_pod.go b/pkg/deployment/reconcile/action_kill_member_pod.go index 59c781eac..b62d7b055 100644 --- a/pkg/deployment/reconcile/action_kill_member_pod.go +++ b/pkg/deployment/reconcile/action_kill_member_pod.go @@ -26,8 +26,8 @@ package reconcile import ( "context" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" "github.com/arangodb/kube-arangodb/pkg/deployment/features" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util/constants" api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" diff --git a/pkg/deployment/rotation/check.go b/pkg/deployment/rotation/check.go index 9d47b5d87..1a2c447e9 100644 --- a/pkg/deployment/rotation/check.go +++ b/pkg/deployment/rotation/check.go @@ -25,7 +25,7 @@ package rotation import ( "github.com/arangodb/kube-arangodb/pkg/apis/deployment" api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util/constants" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector" diff --git a/pkg/exporter/monitor.go b/pkg/exporter/monitor.go index 2ca543fdf..fd5c155cd 100644 --- a/pkg/exporter/monitor.go +++ b/pkg/exporter/monitor.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package exporter diff --git a/pkg/backup/handlers/arango/backup/arango_client.go b/pkg/handlers/backup/arango_client.go similarity index 97% rename from pkg/backup/handlers/arango/backup/arango_client.go rename to pkg/handlers/backup/arango_client.go index 060e98357..d8ac3db30 100644 --- a/pkg/backup/handlers/arango/backup/arango_client.go +++ b/pkg/handlers/backup/arango_client.go @@ -25,7 +25,7 @@ package backup import ( "net/http" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/go-driver" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" diff --git a/pkg/backup/handlers/arango/backup/arango_client_impl.go b/pkg/handlers/backup/arango_client_impl.go similarity index 100% rename from pkg/backup/handlers/arango/backup/arango_client_impl.go rename to pkg/handlers/backup/arango_client_impl.go diff --git a/pkg/backup/handlers/arango/backup/arango_client_mock_test.go b/pkg/handlers/backup/arango_client_mock_test.go similarity index 100% rename from pkg/backup/handlers/arango/backup/arango_client_mock_test.go rename to pkg/handlers/backup/arango_client_mock_test.go diff --git a/pkg/backup/handlers/arango/backup/arango_client_test.go b/pkg/handlers/backup/arango_client_test.go similarity index 100% rename from pkg/backup/handlers/arango/backup/arango_client_test.go rename to pkg/handlers/backup/arango_client_test.go diff --git a/pkg/backup/handlers/arango/backup/backup_suite_test.go b/pkg/handlers/backup/backup_suite_test.go similarity index 97% rename from pkg/backup/handlers/arango/backup/backup_suite_test.go rename to pkg/handlers/backup/backup_suite_test.go index 604c9e2dc..24ffce685 100644 --- a/pkg/backup/handlers/arango/backup/backup_suite_test.go +++ b/pkg/handlers/backup/backup_suite_test.go @@ -35,14 +35,14 @@ import ( "github.com/arangodb/kube-arangodb/pkg/apis/backup" "github.com/arangodb/kube-arangodb/pkg/apis/deployment" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "k8s.io/client-go/kubernetes/fake" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" database "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/state" fakeClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned/fake" + "github.com/arangodb/kube-arangodb/pkg/handlers/backup/state" "github.com/stretchr/testify/require" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" diff --git a/pkg/backup/handlers/arango/backup/errors.go b/pkg/handlers/backup/errors.go similarity index 97% rename from pkg/backup/handlers/arango/backup/errors.go rename to pkg/handlers/backup/errors.go index de4bc5693..c08c1e2d2 100644 --- a/pkg/backup/handlers/arango/backup/errors.go +++ b/pkg/handlers/backup/errors.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" ) func newTemporaryError(err error) error { diff --git a/pkg/backup/handlers/arango/backup/finalizer.go b/pkg/handlers/backup/finalizer.go similarity index 98% rename from pkg/backup/handlers/arango/backup/finalizer.go rename to pkg/handlers/backup/finalizer.go index 7050e9f69..ec801830e 100644 --- a/pkg/backup/handlers/arango/backup/finalizer.go +++ b/pkg/handlers/backup/finalizer.go @@ -27,7 +27,7 @@ import ( "github.com/arangodb/go-driver" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/rs/zerolog/log" apiErrors "k8s.io/apimachinery/pkg/api/errors" meta "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/pkg/backup/handlers/arango/backup/finalizer_test.go b/pkg/handlers/backup/finalizer_test.go similarity index 99% rename from pkg/backup/handlers/arango/backup/finalizer_test.go rename to pkg/handlers/backup/finalizer_test.go index f7268b11b..f2ee0025b 100644 --- a/pkg/backup/handlers/arango/backup/finalizer_test.go +++ b/pkg/handlers/backup/finalizer_test.go @@ -27,7 +27,7 @@ import ( "time" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/pkg/backup/handlers/arango/backup/flow_test.go b/pkg/handlers/backup/flow_test.go similarity index 97% rename from pkg/backup/handlers/arango/backup/flow_test.go rename to pkg/handlers/backup/flow_test.go index cfe9f315f..797b25120 100644 --- a/pkg/backup/handlers/arango/backup/flow_test.go +++ b/pkg/handlers/backup/flow_test.go @@ -26,7 +26,7 @@ import ( "testing" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" ) diff --git a/pkg/backup/handlers/arango/backup/handler.go b/pkg/handlers/backup/handler.go similarity index 97% rename from pkg/backup/handlers/arango/backup/handler.go rename to pkg/handlers/backup/handler.go index 096164842..15c9ba66a 100644 --- a/pkg/backup/handlers/arango/backup/handler.go +++ b/pkg/handlers/backup/handler.go @@ -28,19 +28,18 @@ import ( "sync" "time" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/apis/backup" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "k8s.io/apimachinery/pkg/util/uuid" - "github.com/arangodb/kube-arangodb/pkg/backup/operator" - - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "k8s.io/client-go/kubernetes" diff --git a/pkg/backup/handlers/arango/backup/handler_test.go b/pkg/handlers/backup/handler_test.go similarity index 95% rename from pkg/backup/handlers/arango/backup/handler_test.go rename to pkg/handlers/backup/handler_test.go index 5ac44688d..568a826d0 100644 --- a/pkg/backup/handlers/arango/backup/handler_test.go +++ b/pkg/handlers/backup/handler_test.go @@ -25,7 +25,7 @@ package backup import ( "testing" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" apiErrors "k8s.io/apimachinery/pkg/api/errors" diff --git a/pkg/backup/handlers/arango/backup/lifecycle.go b/pkg/handlers/backup/lifecycle.go similarity index 96% rename from pkg/backup/handlers/arango/backup/lifecycle.go rename to pkg/handlers/backup/lifecycle.go index e01fb98e6..6bb1291b0 100644 --- a/pkg/backup/handlers/arango/backup/lifecycle.go +++ b/pkg/handlers/backup/lifecycle.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/apis/backup" - "github.com/arangodb/kube-arangodb/pkg/backup/operator" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" "github.com/rs/zerolog/log" meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/pkg/backup/handlers/arango/backup/register.go b/pkg/handlers/backup/register.go similarity index 94% rename from pkg/backup/handlers/arango/backup/register.go rename to pkg/handlers/backup/register.go index 872c3b1f7..61674170d 100644 --- a/pkg/backup/handlers/arango/backup/register.go +++ b/pkg/handlers/backup/register.go @@ -25,10 +25,10 @@ package backup import ( "github.com/arangodb/kube-arangodb/pkg/apis/backup" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" arangoInformer "github.com/arangodb/kube-arangodb/pkg/generated/informers/externalversions" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" "k8s.io/client-go/kubernetes" ) diff --git a/pkg/backup/handlers/arango/backup/state.go b/pkg/handlers/backup/state.go similarity index 96% rename from pkg/backup/handlers/arango/backup/state.go rename to pkg/handlers/backup/state.go index ef5e86796..921fe5aa7 100644 --- a/pkg/backup/handlers/arango/backup/state.go +++ b/pkg/handlers/backup/state.go @@ -24,7 +24,7 @@ package backup import ( backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/state" + "github.com/arangodb/kube-arangodb/pkg/handlers/backup/state" ) type stateHolder func(handler *handler, backup *backupApi.ArangoBackup) (*backupApi.ArangoBackupStatus, error) diff --git a/pkg/backup/state/errors.go b/pkg/handlers/backup/state/errors.go similarity index 100% rename from pkg/backup/state/errors.go rename to pkg/handlers/backup/state/errors.go diff --git a/pkg/backup/state/map.go b/pkg/handlers/backup/state/map.go similarity index 100% rename from pkg/backup/state/map.go rename to pkg/handlers/backup/state/map.go diff --git a/pkg/backup/state/map_test.go b/pkg/handlers/backup/state/map_test.go similarity index 100% rename from pkg/backup/state/map_test.go rename to pkg/handlers/backup/state/map_test.go diff --git a/pkg/backup/state/state.go b/pkg/handlers/backup/state/state.go similarity index 100% rename from pkg/backup/state/state.go rename to pkg/handlers/backup/state/state.go diff --git a/pkg/backup/state/state_test.go b/pkg/handlers/backup/state/state_test.go similarity index 100% rename from pkg/backup/state/state_test.go rename to pkg/handlers/backup/state/state_test.go diff --git a/pkg/backup/handlers/arango/backup/state_create.go b/pkg/handlers/backup/state_create.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_create.go rename to pkg/handlers/backup/state_create.go diff --git a/pkg/backup/handlers/arango/backup/state_create_test.go b/pkg/handlers/backup/state_create_test.go similarity index 98% rename from pkg/backup/handlers/arango/backup/state_create_test.go rename to pkg/handlers/backup/state_create_test.go index 73c398743..05d233a67 100644 --- a/pkg/backup/handlers/arango/backup/state_create_test.go +++ b/pkg/handlers/backup/state_create_test.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/go-driver" "github.com/arangodb/kube-arangodb/pkg/util" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_deleted.go b/pkg/handlers/backup/state_deleted.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_deleted.go rename to pkg/handlers/backup/state_deleted.go diff --git a/pkg/backup/handlers/arango/backup/state_deleted_test.go b/pkg/handlers/backup/state_deleted_test.go similarity index 96% rename from pkg/backup/handlers/arango/backup/state_deleted_test.go rename to pkg/handlers/backup/state_deleted_test.go index 209abd1dd..a1de67694 100644 --- a/pkg/backup/handlers/arango/backup/state_deleted_test.go +++ b/pkg/handlers/backup/state_deleted_test.go @@ -25,7 +25,7 @@ package backup import ( "testing" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_download.go b/pkg/handlers/backup/state_download.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_download.go rename to pkg/handlers/backup/state_download.go diff --git a/pkg/backup/handlers/arango/backup/state_download_test.go b/pkg/handlers/backup/state_download_test.go similarity index 98% rename from pkg/backup/handlers/arango/backup/state_download_test.go rename to pkg/handlers/backup/state_download_test.go index 697fd0e22..a026c5890 100644 --- a/pkg/backup/handlers/arango/backup/state_download_test.go +++ b/pkg/handlers/backup/state_download_test.go @@ -26,7 +26,7 @@ import ( "testing" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" ) diff --git a/pkg/backup/handlers/arango/backup/state_downloaderror.go b/pkg/handlers/backup/state_downloaderror.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_downloaderror.go rename to pkg/handlers/backup/state_downloaderror.go diff --git a/pkg/backup/handlers/arango/backup/state_downloaderror_test.go b/pkg/handlers/backup/state_downloaderror_test.go similarity index 97% rename from pkg/backup/handlers/arango/backup/state_downloaderror_test.go rename to pkg/handlers/backup/state_downloaderror_test.go index 3d02763cf..7531832de 100644 --- a/pkg/backup/handlers/arango/backup/state_downloaderror_test.go +++ b/pkg/handlers/backup/state_downloaderror_test.go @@ -26,7 +26,7 @@ import ( "testing" "time" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_downloading.go b/pkg/handlers/backup/state_downloading.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_downloading.go rename to pkg/handlers/backup/state_downloading.go diff --git a/pkg/backup/handlers/arango/backup/state_downloading_test.go b/pkg/handlers/backup/state_downloading_test.go similarity index 99% rename from pkg/backup/handlers/arango/backup/state_downloading_test.go rename to pkg/handlers/backup/state_downloading_test.go index 860724912..a5362f967 100644 --- a/pkg/backup/handlers/arango/backup/state_downloading_test.go +++ b/pkg/handlers/backup/state_downloading_test.go @@ -29,7 +29,7 @@ import ( "github.com/arangodb/go-driver" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" ) diff --git a/pkg/backup/handlers/arango/backup/state_failed.go b/pkg/handlers/backup/state_failed.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_failed.go rename to pkg/handlers/backup/state_failed.go diff --git a/pkg/backup/handlers/arango/backup/state_failed_test.go b/pkg/handlers/backup/state_failed_test.go similarity index 95% rename from pkg/backup/handlers/arango/backup/state_failed_test.go rename to pkg/handlers/backup/state_failed_test.go index 2476a28b7..e4362838f 100644 --- a/pkg/backup/handlers/arango/backup/state_failed_test.go +++ b/pkg/handlers/backup/state_failed_test.go @@ -25,7 +25,7 @@ package backup import ( "testing" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_none.go b/pkg/handlers/backup/state_none.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_none.go rename to pkg/handlers/backup/state_none.go diff --git a/pkg/backup/handlers/arango/backup/state_none_test.go b/pkg/handlers/backup/state_none_test.go similarity index 95% rename from pkg/backup/handlers/arango/backup/state_none_test.go rename to pkg/handlers/backup/state_none_test.go index 3b8e9c784..fecb9460b 100644 --- a/pkg/backup/handlers/arango/backup/state_none_test.go +++ b/pkg/handlers/backup/state_none_test.go @@ -26,7 +26,7 @@ import ( "testing" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" ) diff --git a/pkg/backup/handlers/arango/backup/state_pending.go b/pkg/handlers/backup/state_pending.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_pending.go rename to pkg/handlers/backup/state_pending.go diff --git a/pkg/backup/handlers/arango/backup/state_pending_test.go b/pkg/handlers/backup/state_pending_test.go similarity index 98% rename from pkg/backup/handlers/arango/backup/state_pending_test.go rename to pkg/handlers/backup/state_pending_test.go index ab8a7e1f0..13058176e 100644 --- a/pkg/backup/handlers/arango/backup/state_pending_test.go +++ b/pkg/handlers/backup/state_pending_test.go @@ -31,7 +31,7 @@ import ( "k8s.io/apimachinery/pkg/util/uuid" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_ready.go b/pkg/handlers/backup/state_ready.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_ready.go rename to pkg/handlers/backup/state_ready.go diff --git a/pkg/backup/handlers/arango/backup/state_ready_test.go b/pkg/handlers/backup/state_ready_test.go similarity index 99% rename from pkg/backup/handlers/arango/backup/state_ready_test.go rename to pkg/handlers/backup/state_ready_test.go index 28eb25b96..bc515eedb 100644 --- a/pkg/backup/handlers/arango/backup/state_ready_test.go +++ b/pkg/handlers/backup/state_ready_test.go @@ -32,7 +32,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util" "k8s.io/apimachinery/pkg/util/uuid" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_scheduled.go b/pkg/handlers/backup/state_scheduled.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_scheduled.go rename to pkg/handlers/backup/state_scheduled.go diff --git a/pkg/backup/handlers/arango/backup/state_scheduled_test.go b/pkg/handlers/backup/state_scheduled_test.go similarity index 97% rename from pkg/backup/handlers/arango/backup/state_scheduled_test.go rename to pkg/handlers/backup/state_scheduled_test.go index b5d8f7b69..4e7cc23a6 100644 --- a/pkg/backup/handlers/arango/backup/state_scheduled_test.go +++ b/pkg/handlers/backup/state_scheduled_test.go @@ -25,7 +25,7 @@ package backup import ( "testing" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_unavailable.go b/pkg/handlers/backup/state_unavailable.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_unavailable.go rename to pkg/handlers/backup/state_unavailable.go diff --git a/pkg/backup/handlers/arango/backup/state_unavailable_test.go b/pkg/handlers/backup/state_unavailable_test.go similarity index 98% rename from pkg/backup/handlers/arango/backup/state_unavailable_test.go rename to pkg/handlers/backup/state_unavailable_test.go index 9ac539d93..8dbe5fa44 100644 --- a/pkg/backup/handlers/arango/backup/state_unavailable_test.go +++ b/pkg/handlers/backup/state_unavailable_test.go @@ -26,7 +26,7 @@ import ( "testing" "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" diff --git a/pkg/backup/handlers/arango/backup/state_upload.go b/pkg/handlers/backup/state_upload.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_upload.go rename to pkg/handlers/backup/state_upload.go diff --git a/pkg/backup/handlers/arango/backup/state_upload_test.go b/pkg/handlers/backup/state_upload_test.go similarity index 99% rename from pkg/backup/handlers/arango/backup/state_upload_test.go rename to pkg/handlers/backup/state_upload_test.go index 1a5e0d2a2..8207b4d16 100644 --- a/pkg/backup/handlers/arango/backup/state_upload_test.go +++ b/pkg/handlers/backup/state_upload_test.go @@ -27,7 +27,7 @@ import ( "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/state_uploaderror.go b/pkg/handlers/backup/state_uploaderror.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_uploaderror.go rename to pkg/handlers/backup/state_uploaderror.go diff --git a/pkg/backup/handlers/arango/backup/state_uploaderror_test.go b/pkg/handlers/backup/state_uploaderror_test.go similarity index 98% rename from pkg/backup/handlers/arango/backup/state_uploaderror_test.go rename to pkg/handlers/backup/state_uploaderror_test.go index 11fadfeae..9a87d3fe2 100644 --- a/pkg/backup/handlers/arango/backup/state_uploaderror_test.go +++ b/pkg/handlers/backup/state_uploaderror_test.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" meta "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/pkg/backup/handlers/arango/backup/state_uploading.go b/pkg/handlers/backup/state_uploading.go similarity index 100% rename from pkg/backup/handlers/arango/backup/state_uploading.go rename to pkg/handlers/backup/state_uploading.go diff --git a/pkg/backup/handlers/arango/backup/state_uploading_test.go b/pkg/handlers/backup/state_uploading_test.go similarity index 99% rename from pkg/backup/handlers/arango/backup/state_uploading_test.go rename to pkg/handlers/backup/state_uploading_test.go index 600d82d71..e4a762b8c 100644 --- a/pkg/backup/handlers/arango/backup/state_uploading_test.go +++ b/pkg/handlers/backup/state_uploading_test.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/backup/status.go b/pkg/handlers/backup/status.go similarity index 98% rename from pkg/backup/handlers/arango/backup/status.go rename to pkg/handlers/backup/status.go index 00af57618..671ccbe78 100644 --- a/pkg/backup/handlers/arango/backup/status.go +++ b/pkg/handlers/backup/status.go @@ -30,7 +30,7 @@ import ( "github.com/arangodb/go-driver" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/state" + "github.com/arangodb/kube-arangodb/pkg/handlers/backup/state" "github.com/arangodb/kube-arangodb/pkg/util" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/pkg/backup/handlers/arango/backup/util.go b/pkg/handlers/backup/util.go similarity index 97% rename from pkg/backup/handlers/arango/backup/util.go rename to pkg/handlers/backup/util.go index 0bf1eee7b..64795262d 100644 --- a/pkg/backup/handlers/arango/backup/util.go +++ b/pkg/handlers/backup/util.go @@ -33,7 +33,7 @@ import ( meta "k8s.io/apimachinery/pkg/apis/meta/v1" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/state" + "github.com/arangodb/kube-arangodb/pkg/handlers/backup/state" ) var ( diff --git a/pkg/handlers/job/handler.go b/pkg/handlers/job/handler.go new file mode 100644 index 000000000..8bfae9524 --- /dev/null +++ b/pkg/handlers/job/handler.go @@ -0,0 +1,175 @@ +// +// DISCLAIMER +// +// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package job + +import ( + "context" + "fmt" + "os" + "reflect" + + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" + "github.com/arangodb/kube-arangodb/pkg/util/constants" + + "github.com/arangodb/kube-arangodb/pkg/apis/apps" + appsApi "github.com/arangodb/kube-arangodb/pkg/apis/apps/v1" + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" + arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" + + batchv1 "k8s.io/api/batch/v1" + core "k8s.io/api/core/v1" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" +) + +const ( + jobCreatedUpdated = "ArangoJobCreatedOrUpdated" + jobError = "Error" +) + +type handler struct { + client arangoClientSet.Interface + kubeClient kubernetes.Interface + eventRecorder event.RecorderInstance + + operator operator.Operator +} + +func (*handler) Name() string { + return apps.ArangoJobResourceKind +} + +func (h *handler) Handle(item operation.Item) error { + // Do not act on delete event + if item.Operation == operation.Delete { + return nil + } + + // Get Job object. It also covers NotFound case + job, err := h.client.AppsV1().ArangoJobs(item.Namespace).Get(context.Background(), item.Name, meta.GetOptions{}) + if err != nil { + if k8sutil.IsNotFound(err) { + return nil + } + h.operator.GetLogger().Error().Msgf("ArangoJob fetch error %v", err) + return err + } + + status := h.processArangoJob(job.DeepCopy()) + if reflect.DeepEqual(job.Status, status) { + return nil + } + + job.Status = status + + // Update status on object + if _, err = h.client.AppsV1().ArangoJobs(item.Namespace).UpdateStatus(context.Background(), job, meta.UpdateOptions{}); err != nil { + h.operator.GetLogger().Error().Msgf("ArangoJob status update error %v", err) + return err + } + + return nil +} + +func (h *handler) createFailedJobStatusWithEvent(msg string, job *appsApi.ArangoJob) batchv1.JobStatus { + h.eventRecorder.Warning(job, jobError, msg) + return batchv1.JobStatus{ + Conditions: []batchv1.JobCondition{ + { + Type: batchv1.JobFailed, + Status: core.ConditionUnknown, + Message: msg, + }, + }, + } +} + +func (h *handler) processArangoJob(job *appsApi.ArangoJob) batchv1.JobStatus { + existingJob, err := h.kubeClient.BatchV1().Jobs(job.Namespace).Get(context.Background(), job.Name, meta.GetOptions{}) + if err != nil { + if k8sutil.IsNotFound(err) { + k8sJob, err := h.prepareK8sJob(job) + if err != nil { + return h.createFailedJobStatusWithEvent(fmt.Sprintf("can not prepare k8s Job: %s", err.Error()), job) + } + + existingJob, err = h.kubeClient.BatchV1().Jobs(job.Namespace).Create(context.Background(), k8sJob, meta.CreateOptions{}) + if err != nil { + return h.createFailedJobStatusWithEvent(fmt.Sprintf("can not create k8s Job: %s", err.Error()), job) + } + h.eventRecorder.Normal(job, jobCreatedUpdated, "Arango job has been updated/created") + } else { + return h.createFailedJobStatusWithEvent(fmt.Sprintf("can not check if k8s Job alreadt exist: %s", err.Error()), job) + } + } + + return existingJob.Status +} + +func (h *handler) prepareK8sJob(job *appsApi.ArangoJob) (*batchv1.Job, error) { + k8sJob := batchv1.Job{} + k8sJob.Name = job.Name + k8sJob.Namespace = job.Namespace + k8sJob.Spec = *job.Spec.JobTemplate + k8sJob.Spec.Template.Spec.ServiceAccountName = os.Getenv(constants.EnvArangoJobSAName) + k8sJob.SetOwnerReferences(append(job.GetOwnerReferences(), job.AsOwner())) + + deployment, err := h.client.DatabaseV1().ArangoDeployments(job.Namespace).Get(context.Background(), job.Spec.ArangoDeploymentName, meta.GetOptions{}) + if err != nil { + h.operator.GetLogger().Error().Msgf("ArangoDeployment fetch error %v", err) + return &k8sJob, err + } + + if deployment.Spec.TLS.IsSecure() { + k8sJob.Spec.Template.Spec.Volumes = []core.Volume{ + { + Name: k8sutil.TlsKeyfileVolumeName, + VolumeSource: core.VolumeSource{ + Secret: &core.SecretVolumeSource{ + SecretName: deployment.Spec.TLS.GetCASecretName(), + }, + }, + }, + } + } + + executable, err := os.Executable() + if err != nil { + h.operator.GetLogger().Error().Msgf("reading Operator executable name error %v", err) + return &k8sJob, err + } + + initContainer := k8sutil.ArangodWaiterInitContainer(api.ServerGroupReservedInitContainerNameWait, deployment.Name, executable, + h.operator.Image(), deployment.Spec.TLS.IsSecure(), &core.SecurityContext{}) + + k8sJob.Spec.Template.Spec.InitContainers = append(k8sJob.Spec.Template.Spec.InitContainers, initContainer) + + return &k8sJob, nil +} + +func (*handler) CanBeHandled(item operation.Item) bool { + return item.Group == appsApi.SchemeGroupVersion.Group && + item.Version == appsApi.SchemeGroupVersion.Version && + item.Kind == apps.ArangoJobResourceKind +} diff --git a/pkg/handlers/job/handler_scheduler_test.go b/pkg/handlers/job/handler_scheduler_test.go new file mode 100644 index 000000000..ad35f8394 --- /dev/null +++ b/pkg/handlers/job/handler_scheduler_test.go @@ -0,0 +1,97 @@ +// +// DISCLAIMER +// +// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package job + +import ( + "testing" + + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + batchv1 "k8s.io/api/batch/v1" + "k8s.io/apimachinery/pkg/util/uuid" + + "github.com/stretchr/testify/require" +) + +func Test_Job_Create(t *testing.T) { + // Arrange + handler := newFakeHandler() + + name := string(uuid.NewUUID()) + namespace := string(uuid.NewUUID()) + deployment := string(uuid.NewUUID()) + + job := newArangoJob(name, namespace, deployment) + database := newArangoDeployment(deployment, namespace) + + // Act + createArangoJob(t, handler, job) + createArangoDeployment(t, handler, database) + require.NoError(t, handler.Handle(newItemFromJob(operation.Add, job))) + + // Assert + newJob := refreshArangoJob(t, handler, job) + require.Empty(t, newJob.Status.Conditions) + require.True(t, len(newJob.Spec.JobTemplate.Template.Spec.Containers) == 1) + require.True(t, newJob.Spec.JobTemplate.Template.Spec.Containers[0].Image == job.Spec.JobTemplate.Template.Spec.Containers[0].Image) +} + +func Test_Job_Update(t *testing.T) { + // Arrange + handler := newFakeHandler() + + name := string(uuid.NewUUID()) + namespace := string(uuid.NewUUID()) + deployment := string(uuid.NewUUID()) + + job := newArangoJob(name, namespace, deployment) + k8sJob := newK8sJob(name, namespace) + + // Act + createArangoJob(t, handler, job) + createK8sJob(t, handler, k8sJob) + require.NoError(t, handler.Handle(newItemFromJob(operation.Update, job))) + + // Assert + newJob := refreshArangoJob(t, handler, job) + require.Empty(t, newJob.Status.Conditions) + require.True(t, len(newJob.Spec.JobTemplate.Template.Spec.Containers) == 1) + require.True(t, newJob.Spec.JobTemplate.Template.Spec.Containers[0].Image == job.Spec.JobTemplate.Template.Spec.Containers[0].Image) +} + +func Test_Job_Create_Error_If_Deployment_Not_Exist(t *testing.T) { + // Arrange + handler := newFakeHandler() + + name := string(uuid.NewUUID()) + namespace := string(uuid.NewUUID()) + deployment := string(uuid.NewUUID()) + + job := newArangoJob(name, namespace, deployment) + + // Act + createArangoJob(t, handler, job) + require.NoError(t, handler.Handle(newItemFromJob(operation.Update, job))) + + // Assert + newJob := refreshArangoJob(t, handler, job) + require.True(t, len(newJob.Status.Conditions) == 1) + require.True(t, newJob.Status.Conditions[0].Type == batchv1.JobFailed) +} diff --git a/pkg/backup/utils/json.go b/pkg/handlers/job/handler_test.go similarity index 50% rename from pkg/backup/utils/json.go rename to pkg/handlers/job/handler_test.go index a8de0f879..262606ec7 100644 --- a/pkg/backup/utils/json.go +++ b/pkg/handlers/job/handler_test.go @@ -17,37 +17,42 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Adam Janikowski -// -package utils +package job import ( - jsonpatch "github.com/evanphx/json-patch" - "k8s.io/apimachinery/pkg/util/json" + "testing" + + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + + "github.com/stretchr/testify/require" + apiErrors "k8s.io/apimachinery/pkg/api/errors" ) -// Patch creates JSONPatch -func Patch(old, new interface{}) ([]byte, error) { - oldData, err := json.Marshal(old) - if err != nil { - return nil, err - } +func Test_ObjectNotFound(t *testing.T) { + // Arrange + handler := newFakeHandler() - newData, err := json.Marshal(new) - if err != nil { - return nil, err - } + i := newItem(operation.Add, "test", "test") - patch, err := jsonpatch.CreateMergePatch(oldData, newData) - if err != nil { - return nil, err + actions := map[operation.Operation]bool{ + operation.Add: false, + operation.Update: false, + operation.Delete: false, } - data, err := json.Marshal(patch) - if err != nil { - return nil, err - } + // Act + for operation, shouldFail := range actions { + t.Run(string(operation), func(t *testing.T) { + err := handler.Handle(i) - return data, nil + // Assert + if shouldFail { + require.Error(t, err) + require.True(t, apiErrors.IsNotFound(err)) + } else { + require.NoError(t, err) + } + }) + } } diff --git a/pkg/handlers/job/job_suite_test.go b/pkg/handlers/job/job_suite_test.go new file mode 100644 index 000000000..3ca069f7a --- /dev/null +++ b/pkg/handlers/job/job_suite_test.go @@ -0,0 +1,158 @@ +// +// DISCLAIMER +// +// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package job + +import ( + "context" + "testing" + + "github.com/arangodb/kube-arangodb/pkg/apis/apps" + appsApi "github.com/arangodb/kube-arangodb/pkg/apis/apps/v1" + "github.com/arangodb/kube-arangodb/pkg/apis/deployment" + database "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" + deploymentApi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v2alpha1" + fakeClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned/fake" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" + + "github.com/rs/zerolog/log" + "github.com/stretchr/testify/require" + batchv1 "k8s.io/api/batch/v1" + v1 "k8s.io/api/core/v1" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/uuid" + "k8s.io/client-go/kubernetes/fake" +) + +func newFakeHandler() *handler { + f := fakeClientSet.NewSimpleClientset() + k := fake.NewSimpleClientset() + + h := &handler{ + client: f, + kubeClient: k, + eventRecorder: newEventInstance(event.NewEventRecorder(log.Logger, "mock", k)), + operator: operator.NewOperator(log.Logger, "mock", "mock", "mock"), + } + + return h +} + +func newItem(o operation.Operation, namespace, name string) operation.Item { + return operation.Item{ + Group: appsApi.SchemeGroupVersion.Group, + Version: appsApi.SchemeGroupVersion.Version, + Kind: apps.ArangoJobResourceKind, + + Operation: o, + + Namespace: namespace, + Name: name, + } +} + +func newItemFromJob(operation operation.Operation, job *appsApi.ArangoJob) operation.Item { // nolint:unparam + return newItem(operation, job.Namespace, job.Name) +} + +func refreshArangoJob(t *testing.T, h *handler, job *appsApi.ArangoJob) *appsApi.ArangoJob { + newJob, err := h.client.AppsV1().ArangoJobs(job.Namespace).Get(context.Background(), job.Name, meta.GetOptions{}) + require.NoError(t, err) + + return newJob +} + +func createArangoJob(t *testing.T, h *handler, jobs ...*appsApi.ArangoJob) { + for _, job := range jobs { + _, err := h.client.AppsV1().ArangoJobs(job.Namespace).Create(context.Background(), job, meta.CreateOptions{}) + require.NoError(t, err) + } +} + +func createK8sJob(t *testing.T, h *handler, jobs ...*batchv1.Job) { + for _, job := range jobs { + _, err := h.kubeClient.BatchV1().Jobs(job.Namespace).Create(context.Background(), job, meta.CreateOptions{}) + require.NoError(t, err) + } +} + +func createArangoDeployment(t *testing.T, h *handler, deployments ...*database.ArangoDeployment) { + for _, deployment := range deployments { + _, err := h.client.DatabaseV1().ArangoDeployments(deployment.Namespace).Create(context.Background(), deployment, meta.CreateOptions{}) + require.NoError(t, err) + } +} + +func newArangoJob(name, namespace, deployment string) *appsApi.ArangoJob { + return &appsApi.ArangoJob{ + TypeMeta: meta.TypeMeta{ + APIVersion: appsApi.SchemeGroupVersion.String(), + Kind: apps.ArangoJobResourceKind, + }, + ObjectMeta: meta.ObjectMeta{ + Name: name, + Namespace: namespace, + UID: uuid.NewUUID(), + }, + Spec: appsApi.ArangoJobSpec{ + ArangoDeploymentName: deployment, + JobTemplate: &batchv1.JobSpec{ + Template: v1.PodTemplateSpec{ + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Image: "perl", + Name: "pi", + Args: []string{"perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"}, + }, + }, + RestartPolicy: v1.RestartPolicyNever, + }, + }, + }, + }, + } +} + +func newArangoDeployment(name, namespace string) *database.ArangoDeployment { + return &database.ArangoDeployment{ + TypeMeta: meta.TypeMeta{ + APIVersion: deploymentApi.SchemeGroupVersion.String(), + Kind: deployment.ArangoDeploymentResourceKind, + }, + ObjectMeta: meta.ObjectMeta{ + Name: name, + Namespace: namespace, + UID: uuid.NewUUID(), + }, + } +} + +func newK8sJob(name, namespace string) *batchv1.Job { + return &batchv1.Job{ + ObjectMeta: meta.ObjectMeta{ + Name: name, + Namespace: namespace, + UID: uuid.NewUUID(), + }, + } +} diff --git a/pkg/handlers/job/lifecycle.go b/pkg/handlers/job/lifecycle.go new file mode 100644 index 000000000..710ebfba2 --- /dev/null +++ b/pkg/handlers/job/lifecycle.go @@ -0,0 +1,59 @@ +// +// DISCLAIMER +// +// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// +// Author Adam Janikowski +// + +package job + +import ( + "context" + "time" + + "github.com/arangodb/kube-arangodb/pkg/apis/apps" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" + + "github.com/rs/zerolog/log" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +var _ operator.LifecyclePreStart = &handler{} + +// LifecyclePreStart is executed before operator starts to work, additional checks can be placed here +// Wait for CR to be present +func (h *handler) LifecyclePreStart() error { + log.Info().Msgf("Starting Lifecycle PreStart for %s", h.Name()) + + defer func() { + log.Info().Msgf("Lifecycle PreStart for %s completed", h.Name()) + }() + + for { + _, err := h.client.AppsV1().ArangoJobs(h.operator.Namespace()).List(context.Background(), meta.ListOptions{}) + + if err != nil { + log.Warn().Err(err).Msgf("CR for %s not found", apps.ArangoJobResourceKind) + + time.Sleep(250 * time.Millisecond) + continue + } + + return nil + } +} diff --git a/pkg/handlers/job/register.go b/pkg/handlers/job/register.go new file mode 100644 index 000000000..4c8e526ff --- /dev/null +++ b/pkg/handlers/job/register.go @@ -0,0 +1,62 @@ +// +// DISCLAIMER +// +// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package job + +import ( + "github.com/arangodb/kube-arangodb/pkg/apis/apps" + appsApi "github.com/arangodb/kube-arangodb/pkg/apis/apps/v1" + arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" + arangoInformer "github.com/arangodb/kube-arangodb/pkg/generated/informers/externalversions" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" + + "k8s.io/client-go/kubernetes" +) + +func newEventInstance(eventRecorder event.Recorder) event.RecorderInstance { + return eventRecorder.NewInstance(appsApi.SchemeGroupVersion.Group, + appsApi.SchemeGroupVersion.Version, + apps.ArangoJobResourceKind) +} + +// RegisterInformer into operator +func RegisterInformer(operator operator.Operator, recorder event.Recorder, client arangoClientSet.Interface, kubeClient kubernetes.Interface, informer arangoInformer.SharedInformerFactory) error { + if err := operator.RegisterInformer(informer.Apps().V1().ArangoJobs().Informer(), + appsApi.SchemeGroupVersion.Group, + appsApi.SchemeGroupVersion.Version, + apps.ArangoJobResourceKind); err != nil { + return err + } + + h := &handler{ + client: client, + kubeClient: kubeClient, + eventRecorder: newEventInstance(recorder), + + operator: operator, + } + + if err := operator.RegisterHandler(h); err != nil { + return err + } + + return nil +} diff --git a/pkg/backup/handlers/arango/policy/handler.go b/pkg/handlers/policy/handler.go similarity index 96% rename from pkg/backup/handlers/arango/policy/handler.go rename to pkg/handlers/policy/handler.go index 1f2cc690d..09983ce38 100644 --- a/pkg/backup/handlers/arango/policy/handler.go +++ b/pkg/handlers/policy/handler.go @@ -29,12 +29,11 @@ import ( "time" "github.com/arangodb/kube-arangodb/pkg/apis/backup" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" - "github.com/arangodb/kube-arangodb/pkg/backup/operator" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" - - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "k8s.io/client-go/kubernetes" diff --git a/pkg/backup/handlers/arango/policy/handler_scheduler_test.go b/pkg/handlers/policy/handler_scheduler_test.go similarity index 99% rename from pkg/backup/handlers/arango/policy/handler_scheduler_test.go rename to pkg/handlers/policy/handler_scheduler_test.go index 10950bd69..2cf59a64c 100644 --- a/pkg/backup/handlers/arango/policy/handler_scheduler_test.go +++ b/pkg/handlers/policy/handler_scheduler_test.go @@ -26,7 +26,7 @@ import ( "testing" "time" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" "github.com/stretchr/testify/require" diff --git a/pkg/backup/handlers/arango/policy/handler_suite_test.go b/pkg/handlers/policy/handler_suite_test.go similarity index 97% rename from pkg/backup/handlers/arango/policy/handler_suite_test.go rename to pkg/handlers/policy/handler_suite_test.go index a5d28b441..440f46bff 100644 --- a/pkg/backup/handlers/arango/policy/handler_suite_test.go +++ b/pkg/handlers/policy/handler_suite_test.go @@ -32,8 +32,8 @@ import ( "github.com/arangodb/kube-arangodb/pkg/apis/backup" "github.com/arangodb/kube-arangodb/pkg/apis/deployment" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "k8s.io/client-go/kubernetes/fake" diff --git a/pkg/backup/handlers/arango/policy/handler_test.go b/pkg/handlers/policy/handler_test.go similarity index 95% rename from pkg/backup/handlers/arango/policy/handler_test.go rename to pkg/handlers/policy/handler_test.go index 687c36ec5..63253b587 100644 --- a/pkg/backup/handlers/arango/policy/handler_test.go +++ b/pkg/handlers/policy/handler_test.go @@ -25,7 +25,7 @@ package policy import ( "testing" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" apiErrors "k8s.io/apimachinery/pkg/api/errors" diff --git a/pkg/backup/handlers/arango/policy/lifecycle.go b/pkg/handlers/policy/lifecycle.go similarity index 96% rename from pkg/backup/handlers/arango/policy/lifecycle.go rename to pkg/handlers/policy/lifecycle.go index a34c6e25b..d181efceb 100644 --- a/pkg/backup/handlers/arango/policy/lifecycle.go +++ b/pkg/handlers/policy/lifecycle.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/apis/backup" - "github.com/arangodb/kube-arangodb/pkg/backup/operator" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" "github.com/rs/zerolog/log" meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) diff --git a/pkg/backup/handlers/arango/policy/register.go b/pkg/handlers/policy/register.go similarity index 94% rename from pkg/backup/handlers/arango/policy/register.go rename to pkg/handlers/policy/register.go index ce2b67804..82c67e396 100644 --- a/pkg/backup/handlers/arango/policy/register.go +++ b/pkg/handlers/policy/register.go @@ -25,10 +25,10 @@ package policy import ( "github.com/arangodb/kube-arangodb/pkg/apis/backup" backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/operator" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" arangoInformer "github.com/arangodb/kube-arangodb/pkg/generated/informers/externalversions" + operator "github.com/arangodb/kube-arangodb/pkg/operatorV2" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" "k8s.io/client-go/kubernetes" ) diff --git a/pkg/backup/utils/errors.go b/pkg/handlers/utils/errors.go similarity index 100% rename from pkg/backup/utils/errors.go rename to pkg/handlers/utils/errors.go diff --git a/pkg/backup/utils/ints.go b/pkg/handlers/utils/ints.go similarity index 100% rename from pkg/backup/utils/ints.go rename to pkg/handlers/utils/ints.go diff --git a/pkg/backup/utils/retry.go b/pkg/handlers/utils/retry.go similarity index 100% rename from pkg/backup/utils/retry.go rename to pkg/handlers/utils/retry.go diff --git a/pkg/backup/utils/strings.go b/pkg/handlers/utils/strings.go similarity index 100% rename from pkg/backup/utils/strings.go rename to pkg/handlers/utils/strings.go diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index b5ade5d3b..270d359e8 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -24,47 +24,51 @@ package operator import ( "context" + "fmt" "math/rand" "time" - "github.com/arangodb/kube-arangodb/pkg/operator/scope" - monitoringClient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1" - - "github.com/arangodb/kube-arangodb/pkg/backup/operator/event" - "github.com/arangodb/kube-arangodb/pkg/util/constants" - "github.com/prometheus/client_golang/prometheus" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" kwatch "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" deplapi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" replapi "github.com/arangodb/kube-arangodb/pkg/apis/replication/v1" lsapi "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha" - "github.com/arangodb/kube-arangodb/pkg/backup/handlers/arango/backup" - "github.com/arangodb/kube-arangodb/pkg/backup/handlers/arango/policy" - backupOper "github.com/arangodb/kube-arangodb/pkg/backup/operator" "github.com/arangodb/kube-arangodb/pkg/deployment" "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" + arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" + arangoInformer "github.com/arangodb/kube-arangodb/pkg/generated/informers/externalversions" + "github.com/arangodb/kube-arangodb/pkg/handlers/backup" + "github.com/arangodb/kube-arangodb/pkg/handlers/job" + "github.com/arangodb/kube-arangodb/pkg/handlers/policy" "github.com/arangodb/kube-arangodb/pkg/logging" + "github.com/arangodb/kube-arangodb/pkg/operator/scope" + operatorV2 "github.com/arangodb/kube-arangodb/pkg/operatorV2" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/event" "github.com/arangodb/kube-arangodb/pkg/replication" "github.com/arangodb/kube-arangodb/pkg/storage" + "github.com/arangodb/kube-arangodb/pkg/util/constants" "github.com/arangodb/kube-arangodb/pkg/util/probe" - "k8s.io/client-go/rest" - - arangoClientSet "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned" - arangoInformer "github.com/arangodb/kube-arangodb/pkg/generated/informers/externalversions" ) const ( initRetryWaitTime = 30 * time.Second ) +type operatorV2type string + +const ( + backupOperator operatorV2type = "backup" + appsOperator operatorV2type = "apps" +) + type Event struct { Type kwatch.EventType Deployment *deplapi.ArangoDeployment @@ -93,6 +97,7 @@ type Config struct { EnableDeploymentReplication bool EnableStorage bool EnableBackup bool + EnableApps bool AllowChaos bool ScalingIntegrationEnabled bool SingleMode bool @@ -111,6 +116,7 @@ type Dependencies struct { DeploymentReplicationProbe *probe.ReadyProbe StorageProbe *probe.ReadyProbe BackupProbe *probe.ReadyProbe + AppsProbe *probe.ReadyProbe } // NewOperator instantiates a new operator from given config & dependencies. @@ -156,6 +162,13 @@ func (o *Operator) Run() { go o.runWithoutLeaderElection("arango-backup-operator", constants.BackupLabelRole, o.onStartBackup, o.Dependencies.BackupProbe) } } + if o.Config.EnableApps { + if !o.Config.SingleMode { + go o.runLeaderElection("arango-apps-operator", constants.AppsLabelRole, o.onStartApps, o.Dependencies.AppsProbe) + } else { + go o.runWithoutLeaderElection("arango-apps-operator", constants.AppsLabelRole, o.onStartApps, o.Dependencies.AppsProbe) + } + } // Wait until process terminates <-context.TODO().Done() } @@ -202,8 +215,18 @@ func (o *Operator) onStartStorage(stop <-chan struct{}) { o.runLocalStorages(stop) } -// onStartBackup starts the backup operator and run till given channel is closed. +// onStartBackup starts the operator and run till given channel is closed. func (o *Operator) onStartBackup(stop <-chan struct{}) { + o.onStartOperatorV2(backupOperator, stop) +} + +// onStartApps starts the operator and run till given channel is closed. +func (o *Operator) onStartApps(stop <-chan struct{}) { + o.onStartOperatorV2(appsOperator, stop) +} + +// onStartOperatorV2 run the operatorV2 type +func (o *Operator) onStartOperatorV2(operatorType operatorV2type, stop <-chan struct{}) { for { if err := o.waitForCRD(false, false, false, true); err == nil { break @@ -213,8 +236,8 @@ func (o *Operator) onStartBackup(stop <-chan struct{}) { time.Sleep(initRetryWaitTime) } } - operatorName := "arangodb-backup-operator" - operator := backupOper.NewOperator(o.Dependencies.LogService.MustGetLogger(logging.LoggerNameReconciliation), operatorName, o.Namespace) + operatorName := fmt.Sprintf("arangodb-%s-operator", operatorType) + operator := operatorV2.NewOperator(o.Dependencies.LogService.MustGetLogger(logging.LoggerNameReconciliation), operatorName, o.Namespace, o.OperatorImage) rand.Seed(time.Now().Unix()) @@ -239,12 +262,18 @@ func (o *Operator) onStartBackup(stop <-chan struct{}) { arangoInformer := arangoInformer.NewSharedInformerFactoryWithOptions(arangoClientSet, 10*time.Second, arangoInformer.WithNamespace(o.Namespace)) - if err = backup.RegisterInformer(operator, eventRecorder, arangoClientSet, kubeClientSet, arangoInformer); err != nil { - panic(err) - } - - if err = policy.RegisterInformer(operator, eventRecorder, arangoClientSet, kubeClientSet, arangoInformer); err != nil { - panic(err) + switch operatorType { + case appsOperator: + if err = job.RegisterInformer(operator, eventRecorder, arangoClientSet, kubeClientSet, arangoInformer); err != nil { + panic(err) + } + case backupOperator: + if err = backup.RegisterInformer(operator, eventRecorder, arangoClientSet, kubeClientSet, arangoInformer); err != nil { + panic(err) + } + if err = policy.RegisterInformer(operator, eventRecorder, arangoClientSet, kubeClientSet, arangoInformer); err != nil { + panic(err) + } } if err = operator.RegisterStarter(arangoInformer); err != nil { diff --git a/pkg/backup/operator/event/event.go b/pkg/operatorV2/event/event.go similarity index 100% rename from pkg/backup/operator/event/event.go rename to pkg/operatorV2/event/event.go diff --git a/pkg/backup/operator/event/event_test.go b/pkg/operatorV2/event/event_test.go similarity index 100% rename from pkg/backup/operator/event/event_test.go rename to pkg/operatorV2/event/event_test.go diff --git a/pkg/backup/operator/handler.go b/pkg/operatorV2/handler.go similarity index 92% rename from pkg/backup/operator/handler.go rename to pkg/operatorV2/handler.go index e1a2f6c4a..518af26a7 100644 --- a/pkg/backup/operator/handler.go +++ b/pkg/operatorV2/handler.go @@ -22,7 +22,7 @@ package operator -import "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" +import "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" // Handler define interface for operator actions type Handler interface { diff --git a/pkg/backup/operator/informer.go b/pkg/operatorV2/informer.go similarity index 96% rename from pkg/backup/operator/informer.go rename to pkg/operatorV2/informer.go index 6f0a255c2..09ab9f481 100644 --- a/pkg/backup/operator/informer.go +++ b/pkg/operatorV2/informer.go @@ -23,7 +23,7 @@ package operator import ( - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/cache" ) diff --git a/pkg/backup/operator/lifecycle.go b/pkg/operatorV2/lifecycle.go similarity index 100% rename from pkg/backup/operator/lifecycle.go rename to pkg/operatorV2/lifecycle.go diff --git a/pkg/backup/operator/operation/item.go b/pkg/operatorV2/operation/item.go similarity index 100% rename from pkg/backup/operator/operation/item.go rename to pkg/operatorV2/operation/item.go diff --git a/pkg/backup/operator/operation/item_test.go b/pkg/operatorV2/operation/item_test.go similarity index 100% rename from pkg/backup/operator/operation/item_test.go rename to pkg/operatorV2/operation/item_test.go diff --git a/pkg/backup/operator/operator.go b/pkg/operatorV2/operator.go similarity index 93% rename from pkg/backup/operator/operator.go rename to pkg/operatorV2/operator.go index 0c5f0c097..864b7149b 100644 --- a/pkg/backup/operator/operator.go +++ b/pkg/operatorV2/operator.go @@ -30,7 +30,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/errors" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/prometheus/client_golang/prometheus" @@ -51,6 +51,7 @@ type Operator interface { Name() string Namespace() string + Image() string Start(threadiness int, stopCh <-chan struct{}) error @@ -60,13 +61,16 @@ type Operator interface { EnqueueItem(item operation.Item) ProcessItem(item operation.Item) error + + GetLogger() *zerolog.Logger } // NewOperator creates new operator -func NewOperator(logger zerolog.Logger, name, namespace string) Operator { +func NewOperator(logger zerolog.Logger, name, namespace, image string) Operator { o := &operator{ name: name, namespace: namespace, + image: image, logger: logger, workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name), } @@ -86,6 +90,7 @@ type operator struct { name string namespace string + image string informers []cache.SharedInformer starters []Starter @@ -105,6 +110,10 @@ func (o *operator) Name() string { return o.name } +func (o *operator) Image() string { + return o.image +} + func (o *operator) ProcessItem(item operation.Item) error { { o.lock.Lock() @@ -181,6 +190,10 @@ func (o *operator) RegisterInformer(informer cache.SharedIndexInformer, group, v return nil } +func (o *operator) GetLogger() *zerolog.Logger { + return &o.logger +} + func (o *operator) Start(threadiness int, stopCh <-chan struct{}) error { o.lock.Lock() defer o.lock.Unlock() diff --git a/pkg/backup/operator/operator_suite_test.go b/pkg/operatorV2/operator_suite_test.go similarity index 97% rename from pkg/backup/operator/operator_suite_test.go rename to pkg/operatorV2/operator_suite_test.go index 51c770578..432d316f9 100644 --- a/pkg/backup/operator/operator_suite_test.go +++ b/pkg/operatorV2/operator_suite_test.go @@ -27,7 +27,7 @@ import ( "testing" "time" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/require" ) diff --git a/pkg/backup/operator/operator_test.go b/pkg/operatorV2/operator_test.go similarity index 96% rename from pkg/backup/operator/operator_test.go rename to pkg/operatorV2/operator_test.go index d09eeff1d..f3d13a504 100644 --- a/pkg/backup/operator/operator_test.go +++ b/pkg/operatorV2/operator_test.go @@ -28,7 +28,7 @@ import ( "github.com/rs/zerolog/log" - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/stretchr/testify/assert" @@ -45,7 +45,7 @@ import ( func Test_Operator_InformerProcessing(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) size := 64 objects := make([]string, size) @@ -94,7 +94,7 @@ func Test_Operator_InformerProcessing(t *testing.T) { func Test_Operator_MultipleInformers(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) size := 16 objects := make([]string, size) @@ -155,7 +155,7 @@ func Test_Operator_MultipleInformers(t *testing.T) { func Test_Operator_MultipleInformers_IgnoredTypes(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) size := 16 objects := make([]string, size) @@ -215,7 +215,7 @@ func Test_Operator_MultipleInformers_IgnoredTypes(t *testing.T) { func Test_Operator_MultipleInformers_MultipleHandlers(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) size := 16 objects := make([]string, size) @@ -323,7 +323,7 @@ func Test_Operator_MultipleInformers_MultipleHandlers(t *testing.T) { func Test_Operator_InformerProcessing_Namespaced(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) size := 16 objects := make([]string, size) diff --git a/pkg/backup/operator/operator_worker.go b/pkg/operatorV2/operator_worker.go similarity index 97% rename from pkg/backup/operator/operator_worker.go rename to pkg/operatorV2/operator_worker.go index 1b692ba82..0812cc100 100644 --- a/pkg/backup/operator/operator_worker.go +++ b/pkg/operatorV2/operator_worker.go @@ -23,7 +23,7 @@ package operator import ( - "github.com/arangodb/kube-arangodb/pkg/backup/operator/operation" + "github.com/arangodb/kube-arangodb/pkg/operatorV2/operation" "github.com/arangodb/kube-arangodb/pkg/util/errors" ) diff --git a/pkg/backup/operator/operator_worker_test.go b/pkg/operatorV2/operator_worker_test.go similarity index 93% rename from pkg/backup/operator/operator_worker_test.go rename to pkg/operatorV2/operator_worker_test.go index d54ce673c..c54edbbbb 100644 --- a/pkg/backup/operator/operator_worker_test.go +++ b/pkg/operatorV2/operator_worker_test.go @@ -35,7 +35,7 @@ import ( func Test_Worker_Empty(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) stopCh := make(chan struct{}) @@ -55,7 +55,7 @@ func Test_Worker_Empty(t *testing.T) { func Test_Worker_CatchAll(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) stopCh := make(chan struct{}) @@ -84,7 +84,7 @@ func Test_Worker_CatchAll(t *testing.T) { func Test_Worker_EnsureFirstProcessStopLoop(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) stopCh := make(chan struct{}) @@ -118,7 +118,7 @@ func Test_Worker_EnsureFirstProcessStopLoop(t *testing.T) { func Test_Worker_EnsureObjectIsProcessedBySecondHandler(t *testing.T) { // Arrange name := string(uuid.NewUUID()) - o := NewOperator(log.Logger, name, name) + o := NewOperator(log.Logger, name, name, name) stopCh := make(chan struct{}) diff --git a/pkg/backup/operator/prometheus.go b/pkg/operatorV2/prometheus.go similarity index 100% rename from pkg/backup/operator/prometheus.go rename to pkg/operatorV2/prometheus.go diff --git a/pkg/server/server.go b/pkg/server/server.go index b0b212a8f..dd8e4783c 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -72,6 +72,7 @@ type Dependencies struct { DeploymentReplication OperatorDependency Storage OperatorDependency Backup OperatorDependency + Apps OperatorDependency Operators Operators Secrets corev1.SecretInterface } diff --git a/pkg/util/constants/constants.go b/pkg/util/constants/constants.go index 817115d8b..5520706f0 100644 --- a/pkg/util/constants/constants.go +++ b/pkg/util/constants/constants.go @@ -29,6 +29,7 @@ const ( EnvOperatorPodName = "MY_POD_NAME" EnvOperatorPodNamespace = "MY_POD_NAMESPACE" EnvOperatorPodIP = "MY_POD_IP" + EnvArangoJobSAName = "ARANGOJOB_SA_NAME" EnvArangoLicenseKey = "ARANGO_LICENSE_KEY" // Contains the License Key for the Docker Image EnvArangoSyncMonitoringToken = "ARANGOSYNC_MONITORING_TOKEN" // Constains monitoring token for ArangoSync servers @@ -59,6 +60,7 @@ const ( AnnotationEnforceAntiAffinity = "database.arangodb.com/enforce-anti-affinity" // Key of annotation added to PVC. Value is a boolean "true" or "false" BackupLabelRole = "backup/role" + AppsLabelRole = "apps/role" LabelRole = "role" LabelRoleLeader = "leader" ) diff --git a/pkg/util/k8sutil/pods.go b/pkg/util/k8sutil/pods.go index afe34f7ae..dba1ef5ba 100644 --- a/pkg/util/k8sutil/pods.go +++ b/pkg/util/k8sutil/pods.go @@ -23,6 +23,7 @@ package k8sutil import ( "context" "fmt" + "os" "path/filepath" "strings" "time" @@ -33,8 +34,9 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/json" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" "github.com/arangodb/kube-arangodb/pkg/util" + "github.com/arangodb/kube-arangodb/pkg/util/constants" "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/interfaces" @@ -388,7 +390,7 @@ func RocksdbEncryptionReadOnlyVolumeMount() core.VolumeMount { } } -// ArangodInitContainer creates a container configured to initalize a UUID file. +// ArangodInitContainer creates a container configured to initialize a UUID file. func ArangodInitContainer(name, id, engine, executable, operatorImage string, requireUUID bool, securityContext *core.SecurityContext) core.Container { uuidFile := filepath.Join(ArangodVolumeMountDir, "UUID") engineFile := filepath.Join(ArangodVolumeMountDir, "ENGINE") @@ -407,6 +409,32 @@ func ArangodInitContainer(name, id, engine, executable, operatorImage string, re if requireUUID { command = append(command, "--require") } + + volumes := []core.VolumeMount{ + ArangodVolumeMount(), + } + return operatorInitContainer(name, operatorImage, command, securityContext, volumes) +} + +// ArangodWaiterInitContainer creates a container configured to wait for specific ArangoDeployment to be ready +func ArangodWaiterInitContainer(name, deploymentName, executable, operatorImage string, isSecured bool, securityContext *core.SecurityContext) core.Container { + var command = []string{ + executable, + "lifecycle", + "wait", + "--deployment-name", + deploymentName, + } + + var volumes []core.VolumeMount + if isSecured { + volumes = append(volumes, TlsKeyfileVolumeMount()) + } + return operatorInitContainer(name, operatorImage, command, securityContext, volumes) +} + +// createInitContainer creates operator-specific init container +func operatorInitContainer(name, operatorImage string, command []string, securityContext *core.SecurityContext, volumes []core.VolumeMount) core.Container { c := core.Container{ Name: name, Image: operatorImage, @@ -421,9 +449,13 @@ func ArangodInitContainer(name, id, engine, executable, operatorImage string, re core.ResourceMemory: resource.MustParse("50Mi"), }, }, - VolumeMounts: []core.VolumeMount{ - ArangodVolumeMount(), + Env: []core.EnvVar{ + { + Name: "MY_POD_NAMESPACE", + Value: os.Getenv(constants.EnvOperatorPodNamespace), + }, }, + VolumeMounts: volumes, SecurityContext: securityContext, } return c diff --git a/pkg/util/k8sutil/pods_test.go b/pkg/util/k8sutil/pods_test.go index 7c517d44a..22d278870 100644 --- a/pkg/util/k8sutil/pods_test.go +++ b/pkg/util/k8sutil/pods_test.go @@ -26,7 +26,7 @@ import ( "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" - "github.com/arangodb/kube-arangodb/pkg/backup/utils" + "github.com/arangodb/kube-arangodb/pkg/handlers/utils" ) // TestIsPodReady tests IsPodReady. diff --git a/pkg/util/signal.go b/pkg/util/signal.go index d6fb2f00a..4cc1355fc 100644 --- a/pkg/util/signal.go +++ b/pkg/util/signal.go @@ -17,8 +17,6 @@ // // Copyright holder is ArangoDB GmbH, Cologne, Germany // -// Author Jakub Wierzbowski -// package util