1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

ArangoJob controller (#874)

This commit is contained in:
jwierzbo 2021-12-31 12:38:53 +01:00 committed by GitHub
parent 69f08e18d3
commit 5846a3f8be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
126 changed files with 1026 additions and 146 deletions

View file

@ -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

View file

@ -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

View file

@ -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: {}

View file

@ -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 }}

View file

@ -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 }}

View file

@ -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 }}

View file

@ -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 }}

View file

@ -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 }}

View file

@ -21,6 +21,5 @@ subjects:
name: {{ template "kube-arangodb.operatorName" . }}
namespace: {{ .Release.Namespace }}
{{- end }}
{{- end }}

View file

@ -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

View file

@ -38,6 +38,7 @@ operator:
deploymentReplications: true
storage: false
backup: false
apps: false
images:
base: alpine:3.11

15
examples/arangojob.yaml Normal file
View file

@ -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

2
go.mod
View file

@ -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

View file

@ -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"

15
main.go
View file

@ -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

View file

@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- arango-apps.yaml

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
package apps

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
// +k8s:deepcopy-gen=package
// +groupName=apps.arangodb.com

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
package v1

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
package v1

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
package v1

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
package v1

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -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"
)

View file

@ -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"
)

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -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"

View file

@ -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"
)

View file

@ -33,6 +33,7 @@ import (
const (
ServerGroupReservedInitContainerNameLifecycle = "init-lifecycle"
ServerGroupReservedInitContainerNameUUID = "uuid"
ServerGroupReservedInitContainerNameWait = "wait"
ServerGroupReservedInitContainerNameUpgrade = "upgrade"
ServerGroupReservedInitContainerNameVersionCheck = "version-check"
)

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -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"

View file

@ -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"
)

View file

@ -33,6 +33,7 @@ import (
const (
ServerGroupReservedInitContainerNameLifecycle = "init-lifecycle"
ServerGroupReservedInitContainerNameUUID = "uuid"
ServerGroupReservedInitContainerNameWait = "wait"
ServerGroupReservedInitContainerNameUpgrade = "upgrade"
ServerGroupReservedInitContainerNameVersionCheck = "version-check"
)

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -1,3 +1,4 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
//

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Jakub Wierzbowski
//
package exporter

View file

@ -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"

View file

@ -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"

View file

@ -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 {

View file

@ -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"

View file

@ -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"
)

View file

@ -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"
)

View file

@ -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"

View file

@ -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"

View file

@ -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"
)

View file

@ -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"
)

View file

@ -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)

View file

@ -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"

View file

@ -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"

View file

@ -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"
)

View file

@ -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"

View file

@ -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"
)

View file

@ -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"

View file

@ -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"
)

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"
)

View file

@ -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 (

175
pkg/handlers/job/handler.go Normal file
View file

@ -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
}

View file

@ -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)
}

View file

@ -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()
i := newItem(operation.Add, "test", "test")
actions := map[operation.Operation]bool{
operation.Add: false,
operation.Update: false,
operation.Delete: false,
}
newData, err := json.Marshal(new)
if err != nil {
return nil, err
}
// Act
for operation, shouldFail := range actions {
t.Run(string(operation), func(t *testing.T) {
err := handler.Handle(i)
patch, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return nil, err
// Assert
if shouldFail {
require.Error(t, err)
require.True(t, apiErrors.IsNotFound(err))
} else {
require.NoError(t, err)
}
})
}
data, err := json.Marshal(patch)
if err != nil {
return nil, err
}
return data, nil
}

View file

@ -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(),
},
}
}

View file

@ -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
}
}

View file

@ -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
}

View file

@ -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"

View file

@ -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"

View file

@ -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"

Some files were not shown because too many files have changed in this diff Show more