From 49a90e7c5cd41ac62c238ec79e41412c3a9fe488 Mon Sep 17 00:00:00 2001 From: jwierzbo Date: Wed, 6 Dec 2023 00:17:35 +0100 Subject: [PATCH] MLCronJob status update --- CHANGELOG.md | 1 + examples/ml/ml-cronjob.yaml | 18 ++++++ pkg/apis/ml/v1alpha1/cronjob_spec.go | 2 +- pkg/apis/ml/v1alpha1/cronjob_status.go | 7 +- pkg/apis/ml/v1alpha1/extension_conditions.go | 3 + pkg/apis/ml/v1alpha1/zz_generated.deepcopy.go | 4 +- pkg/util/tests/kubernetes.go | 64 +++++++++++++++++++ 7 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 examples/ml/ml-cronjob.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 341baf69f..c74677641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - (Feature) (ML) Extension Storage Condition - (Improvement) (ML) Switch to fsnotify for file watching for MacOS support - (Feature) (ML) Unify Images, Resources and Lifecycle +- (Improvement) (ML) CronJob status update ## [1.2.35](https://github.com/arangodb/kube-arangodb/tree/1.2.35) (2023-11-06) - (Maintenance) Update go-driver to v1.6.0, update IsNotFound() checks diff --git a/examples/ml/ml-cronjob.yaml b/examples/ml/ml-cronjob.yaml new file mode 100644 index 000000000..37c8c9edf --- /dev/null +++ b/examples/ml/ml-cronjob.yaml @@ -0,0 +1,18 @@ +apiVersion: ml.arangodb.com/v1alpha1 +kind: ArangoMLCronJob +metadata: + name: example-arangomlcronjob + namespace: default +spec: + schedule: "*/1 * * * *" + jobTemplate: + spec: + template: + spec: + containers: + - name: curl-google + image: appropriate/curl + args: + - curl + - https://www.google.com + restartPolicy: OnFailure \ No newline at end of file diff --git a/pkg/apis/ml/v1alpha1/cronjob_spec.go b/pkg/apis/ml/v1alpha1/cronjob_spec.go index 835fa2651..e07162f0c 100644 --- a/pkg/apis/ml/v1alpha1/cronjob_spec.go +++ b/pkg/apis/ml/v1alpha1/cronjob_spec.go @@ -21,7 +21,7 @@ package v1alpha1 import ( - batchApi "k8s.io/api/batch/v1beta1" + batchApi "k8s.io/api/batch/v1" "github.com/arangodb/kube-arangodb/pkg/apis/shared" ) diff --git a/pkg/apis/ml/v1alpha1/cronjob_status.go b/pkg/apis/ml/v1alpha1/cronjob_status.go index 72160bbe1..491a31f6a 100644 --- a/pkg/apis/ml/v1alpha1/cronjob_status.go +++ b/pkg/apis/ml/v1alpha1/cronjob_status.go @@ -21,9 +21,10 @@ package v1alpha1 import ( - batchApi "k8s.io/api/batch/v1beta1" + batchApi "k8s.io/api/batch/v1" api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" + "github.com/arangodb/kube-arangodb/pkg/apis/shared" ) type ArangoMLCronJobStatus struct { @@ -35,3 +36,7 @@ type ArangoMLCronJobStatus struct { // +doc/link: Kubernetes Documentation|https://godoc.org/k8s.io/api/batch/v1beta1#CronJobStatus batchApi.CronJobStatus `json:",inline"` } + +func (a *ArangoMLCronJobStatus) Validate() error { + return shared.WithErrors(shared.PrefixResourceErrors("spec")) +} diff --git a/pkg/apis/ml/v1alpha1/extension_conditions.go b/pkg/apis/ml/v1alpha1/extension_conditions.go index a5af8f0ff..6e3e1f3b9 100644 --- a/pkg/apis/ml/v1alpha1/extension_conditions.go +++ b/pkg/apis/ml/v1alpha1/extension_conditions.go @@ -28,4 +28,7 @@ const ( ExtensionBootstrapCompletedCondition api.ConditionType = "BootstrapCompleted" ExtensionMetadataServiceValidCondition api.ConditionType = "MetadataServiceValid" LicenseValidCondition api.ConditionType = "LicenseValid" + CronJobCreatedCondition api.ConditionType = "CronJobCreated" + CronJobActiveCondition api.ConditionType = "CronJobActive" + CronJobSucceedCondition api.ConditionType = "CronJobSucceed" ) diff --git a/pkg/apis/ml/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/ml/v1alpha1/zz_generated.deepcopy.go index 46f1bdcb2..0a0bc4eed 100644 --- a/pkg/apis/ml/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/ml/v1alpha1/zz_generated.deepcopy.go @@ -28,7 +28,7 @@ package v1alpha1 import ( v1 "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" sharedv1 "github.com/arangodb/kube-arangodb/pkg/apis/shared/v1" - v1beta1 "k8s.io/api/batch/v1beta1" + batchv1 "k8s.io/api/batch/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -198,7 +198,7 @@ func (in *ArangoMLCronJobSpec) DeepCopyInto(out *ArangoMLCronJobSpec) { *out = *in if in.CronJobSpec != nil { in, out := &in.CronJobSpec, &out.CronJobSpec - *out = new(v1beta1.CronJobSpec) + *out = new(batchv1.CronJobSpec) (*in).DeepCopyInto(*out) } return diff --git a/pkg/util/tests/kubernetes.go b/pkg/util/tests/kubernetes.go index 691cf011d..1d83d5aa5 100644 --- a/pkg/util/tests/kubernetes.go +++ b/pkg/util/tests/kubernetes.go @@ -74,6 +74,12 @@ type KubernetesObject interface { func CreateObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientSet.Interface, objects ...interface{}) func(t *testing.T) { for _, object := range objects { switch v := object.(type) { + case **batch.CronJob: + require.NotNil(t, v) + + vl := *v + _, err := k8s.BatchV1().CronJobs(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) + require.NoError(t, err) case **batch.Job: require.NotNil(t, v) @@ -122,6 +128,12 @@ func CreateObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientSe vl := *v _, err := arango.MlV1alpha1().ArangoMLStorages(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) require.NoError(t, err) + case **mlApi.ArangoMLCronJob: + require.NotNil(t, v) + + vl := *v + _, err := arango.MlV1alpha1().ArangoMLCronJobs(vl.GetNamespace()).Create(context.Background(), vl, meta.CreateOptions{}) + require.NoError(t, err) default: require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) } @@ -196,6 +208,21 @@ func UpdateObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientSe func RefreshObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientSet.Interface, objects ...interface{}) { for _, object := range objects { switch v := object.(type) { + case **batch.CronJob: + require.NotNil(t, v) + + vl := *v + + vn, err := k8s.BatchV1().CronJobs(vl.GetNamespace()).Get(context.Background(), vl.GetName(), meta.GetOptions{}) + if err != nil { + if kerrors.IsNotFound(err) { + *v = nil + } else { + require.NoError(t, err) + } + } else { + *v = vn + } case **batch.Job: require.NotNil(t, v) @@ -316,6 +343,21 @@ func RefreshObjects(t *testing.T, k8s kubernetes.Interface, arango arangoClientS } else { *v = vn } + case **mlApi.ArangoMLCronJob: + require.NotNil(t, v) + + vl := *v + + vn, err := arango.MlV1alpha1().ArangoMLCronJobs(vl.GetNamespace()).Get(context.Background(), vl.GetName(), meta.GetOptions{}) + if err != nil { + if kerrors.IsNotFound(err) { + *v = nil + } else { + require.NoError(t, err) + } + } else { + *v = vn + } default: require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) } @@ -326,6 +368,12 @@ type MetaObjectMod[T meta.Object] func(t *testing.T, obj T) func SetMetaBasedOnType(t *testing.T, object meta.Object) { switch v := object.(type) { + case *batch.CronJob: + v.Kind = "CronJob" + v.APIVersion = "batch/v1" + v.SetSelfLink(fmt.Sprintf("/api/batch/v1/cronjobs/%s/%s", + object.GetNamespace(), + object.GetName())) case *batch.Job: v.Kind = "Job" v.APIVersion = "batch/v1" @@ -384,6 +432,14 @@ func SetMetaBasedOnType(t *testing.T, object meta.Object) { ml.ArangoMLStorageResourcePlural, object.GetNamespace(), object.GetName())) + case *mlApi.ArangoMLCronJob: + v.Kind = ml.ArangoMLCronJobResourceKind + v.APIVersion = mlApi.SchemeGroupVersion.String() + v.SetSelfLink(fmt.Sprintf("/api/%s/%s/%s/%s", + mlApi.SchemeGroupVersion.String(), + ml.ArangoMLCronJobResourcePlural, + object.GetNamespace(), + object.GetName())) default: require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) } @@ -420,6 +476,10 @@ func NewItem(t *testing.T, o operation.Operation, object meta.Object) operation. } switch v := object.(type) { + case *batch.CronJob: + item.Group = "batch" + item.Version = "v1" + item.Kind = "CronJob" case *batch.Job: item.Group = "batch" item.Version = "v1" @@ -452,6 +512,10 @@ func NewItem(t *testing.T, o operation.Operation, object meta.Object) operation. item.Group = ml.ArangoMLGroupName item.Version = mlApi.ArangoMLVersion item.Kind = ml.ArangoMLStorageResourceKind + case *mlApi.ArangoMLCronJob: + item.Group = ml.ArangoMLGroupName + item.Version = mlApi.ArangoMLVersion + item.Kind = ml.ArangoMLCronJobResourceKind default: require.Fail(t, fmt.Sprintf("Unable to create object: %s", reflect.TypeOf(v).String())) }