From 2397c7523a784af0e1fdc36fbc71c4b42ea2f202 Mon Sep 17 00:00:00 2001 From: Adam Janikowski <12255597+ajanikow@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:56:10 +0200 Subject: [PATCH] [Bugfix] Fix Schema Apply Checksum (#1652) --- CHANGELOG.md | 1 + cmd/cmd.go | 3 +- cmd/crd.go | 3 +- pkg/crd/apply.go | 38 +++++++--- pkg/crd/apply_test.go | 75 ++++++++++++++++++- pkg/crd/crds/apps-job.go | 22 +++--- pkg/crd/crds/backups-backup.go | 22 +++--- pkg/crd/crds/backups-backuppolicy.go | 22 +++--- pkg/crd/crds/crds.go | 43 +++++++++-- pkg/crd/crds/crds_test.go | 14 +++- .../crds/database-clustersynchronization.go | 22 +++--- pkg/crd/crds/database-deployment.go | 22 +++--- pkg/crd/crds/database-member.go | 22 +++--- pkg/crd/crds/database-task.go | 22 +++--- pkg/crd/crds/ml-extension.go | 22 +++--- pkg/crd/crds/ml-job-batch.go | 22 +++--- pkg/crd/crds/ml-job-cron.go | 22 +++--- pkg/crd/crds/ml-storage.go | 22 +++--- .../crds/replication-deploymentreplication.go | 22 +++--- pkg/crd/crds/scheduler-profile.go | 22 +++--- pkg/crd/crds/storage-localstorage.go | 22 +++--- pkg/crd/definitions.go | 5 +- pkg/crd/version.go | 57 -------------- pkg/crd/version_test.go | 73 ------------------ pkg/util/tests/logging.go | 4 +- 25 files changed, 284 insertions(+), 340 deletions(-) delete mode 100644 pkg/crd/version.go delete mode 100644 pkg/crd/version_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index cf4d3378d..f0e186348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - (Maintenance) Bump Prometheus API Version - (Bugfix) Prevent unexpected rotation in case of SecurityContext change - (Bugfix) Ensure PDB is created +- (Bugfix) Fix Schema Apply Checksum ## [1.2.40](https://github.com/arangodb/kube-arangodb/tree/1.2.40) (2024-04-10) - (Feature) Add Core fields to the Scheduler Container Spec diff --git a/cmd/cmd.go b/cmd/cmd.go index f77d3d35f..f2a2bffce 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -67,6 +67,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/metrics" "github.com/arangodb/kube-arangodb/pkg/util/probe" "github.com/arangodb/kube-arangodb/pkg/util/retry" + "github.com/arangodb/kube-arangodb/pkg/util/shutdown" "github.com/arangodb/kube-arangodb/pkg/version" ) @@ -378,7 +379,7 @@ func executeMain(cmd *cobra.Command, args []string) { } if crdOptions.install { - ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + ctx, cancel := context.WithTimeout(shutdown.Context(), time.Minute) defer cancel() crdOpts, err := prepareCRDOptions(crdOptions.validationSchema) diff --git a/cmd/crd.go b/cmd/crd.go index 3c843b258..3f580d389 100644 --- a/cmd/crd.go +++ b/cmd/crd.go @@ -34,6 +34,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/crd/crds" "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/util/kclient" + "github.com/arangodb/kube-arangodb/pkg/util/shutdown" ) var ( @@ -108,7 +109,7 @@ func cmdCRDInstallRun(cmd *cobra.Command, args []string) { return } - ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + ctx, cancel := context.WithTimeout(shutdown.Context(), time.Minute) defer cancel() err = crd.EnsureCRDWithOptions(ctx, client, crd.EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts, ForceUpdate: crdInstallOptions.force}) diff --git a/pkg/crd/apply.go b/pkg/crd/apply.go index 73a625307..027f6462d 100644 --- a/pkg/crd/apply.go +++ b/pkg/crd/apply.go @@ -28,8 +28,6 @@ import ( "k8s.io/apimachinery/pkg/api/errors" meta "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/crd/crds" "github.com/arangodb/kube-arangodb/pkg/logging" kresources "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources" @@ -65,13 +63,13 @@ func EnsureCRDWithOptions(ctx context.Context, client kclient.Client, opts Ensur continue } - var opt *crds.CRDOptions + var opt = &crdReg.defaultOpts if o, ok := opts.CRDOptions[crdName]; ok { opt = &o } def := crdReg.getter(opt) - err := tryApplyCRD(ctx, client, def, opts.ForceUpdate) + err := tryApplyCRD(ctx, client, def, opt, opts.ForceUpdate) if !opts.IgnoreErrors && err != nil { return err } @@ -79,11 +77,19 @@ func EnsureCRDWithOptions(ctx context.Context, client kclient.Client, opts Ensur return nil } -func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition, forceUpdate bool) error { +func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition, opts *crds.CRDOptions, forceUpdate bool) error { crdDefinitions := client.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions() crdName := def.CRD.Name + definitionVersion, definitionSchemaVersion := def.DefinitionData.Checksum() + + logger := logger.Str("version", definitionVersion) + + if opts.GetWithSchema() { + logger = logger.Str("schema", definitionSchemaVersion) + } + c, err := crdDefinitions.Get(ctx, crdName, meta.GetOptions{}) if err != nil { if !errors.IsNotFound(err) { @@ -102,12 +108,16 @@ func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition ObjectMeta: meta.ObjectMeta{ Name: crdName, Labels: map[string]string{ - Version: string(def.Version), + Version: definitionVersion, }, }, Spec: def.CRD.Spec, } + if opts.GetWithSchema() { + c.Labels[Schema] = definitionSchemaVersion + } + if _, err := crdDefinitions.Create(ctx, c, meta.CreateOptions{}); err != nil { logger.Err(err).Str("crd", crdName).Warn("Create Operations is not allowed due to error") return err @@ -127,14 +137,20 @@ func tryApplyCRD(ctx context.Context, client kclient.Client, def crds.Definition c.ObjectMeta.Labels = map[string]string{} } - if v, ok := c.ObjectMeta.Labels[Version]; ok && v != "" { - if !forceUpdate && !isUpdateRequired(def.Version, driver.Version(v)) { - logger.Str("crd", crdName).Info("CRD Update not required") - return nil + if !forceUpdate { + if v, ok := c.ObjectMeta.Labels[Version]; ok && v == definitionVersion { + if v, ok := c.ObjectMeta.Labels[Schema]; (opts.GetWithSchema() && (ok && v == definitionSchemaVersion)) || (!opts.GetWithSchema() && !ok) { + logger.Str("crd", crdName).Info("CRD Update not required") + return nil + } } } - c.ObjectMeta.Labels[Version] = string(def.Version) + c.ObjectMeta.Labels[Version] = definitionVersion + delete(c.ObjectMeta.Labels, Schema) + if opts.GetWithSchema() { + c.ObjectMeta.Labels[Schema] = definitionSchemaVersion + } c.Spec = def.CRD.Spec if _, err := crdDefinitions.Update(ctx, c, meta.UpdateOptions{}); err != nil { diff --git a/pkg/crd/apply_test.go b/pkg/crd/apply_test.go index cdd9e8b58..61b66fb0c 100644 --- a/pkg/crd/apply_test.go +++ b/pkg/crd/apply_test.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany +// Copyright 2016-2024 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. @@ -22,6 +22,7 @@ package crd import ( "context" + "encoding/json" "testing" "time" @@ -51,6 +52,11 @@ func dropLogMessages(t *testing.T, s tests.LogScanner) map[string]string { lines[p] = m } + d, err := json.Marshal(lines) + require.NoError(t, err) + + t.Logf("Lines: %s", string(d)) + return lines } @@ -133,27 +139,88 @@ func runApply(t *testing.T, crdOpts map[string]crds.CRDOptions) { t.Run("Create", func(t *testing.T) { d := crds.AllDefinitions()[0] + q := d.CRD.DeepCopy() q.Labels = map[string]string{ - Version: string(d.Version), + Version: "version", } _, err := c.KubernetesExtensions().ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), q, meta.CreateOptions{}) require.NoError(t, err) }) - t.Run("Ensure", func(t *testing.T) { + t.Run("Ensure without schema", func(t *testing.T) { + crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions { + el.WithSchema = false + return el + }) + require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts})) for k, v := range dropLogMessages(t, s) { t.Run(k, func(t *testing.T) { if k == crds.AllDefinitions()[0].CRD.GetName() { - require.Equal(t, "CRD Update not required", v) + require.Equal(t, "CRD Updated", v) } else { require.Equal(t, "CRD Created", v) } }) } }) + + t.Run("Rerun without schema", func(t *testing.T) { + crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions { + el.WithSchema = false + return el + }) + + require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts})) + + for k, v := range dropLogMessages(t, s) { + t.Run(k, func(t *testing.T) { + require.Equal(t, "CRD Update not required", v) + }) + } + }) + + t.Run("Ensure with schema", func(t *testing.T) { + crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions { + el.WithSchema = true + return el + }) + + require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts})) + + for k, v := range dropLogMessages(t, s) { + t.Run(k, func(t *testing.T) { + require.Equal(t, "CRD Updated", v) + }) + } + }) + + t.Run("Rerun with schema", func(t *testing.T) { + crdOpts = updateMap(t, crdOpts, func(t *testing.T, key string, el crds.CRDOptions) crds.CRDOptions { + el.WithSchema = true + return el + }) + + require.NoError(t, EnsureCRDWithOptions(context.Background(), c, EnsureCRDOptions{IgnoreErrors: false, CRDOptions: crdOpts})) + + for k, v := range dropLogMessages(t, s) { + t.Run(k, func(t *testing.T) { + require.Equal(t, "CRD Update not required", v) + }) + } + }) }) }) } + +func updateMap[T comparable](t *testing.T, in map[string]T, f func(t *testing.T, key string, el T) T) map[string]T { + r := make(map[string]T, len(in)) + + for k, v := range in { + r[k] = f(t, k, v) + } + + return r +} diff --git a/pkg/crd/crds/apps-job.go b/pkg/crd/crds/apps-job.go index 5733a1231..371434d08 100644 --- a/pkg/crd/crds/apps-job.go +++ b/pkg/crd/crds/apps-job.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - AppsJobVersion = driver.Version("1.0.1") ) // Deprecated: use AppsJobWithOptions instead @@ -40,7 +32,7 @@ func AppsJob() *apiextensions.CustomResourceDefinition { } func AppsJobWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(appsJobsCRD, appsJobsCRDSchemas, opts...) + return getCRD(AppsJobDefinitionData(), opts...) } // Deprecated: use AppsJobDefinitionWithOptions instead @@ -50,13 +42,17 @@ func AppsJobDefinition() Definition { func AppsJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: AppsJobVersion, - CRD: AppsJobWithOptions(opts...), + DefinitionData: AppsJobDefinitionData(), + CRD: AppsJobWithOptions(opts...), } } -var appsJobsCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](appsJobs) -var appsJobsCRDSchemas = util.NewYamlLoader[crdSchemas](appsJobsSchemaRaw) +func AppsJobDefinitionData() DefinitionData { + return DefinitionData{ + definition: appsJobs, + schemaDefinition: appsJobsSchemaRaw, + } +} //go:embed apps-job.yaml var appsJobs []byte diff --git a/pkg/crd/crds/backups-backup.go b/pkg/crd/crds/backups-backup.go index a63def401..e8d83cd0b 100644 --- a/pkg/crd/crds/backups-backup.go +++ b/pkg/crd/crds/backups-backup.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - BackupsBackupVersion = driver.Version("1.0.1") ) // Deprecated: use BackupsBackupWithOptions instead @@ -40,7 +32,7 @@ func BackupsBackup() *apiextensions.CustomResourceDefinition { } func BackupsBackupWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(backupsBackupCRD, backupsBackupCRDSchemas, opts...) + return getCRD(BackupsBackupDefinitionData(), opts...) } // Deprecated: use BackupsBackupDefinitionWithOptions instead @@ -50,13 +42,17 @@ func BackupsBackupDefinition() Definition { func BackupsBackupDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: BackupsBackupVersion, - CRD: BackupsBackupWithOptions(opts...), + DefinitionData: BackupsBackupDefinitionData(), + CRD: BackupsBackupWithOptions(opts...), } } -var backupsBackupCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](backupsBackup) -var backupsBackupCRDSchemas = util.NewYamlLoader[crdSchemas](backupsBackupSchemaRaw) +func BackupsBackupDefinitionData() DefinitionData { + return DefinitionData{ + definition: backupsBackup, + schemaDefinition: backupsBackupSchemaRaw, + } +} //go:embed backups-backup.yaml var backupsBackup []byte diff --git a/pkg/crd/crds/backups-backuppolicy.go b/pkg/crd/crds/backups-backuppolicy.go index 60e79734c..527edebac 100644 --- a/pkg/crd/crds/backups-backuppolicy.go +++ b/pkg/crd/crds/backups-backuppolicy.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - BackupsBackupPolicyPolicyVersion = driver.Version("1.0.1") ) // Deprecated: use BackupsBackupPolicyPolicyWithOptions instead @@ -40,7 +32,7 @@ func BackupsBackupPolicyPolicy() *apiextensions.CustomResourceDefinition { } func BackupsBackupPolicyPolicyWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(backupsBackupPolicyCRD, backupsBackupPolicyCRDSchemas, opts...) + return getCRD(BackupsBackupPolicyDefinitionData(), opts...) } // Deprecated: use func BackupsBackupPolicyDefinitionWithOptions instead @@ -50,13 +42,17 @@ func BackupsBackupPolicyDefinition() Definition { func BackupsBackupPolicyDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: BackupsBackupPolicyPolicyVersion, - CRD: BackupsBackupPolicyPolicyWithOptions(opts...), + DefinitionData: BackupsBackupPolicyDefinitionData(), + CRD: BackupsBackupPolicyPolicyWithOptions(opts...), } } -var backupsBackupPolicyCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](backupsBackupPolicy) -var backupsBackupPolicyCRDSchemas = util.NewYamlLoader[crdSchemas](backupsBackupPolicySchemaRaw) +func BackupsBackupPolicyDefinitionData() DefinitionData { + return DefinitionData{ + definition: backupsBackupPolicy, + schemaDefinition: backupsBackupPolicySchemaRaw, + } +} //go:embed backups-backuppolicy.yaml var backupsBackupPolicy []byte diff --git a/pkg/crd/crds/crds.go b/pkg/crd/crds/crds.go index 16ec303a0..e401280a6 100644 --- a/pkg/crd/crds/crds.go +++ b/pkg/crd/crds/crds.go @@ -25,14 +25,35 @@ import ( apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "github.com/arangodb/go-driver" - "github.com/arangodb/kube-arangodb/pkg/util" ) type Definition struct { - Version driver.Version - CRD *apiextensions.CustomResourceDefinition + DefinitionData + CRD *apiextensions.CustomResourceDefinition +} + +type DefinitionData struct { + definition []byte + schemaDefinition []byte +} + +func (d DefinitionData) definitionLoader() util.Loader[apiextensions.CustomResourceDefinition] { + return util.NewYamlLoader[apiextensions.CustomResourceDefinition](d.definition) +} + +func (d DefinitionData) schemaDefinitionLoader() util.Loader[crdSchemas] { + return util.NewYamlLoader[crdSchemas](d.schemaDefinition) +} + +func (d DefinitionData) Checksum() (definition, schema string) { + if len(d.definition) > 0 { + definition = util.SHA256(d.definition) + } + if len(d.schemaDefinition) > 0 { + schema = util.SHA256(d.schemaDefinition) + } + return } func AllDefinitions() []Definition { @@ -75,6 +96,14 @@ type CRDOptions struct { WithSchema bool } +func (o *CRDOptions) GetWithSchema() bool { + if o == nil { + return false + } + + return o.WithSchema +} + func (o *CRDOptions) AsFunc() func(*CRDOptions) { return func(opts *CRDOptions) { if o == nil || opts == nil { @@ -91,18 +120,18 @@ func WithSchema() func(*CRDOptions) { } } -func getCRD(crdLoader util.Loader[apiextensions.CustomResourceDefinition], schemasLoader util.Loader[crdSchemas], opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { +func getCRD(data DefinitionData, opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { o := &CRDOptions{} for _, fn := range opts { fn(o) } - crd := crdLoader.MustGet() + crd := data.definitionLoader().MustGet() if o.WithSchema { crdWithSchema := crd.DeepCopy() - schemas := schemasLoader.MustGet() + schemas := data.schemaDefinitionLoader().MustGet() for i, v := range crdWithSchema.Spec.Versions { schema, ok := schemas[v.Name] diff --git a/pkg/crd/crds/crds_test.go b/pkg/crd/crds/crds_test.go index cd70d598d..2eba7131c 100644 --- a/pkg/crd/crds/crds_test.go +++ b/pkg/crd/crds/crds_test.go @@ -88,8 +88,20 @@ func Test_CRD(t *testing.T) { } func Test_AllDefinitionsDefined(t *testing.T) { + registered := map[string]bool{} + for _, def := range AllDefinitions() { - require.NotEmpty(t, def.Version) + a, b := def.Checksum() + + require.NotEmpty(t, a) + require.NotEmpty(t, b) + + require.NotContains(t, registered, a) + require.NotContains(t, registered, b) + + registered[a] = true + registered[b] = true + require.NotNil(t, def.CRD) } } diff --git a/pkg/crd/crds/database-clustersynchronization.go b/pkg/crd/crds/database-clustersynchronization.go index f010b51ad..e5a5e50bc 100644 --- a/pkg/crd/crds/database-clustersynchronization.go +++ b/pkg/crd/crds/database-clustersynchronization.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - DatabaseClusterSynchronizationVersion = driver.Version("1.0.1") ) // Deprecated: use DatabaseClusterSynchronizationWithOptions instead @@ -40,7 +32,7 @@ func DatabaseClusterSynchronization() *apiextensions.CustomResourceDefinition { } func DatabaseClusterSynchronizationWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(databaseClusterSynchronizationCRD, databaseClusterSynchronizationCRDSchemas, opts...) + return getCRD(DatabaseClusterSynchronizationDefinitionData(), opts...) } // Deprecated: use DatabaseClusterSynchronizationDefinitionWithOptions instead @@ -50,13 +42,17 @@ func DatabaseClusterSynchronizationDefinition() Definition { func DatabaseClusterSynchronizationDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: DatabaseClusterSynchronizationVersion, - CRD: DatabaseClusterSynchronizationWithOptions(opts...), + DefinitionData: DatabaseClusterSynchronizationDefinitionData(), + CRD: DatabaseClusterSynchronizationWithOptions(opts...), } } -var databaseClusterSynchronizationCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseClusterSynchronization) -var databaseClusterSynchronizationCRDSchemas = util.NewYamlLoader[crdSchemas](databaseClusterSynchronizationSchemaRaw) +func DatabaseClusterSynchronizationDefinitionData() DefinitionData { + return DefinitionData{ + definition: databaseClusterSynchronization, + schemaDefinition: databaseClusterSynchronizationSchemaRaw, + } +} //go:embed database-clustersynchronization.yaml var databaseClusterSynchronization []byte diff --git a/pkg/crd/crds/database-deployment.go b/pkg/crd/crds/database-deployment.go index 1fcd65fd2..5cadfac44 100644 --- a/pkg/crd/crds/database-deployment.go +++ b/pkg/crd/crds/database-deployment.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - DatabaseDeploymentVersion = driver.Version("1.0.1") ) // Deprecated: use DatabaseDeploymentWithOptions instead @@ -40,7 +32,7 @@ func DatabaseDeployment() *apiextensions.CustomResourceDefinition { } func DatabaseDeploymentWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(databaseDeploymentCRD, databaseDeploymentCRDSchemas, opts...) + return getCRD(DatabaseDeploymentDefinitionData(), opts...) } // Deprecated: use DatabaseDeploymentDefinitionWithOptions instead @@ -50,13 +42,17 @@ func DatabaseDeploymentDefinition() Definition { func DatabaseDeploymentDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: DatabaseDeploymentVersion, - CRD: DatabaseDeploymentWithOptions(opts...), + DefinitionData: DatabaseDeploymentDefinitionData(), + CRD: DatabaseDeploymentWithOptions(opts...), } } -var databaseDeploymentCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseDeployment) -var databaseDeploymentCRDSchemas = util.NewYamlLoader[crdSchemas](databaseDeploymentSchemaRaw) +func DatabaseDeploymentDefinitionData() DefinitionData { + return DefinitionData{ + definition: databaseDeployment, + schemaDefinition: databaseDeploymentSchemaRaw, + } +} //go:embed database-deployment.yaml var databaseDeployment []byte diff --git a/pkg/crd/crds/database-member.go b/pkg/crd/crds/database-member.go index 205742d34..84019f6e7 100644 --- a/pkg/crd/crds/database-member.go +++ b/pkg/crd/crds/database-member.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - DatabaseMemberVersion = driver.Version("1.0.1") ) // Deprecated: use DatabaseMemberWithOptions instead @@ -40,7 +32,7 @@ func DatabaseMember() *apiextensions.CustomResourceDefinition { } func DatabaseMemberWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(databaseMemberCRD, databaseMemberCRDSchemas, opts...) + return getCRD(DatabaseMemberDefinitionData(), opts...) } // Deprecated: use DatabaseMemberDefinitionWithOptions instead @@ -50,13 +42,17 @@ func DatabaseMemberDefinition() Definition { func DatabaseMemberDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: DatabaseMemberVersion, - CRD: DatabaseMemberWithOptions(opts...), + DefinitionData: DatabaseMemberDefinitionData(), + CRD: DatabaseMemberWithOptions(opts...), } } -var databaseMemberCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseMember) -var databaseMemberCRDSchemas = util.NewYamlLoader[crdSchemas](databaseMemberSchemaRaw) +func DatabaseMemberDefinitionData() DefinitionData { + return DefinitionData{ + definition: databaseMember, + schemaDefinition: databaseMemberSchemaRaw, + } +} //go:embed database-member.yaml var databaseMember []byte diff --git a/pkg/crd/crds/database-task.go b/pkg/crd/crds/database-task.go index 8e25261ab..87d81d4b1 100644 --- a/pkg/crd/crds/database-task.go +++ b/pkg/crd/crds/database-task.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - DatabaseTaskVersion = driver.Version("1.0.1") ) // Deprecated: use DatabaseTaskWithOptions instead @@ -40,7 +32,7 @@ func DatabaseTask() *apiextensions.CustomResourceDefinition { } func DatabaseTaskWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(databaseTaskCRD, databaseTaskCRDSchemas, opts...) + return getCRD(DatabaseTaskDefinitionData(), opts...) } // Deprecated: use DatabaseTaskDefinitionWithOptions instead @@ -50,13 +42,17 @@ func DatabaseTaskDefinition() Definition { func DatabaseTaskDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: DatabaseTaskVersion, - CRD: DatabaseTaskWithOptions(opts...), + DefinitionData: DatabaseTaskDefinitionData(), + CRD: DatabaseTaskWithOptions(opts...), } } -var databaseTaskCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](databaseTask) -var databaseTaskCRDSchemas = util.NewYamlLoader[crdSchemas](databaseTaskSchemaRaw) +func DatabaseTaskDefinitionData() DefinitionData { + return DefinitionData{ + definition: databaseTask, + schemaDefinition: databaseTaskSchemaRaw, + } +} //go:embed database-task.yaml var databaseTask []byte diff --git a/pkg/crd/crds/ml-extension.go b/pkg/crd/crds/ml-extension.go index 4e918e3f6..ccc11d68c 100644 --- a/pkg/crd/crds/ml-extension.go +++ b/pkg/crd/crds/ml-extension.go @@ -24,29 +24,25 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - MLExtensionVersion = driver.Version("1.0.0") ) func MLExtensionWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(mlExtensionCRD, mlExtensionCRDSchemas, opts...) + return getCRD(MLExtensionDefinitionData(), opts...) } func MLExtensionDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: MLExtensionVersion, - CRD: MLExtensionWithOptions(opts...), + DefinitionData: MLExtensionDefinitionData(), + CRD: MLExtensionWithOptions(opts...), } } -var mlExtensionCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlExtension) -var mlExtensionCRDSchemas = util.NewYamlLoader[crdSchemas](mlExtensionSchemaRaw) +func MLExtensionDefinitionData() DefinitionData { + return DefinitionData{ + definition: mlExtension, + schemaDefinition: mlExtensionSchemaRaw, + } +} //go:embed ml-extension.yaml var mlExtension []byte diff --git a/pkg/crd/crds/ml-job-batch.go b/pkg/crd/crds/ml-job-batch.go index 8579171a0..0918acfdb 100644 --- a/pkg/crd/crds/ml-job-batch.go +++ b/pkg/crd/crds/ml-job-batch.go @@ -24,29 +24,25 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - MLBatchJobVersion = driver.Version("1.0.0") ) func MLBatchJobWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(mlBatchJobCRD, mlBatchJobCRDSchemas, opts...) + return getCRD(MLBatchJobDefinitionData(), opts...) } func MLBatchJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: MLBatchJobVersion, - CRD: MLBatchJobWithOptions(opts...), + DefinitionData: MLBatchJobDefinitionData(), + CRD: MLBatchJobWithOptions(opts...), } } -var mlBatchJobCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlBatchJob) -var mlBatchJobCRDSchemas = util.NewYamlLoader[crdSchemas](mlBatchJobSchemaRaw) +func MLBatchJobDefinitionData() DefinitionData { + return DefinitionData{ + definition: mlBatchJob, + schemaDefinition: mlBatchJobSchemaRaw, + } +} //go:embed ml-job-batch.yaml var mlBatchJob []byte diff --git a/pkg/crd/crds/ml-job-cron.go b/pkg/crd/crds/ml-job-cron.go index baf77a69e..9beddfd35 100644 --- a/pkg/crd/crds/ml-job-cron.go +++ b/pkg/crd/crds/ml-job-cron.go @@ -24,29 +24,25 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - MLCronJobVersion = driver.Version("1.0.0") ) func MLCronJobWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(mlCronJobCRD, mlCronJobCRDSchemas, opts...) + return getCRD(MLCronJobDefinitionData(), opts...) } func MLCronJobDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: MLCronJobVersion, - CRD: MLCronJobWithOptions(opts...), + DefinitionData: MLCronJobDefinitionData(), + CRD: MLCronJobWithOptions(opts...), } } -var mlCronJobCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlCronJob) -var mlCronJobCRDSchemas = util.NewYamlLoader[crdSchemas](mlCronJobSchemaRaw) +func MLCronJobDefinitionData() DefinitionData { + return DefinitionData{ + definition: mlCronJob, + schemaDefinition: mlCronJobSchemaRaw, + } +} //go:embed ml-job-cron.yaml var mlCronJob []byte diff --git a/pkg/crd/crds/ml-storage.go b/pkg/crd/crds/ml-storage.go index e7e8c0826..b5c6f1489 100644 --- a/pkg/crd/crds/ml-storage.go +++ b/pkg/crd/crds/ml-storage.go @@ -24,29 +24,25 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - MLStorageVersion = driver.Version("1.0.0") ) func MLStorageWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(mlStorageCRD, mlStorageCRDSchemas, opts...) + return getCRD(MLStorageDefinitionData(), opts...) } func MLStorageDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: MLStorageVersion, - CRD: MLStorageWithOptions(opts...), + DefinitionData: MLStorageDefinitionData(), + CRD: MLStorageWithOptions(opts...), } } -var mlStorageCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](mlStorage) -var mlStorageCRDSchemas = util.NewYamlLoader[crdSchemas](mlStorageSchemaRaw) +func MLStorageDefinitionData() DefinitionData { + return DefinitionData{ + definition: mlStorage, + schemaDefinition: mlStorageSchemaRaw, + } +} //go:embed ml-storage.yaml var mlStorage []byte diff --git a/pkg/crd/crds/replication-deploymentreplication.go b/pkg/crd/crds/replication-deploymentreplication.go index ac9bff992..45c7e64aa 100644 --- a/pkg/crd/crds/replication-deploymentreplication.go +++ b/pkg/crd/crds/replication-deploymentreplication.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - ReplicationDeploymentReplicationVersion = driver.Version("1.0.1") ) // Deprecated: use ReplicationDeploymentReplicationWithOptions instead @@ -40,7 +32,7 @@ func ReplicationDeploymentReplication() *apiextensions.CustomResourceDefinition } func ReplicationDeploymentReplicationWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(replicationDeploymentReplicationCRD, replicationDeploymentReplicationCRDSchemas, opts...) + return getCRD(ReplicationDeploymentReplicationDefinitionData(), opts...) } // Deprecated: use ReplicationDeploymentReplicationDefinitionWithOptions instead @@ -50,13 +42,17 @@ func ReplicationDeploymentReplicationDefinition() Definition { func ReplicationDeploymentReplicationDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: ReplicationDeploymentReplicationVersion, - CRD: ReplicationDeploymentReplicationWithOptions(opts...), + DefinitionData: ReplicationDeploymentReplicationDefinitionData(), + CRD: ReplicationDeploymentReplicationWithOptions(opts...), } } -var replicationDeploymentReplicationCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](replicationDeploymentReplication) -var replicationDeploymentReplicationCRDSchemas = util.NewYamlLoader[crdSchemas](replicationDeploymentReplicationSchemaRaw) +func ReplicationDeploymentReplicationDefinitionData() DefinitionData { + return DefinitionData{ + definition: replicationDeploymentReplication, + schemaDefinition: replicationDeploymentReplicationSchemaRaw, + } +} //go:embed replication-deploymentreplication.yaml var replicationDeploymentReplication []byte diff --git a/pkg/crd/crds/scheduler-profile.go b/pkg/crd/crds/scheduler-profile.go index 8baf57301..488024822 100644 --- a/pkg/crd/crds/scheduler-profile.go +++ b/pkg/crd/crds/scheduler-profile.go @@ -24,29 +24,25 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - SchedulerProfileVersion = driver.Version("1.0.0") ) func SchedulerProfileWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(schedulerProfileCRD, schedulerProfileCRDSchemas, opts...) + return getCRD(SchedulerProfileDefinitionData(), opts...) } func SchedulerProfileDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: SchedulerProfileVersion, - CRD: SchedulerProfileWithOptions(opts...), + DefinitionData: SchedulerProfileDefinitionData(), + CRD: SchedulerProfileWithOptions(opts...), } } -var schedulerProfileCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](schedulerProfile) -var schedulerProfileCRDSchemas = util.NewYamlLoader[crdSchemas](schedulerProfileSchemaRaw) +func SchedulerProfileDefinitionData() DefinitionData { + return DefinitionData{ + definition: schedulerProfile, + schemaDefinition: schedulerProfileSchemaRaw, + } +} //go:embed scheduler-profile.yaml var schedulerProfile []byte diff --git a/pkg/crd/crds/storage-localstorage.go b/pkg/crd/crds/storage-localstorage.go index 6e5d34ad1..158c4fb30 100644 --- a/pkg/crd/crds/storage-localstorage.go +++ b/pkg/crd/crds/storage-localstorage.go @@ -24,14 +24,6 @@ import ( _ "embed" apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - - "github.com/arangodb/go-driver" - - "github.com/arangodb/kube-arangodb/pkg/util" -) - -const ( - StorageLocalStorageVersion = driver.Version("1.0.1") ) // Deprecated: use StorageLocalStorageWithOptions instead @@ -40,7 +32,7 @@ func StorageLocalStorage() *apiextensions.CustomResourceDefinition { } func StorageLocalStorageWithOptions(opts ...func(*CRDOptions)) *apiextensions.CustomResourceDefinition { - return getCRD(storageLocalStorageCRD, storageLocalStorageCRDSchemas, opts...) + return getCRD(StorageLocalStorageDefinitionData(), opts...) } // Deprecated: use StorageLocalStorageDefinitionWithOptions instead @@ -50,13 +42,17 @@ func StorageLocalStorageDefinition() Definition { func StorageLocalStorageDefinitionWithOptions(opts ...func(*CRDOptions)) Definition { return Definition{ - Version: StorageLocalStorageVersion, - CRD: StorageLocalStorageWithOptions(opts...), + DefinitionData: StorageLocalStorageDefinitionData(), + CRD: StorageLocalStorageWithOptions(opts...), } } -var storageLocalStorageCRD = util.NewYamlLoader[apiextensions.CustomResourceDefinition](storageLocalStorage) -var storageLocalStorageCRDSchemas = util.NewYamlLoader[crdSchemas](storageLocalStorageSchemaRaw) +func StorageLocalStorageDefinitionData() DefinitionData { + return DefinitionData{ + definition: storageLocalStorage, + schemaDefinition: storageLocalStorageSchemaRaw, + } +} //go:embed storage-localstorage.yaml var storageLocalStorage []byte diff --git a/pkg/crd/definitions.go b/pkg/crd/definitions.go index 6135692ce..325e37597 100644 --- a/pkg/crd/definitions.go +++ b/pkg/crd/definitions.go @@ -27,7 +27,10 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/errors" ) -const Version = "arangodb.com/version" +const ( + Version = "arangodb.com/version" + Schema = "arangodb.com/schema" +) type crdDefinitionGetter func(opts *crds.CRDOptions) crds.Definition diff --git a/pkg/crd/version.go b/pkg/crd/version.go deleted file mode 100644 index 45dfd03a4..000000000 --- a/pkg/crd/version.go +++ /dev/null @@ -1,57 +0,0 @@ -// -// DISCLAIMER -// -// Copyright 2016-2022 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 crd - -import ( - "strconv" - "strings" - - "github.com/arangodb/go-driver" -) - -func isVersionValid(a driver.Version) bool { - q := strings.SplitN(string(a), ".", 3) - - if len(q) < 2 { - // We do not have 2 parts - return false - } - - _, err := strconv.Atoi(q[0]) - if err != nil { - return false - } - - _, err = strconv.Atoi(q[1]) - return err == nil -} - -func isUpdateRequired(a, b driver.Version) bool { - if a == b { - return false - } - - if !isVersionValid(b) { - return true - } - - return a.CompareTo(b) > 0 -} diff --git a/pkg/crd/version_test.go b/pkg/crd/version_test.go deleted file mode 100644 index 76d5dc343..000000000 --- a/pkg/crd/version_test.go +++ /dev/null @@ -1,73 +0,0 @@ -// -// DISCLAIMER -// -// Copyright 2016-2022 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 crd - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/arangodb/go-driver" -) - -func Test_Versions(t *testing.T) { - type c struct { - update bool - a, b driver.Version - } - - cases := map[string]c{ - "Empty": { - update: false, - }, - "Local to empty": { - update: true, - a: "1.0.0", - b: "", - }, - "Local to nonparsable": { - update: true, - a: "1.0.0", - b: "ffdfdasfdasdfsa", - }, - "Same": { - update: false, - a: "1.0.0", - b: "1.0.0", - }, - "Below": { - update: true, - a: "1.1.0", - b: "1.0.0", - }, - "Above": { - update: false, - a: "1.0.0", - b: "1.1.0", - }, - } - - for name, c := range cases { - t.Run(name, func(t *testing.T) { - require.Equal(t, c.update, isUpdateRequired(c.a, c.b)) - }) - } -} diff --git a/pkg/util/tests/logging.go b/pkg/util/tests/logging.go index e5bf51c58..b4929181c 100644 --- a/pkg/util/tests/logging.go +++ b/pkg/util/tests/logging.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany +// Copyright 2016-2024 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. @@ -49,7 +49,7 @@ func (l *logScanner) GetData(t *testing.T, timeout time.Duration, obj interface{ if s, ok := l.Get(timeout); !ok { return false } else { - require.NoError(t, json.Unmarshal([]byte(s), obj)) + require.NoError(t, json.Unmarshal([]byte(s), obj), s) return true } }