From 15d1c107db5dad2e77cc6f7106a7139cc0520917 Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Sun, 30 Jun 2024 11:56:09 +0300 Subject: [PATCH 01/12] ignore CREATE event from owns objects --- deploy/ydb-operator/Chart.yaml | 4 +- internal/controllers/database/controller.go | 41 +++++---- .../controllers/databasenodeset/controller.go | 14 +-- .../remotedatabasenodeset/controller.go | 19 ++-- .../remotestoragenodeset/controller.go | 19 ++-- internal/controllers/storage/controller.go | 35 ++++--- internal/controllers/storage/init.go | 12 +++ .../controllers/storagenodeset/controller.go | 14 +-- internal/resources/databasenodeset.go | 4 + internal/resources/predicate.go | 91 +++++++++++++++++++ internal/resources/remotedatabasenodeset.go | 4 + internal/resources/remotestoragenodeset.go | 4 + internal/resources/resource.go | 47 ---------- internal/resources/storagenodeset.go | 6 +- 14 files changed, 200 insertions(+), 114 deletions(-) create mode 100644 internal/resources/predicate.go diff --git a/deploy/ydb-operator/Chart.yaml b/deploy/ydb-operator/Chart.yaml index c1a658df..c1595dc3 100644 --- a/deploy/ydb-operator/Chart.yaml +++ b/deploy/ydb-operator/Chart.yaml @@ -15,10 +15,10 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.5.17 +version: 0.5.18 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "0.5.17" +appVersion: "0.5.18" diff --git a/internal/controllers/database/controller.go b/internal/controllers/database/controller.go index c2060038..39455610 100644 --- a/internal/controllers/database/controller.go +++ b/internal/controllers/database/controller.go @@ -13,6 +13,7 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/log" @@ -69,12 +70,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu r.Log.Error(err, "unexpected Get error") return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } - result, err := r.Sync(ctx, resource) - if err != nil { - r.Log.Error(err, "unexpected Sync error") - } - return result, err + return r.Sync(ctx, resource) } // Create FieldIndexer to usage for List requests in Reconcile @@ -152,23 +149,31 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { } return controller. - For(&v1alpha1.Database{}). - Owns(&v1alpha1.RemoteDatabaseNodeSet{}). - Owns(&v1alpha1.DatabaseNodeSet{}). - Owns(&appsv1.StatefulSet{}). - Owns(&corev1.ConfigMap{}). - Owns(&corev1.Service{}). + For(&v1alpha1.Database{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), + ). + Owns(&v1alpha1.RemoteDatabaseNodeSet{}, + builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194 + ). + Owns(&v1alpha1.DatabaseNodeSet{}, + builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194 + ). + Owns(&appsv1.StatefulSet{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), + ). + Owns(&corev1.ConfigMap{}, + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), + ). + Owns(&corev1.Service{}, + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), + ). Watches( &source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(r.findDatabasesForSecret), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). - WithEventFilter(predicate.Or( - predicate.GenerationChangedPredicate{}, - resources.LastAppliedAnnotationPredicate(), - resources.IsServicePredicate(), - resources.IsSecretPredicate(), - )). - WithEventFilter(resources.IgnoreDeletetionPredicate()). + WithEventFilter(resources.IsDatabaseCreatePredicate()). + WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()). Complete(r) } diff --git a/internal/controllers/databasenodeset/controller.go b/internal/controllers/databasenodeset/controller.go index f1045dfa..36219778 100644 --- a/internal/controllers/databasenodeset/controller.go +++ b/internal/controllers/databasenodeset/controller.go @@ -10,6 +10,7 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" @@ -66,12 +67,13 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { controller := ctrl.NewControllerManagedBy(mgr) return controller. - For(&v1alpha1.DatabaseNodeSet{}). - Owns(&appsv1.StatefulSet{}). - WithEventFilter(predicate.Or( - predicate.GenerationChangedPredicate{}, - resources.LastAppliedAnnotationPredicate()), + For(&v1alpha1.DatabaseNodeSet{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), ). - WithEventFilter(resources.IgnoreDeletetionPredicate()). + Owns(&appsv1.StatefulSet{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), + ). + WithEventFilter(resources.IsDatabaseNodeSetCreatePredicate()). + WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()). Complete(r) } diff --git a/internal/controllers/remotedatabasenodeset/controller.go b/internal/controllers/remotedatabasenodeset/controller.go index c9479c25..ae0d662c 100644 --- a/internal/controllers/remotedatabasenodeset/controller.go +++ b/internal/controllers/remotedatabasenodeset/controller.go @@ -165,31 +165,30 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C Watches( source.NewKindWithCache(&v1alpha1.RemoteDatabaseNodeSet{}, cluster.GetCache()), &handler.EnqueueRequestForObject{}, - builder.WithPredicates(predicate.Or( - predicate.GenerationChangedPredicate{}, - resources.LastAppliedAnnotationPredicate(), - )), + builder.WithPredicates(predicate.GenerationChangedPredicate{}), ). Watches( &source.Kind{Type: &v1alpha1.DatabaseNodeSet{}}, &handler.EnqueueRequestForObject{}, - builder.WithPredicates( - resources.LabelExistsPredicate(isNodeSetFromMgmt), - ), + builder.WithPredicates(resources.LabelExistsPredicate(isNodeSetFromMgmt)), ). Watches( &source.Kind{Type: &corev1.Service{}}, handler.EnqueueRequestsFromMapFunc(annotationFilter), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Watches( - &source.Kind{Type: &corev1.Secret{}}, + &source.Kind{Type: &corev1.ConfigMap{}}, handler.EnqueueRequestsFromMapFunc(annotationFilter), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Watches( - &source.Kind{Type: &corev1.ConfigMap{}}, + &source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(annotationFilter), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). - WithEventFilter(resources.IgnoreDeletetionPredicate()). + WithEventFilter(resources.IsRemoteDatabaseNodeSetCreatePredicate()). + WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()). Complete(r) } diff --git a/internal/controllers/remotestoragenodeset/controller.go b/internal/controllers/remotestoragenodeset/controller.go index aafe961a..91ce95ea 100644 --- a/internal/controllers/remotestoragenodeset/controller.go +++ b/internal/controllers/remotestoragenodeset/controller.go @@ -165,31 +165,30 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C Watches( source.NewKindWithCache(&v1alpha1.RemoteStorageNodeSet{}, cluster.GetCache()), &handler.EnqueueRequestForObject{}, - builder.WithPredicates(predicate.Or( - predicate.GenerationChangedPredicate{}, - resources.LastAppliedAnnotationPredicate(), - )), + builder.WithPredicates(predicate.GenerationChangedPredicate{}), ). Watches( &source.Kind{Type: &v1alpha1.StorageNodeSet{}}, &handler.EnqueueRequestForObject{}, - builder.WithPredicates( - resources.LabelExistsPredicate(isNodeSetFromMgmt), - ), + builder.WithPredicates(resources.LabelExistsPredicate(isNodeSetFromMgmt)), ). Watches( &source.Kind{Type: &corev1.Service{}}, handler.EnqueueRequestsFromMapFunc(annotationFilter), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Watches( - &source.Kind{Type: &corev1.Secret{}}, + &source.Kind{Type: &corev1.ConfigMap{}}, handler.EnqueueRequestsFromMapFunc(annotationFilter), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). Watches( - &source.Kind{Type: &corev1.ConfigMap{}}, + &source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(annotationFilter), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). - WithEventFilter(resources.IgnoreDeletetionPredicate()). + WithEventFilter(resources.IsRemoteStorageNodeSetCreatePredicate()). + WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()). Complete(r) } diff --git a/internal/controllers/storage/controller.go b/internal/controllers/storage/controller.go index 15dfec9a..23354aeb 100644 --- a/internal/controllers/storage/controller.go +++ b/internal/controllers/storage/controller.go @@ -14,6 +14,7 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/log" @@ -160,23 +161,31 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { } return controller. - For(&v1alpha1.Storage{}). - Owns(&v1alpha1.RemoteStorageNodeSet{}). - Owns(&v1alpha1.StorageNodeSet{}). - Owns(&appsv1.StatefulSet{}). - Owns(&corev1.ConfigMap{}). - Owns(&corev1.Service{}). + For(&v1alpha1.Storage{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), + ). + Owns(&v1alpha1.RemoteStorageNodeSet{}, + builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194 + ). + Owns(&v1alpha1.StorageNodeSet{}, + builder.WithPredicates(resources.LastAppliedAnnotationPredicate()), // TODO: YDBOPS-9194 + ). + Owns(&appsv1.StatefulSet{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), + ). + Owns(&corev1.ConfigMap{}, + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), + ). + Owns(&corev1.Service{}, + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), + ). Watches( &source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(r.findStoragesForSecret), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). - WithEventFilter(predicate.Or( - predicate.GenerationChangedPredicate{}, - resources.LastAppliedAnnotationPredicate(), - resources.IsServicePredicate(), - resources.IsSecretPredicate(), - )). - WithEventFilter(resources.IgnoreDeletetionPredicate()). + WithEventFilter(resources.IsStorageCreatePredicate()). + WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()). Complete(r) } diff --git a/internal/controllers/storage/init.go b/internal/controllers/storage/init.go index cdcb5b78..478e6721 100644 --- a/internal/controllers/storage/init.go +++ b/internal/controllers/storage/init.go @@ -119,6 +119,12 @@ func (r *Reconciler) initializeBlobstorage( return Stop, ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } + r.Recorder.Event( + storage, + corev1.EventTypeNormal, + "InitializingStorage", + fmt.Sprintf("Successfuly created Job %s", fmt.Sprintf(resources.InitJobNameFormat, storage.Name)), + ) return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, nil } @@ -207,6 +213,12 @@ func (r *Reconciler) initializeBlobstorage( return r.updateStatus(ctx, storage, StatusUpdateRequeueDelay) } + r.Recorder.Event( + storage, + corev1.EventTypeNormal, + "InitializingStorage", + fmt.Sprintf("Waiting for Job %s status update", initJob.Name), + ) return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, nil } diff --git a/internal/controllers/storagenodeset/controller.go b/internal/controllers/storagenodeset/controller.go index e819c912..e65e2070 100644 --- a/internal/controllers/storagenodeset/controller.go +++ b/internal/controllers/storagenodeset/controller.go @@ -10,6 +10,7 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" @@ -65,12 +66,13 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { controller := ctrl.NewControllerManagedBy(mgr) return controller. - For(&v1alpha1.StorageNodeSet{}). - Owns(&appsv1.StatefulSet{}). - WithEventFilter(predicate.Or( - predicate.GenerationChangedPredicate{}, - resources.LastAppliedAnnotationPredicate()), + For(&v1alpha1.StorageNodeSet{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), ). - WithEventFilter(resources.IgnoreDeletetionPredicate()). + Owns(&appsv1.StatefulSet{}, + builder.WithPredicates(predicate.GenerationChangedPredicate{}), + ). + WithEventFilter(resources.IsStorageNodeSetCreatePredicate()). + WithEventFilter(resources.IgnoreDeleteStateUnknownPredicate()). Complete(r) } diff --git a/internal/resources/databasenodeset.go b/internal/resources/databasenodeset.go index 9e57519a..a88ffe4e 100644 --- a/internal/resources/databasenodeset.go +++ b/internal/resources/databasenodeset.go @@ -82,6 +82,10 @@ func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [ func NewDatabaseNodeSet(databaseNodeSet *api.DatabaseNodeSet) DatabaseNodeSetResource { crDatabaseNodeSet := databaseNodeSet.DeepCopy() + if crDatabaseNodeSet.Spec.Service.Status.TLSConfiguration == nil { + crDatabaseNodeSet.Spec.Service.Status.TLSConfiguration = &api.TLSConfiguration{Enabled: false} + } + return DatabaseNodeSetResource{DatabaseNodeSet: crDatabaseNodeSet} } diff --git a/internal/resources/predicate.go b/internal/resources/predicate.go new file mode 100644 index 00000000..c0f2bdf5 --- /dev/null +++ b/internal/resources/predicate.go @@ -0,0 +1,91 @@ +package resources + +import ( + "k8s.io/apimachinery/pkg/labels" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/event" + "sigs.k8s.io/controller-runtime/pkg/predicate" + + api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" +) + +func LastAppliedAnnotationPredicate() predicate.Predicate { + return predicate.Funcs{ + UpdateFunc: func(e event.UpdateEvent) bool { + return !annotations.CompareYdbTechAnnotations( + e.ObjectOld.GetAnnotations(), + e.ObjectNew.GetAnnotations(), + ) + }, + } +} + +func IsStorageCreatePredicate() predicate.Predicate { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + _, isStorage := e.Object.(*api.Storage) + return isStorage + }, + } +} + +func IsStorageNodeSetCreatePredicate() predicate.Predicate { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + _, isStorageNodeSet := e.Object.(*api.StorageNodeSet) + return isStorageNodeSet + }, + } +} + +func IsRemoteStorageNodeSetCreatePredicate() predicate.Predicate { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + _, isRemoteStorageNodeSet := e.Object.(*api.RemoteStorageNodeSet) + return isRemoteStorageNodeSet + }, + } +} + +func IsDatabaseCreatePredicate() predicate.Predicate { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + _, isDatabase := e.Object.(*api.Database) + return isDatabase + }, + } +} + +func IsDatabaseNodeSetCreatePredicate() predicate.Predicate { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + _, isDatabaseNodeSet := e.Object.(*api.DatabaseNodeSet) + return isDatabaseNodeSet + }, + } +} + +func IsRemoteDatabaseNodeSetCreatePredicate() predicate.Predicate { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + _, isRemoteDatabaseNodeSet := e.Object.(*api.RemoteDatabaseNodeSet) + return isRemoteDatabaseNodeSet + }, + } +} + +func IgnoreDeleteStateUnknownPredicate() predicate.Predicate { + return predicate.Funcs{ + DeleteFunc: func(e event.DeleteEvent) bool { + // Evaluates to false if the object has been confirmed deleted. + return !e.DeleteStateUnknown + }, + } +} + +func LabelExistsPredicate(selector labels.Selector) predicate.Predicate { + return predicate.NewPredicateFuncs(func(o client.Object) bool { + return selector.Matches(labels.Set(o.GetLabels())) + }) +} diff --git a/internal/resources/remotedatabasenodeset.go b/internal/resources/remotedatabasenodeset.go index c3a7efd4..93fcf5c1 100644 --- a/internal/resources/remotedatabasenodeset.go +++ b/internal/resources/remotedatabasenodeset.go @@ -82,6 +82,10 @@ func (b *RemoteDatabaseNodeSetResource) GetResourceBuilders() []ResourceBuilder func NewRemoteDatabaseNodeSet(remoteDatabaseNodeSet *api.RemoteDatabaseNodeSet) RemoteDatabaseNodeSetResource { crRemoteDatabaseNodeSet := remoteDatabaseNodeSet.DeepCopy() + if crRemoteDatabaseNodeSet.Spec.Service.Status.TLSConfiguration == nil { + crRemoteDatabaseNodeSet.Spec.Service.Status.TLSConfiguration = &api.TLSConfiguration{Enabled: false} + } + return RemoteDatabaseNodeSetResource{RemoteDatabaseNodeSet: crRemoteDatabaseNodeSet} } diff --git a/internal/resources/remotestoragenodeset.go b/internal/resources/remotestoragenodeset.go index 635ed591..c8dbf8d9 100644 --- a/internal/resources/remotestoragenodeset.go +++ b/internal/resources/remotestoragenodeset.go @@ -82,6 +82,10 @@ func (b *RemoteStorageNodeSetResource) GetResourceBuilders() []ResourceBuilder { func NewRemoteStorageNodeSet(remoteStorageNodeSet *api.RemoteStorageNodeSet) RemoteStorageNodeSetResource { crRemoteStorageNodeSet := remoteStorageNodeSet.DeepCopy() + if crRemoteStorageNodeSet.Spec.Service.Status.TLSConfiguration == nil { + crRemoteStorageNodeSet.Spec.Service.Status.TLSConfiguration = &api.TLSConfiguration{Enabled: false} + } + return RemoteStorageNodeSetResource{RemoteStorageNodeSet: crRemoteStorageNodeSet} } diff --git a/internal/resources/resource.go b/internal/resources/resource.go index ac3f0e52..0a6be746 100644 --- a/internal/resources/resource.go +++ b/internal/resources/resource.go @@ -16,7 +16,6 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/rest" @@ -24,8 +23,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/event" - "sigs.k8s.io/controller-runtime/pkg/predicate" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" @@ -353,50 +350,6 @@ func EqualRemoteResourceWithObject( return false } -func LastAppliedAnnotationPredicate() predicate.Predicate { - return predicate.Funcs{ - UpdateFunc: func(e event.UpdateEvent) bool { - return !ydbannotations.CompareYdbTechAnnotations( - e.ObjectOld.GetAnnotations(), - e.ObjectNew.GetAnnotations(), - ) - }, - } -} - -func IgnoreDeletetionPredicate() predicate.Predicate { - return predicate.Funcs{ - DeleteFunc: func(e event.DeleteEvent) bool { - // Evaluates to false if the object has been confirmed deleted. - return !e.DeleteStateUnknown - }, - } -} - -func IsServicePredicate() predicate.Predicate { - return predicate.Funcs{ - UpdateFunc: func(e event.UpdateEvent) bool { - _, isService := e.ObjectOld.(*corev1.Service) - return isService - }, - } -} - -func IsSecretPredicate() predicate.Predicate { - return predicate.Funcs{ - UpdateFunc: func(e event.UpdateEvent) bool { - _, isSecret := e.ObjectOld.(*corev1.Secret) - return isSecret - }, - } -} - -func LabelExistsPredicate(selector labels.Selector) predicate.Predicate { - return predicate.NewPredicateFuncs(func(o client.Object) bool { - return selector.Matches(labels.Set(o.GetLabels())) - }) -} - func getYDBStaticCredentials( ctx context.Context, storage *api.Storage, diff --git a/internal/resources/storagenodeset.go b/internal/resources/storagenodeset.go index d206474f..d3985873 100644 --- a/internal/resources/storagenodeset.go +++ b/internal/resources/storagenodeset.go @@ -84,9 +84,11 @@ func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [] func NewStorageNodeSet(storageNodeSet *api.StorageNodeSet) StorageNodeSetResource { crStorageNodeSet := storageNodeSet.DeepCopy() - return StorageNodeSetResource{ - StorageNodeSet: crStorageNodeSet, + if crStorageNodeSet.Spec.Service.Status.TLSConfiguration == nil { + crStorageNodeSet.Spec.Service.Status.TLSConfiguration = &api.TLSConfiguration{Enabled: false} } + + return StorageNodeSetResource{StorageNodeSet: crStorageNodeSet} } func (b *StorageNodeSetResource) Unwrap() *api.StorageNodeSet { From bcaeb6c80f1d1392ace20c0f3b8739d518f63592 Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 14:08:29 +0300 Subject: [PATCH 02/12] unit-test detect StatefulSet delete event --- Makefile | 4 ++-- internal/controllers/storage/controller_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 03c416e9..8807f845 100644 --- a/Makefile +++ b/Makefile @@ -79,11 +79,11 @@ kind-load: .PHONY: unit-test unit-test: manifests generate fmt vet envtest ## Run unit tests - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --arch=amd64 $(ENVTEST_K8S_VERSION) -p path)" go test -v -timeout 1800s -p 1 ./internal/... -ginkgo.v -coverprofile cover.out + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --arch=amd64 $(ENVTEST_K8S_VERSION) -p path)" go test -v -timeout 900s -p 1 ./internal/... -ginkgo.v -coverprofile cover.out .PHONY: e2e-test e2e-test: manifests generate fmt vet docker-build kind-init kind-load ## Run e2e tests - go test -v -timeout 1800s -p 1 ./e2e/... -args -ginkgo.v + go test -v -timeout 3600s -p 1 ./e2e/... -args -ginkgo.v .PHONY: test test: unit-test e2e-test ## Run all tests diff --git a/internal/controllers/storage/controller_test.go b/internal/controllers/storage/controller_test.go index 7500535a..2f7ed7ae 100644 --- a/internal/controllers/storage/controller_test.go +++ b/internal/controllers/storage/controller_test.go @@ -186,6 +186,22 @@ var _ = Describe("Storage controller medium tests", func() { }, }, )) + + By("check that delete StatefulSet event was detected...") + Expect(k8sClient.List(ctx, + &foundStatefulSets, + client.InNamespace(testobjects.YdbNamespace), + )).ShouldNot(HaveOccurred()) + Expect(len(foundStatefulSets.Items)).Should(Equal(1)) + Expect(k8sClient.Delete(ctx, + &foundStatefulSets.Items[0], + )).ShouldNot(HaveOccurred()) + Eventually(func() int { + Expect(k8sClient.List(ctx, &foundStatefulSets, + client.InNamespace(testobjects.YdbNamespace), + )) + return len(foundStatefulSets.Items) + }, test.Timeout, test.Interval).Should(Equal(1)) }) }) }) From f48982dc1827cc7d9b58c815c03961a8bf220b61 Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 14:51:10 +0300 Subject: [PATCH 03/12] compare only lastApplied annotation --- internal/annotations/annotations.go | 33 +++++-------------- internal/controllers/database/controller.go | 7 +++- .../controllers/storage/controller_test.go | 13 ++------ internal/resources/predicate.go | 2 +- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/internal/annotations/annotations.go b/internal/annotations/annotations.go index e3942940..db590ad8 100644 --- a/internal/annotations/annotations.go +++ b/internal/annotations/annotations.go @@ -1,7 +1,5 @@ package annotations -import "strings" - const ( PrimaryResourceStorageAnnotation = "ydb.tech/primary-resource-storage" PrimaryResourceDatabaseAnnotation = "ydb.tech/primary-resource-database" @@ -11,30 +9,17 @@ const ( LastAppliedAnnotation = "ydb.tech/last-applied" ) -func GetYdbTechAnnotations(annotations map[string]string) map[string]string { - result := make(map[string]string) - for key, value := range annotations { - if strings.HasPrefix(key, "ydb.tech/") { - result[key] = value - } - } - return result +func CompareLastAppliedAnnotation(map1, map2 map[string]string) bool { + value1 := getLastAppliedAnnotation(map1) + value2 := getLastAppliedAnnotation(map2) + return value1 == value2 } -func CompareMaps(map1, map2 map[string]string) bool { - if len(map1) != len(map2) { - return false - } - for key1, value1 := range map1 { - if value2, ok := map2[key1]; !ok || value2 != value1 { - return false +func getLastAppliedAnnotation(annotations map[string]string) string { + for key, value := range annotations { + if key == LastAppliedAnnotation { + return value } } - return true -} - -func CompareYdbTechAnnotations(map1, map2 map[string]string) bool { - map1 = GetYdbTechAnnotations(map1) - map2 = GetYdbTechAnnotations(map2) - return CompareMaps(map1, map2) + return "" } diff --git a/internal/controllers/database/controller.go b/internal/controllers/database/controller.go index 39455610..71fa7f18 100644 --- a/internal/controllers/database/controller.go +++ b/internal/controllers/database/controller.go @@ -71,7 +71,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } - return r.Sync(ctx, resource) + result, err := r.Sync(ctx, resource) + if err != nil { + r.Log.Error(err, "unexpected Sync error") + } + + return result, err } // Create FieldIndexer to usage for List requests in Reconcile diff --git a/internal/controllers/storage/controller_test.go b/internal/controllers/storage/controller_test.go index 2f7ed7ae..92e74ff3 100644 --- a/internal/controllers/storage/controller_test.go +++ b/internal/controllers/storage/controller_test.go @@ -188,18 +188,11 @@ var _ = Describe("Storage controller medium tests", func() { )) By("check that delete StatefulSet event was detected...") - Expect(k8sClient.List(ctx, - &foundStatefulSets, - client.InNamespace(testobjects.YdbNamespace), - )).ShouldNot(HaveOccurred()) + Expect(k8sClient.List(ctx, &foundStatefulSets, client.InNamespace(testobjects.YdbNamespace))).ShouldNot(HaveOccurred()) Expect(len(foundStatefulSets.Items)).Should(Equal(1)) - Expect(k8sClient.Delete(ctx, - &foundStatefulSets.Items[0], - )).ShouldNot(HaveOccurred()) + Expect(k8sClient.Delete(ctx, &foundStatefulSets.Items[0])).ShouldNot(HaveOccurred()) Eventually(func() int { - Expect(k8sClient.List(ctx, &foundStatefulSets, - client.InNamespace(testobjects.YdbNamespace), - )) + Expect(k8sClient.List(ctx, &foundStatefulSets, client.InNamespace(testobjects.YdbNamespace))).ShouldNot(HaveOccurred()) return len(foundStatefulSets.Items) }, test.Timeout, test.Interval).Should(Equal(1)) }) diff --git a/internal/resources/predicate.go b/internal/resources/predicate.go index c0f2bdf5..65e1a241 100644 --- a/internal/resources/predicate.go +++ b/internal/resources/predicate.go @@ -13,7 +13,7 @@ import ( func LastAppliedAnnotationPredicate() predicate.Predicate { return predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { - return !annotations.CompareYdbTechAnnotations( + return !annotations.CompareLastAppliedAnnotation( e.ObjectOld.GetAnnotations(), e.ObjectNew.GetAnnotations(), ) From 5ba1e13f6495cc2cd7368297a445c53a8c4b3ded Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 15:07:06 +0300 Subject: [PATCH 04/12] fix misspell lint --- internal/controllers/storage/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controllers/storage/init.go b/internal/controllers/storage/init.go index 478e6721..d92f6c88 100644 --- a/internal/controllers/storage/init.go +++ b/internal/controllers/storage/init.go @@ -123,7 +123,7 @@ func (r *Reconciler) initializeBlobstorage( storage, corev1.EventTypeNormal, "InitializingStorage", - fmt.Sprintf("Successfuly created Job %s", fmt.Sprintf(resources.InitJobNameFormat, storage.Name)), + fmt.Sprintf("Successfully created Job %s", fmt.Sprintf(resources.InitJobNameFormat, storage.Name)), ) return Stop, ctrl.Result{RequeueAfter: StorageInitializationRequeueDelay}, nil } From 9af2269e0f986b50979c8b607c3b41b23dee334a Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 15:15:59 +0300 Subject: [PATCH 05/12] bump helm chart version --- deploy/ydb-operator/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deploy/ydb-operator/Chart.yaml b/deploy/ydb-operator/Chart.yaml index c1595dc3..049a171d 100644 --- a/deploy/ydb-operator/Chart.yaml +++ b/deploy/ydb-operator/Chart.yaml @@ -15,10 +15,10 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.5.18 +version: 0.5.19 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. # It is recommended to use it with quotes. -appVersion: "0.5.18" +appVersion: "0.5.19" From 35a3c4ff15bc2dd3b4cb9ca07a743982e305d99f Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 19:31:21 +0300 Subject: [PATCH 06/12] move annotations to separate package --- api/v1alpha1/const.go | 2 +- internal/annotations/annotations.go | 85 +++++++++++++++++-- internal/annotations/annotations_test.go | 48 +++++++++++ internal/controllers/database/init.go | 21 +++-- .../remotedatabasenodeset/controller.go | 38 ++++----- .../remotedatabasenodeset/remote_objects.go | 4 +- .../remotestoragenodeset/controller.go | 14 +-- .../remotestoragenodeset/remote_objects.go | 6 +- internal/controllers/storage/init.go | 21 +++-- 9 files changed, 182 insertions(+), 57 deletions(-) create mode 100644 internal/annotations/annotations_test.go diff --git a/api/v1alpha1/const.go b/api/v1alpha1/const.go index e67b55c9..63d53f0e 100644 --- a/api/v1alpha1/const.go +++ b/api/v1alpha1/const.go @@ -51,7 +51,7 @@ const ( AnnotationNodeHost = "ydb.tech/node-host" AnnotationNodeDomain = "ydb.tech/node-domain" - AnnotationValueTrue = "true" + FinalizerRemote = "ydb.tech/remote-finalizer" legacyTenantNameFormat = "/%s/%s" ) diff --git a/internal/annotations/annotations.go b/internal/annotations/annotations.go index db590ad8..05cc3df2 100644 --- a/internal/annotations/annotations.go +++ b/internal/annotations/annotations.go @@ -1,14 +1,85 @@ package annotations +import ( + "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" + corev1 "k8s.io/api/core/v1" +) + const ( - PrimaryResourceStorageAnnotation = "ydb.tech/primary-resource-storage" - PrimaryResourceDatabaseAnnotation = "ydb.tech/primary-resource-database" - RemoteResourceVersionAnnotation = "ydb.tech/remote-resource-version" - ConfigurationChecksum = "ydb.tech/configuration-checksum" - RemoteFinalizerKey = "ydb.tech/remote-finalizer" - LastAppliedAnnotation = "ydb.tech/last-applied" + PrimaryResourceStorage = "ydb.tech/primary-resource-storage" + PrimaryResourceDatabase = "ydb.tech/primary-resource-database" + RemoteResourceVersion = "ydb.tech/remote-resource-version" + ConfigurationChecksum = "ydb.tech/configuration-checksum" + LastApplied = "ydb.tech/last-applied" +) + +var ( + AcceptedDNSPolicy = []string{ + string(corev1.DNSClusterFirstWithHostNet), + string(corev1.DNSClusterFirst), + string(corev1.DNSDefault), + string(corev1.DNSNone), + } + UserAnnotations = map[string]struct{}{ + v1alpha1.AnnotationSkipInitialization: struct{}{}, + v1alpha1.AnnotationUpdateStrategyOnDelete: struct{}{}, + v1alpha1.AnnotationUpdateDNSPolicy: struct{}{}, + v1alpha1.AnnotationDisableLivenessProbe: struct{}{}, + v1alpha1.AnnotationDataCenter: struct{}{}, + v1alpha1.AnnotationGRPCPublicHost: struct{}{}, + v1alpha1.AnnotationNodeHost: struct{}{}, + v1alpha1.AnnotationNodeDomain: struct{}{}, + } ) +type Annotations map[string]string + +func Common(objAnnotations Annotations) Annotations { + an := Annotations{} + + an.Merge(getUserAnnotations(objAnnotations)) + + return an +} + +func (an Annotations) Merge(other map[string]string) map[string]string { + if other == nil { + return an + } + + for k, v := range other { + an[k] = v + } + + return an +} + +func (an Annotations) AsMap() map[string]string { + return an +} + +func (an Annotations) Copy() Annotations { + res := Annotations{} + + for k, v := range an { + res[k] = v + } + + return res +} + +func getUserAnnotations(annotations map[string]string) map[string]string { + common := make(map[string]string) + + for key, value := range annotations { + if _, exists := UserAnnotations[key]; exists { + common[key] = value + } + } + + return common +} + func CompareLastAppliedAnnotation(map1, map2 map[string]string) bool { value1 := getLastAppliedAnnotation(map1) value2 := getLastAppliedAnnotation(map2) @@ -17,7 +88,7 @@ func CompareLastAppliedAnnotation(map1, map2 map[string]string) bool { func getLastAppliedAnnotation(annotations map[string]string) string { for key, value := range annotations { - if key == LastAppliedAnnotation { + if key == LastApplied { return value } } diff --git a/internal/annotations/annotations_test.go b/internal/annotations/annotations_test.go new file mode 100644 index 00000000..a5048726 --- /dev/null +++ b/internal/annotations/annotations_test.go @@ -0,0 +1,48 @@ +package annotations_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" +) + +func TestLabels(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Label suite") +} + +var _ = Describe("Testing annotations", func() { + It("merges two sets of annotations", func() { + fstLabels := annotations.Annotations{ + "a": "a", + "b": "b", + } + + sndLabels := annotations.Annotations{ + "c": "c", + "d": "d", + } + + Expect(fstLabels.Merge(sndLabels)).To(BeEquivalentTo(map[string]string{ + "a": "a", + "b": "b", + "c": "c", + "d": "d", + })) + }) + + It("sets correct defaults", func() { + Expect(annotations.Common(map[string]string{ + "ydb.tech/skip-initialization": "true", + "ydb.tech/node-host": "ydb-testing.k8s-c.yandex.net", + "ydb.tech/last-applied": "some-body", + "sample-annotation": "test", + })).To(BeEquivalentTo(map[string]string{ + "ydb.tech/skip-initialization": "true", + "ydb.tech/node-host": "ydb-testing.k8s-c.yandex.net", + })) + }) +}) diff --git a/internal/controllers/database/init.go b/internal/controllers/database/init.go index 6b878266..edb49253 100644 --- a/internal/controllers/database/init.go +++ b/internal/controllers/database/init.go @@ -3,6 +3,7 @@ package database import ( "context" "fmt" + "strconv" "github.com/ydb-platform/ydb-go-sdk/v3" corev1 "k8s.io/api/core/v1" @@ -34,15 +35,17 @@ func (r *Reconciler) setInitPipelineStatus( } // This block is special internal logic that skips all Database initialization. - if value, ok := database.Annotations[v1alpha1.AnnotationSkipInitialization]; ok && value == v1alpha1.AnnotationValueTrue { - r.Log.Info("Database initialization disabled (with annotation), proceed with caution") - r.Recorder.Event( - database, - corev1.EventTypeWarning, - "SkippingInit", - "Skipping initialization due to skip annotation present, be careful!", - ) - return r.setInitDatabaseCompleted(ctx, database, "Database initialization not performed because initialization is skipped") + if value, ok := database.Annotations[v1alpha1.AnnotationSkipInitialization]; ok { + if isTrue, _ := strconv.ParseBool(value); isTrue { + r.Log.Info("Database initialization disabled (with annotation), proceed with caution") + r.Recorder.Event( + database, + corev1.EventTypeWarning, + "SkippingInit", + "Skipping initialization due to skip annotation present, be careful!", + ) + return r.setInitDatabaseCompleted(ctx, database, "Database initialization not performed because initialization is skipped") + } } if meta.IsStatusConditionTrue(database.Status.Conditions, OldDatabaseInitializedCondition) { diff --git a/internal/controllers/remotedatabasenodeset/controller.go b/internal/controllers/remotedatabasenodeset/controller.go index ae0d662c..5ec2ca40 100644 --- a/internal/controllers/remotedatabasenodeset/controller.go +++ b/internal/controllers/remotedatabasenodeset/controller.go @@ -6,7 +6,7 @@ import ( "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/labels" + apilabels "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/selection" "k8s.io/apimachinery/pkg/types" @@ -23,9 +23,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck - ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" ) @@ -72,15 +72,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu // The object is not being deleted, so if it does not have our finalizer, // then lets add the finalizer and update the object. This is equivalent // to registering our finalizer. - if !controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey) { - controllerutil.AddFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey) + if !controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote) { + controllerutil.AddFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote) if err := r.RemoteClient.Update(ctx, remoteDatabaseNodeSet); err != nil { return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } } } else { // The object is being deleted - if controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey) { + if controllerutil.ContainsFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote) { // our finalizer is present, so lets handle any external dependency if err := r.deleteExternalResources(ctx, remoteDatabaseNodeSet); err != nil { // if fail to delete the external dependency here, return with error @@ -89,7 +89,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } // remove our finalizer from the list and update it. - controllerutil.RemoveFinalizer(remoteDatabaseNodeSet, ydbannotations.RemoteFinalizerKey) + controllerutil.RemoveFinalizer(remoteDatabaseNodeSet, v1alpha1.FinalizerRemote) if err := r.RemoteClient.Update(ctx, remoteDatabaseNodeSet); err != nil { return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } @@ -118,8 +118,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C annotationFilter := func(mapObj client.Object) []reconcile.Request { requests := make([]reconcile.Request, 0) - annotations := mapObj.GetAnnotations() - primaryResourceName, exist := annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] + an := mapObj.GetAnnotations() + primaryResourceName, exist := an[annotations.PrimaryResourceDatabase] if exist { databaseNodeSets := &v1alpha1.DatabaseNodeSetList{} if err := r.Client.List( @@ -192,10 +192,10 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C Complete(r) } -func buildLocalSelector() (labels.Selector, error) { - labelRequirements := []labels.Requirement{} - localClusterRequirement, err := labels.NewRequirement( - ydblabels.RemoteClusterKey, +func buildLocalSelector() (apilabels.Selector, error) { + labelRequirements := []apilabels.Requirement{} + localClusterRequirement, err := apilabels.NewRequirement( + labels.RemoteClusterKey, selection.Exists, []string{}, ) @@ -203,13 +203,13 @@ func buildLocalSelector() (labels.Selector, error) { return nil, err } labelRequirements = append(labelRequirements, *localClusterRequirement) - return labels.NewSelector().Add(labelRequirements...), nil + return apilabels.NewSelector().Add(labelRequirements...), nil } -func BuildRemoteSelector(remoteCluster string) (labels.Selector, error) { - labelRequirements := []labels.Requirement{} - remoteClusterRequirement, err := labels.NewRequirement( - ydblabels.RemoteClusterKey, +func BuildRemoteSelector(remoteCluster string) (apilabels.Selector, error) { + labelRequirements := []apilabels.Requirement{} + remoteClusterRequirement, err := apilabels.NewRequirement( + labels.RemoteClusterKey, selection.Equals, []string{remoteCluster}, ) @@ -217,7 +217,7 @@ func BuildRemoteSelector(remoteCluster string) (labels.Selector, error) { return nil, err } labelRequirements = append(labelRequirements, *remoteClusterRequirement) - return labels.NewSelector().Add(labelRequirements...), nil + return apilabels.NewSelector().Add(labelRequirements...), nil } func (r *Reconciler) deleteExternalResources( diff --git a/internal/controllers/remotedatabasenodeset/remote_objects.go b/internal/controllers/remotedatabasenodeset/remote_objects.go index d600e93d..2ae3c8d8 100644 --- a/internal/controllers/remotedatabasenodeset/remote_objects.go +++ b/internal/controllers/remotedatabasenodeset/remote_objects.go @@ -278,7 +278,7 @@ func (r *Reconciler) removeUnusedRemoteObjects( // Remove annotation if no one another DatabaseNodeSet if !existInDatabase { // Try to update existing object in local cluster by rawPatch - patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorageAnnotation)) + patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorage)) updateErr := r.Client.Patch(ctx, localObj, client.RawPatch(types.StrategicMergePatchType, patch)) if updateErr != nil { r.Recorder.Event( @@ -298,7 +298,7 @@ func (r *Reconciler) removeUnusedRemoteObjects( } // Delete resource if annotation `ydb.tech/primary-resource-storage` does not exist - _, existInStorage := localObj.GetAnnotations()[ydbannotations.PrimaryResourceStorageAnnotation] + _, existInStorage := localObj.GetAnnotations()[ydbannotations.PrimaryResourceStorage] if !existInStorage { // Try to delete unused resource from local cluster deleteErr := r.Client.Delete(ctx, localObj) diff --git a/internal/controllers/remotestoragenodeset/controller.go b/internal/controllers/remotestoragenodeset/controller.go index 91ce95ea..fc495af7 100644 --- a/internal/controllers/remotestoragenodeset/controller.go +++ b/internal/controllers/remotestoragenodeset/controller.go @@ -23,7 +23,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" @@ -72,15 +72,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu // The object is not being deleted, so if it does not have our finalizer, // then lets add the finalizer and update the object. This is equivalent // to registering our finalizer. - if !controllerutil.ContainsFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey) { - controllerutil.AddFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey) + if !controllerutil.ContainsFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote) { + controllerutil.AddFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote) if err := r.RemoteClient.Update(ctx, remoteStorageNodeSet); err != nil { return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } } } else { // The object is being deleted - if controllerutil.ContainsFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey) { + if controllerutil.ContainsFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote) { // our finalizer is present, so lets handle any external dependency if err := r.deleteExternalResources(ctx, remoteStorageNodeSet); err != nil { // if fail to delete the external dependency here, return with error @@ -89,7 +89,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } // remove our finalizer from the list and update it. - controllerutil.RemoveFinalizer(remoteStorageNodeSet, ydbannotations.RemoteFinalizerKey) + controllerutil.RemoveFinalizer(remoteStorageNodeSet, v1alpha1.FinalizerRemote) if err := r.RemoteClient.Update(ctx, remoteStorageNodeSet); err != nil { return ctrl.Result{RequeueAfter: DefaultRequeueDelay}, err } @@ -118,8 +118,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C annotationFilter := func(mapObj client.Object) []reconcile.Request { requests := make([]reconcile.Request, 0) - annotations := mapObj.GetAnnotations() - primaryResourceName, exist := annotations[ydbannotations.PrimaryResourceStorageAnnotation] + an := mapObj.GetAnnotations() + primaryResourceName, exist := an[annotations.PrimaryResourceStorage] if exist { storageNodeSets := &v1alpha1.StorageNodeSetList{} if err := r.Client.List( diff --git a/internal/controllers/remotestoragenodeset/remote_objects.go b/internal/controllers/remotestoragenodeset/remote_objects.go index a54f6a39..9e43953b 100644 --- a/internal/controllers/remotestoragenodeset/remote_objects.go +++ b/internal/controllers/remotestoragenodeset/remote_objects.go @@ -16,7 +16,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" @@ -277,7 +277,7 @@ func (r *Reconciler) removeUnusedRemoteObjects( // Remove annotation if no one another StorageNodeSet if !existInStorage { - patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorageAnnotation)) + patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, annotations.PrimaryResourceStorage)) updateErr := r.Client.Patch(ctx, localObj, client.RawPatch(types.StrategicMergePatchType, patch)) if updateErr != nil { r.Recorder.Event( @@ -297,7 +297,7 @@ func (r *Reconciler) removeUnusedRemoteObjects( } // Delete resource if annotation `ydb.tech/primary-resource-database` does not exist - _, existInDatabase := localObj.GetAnnotations()[ydbannotations.PrimaryResourceDatabaseAnnotation] + _, existInDatabase := localObj.GetAnnotations()[annotations.PrimaryResourceDatabase] if !existInDatabase { // Try to delete unused resource from local cluster deleteErr := r.Client.Delete(ctx, localObj) diff --git a/internal/controllers/storage/init.go b/internal/controllers/storage/init.go index d92f6c88..77d74454 100644 --- a/internal/controllers/storage/init.go +++ b/internal/controllers/storage/init.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "regexp" + "strconv" "strings" "time" @@ -44,15 +45,17 @@ func (r *Reconciler) setInitPipelineStatus( } // This block is special internal logic that skips all Storage initialization. - if value, ok := storage.Annotations[v1alpha1.AnnotationSkipInitialization]; ok && value == v1alpha1.AnnotationValueTrue { - r.Log.Info("Storage initialization disabled (with annotation), proceed with caution") - r.Recorder.Event( - storage, - corev1.EventTypeWarning, - "SkippingInit", - "Skipping initialization due to skip annotation present, be careful!", - ) - return r.setInitStorageCompleted(ctx, storage, "Storage initialization not performed because initialization is skipped") + if value, ok := storage.Annotations[v1alpha1.AnnotationSkipInitialization]; ok { + if isTrue, _ := strconv.ParseBool(value); isTrue { + r.Log.Info("Storage initialization disabled (with annotation), proceed with caution") + r.Recorder.Event( + storage, + corev1.EventTypeWarning, + "SkippingInit", + "Skipping initialization due to skip annotation present, be careful!", + ) + return r.setInitStorageCompleted(ctx, storage, "Storage initialization not performed because initialization is skipped") + } } if meta.IsStatusConditionTrue(storage.Status.Conditions, OldStorageInitializedCondition) { From 5affe38178c5e25c8553dbfd6989952d5e70cb8d Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 19:34:07 +0300 Subject: [PATCH 07/12] update resources builder logic --- internal/resources/database_statefulset.go | 39 +++++++++++++-------- internal/resources/remotedatabasenodeset.go | 22 ++++++------ internal/resources/remotestoragenodeset.go | 22 ++++++------ internal/resources/resource.go | 22 ++++++------ internal/resources/storage_init_job.go | 10 +++--- internal/resources/storage_statefulset.go | 27 ++++++++------ 6 files changed, 77 insertions(+), 65 deletions(-) diff --git a/internal/resources/database_statefulset.go b/internal/resources/database_statefulset.go index 687ed8e4..a9efadff 100644 --- a/internal/resources/database_statefulset.go +++ b/internal/resources/database_statefulset.go @@ -7,6 +7,7 @@ import ( "log" "regexp" "strconv" + "strings" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -16,6 +17,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr" ) @@ -63,9 +65,11 @@ func (b *DatabaseStatefulSetBuilder) Build(obj client.Object) error { Template: b.buildPodTemplateSpec(), } - if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok && value == api.AnnotationValueTrue { - sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{ - Type: "OnDelete", + if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok { + if isTrue, _ := strconv.ParseBool(value); isTrue { + sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{ + Type: "OnDelete", + } } } @@ -129,12 +133,10 @@ func (b *DatabaseStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSp } if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateDNSPolicy]; ok { - switch value { - case string(corev1.DNSClusterFirstWithHostNet), string(corev1.DNSClusterFirst), string(corev1.DNSDefault), string(corev1.DNSNone): - podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) - case "": - podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst - default: + for _, acceptedPolicy := range annotations.AcceptedDNSPolicy { + if value == acceptedPolicy { + podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) + } } } @@ -412,13 +414,17 @@ func (b *DatabaseStatefulSetBuilder) buildContainer() corev1.Container { }, } - if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; !ok || value != api.AnnotationValueTrue { - container.LivenessProbe = &corev1.Probe{ - ProbeHandler: corev1.ProbeHandler{ - TCPSocket: &corev1.TCPSocketAction{ - Port: intstr.FromInt(api.GRPCPort), - }, + container.LivenessProbe = &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + TCPSocket: &corev1.TCPSocketAction{ + Port: intstr.FromInt(api.GRPCPort), }, + }, + } + + if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; ok { + if isTrue, _ := strconv.ParseBool(value); isTrue { + container.LivenessProbe = nil } } @@ -648,6 +654,9 @@ func (b *DatabaseStatefulSetBuilder) buildContainerArgs() ([]string, []string) { } if value, ok := b.ObjectMeta.Annotations[api.AnnotationNodeHost]; ok { + if !strings.HasPrefix(value, "$(NODE_NAME).") { + value = fmt.Sprintf("%s.%s", "$(NODE_NAME)", value) + } args = append(args, "--node-host", value, diff --git a/internal/resources/remotedatabasenodeset.go b/internal/resources/remotedatabasenodeset.go index 93fcf5c1..acaa90d5 100644 --- a/internal/resources/remotedatabasenodeset.go +++ b/internal/resources/remotedatabasenodeset.go @@ -12,7 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/apiutil" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ) @@ -62,7 +62,7 @@ func (b *RemoteDatabaseNodeSetResource) GetResourceBuilders() []ResourceBuilder var resourceBuilders []ResourceBuilder nodeSetAnnotations := CopyDict(b.Annotations) - delete(nodeSetAnnotations, ydbannotations.LastAppliedAnnotation) + delete(nodeSetAnnotations, annotations.LastApplied) resourceBuilders = append(resourceBuilders, &DatabaseNodeSetBuilder{ @@ -164,26 +164,26 @@ func (b *RemoteDatabaseNodeSetResource) GetRemoteObjects( } func (b *RemoteDatabaseNodeSetResource) SetPrimaryResourceAnnotations(obj client.Object) { - annotations := make(map[string]string) + an := make(map[string]string) for key, value := range obj.GetAnnotations() { - annotations[key] = value + an[key] = value } - if _, exist := annotations[ydbannotations.PrimaryResourceDatabaseAnnotation]; !exist { - annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] = b.Spec.DatabaseRef.Name + if _, exist := an[annotations.PrimaryResourceDatabase]; !exist { + an[annotations.PrimaryResourceDatabase] = b.Spec.DatabaseRef.Name } - obj.SetAnnotations(annotations) + obj.SetAnnotations(an) } func (b *RemoteDatabaseNodeSetResource) UnsetPrimaryResourceAnnotations(obj client.Object) { - annotations := make(map[string]string) + an := make(map[string]string) for key, value := range obj.GetAnnotations() { - if key != annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] { - annotations[key] = value + if key != an[annotations.PrimaryResourceDatabase] { + an[key] = value } } - obj.SetAnnotations(annotations) + obj.SetAnnotations(an) } func (b *RemoteDatabaseNodeSetResource) CreateRemoteResourceStatus( diff --git a/internal/resources/remotestoragenodeset.go b/internal/resources/remotestoragenodeset.go index c8dbf8d9..193b916b 100644 --- a/internal/resources/remotestoragenodeset.go +++ b/internal/resources/remotestoragenodeset.go @@ -12,7 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/apiutil" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ) @@ -62,7 +62,7 @@ func (b *RemoteStorageNodeSetResource) GetResourceBuilders() []ResourceBuilder { var resourceBuilders []ResourceBuilder nodeSetAnnotations := CopyDict(b.Annotations) - delete(nodeSetAnnotations, ydbannotations.LastAppliedAnnotation) + delete(nodeSetAnnotations, annotations.LastApplied) resourceBuilders = append(resourceBuilders, &StorageNodeSetBuilder{ @@ -145,26 +145,26 @@ func (b *RemoteStorageNodeSetResource) GetRemoteObjects( } func (b *RemoteStorageNodeSetResource) SetPrimaryResourceAnnotations(obj client.Object) { - annotations := make(map[string]string) + an := make(map[string]string) for key, value := range obj.GetAnnotations() { - annotations[key] = value + an[key] = value } - if _, exist := annotations[ydbannotations.PrimaryResourceStorageAnnotation]; !exist { - annotations[ydbannotations.PrimaryResourceStorageAnnotation] = b.Spec.StorageRef.Name + if _, exist := an[annotations.PrimaryResourceStorage]; !exist { + an[annotations.PrimaryResourceStorage] = b.Spec.StorageRef.Name } - obj.SetAnnotations(annotations) + obj.SetAnnotations(an) } func (b *RemoteStorageNodeSetResource) UnsetPrimaryResourceAnnotations(obj client.Object) { - annotations := make(map[string]string) + an := make(map[string]string) for key, value := range obj.GetAnnotations() { - if key != annotations[ydbannotations.PrimaryResourceStorageAnnotation] { - annotations[key] = value + if key != an[annotations.PrimaryResourceStorage] { + an[key] = value } } - obj.SetAnnotations(annotations) + obj.SetAnnotations(an) } func (b *RemoteStorageNodeSetResource) CreateRemoteResourceStatus(remoteObj client.Object) { diff --git a/internal/resources/resource.go b/internal/resources/resource.go index 0a6be746..1e1bc919 100644 --- a/internal/resources/resource.go +++ b/internal/resources/resource.go @@ -25,7 +25,7 @@ import ( ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/connection" ) @@ -82,7 +82,7 @@ type ResourceBuilder interface { } var ( - annotator = patch.NewAnnotator(ydbannotations.LastAppliedAnnotation) + annotator = patch.NewAnnotator(annotations.LastApplied) patchMaker = patch.NewPatchMaker(annotator) ) @@ -263,23 +263,23 @@ func UpdateResource(oldObj, newObj client.Object) client.Object { } func CopyPrimaryResourceObjectAnnotation(obj client.Object, oldAnnotations map[string]string) { - annotations := CopyDict(obj.GetAnnotations()) + an := CopyDict(obj.GetAnnotations()) for key, value := range oldAnnotations { - if key == ydbannotations.PrimaryResourceDatabaseAnnotation || - key == ydbannotations.PrimaryResourceStorageAnnotation { - annotations[key] = value + if key == annotations.PrimaryResourceDatabase || + key == annotations.PrimaryResourceStorage { + an[key] = value } } - obj.SetAnnotations(annotations) + obj.SetAnnotations(an) } func SetRemoteResourceVersionAnnotation(obj client.Object, resourceVersion string) { - annotations := make(map[string]string) + an := make(map[string]string) for key, value := range obj.GetAnnotations() { - annotations[key] = value + an[key] = value } - annotations[ydbannotations.RemoteResourceVersionAnnotation] = resourceVersion - obj.SetAnnotations(annotations) + an[annotations.RemoteResourceVersion] = resourceVersion + obj.SetAnnotations(an) } func ConvertRemoteResourceToObject(remoteResource api.RemoteResource, namespace string) (client.Object, error) { diff --git a/internal/resources/storage_init_job.go b/internal/resources/storage_init_job.go index 0507623e..b6454eec 100644 --- a/internal/resources/storage_init_job.go +++ b/internal/resources/storage_init_job.go @@ -134,12 +134,10 @@ func (b *StorageInitJobBuilder) buildInitJobPodTemplateSpec() corev1.PodTemplate } if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateDNSPolicy]; ok { - switch value { - case string(corev1.DNSClusterFirstWithHostNet), string(corev1.DNSClusterFirst), string(corev1.DNSDefault), string(corev1.DNSNone): - podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) - case "": - podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst - default: + for _, acceptedPolicy := range annotations.AcceptedDNSPolicy { + if value == acceptedPolicy { + podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) + } } } diff --git a/internal/resources/storage_statefulset.go b/internal/resources/storage_statefulset.go index 23576227..7d401b64 100644 --- a/internal/resources/storage_statefulset.go +++ b/internal/resources/storage_statefulset.go @@ -80,9 +80,11 @@ func (b *StorageStatefulSetBuilder) Build(obj client.Object) error { Template: b.buildPodTemplateSpec(), } - if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok && value == api.AnnotationValueTrue { - sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{ - Type: "OnDelete", + if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateStrategyOnDelete]; ok { + if isTrue, _ := strconv.ParseBool(value); isTrue { + sts.Spec.UpdateStrategy = appsv1.StatefulSetUpdateStrategy{ + Type: "OnDelete", + } } } @@ -153,9 +155,8 @@ func (b *StorageStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSpe switch value { case string(corev1.DNSClusterFirstWithHostNet), string(corev1.DNSClusterFirst), string(corev1.DNSDefault), string(corev1.DNSNone): podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) - case "": - podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst default: + podTemplate.Spec.DNSPolicy = corev1.DNSClusterFirst } } @@ -379,13 +380,17 @@ func (b *StorageStatefulSetBuilder) buildContainer() corev1.Container { // todo Resources: containerResources, } - if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; !ok || value != api.AnnotationValueTrue { - container.LivenessProbe = &corev1.Probe{ - ProbeHandler: corev1.ProbeHandler{ - TCPSocket: &corev1.TCPSocketAction{ - Port: intstr.FromInt(api.GRPCPort), - }, + container.LivenessProbe = &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + TCPSocket: &corev1.TCPSocketAction{ + Port: intstr.FromInt(api.GRPCPort), }, + }, + } + + if value, ok := b.ObjectMeta.Annotations[api.AnnotationDisableLivenessProbe]; ok { + if isTrue, _ := strconv.ParseBool(value); isTrue { + container.LivenessProbe = nil } } From 7225c974e7af0f8779cb7b874c4261038fc30c0e Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 19:34:12 +0300 Subject: [PATCH 08/12] fix tests --- .../remotedatabasenodeset/controller_test.go | 34 +++++++++---------- .../remotestoragenodeset/controller_test.go | 10 +++--- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/internal/controllers/remotedatabasenodeset/controller_test.go b/internal/controllers/remotedatabasenodeset/controller_test.go index 3780c6cf..0c76a327 100644 --- a/internal/controllers/remotedatabasenodeset/controller_test.go +++ b/internal/controllers/remotedatabasenodeset/controller_test.go @@ -29,7 +29,7 @@ import ( "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/database" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/databasenodeset" @@ -530,25 +530,25 @@ var _ = Describe("RemoteDatabaseNodeSet controller tests", func() { return err } - primaryResourceStorage, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceStorageAnnotation] + primaryResourceStorage, exist := remoteSecret.Annotations[annotations.PrimaryResourceStorage] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceStorageAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceStorage, remoteSecret.Name) } if primaryResourceStorage != foundRemoteStorageNodeSet.Spec.StorageRef.Name { return fmt.Errorf("primaryResourceName %s does not equal storageRef name %s", primaryResourceStorage, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - primaryResourceDatabase, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] + primaryResourceDatabase, exist := remoteSecret.Annotations[annotations.PrimaryResourceDatabase] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabaseAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceDatabase, remoteSecret.Name) } if primaryResourceDatabase != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name { return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceDatabase, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersionAnnotation] + remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersionAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) @@ -621,22 +621,22 @@ var _ = Describe("RemoteDatabaseNodeSet controller tests", func() { return err } - _, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceStorageAnnotation] + _, exist := remoteSecret.Annotations[annotations.PrimaryResourceStorage] if exist { - return fmt.Errorf("annotation %s still exist on remoteSecret %s", ydbannotations.PrimaryResourceStorageAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s still exist on remoteSecret %s", annotations.PrimaryResourceStorage, remoteSecret.Name) } - primaryResourceDatabase, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] + primaryResourceDatabase, exist := remoteSecret.Annotations[annotations.PrimaryResourceDatabase] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabaseAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceDatabase, remoteSecret.Name) } if primaryResourceDatabase != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name { return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceDatabase, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersionAnnotation] + remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersionAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) @@ -687,17 +687,17 @@ var _ = Describe("RemoteDatabaseNodeSet controller tests", func() { return err } - primaryResourceDatabase, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabaseAnnotation] + primaryResourceDatabase, exist := remoteSecret.Annotations[annotations.PrimaryResourceDatabase] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabaseAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceDatabase, remoteSecret.Name) } if primaryResourceDatabase != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name { return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceDatabase, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersionAnnotation] + remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersionAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) diff --git a/internal/controllers/remotestoragenodeset/controller_test.go b/internal/controllers/remotestoragenodeset/controller_test.go index 343eace5..25fbd630 100644 --- a/internal/controllers/remotestoragenodeset/controller_test.go +++ b/internal/controllers/remotestoragenodeset/controller_test.go @@ -29,7 +29,7 @@ import ( "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects" - ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + annotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/remotestoragenodeset" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage" @@ -403,17 +403,17 @@ var _ = Describe("RemoteStorageNodeSet controller tests", func() { return err } - primaryResourceName, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceStorageAnnotation] + primaryResourceName, exist := remoteSecret.Annotations[annotations.PrimaryResourceStorage] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceStorageAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceStorage, remoteSecret.Name) } if primaryResourceName != foundRemoteStorageNodeSet.Spec.StorageRef.Name { return fmt.Errorf("primaryResourceName %s does not equal storageRef name %s", primaryResourceName, foundRemoteStorageNodeSet.Spec.StorageRef.Name) } - remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersionAnnotation] + remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersionAnnotation, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) From 826806ca35bb1ea44fba05d2ed75896d21d89c8a Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 23:01:03 +0300 Subject: [PATCH 09/12] propagate ydb.tech annotations --- .../controllers/storage/controller_test.go | 2 +- internal/controllers/storage/init.go | 2 +- internal/labels/label.go | 5 +- internal/resources/database.go | 55 ++++++++---- internal/resources/databasenodeset.go | 48 ++++++---- internal/resources/resource.go | 13 +-- internal/resources/storage.go | 88 +++++++++++++++---- internal/resources/storage_init_job.go | 41 +++------ internal/resources/storagenodeset.go | 48 ++++++---- 9 files changed, 193 insertions(+), 109 deletions(-) diff --git a/internal/controllers/storage/controller_test.go b/internal/controllers/storage/controller_test.go index 92e74ff3..70ce9dc3 100644 --- a/internal/controllers/storage/controller_test.go +++ b/internal/controllers/storage/controller_test.go @@ -123,7 +123,7 @@ var _ = Describe("Storage controller medium tests", func() { }, &foundStorage)).Should(Succeed()) foundConfigurationChecksumAnnotation := false - if podAnnotations[annotations.ConfigurationChecksum] == resources.GetConfigurationChecksum(foundStorage.Spec.Configuration) { + if podAnnotations[annotations.ConfigurationChecksum] == resources.GetSHA256Checksum(foundStorage.Spec.Configuration) { foundConfigurationChecksumAnnotation = true } Expect(foundConfigurationChecksumAnnotation).To(BeTrue()) diff --git a/internal/controllers/storage/init.go b/internal/controllers/storage/init.go index 77d74454..46173d84 100644 --- a/internal/controllers/storage/init.go +++ b/internal/controllers/storage/init.go @@ -306,7 +306,7 @@ func (r *Reconciler) createInitBlobstorageJob( ctx context.Context, storage *resources.StorageClusterBuilder, ) error { - builder := resources.GetInitJobBuilder(storage.DeepCopy()) + builder := storage.GetInitJobBuilder() newResource := builder.Placeholder(storage) _, err := resources.CreateOrUpdateOrMaybeIgnore(ctx, r.Client, newResource, func() error { var err error diff --git a/internal/labels/label.go b/internal/labels/label.go index 19ff5d94..ab2e19ff 100644 --- a/internal/labels/label.go +++ b/internal/labels/label.go @@ -31,8 +31,9 @@ const ( StorageGeneration = "ydb.tech/storage-generation" DatabaseGeneration = "ydb.tech/database-generation" - StorageComponent = "storage-node" - DynamicComponent = "dynamic-node" + StorageComponent = "storage-node" + DynamicComponent = "dynamic-node" + BlobstorageInitComponent = "blobstorage-init" GRPCComponent = "grpc" InterconnectComponent = "interconnect" diff --git a/internal/resources/database.go b/internal/resources/database.go index 4276b3d0..16997c34 100644 --- a/internal/resources/database.go +++ b/internal/resources/database.go @@ -1,6 +1,8 @@ package resources import ( + "fmt" + corev1 "k8s.io/api/core/v1" "k8s.io/client-go/rest" @@ -25,6 +27,28 @@ func NewDatabase(ydbCr *api.Database) DatabaseBuilder { return DatabaseBuilder{Database: cr, Storage: nil} } +func (b *DatabaseBuilder) NewLabels() labels.Labels { + l := labels.Common(b.Name, b.Labels) + + l.Merge(b.Spec.AdditionalLabels) + l.Merge(map[string]string{labels.ComponentKey: labels.DynamicComponent}) + + return l +} + +func (b *DatabaseBuilder) NewAnnotations() map[string]string { + an := annotations.Common(b.Annotations) + + an.Merge(b.Spec.AdditionalAnnotations) + if b.Spec.Configuration != "" { + an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + } else { + an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) + } + + return an +} + func (b *DatabaseBuilder) Unwrap() *api.Database { return b.DeepCopy() } @@ -34,14 +58,12 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc return []ResourceBuilder{} } - databaseLabels := labels.DatabaseLabels(b.Unwrap()) + databaseLabels := b.NewLabels() + databaseAnnotations := b.NewAnnotations() statefulSetLabels := databaseLabels.Copy() statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) - statefulSetAnnotations := CopyDict(b.Spec.AdditionalAnnotations) - statefulSetAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration) - grpcServiceLabels := databaseLabels.Copy() grpcServiceLabels.Merge(b.Spec.Service.GRPC.AdditionalLabels) grpcServiceLabels.Merge(map[string]string{labels.ServiceComponent: labels.GRPCComponent}) @@ -183,20 +205,27 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc Name: b.Name, Labels: statefulSetLabels, - Annotations: statefulSetAnnotations, + Annotations: databaseAnnotations, }, ) } else { - optionalBuilders = append(optionalBuilders, b.getNodeSetBuilders(databaseLabels)...) + optionalBuilders = append( + optionalBuilders, + b.getNodeSetBuilders(databaseLabels, databaseAnnotations)...) } return optionalBuilders } -func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []ResourceBuilder { +func (b *DatabaseBuilder) getNodeSetBuilders( + databaseLabels labels.Labels, + databaseAnnotations annotations.Annotations, +) []ResourceBuilder { var nodeSetBuilders []ResourceBuilder for _, nodeSetSpecInline := range b.Spec.NodeSets { + nodeSetName := fmt.Sprintf("%s-%s", b.Name, nodeSetSpecInline.Name) + nodeSetLabels := databaseLabels.Copy() nodeSetLabels.Merge(nodeSetSpecInline.Labels) nodeSetLabels.Merge(map[string]string{labels.DatabaseNodeSetComponent: nodeSetSpecInline.Name}) @@ -204,12 +233,8 @@ func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []Res nodeSetLabels.Merge(map[string]string{labels.RemoteClusterKey: nodeSetSpecInline.Remote.Cluster}) } - nodeSetAnnotations := CopyDict(b.Annotations) - if nodeSetSpecInline.Annotations != nil { - for k, v := range nodeSetSpecInline.Annotations { - nodeSetAnnotations[k] = v - } - } + nodeSetAnnotations := databaseAnnotations.Copy() + nodeSetAnnotations.Merge(nodeSetSpecInline.Annotations) databaseNodeSetSpec := b.recastDatabaseNodeSetSpecInline(nodeSetSpecInline.DeepCopy()) if nodeSetSpecInline.Remote != nil { @@ -218,7 +243,7 @@ func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []Res &RemoteDatabaseNodeSetBuilder{ Object: b, - Name: b.Name + "-" + nodeSetSpecInline.Name, + Name: nodeSetName, Labels: nodeSetLabels, Annotations: nodeSetAnnotations, @@ -231,7 +256,7 @@ func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []Res &DatabaseNodeSetBuilder{ Object: b, - Name: b.Name + "-" + nodeSetSpecInline.Name, + Name: nodeSetName, Labels: nodeSetLabels, Annotations: nodeSetAnnotations, diff --git a/internal/resources/databasenodeset.go b/internal/resources/databasenodeset.go index 5050fbc2..db7f2cf3 100644 --- a/internal/resources/databasenodeset.go +++ b/internal/resources/databasenodeset.go @@ -22,10 +22,6 @@ type DatabaseNodeSetBuilder struct { DatabaseNodeSetSpec api.DatabaseNodeSetSpec } -type DatabaseNodeSetResource struct { - *api.DatabaseNodeSet -} - func (b *DatabaseNodeSetBuilder) Build(obj client.Object) error { dns, ok := obj.(*api.DatabaseNodeSet) if !ok { @@ -54,32 +50,50 @@ func (b *DatabaseNodeSetBuilder) Placeholder(cr client.Object) client.Object { } } -func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder { - ydbCr := api.RecastDatabaseNodeSet(b.Unwrap()) - databaseLabels := labels.DatabaseLabels(ydbCr) +type DatabaseNodeSetResource struct { + *api.DatabaseNodeSet +} + +func (b *DatabaseNodeSetResource) NewLabels() labels.Labels { + l := labels.Common(b.Name, b.Labels) - statefulSetName := b.Name - statefulSetLabels := databaseLabels.Copy() - statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: statefulSetName}) + l.Merge(b.Spec.AdditionalLabels) + l.Merge(map[string]string{labels.ComponentKey: labels.DynamicComponent}) databaseNodeSetName := b.Labels[labels.DatabaseNodeSetComponent] - statefulSetLabels.Merge(map[string]string{labels.DatabaseNodeSetComponent: databaseNodeSetName}) + l.Merge(map[string]string{labels.DatabaseNodeSetComponent: databaseNodeSetName}) + if remoteCluster, exist := b.Labels[labels.RemoteClusterKey]; exist { - statefulSetLabels.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster}) + l.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster}) } - statefulSetAnnotations := CopyDict(b.Spec.AdditionalAnnotations) - statefulSetAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration) + return l +} + +func (b *DatabaseNodeSetResource) NewAnnotations() map[string]string { + an := annotations.Common(b.Annotations) + + an.Merge(b.Spec.AdditionalAnnotations) + + return an +} + +func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder { + nodeSetLabels := b.NewLabels() + nodeSetAnnotations := b.NewAnnotations() + + statefulSetLabels := nodeSetLabels.Copy() + statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) var resourceBuilders []ResourceBuilder resourceBuilders = append(resourceBuilders, &DatabaseStatefulSetBuilder{ - Database: ydbCr, + Database: api.RecastDatabaseNodeSet(b.Unwrap()), RestConfig: restConfig, - Name: statefulSetName, + Name: b.Name, Labels: statefulSetLabels, - Annotations: statefulSetAnnotations, + Annotations: nodeSetAnnotations, }, ) return resourceBuilders diff --git a/internal/resources/resource.go b/internal/resources/resource.go index 1e1bc919..60398ae4 100644 --- a/internal/resources/resource.go +++ b/internal/resources/resource.go @@ -574,7 +574,7 @@ func buildCAStorePatchingCommandArgs( return command, args } -func GetConfigurationChecksum(configuration string) string { +func GetSHA256Checksum(configuration string) string { hasher := sha256.New() hasher.Write([]byte(configuration)) return hex.EncodeToString(hasher.Sum(nil)) @@ -592,17 +592,6 @@ func CompareMaps(map1, map2 map[string]string) bool { return true } -func PodIsReady(e corev1.Pod) bool { - if e.Status.Phase == corev1.PodRunning { - for _, condition := range e.Status.Conditions { - if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue { - return true - } - } - } - return false -} - func isSignAlgorithmSupported(alg string) bool { supportedAlgs := jwt.GetAlgorithms() diff --git a/internal/resources/storage.go b/internal/resources/storage.go index 38169a60..dd8c44d0 100644 --- a/internal/resources/storage.go +++ b/internal/resources/storage.go @@ -1,6 +1,8 @@ package resources import ( + "fmt" + "gopkg.in/yaml.v3" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/rest" @@ -29,15 +31,67 @@ func (b *StorageClusterBuilder) Unwrap() *api.Storage { return b.DeepCopy() } +func (b *StorageClusterBuilder) NewLabels() labels.Labels { + l := labels.Common(b.Name, b.Labels) + + l.Merge(b.Spec.AdditionalLabels) + l.Merge(map[string]string{labels.ComponentKey: labels.StorageComponent}) + + return l +} + +func (b *StorageClusterBuilder) NewAnnotations() map[string]string { + an := annotations.Common(b.Annotations) + + an.Merge(b.Spec.AdditionalAnnotations) + an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + + return an +} + +func (b *StorageClusterBuilder) NewInitJobLabels() labels.Labels { + l := labels.Common(b.Name, b.Labels) + + if b.Spec.InitJob != nil { + l.Merge(b.Spec.InitJob.AdditionalLabels) + } + l.Merge(map[string]string{labels.ComponentKey: labels.BlobstorageInitComponent}) + + return l +} + +func (b *StorageClusterBuilder) NewInitJobAnnotations() map[string]string { + an := annotations.Common(b.Annotations) + + if b.Spec.InitJob != nil { + an.Merge(b.Spec.InitJob.AdditionalLabels) + } + an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + + return an +} + +func (b *StorageClusterBuilder) GetInitJobBuilder() ResourceBuilder { + jobName := fmt.Sprintf(InitJobNameFormat, b.Name) + jobLabels := b.NewInitJobLabels() + jobAnnotations := b.NewInitJobAnnotations() + + return &StorageInitJobBuilder{ + Storage: b.Unwrap(), + + Name: jobName, + Labels: jobLabels, + Annotations: jobAnnotations, + } +} + func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder { - storageLabels := labels.StorageLabels(b.Unwrap()) + storageLabels := b.NewLabels() + storageAnnotations := b.NewAnnotations() statefulSetLabels := storageLabels.Copy() statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) - statefulSetAnnotations := CopyDict(b.Spec.AdditionalAnnotations) - statefulSetAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration) - grpcServiceLabels := storageLabels.Copy() grpcServiceLabels.Merge(b.Spec.Service.GRPC.AdditionalLabels) grpcServiceLabels.Merge(map[string]string{labels.ServiceComponent: labels.GRPCComponent}) @@ -106,11 +160,14 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R Name: b.Name, Labels: statefulSetLabels, - Annotations: statefulSetAnnotations, + Annotations: storageAnnotations, }, ) } else { - optionalBuilders = append(optionalBuilders, b.getNodeSetBuilders(storageLabels)...) + optionalBuilders = append( + optionalBuilders, + b.getNodeSetBuilders(storageLabels, storageAnnotations)..., + ) } return append( @@ -158,10 +215,15 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R ) } -func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels) []ResourceBuilder { +func (b *StorageClusterBuilder) getNodeSetBuilders( + storageLabels labels.Labels, + storageAnnotations annotations.Annotations, +) []ResourceBuilder { var nodeSetBuilders []ResourceBuilder for _, nodeSetSpecInline := range b.Spec.NodeSets { + nodeSetName := fmt.Sprintf("%s-%s", b.Name, nodeSetSpecInline.Name) + nodeSetLabels := storageLabels.Copy() nodeSetLabels.Merge(nodeSetSpecInline.Labels) nodeSetLabels.Merge(map[string]string{labels.StorageNodeSetComponent: nodeSetSpecInline.Name}) @@ -169,12 +231,8 @@ func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels) nodeSetLabels.Merge(map[string]string{labels.RemoteClusterKey: nodeSetSpecInline.Remote.Cluster}) } - nodeSetAnnotations := CopyDict(b.Annotations) - if nodeSetSpecInline.Annotations != nil { - for k, v := range nodeSetSpecInline.Annotations { - nodeSetAnnotations[k] = v - } - } + nodeSetAnnotations := storageAnnotations.Copy() + nodeSetAnnotations.Merge(nodeSetSpecInline.Annotations) storageNodeSetSpec := b.recastStorageNodeSetSpecInline(nodeSetSpecInline.DeepCopy()) if nodeSetSpecInline.Remote != nil { @@ -183,7 +241,7 @@ func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels) &RemoteStorageNodeSetBuilder{ Object: b, - Name: b.Name + "-" + nodeSetSpecInline.Name, + Name: nodeSetName, Labels: nodeSetLabels, Annotations: nodeSetAnnotations, @@ -196,7 +254,7 @@ func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels) &StorageNodeSetBuilder{ Object: b, - Name: b.Name + "-" + nodeSetSpecInline.Name, + Name: nodeSetName, Labels: nodeSetLabels, Annotations: nodeSetAnnotations, diff --git a/internal/resources/storage_init_job.go b/internal/resources/storage_init_job.go index b6454eec..3a7c027b 100644 --- a/internal/resources/storage_init_job.go +++ b/internal/resources/storage_init_job.go @@ -3,7 +3,6 @@ package resources import ( "errors" "fmt" - "strconv" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -12,7 +11,6 @@ import ( api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr" ) @@ -25,6 +23,16 @@ type StorageInitJobBuilder struct { Annotations map[string]string } +func NewInitJob(ydbCr *api.Storage) StorageInitJobBuilder { + cr := ydbCr.DeepCopy() + + return StorageInitJobBuilder{Storage: cr} +} + +func (b *StorageInitJobBuilder) Unwrap() *api.Storage { + return b.DeepCopy() +} + func (b *StorageInitJobBuilder) Build(obj client.Object) error { job, ok := obj.(*batchv1.Job) if !ok { @@ -58,39 +66,14 @@ func (b *StorageInitJobBuilder) Placeholder(cr client.Object) client.Object { } } -func GetInitJobBuilder(storage *api.Storage) ResourceBuilder { - jobName := fmt.Sprintf(InitJobNameFormat, storage.Name) - jobLabels := labels.Common(storage.Name, make(map[string]string)) - jobAnnotations := make(map[string]string) - - if storage.Spec.InitJob != nil { - if storage.Spec.InitJob.AdditionalLabels != nil { - jobLabels.Merge(storage.Spec.InitJob.AdditionalLabels) - jobLabels[labels.StorageGeneration] = strconv.FormatInt(storage.ObjectMeta.Generation, 10) - } - if storage.Spec.InitJob.AdditionalAnnotations != nil { - jobAnnotations = CopyDict(storage.Spec.InitJob.AdditionalAnnotations) - jobAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(storage.Spec.Configuration) - } - } - - return &StorageInitJobBuilder{ - Storage: storage, - - Name: jobName, - Labels: jobLabels, - Annotations: jobAnnotations, - } -} - func (b *StorageInitJobBuilder) buildInitJobPodTemplateSpec() corev1.PodTemplateSpec { dnsConfigSearches := []string{ fmt.Sprintf(api.InterconnectServiceFQDNFormat, b.Storage.Name, b.GetNamespace()), } podTemplate := corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: b.Labels, - Annotations: b.Annotations, + Labels: CopyDict(b.Labels), + Annotations: CopyDict(b.Annotations), }, Spec: corev1.PodSpec{ Containers: []corev1.Container{b.buildInitJobContainer()}, diff --git a/internal/resources/storagenodeset.go b/internal/resources/storagenodeset.go index efbc446c..b58a0ff7 100644 --- a/internal/resources/storagenodeset.go +++ b/internal/resources/storagenodeset.go @@ -22,10 +22,6 @@ type StorageNodeSetBuilder struct { StorageNodeSetSpec api.StorageNodeSetSpec } -type StorageNodeSetResource struct { - *api.StorageNodeSet -} - func (b *StorageNodeSetBuilder) Build(obj client.Object) error { sns, ok := obj.(*api.StorageNodeSet) if !ok { @@ -54,33 +50,51 @@ func (b *StorageNodeSetBuilder) Placeholder(cr client.Object) client.Object { } } -func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder { - ydbCr := api.RecastStorageNodeSet(b.Unwrap()) - storageLabels := labels.StorageLabels(ydbCr) +type StorageNodeSetResource struct { + *api.StorageNodeSet +} + +func (b *StorageNodeSetResource) NewLabels() labels.Labels { + l := labels.Common(b.Name, b.Labels) - statefulSetName := b.Name - statefulSetLabels := storageLabels.Copy() - statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: statefulSetName}) + l.Merge(b.Spec.AdditionalLabels) + l.Merge(map[string]string{labels.ComponentKey: labels.StorageComponent}) storageNodeSetName := b.Labels[labels.StorageNodeSetComponent] - statefulSetLabels.Merge(map[string]string{labels.StorageNodeSetComponent: storageNodeSetName}) + l.Merge(map[string]string{labels.StorageNodeSetComponent: storageNodeSetName}) + if remoteCluster, exist := b.Labels[labels.RemoteClusterKey]; exist { - statefulSetLabels.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster}) + l.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster}) } - statefulSetAnnotations := CopyDict(b.Spec.AdditionalAnnotations) - statefulSetAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration) + return l +} + +func (b *StorageNodeSetResource) NewAnnotations() map[string]string { + an := annotations.Common(b.Annotations) + + an.Merge(b.Spec.AdditionalAnnotations) + + return an +} + +func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder { + nodeSetLabels := b.NewLabels() + nodeSetAnnotations := b.NewAnnotations() + + statefulSetLabels := nodeSetLabels.Copy() + statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) var resourceBuilders []ResourceBuilder resourceBuilders = append( resourceBuilders, &StorageStatefulSetBuilder{ - Storage: ydbCr, + Storage: api.RecastStorageNodeSet(b.StorageNodeSet), RestConfig: restConfig, - Name: statefulSetName, + Name: b.Name, Labels: statefulSetLabels, - Annotations: statefulSetAnnotations, + Annotations: nodeSetAnnotations, }, ) From aef232b18650cdb7498b49940e7560f7b90d5ace Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Mon, 1 Jul 2024 23:51:55 +0300 Subject: [PATCH 10/12] fix lint --- internal/annotations/annotations.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/internal/annotations/annotations.go b/internal/annotations/annotations.go index 05cc3df2..f277943b 100644 --- a/internal/annotations/annotations.go +++ b/internal/annotations/annotations.go @@ -1,8 +1,9 @@ package annotations import ( - "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" corev1 "k8s.io/api/core/v1" + + "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" ) const ( @@ -21,14 +22,14 @@ var ( string(corev1.DNSNone), } UserAnnotations = map[string]struct{}{ - v1alpha1.AnnotationSkipInitialization: struct{}{}, - v1alpha1.AnnotationUpdateStrategyOnDelete: struct{}{}, - v1alpha1.AnnotationUpdateDNSPolicy: struct{}{}, - v1alpha1.AnnotationDisableLivenessProbe: struct{}{}, - v1alpha1.AnnotationDataCenter: struct{}{}, - v1alpha1.AnnotationGRPCPublicHost: struct{}{}, - v1alpha1.AnnotationNodeHost: struct{}{}, - v1alpha1.AnnotationNodeDomain: struct{}{}, + v1alpha1.AnnotationSkipInitialization: {}, + v1alpha1.AnnotationUpdateStrategyOnDelete: {}, + v1alpha1.AnnotationUpdateDNSPolicy: {}, + v1alpha1.AnnotationDisableLivenessProbe: {}, + v1alpha1.AnnotationDataCenter: {}, + v1alpha1.AnnotationGRPCPublicHost: {}, + v1alpha1.AnnotationNodeHost: {}, + v1alpha1.AnnotationNodeDomain: {}, } ) From 4da7be2d0efa444a5ba5cf83ea691674e7b14cab Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Fri, 12 Jul 2024 11:58:57 +0300 Subject: [PATCH 11/12] fixes labels --- internal/labels/label.go | 36 --------------------- internal/resources/database.go | 27 +++++++++------- internal/resources/databasenodeset.go | 12 +++---- internal/resources/remotedatabasenodeset.go | 5 +-- internal/resources/storage.go | 14 ++++---- internal/resources/storagenodeset.go | 12 +++---- 6 files changed, 36 insertions(+), 70 deletions(-) diff --git a/internal/labels/label.go b/internal/labels/label.go index ab2e19ff..5a0659d7 100644 --- a/internal/labels/label.go +++ b/internal/labels/label.go @@ -1,9 +1,5 @@ package labels -import ( - "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" -) - // https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/ const ( // NameKey The name of a higher level application this one is part of @@ -51,28 +47,6 @@ func Common(name string, defaultLabels Labels) Labels { return l } -func StorageLabels(cluster *v1alpha1.Storage) Labels { - l := Common(cluster.Name, cluster.Labels) - - l.Merge(cluster.Spec.AdditionalLabels) - l.Merge(map[string]string{ - ComponentKey: StorageComponent, - }) - - return l -} - -func DatabaseLabels(database *v1alpha1.Database) Labels { - l := Common(database.Name, database.Labels) - - l.Merge(database.Spec.AdditionalLabels) - l.Merge(map[string]string{ - ComponentKey: DynamicComponent, - }) - - return l -} - func (l Labels) AsMap() map[string]string { return l } @@ -99,16 +73,6 @@ func (l Labels) Merge(other map[string]string) map[string]string { return l } -func (l Labels) MergeInPlace(other map[string]string) map[string]string { - result := l.Copy() - - for k, v := range other { - result[k] = v - } - - return result -} - func makeCommonLabels(other map[string]string, instance string) map[string]string { common := make(map[string]string) diff --git a/internal/resources/database.go b/internal/resources/database.go index 16997c34..d3ee6c47 100644 --- a/internal/resources/database.go +++ b/internal/resources/database.go @@ -29,23 +29,14 @@ func NewDatabase(ydbCr *api.Database) DatabaseBuilder { func (b *DatabaseBuilder) NewLabels() labels.Labels { l := labels.Common(b.Name, b.Labels) - - l.Merge(b.Spec.AdditionalLabels) l.Merge(map[string]string{labels.ComponentKey: labels.DynamicComponent}) return l } -func (b *DatabaseBuilder) NewAnnotations() map[string]string { +func (b *DatabaseBuilder) NewAnnotations() annotations.Annotations { an := annotations.Common(b.Annotations) - an.Merge(b.Spec.AdditionalAnnotations) - if b.Spec.Configuration != "" { - an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) - } else { - an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) - } - return an } @@ -62,8 +53,17 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc databaseAnnotations := b.NewAnnotations() statefulSetLabels := databaseLabels.Copy() + statefulSetLabels.Merge(b.Spec.AdditionalLabels) statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) + statefulSetAnnotations := databaseAnnotations.Copy() + statefulSetAnnotations.Merge(b.Spec.AdditionalAnnotations) + if b.Spec.Configuration != "" { + statefulSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + } else { + statefulSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) + } + grpcServiceLabels := databaseLabels.Copy() grpcServiceLabels.Merge(b.Spec.Service.GRPC.AdditionalLabels) grpcServiceLabels.Merge(map[string]string{labels.ServiceComponent: labels.GRPCComponent}) @@ -205,7 +205,7 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc Name: b.Name, Labels: statefulSetLabels, - Annotations: databaseAnnotations, + Annotations: statefulSetAnnotations, }, ) } else { @@ -235,6 +235,11 @@ func (b *DatabaseBuilder) getNodeSetBuilders( nodeSetAnnotations := databaseAnnotations.Copy() nodeSetAnnotations.Merge(nodeSetSpecInline.Annotations) + if b.Spec.Configuration != "" { + nodeSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + } else { + nodeSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) + } databaseNodeSetSpec := b.recastDatabaseNodeSetSpecInline(nodeSetSpecInline.DeepCopy()) if nodeSetSpecInline.Remote != nil { diff --git a/internal/resources/databasenodeset.go b/internal/resources/databasenodeset.go index db7f2cf3..2bbd4812 100644 --- a/internal/resources/databasenodeset.go +++ b/internal/resources/databasenodeset.go @@ -56,8 +56,6 @@ type DatabaseNodeSetResource struct { func (b *DatabaseNodeSetResource) NewLabels() labels.Labels { l := labels.Common(b.Name, b.Labels) - - l.Merge(b.Spec.AdditionalLabels) l.Merge(map[string]string{labels.ComponentKey: labels.DynamicComponent}) databaseNodeSetName := b.Labels[labels.DatabaseNodeSetComponent] @@ -70,11 +68,9 @@ func (b *DatabaseNodeSetResource) NewLabels() labels.Labels { return l } -func (b *DatabaseNodeSetResource) NewAnnotations() map[string]string { +func (b *DatabaseNodeSetResource) NewAnnotations() annotations.Annotations { an := annotations.Common(b.Annotations) - an.Merge(b.Spec.AdditionalAnnotations) - return an } @@ -83,8 +79,12 @@ func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [ nodeSetAnnotations := b.NewAnnotations() statefulSetLabels := nodeSetLabels.Copy() + statefulSetLabels.Merge(b.Spec.AdditionalLabels) statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) + statefulSetAnnotations := nodeSetAnnotations.Copy() + statefulSetAnnotations.Merge(b.Spec.AdditionalAnnotations) + var resourceBuilders []ResourceBuilder resourceBuilders = append(resourceBuilders, &DatabaseStatefulSetBuilder{ @@ -93,7 +93,7 @@ func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [ Name: b.Name, Labels: statefulSetLabels, - Annotations: nodeSetAnnotations, + Annotations: statefulSetAnnotations, }, ) return resourceBuilders diff --git a/internal/resources/remotedatabasenodeset.go b/internal/resources/remotedatabasenodeset.go index acaa90d5..f65dd835 100644 --- a/internal/resources/remotedatabasenodeset.go +++ b/internal/resources/remotedatabasenodeset.go @@ -61,16 +61,13 @@ func (b *RemoteDatabaseNodeSetBuilder) Placeholder(cr client.Object) client.Obje func (b *RemoteDatabaseNodeSetResource) GetResourceBuilders() []ResourceBuilder { var resourceBuilders []ResourceBuilder - nodeSetAnnotations := CopyDict(b.Annotations) - delete(nodeSetAnnotations, annotations.LastApplied) - resourceBuilders = append(resourceBuilders, &DatabaseNodeSetBuilder{ Object: b, Name: b.Name, Labels: b.Labels, - Annotations: nodeSetAnnotations, + Annotations: b.Annotations, DatabaseNodeSetSpec: b.Spec, }, diff --git a/internal/resources/storage.go b/internal/resources/storage.go index dd8c44d0..796fdc65 100644 --- a/internal/resources/storage.go +++ b/internal/resources/storage.go @@ -33,17 +33,13 @@ func (b *StorageClusterBuilder) Unwrap() *api.Storage { func (b *StorageClusterBuilder) NewLabels() labels.Labels { l := labels.Common(b.Name, b.Labels) - - l.Merge(b.Spec.AdditionalLabels) l.Merge(map[string]string{labels.ComponentKey: labels.StorageComponent}) return l } -func (b *StorageClusterBuilder) NewAnnotations() map[string]string { +func (b *StorageClusterBuilder) NewAnnotations() annotations.Annotations { an := annotations.Common(b.Annotations) - - an.Merge(b.Spec.AdditionalAnnotations) an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) return an @@ -60,7 +56,7 @@ func (b *StorageClusterBuilder) NewInitJobLabels() labels.Labels { return l } -func (b *StorageClusterBuilder) NewInitJobAnnotations() map[string]string { +func (b *StorageClusterBuilder) NewInitJobAnnotations() annotations.Annotations { an := annotations.Common(b.Annotations) if b.Spec.InitJob != nil { @@ -90,8 +86,12 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R storageAnnotations := b.NewAnnotations() statefulSetLabels := storageLabels.Copy() + statefulSetLabels.Merge(b.Spec.AdditionalLabels) statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) + statefulSetAnnotations := storageAnnotations.Copy() + statefulSetAnnotations.Merge(b.Spec.AdditionalAnnotations) + grpcServiceLabels := storageLabels.Copy() grpcServiceLabels.Merge(b.Spec.Service.GRPC.AdditionalLabels) grpcServiceLabels.Merge(map[string]string{labels.ServiceComponent: labels.GRPCComponent}) @@ -160,7 +160,7 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R Name: b.Name, Labels: statefulSetLabels, - Annotations: storageAnnotations, + Annotations: statefulSetAnnotations, }, ) } else { diff --git a/internal/resources/storagenodeset.go b/internal/resources/storagenodeset.go index b58a0ff7..4e0b53dd 100644 --- a/internal/resources/storagenodeset.go +++ b/internal/resources/storagenodeset.go @@ -56,8 +56,6 @@ type StorageNodeSetResource struct { func (b *StorageNodeSetResource) NewLabels() labels.Labels { l := labels.Common(b.Name, b.Labels) - - l.Merge(b.Spec.AdditionalLabels) l.Merge(map[string]string{labels.ComponentKey: labels.StorageComponent}) storageNodeSetName := b.Labels[labels.StorageNodeSetComponent] @@ -70,11 +68,9 @@ func (b *StorageNodeSetResource) NewLabels() labels.Labels { return l } -func (b *StorageNodeSetResource) NewAnnotations() map[string]string { +func (b *StorageNodeSetResource) NewAnnotations() annotations.Annotations { an := annotations.Common(b.Annotations) - an.Merge(b.Spec.AdditionalAnnotations) - return an } @@ -83,8 +79,12 @@ func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [] nodeSetAnnotations := b.NewAnnotations() statefulSetLabels := nodeSetLabels.Copy() + statefulSetLabels.Merge(b.Spec.AdditionalLabels) statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name}) + statefulSetAnnotations := nodeSetAnnotations.Copy() + statefulSetAnnotations.Merge(b.Spec.AdditionalAnnotations) + var resourceBuilders []ResourceBuilder resourceBuilders = append( resourceBuilders, @@ -94,7 +94,7 @@ func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) [] Name: b.Name, Labels: statefulSetLabels, - Annotations: nodeSetAnnotations, + Annotations: statefulSetAnnotations, }, ) From 6a1aeec058ed015bd3aecd95fde14f558d36a921 Mon Sep 17 00:00:00 2001 From: Aleksei Kobzev Date: Wed, 4 Dec 2024 13:23:27 +0700 Subject: [PATCH 12/12] rename import package to ydbannotations --- internal/annotations/annotations.go | 6 ++-- internal/annotations/annotations_test.go | 9 +++-- .../remotedatabasenodeset/controller.go | 6 ++-- .../remotedatabasenodeset/controller_test.go | 34 +++++++++---------- .../remotestoragenodeset/controller.go | 6 ++-- .../remotestoragenodeset/controller_test.go | 2 +- .../remotestoragenodeset/remote_objects.go | 6 ++-- .../controllers/storage/controller_test.go | 4 +-- internal/resources/database.go | 18 +++++----- internal/resources/database_statefulset.go | 4 +-- internal/resources/databasenodeset.go | 8 ++--- internal/resources/predicate.go | 4 +-- internal/resources/remotedatabasenodeset.go | 20 +++++------ internal/resources/remotestoragenodeset.go | 22 ++++++------ internal/resources/resource.go | 22 ++++++------ internal/resources/storage.go | 22 ++++++------ internal/resources/storage_init_job.go | 6 ++-- internal/resources/storagenodeset.go | 8 ++--- 18 files changed, 103 insertions(+), 104 deletions(-) diff --git a/internal/annotations/annotations.go b/internal/annotations/annotations.go index 8de34970..9789663a 100644 --- a/internal/annotations/annotations.go +++ b/internal/annotations/annotations.go @@ -36,11 +36,11 @@ var ( type Annotations map[string]string func Common(objAnnotations Annotations) Annotations { - an := Annotations{} + annotations := Annotations{} - an.Merge(getSupportedAnnotations(objAnnotations)) + annotations.Merge(getSupportedAnnotations(objAnnotations)) - return an + return annotations } func (an Annotations) Merge(other map[string]string) map[string]string { diff --git a/internal/annotations/annotations_test.go b/internal/annotations/annotations_test.go index a5048726..07e47107 100644 --- a/internal/annotations/annotations_test.go +++ b/internal/annotations/annotations_test.go @@ -5,8 +5,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" ) func TestLabels(t *testing.T) { @@ -16,12 +15,12 @@ func TestLabels(t *testing.T) { var _ = Describe("Testing annotations", func() { It("merges two sets of annotations", func() { - fstLabels := annotations.Annotations{ + fstLabels := ydbannotations.Annotations{ "a": "a", "b": "b", } - sndLabels := annotations.Annotations{ + sndLabels := ydbannotations.Annotations{ "c": "c", "d": "d", } @@ -35,7 +34,7 @@ var _ = Describe("Testing annotations", func() { }) It("sets correct defaults", func() { - Expect(annotations.Common(map[string]string{ + Expect(ydbannotations.Common(map[string]string{ "ydb.tech/skip-initialization": "true", "ydb.tech/node-host": "ydb-testing.k8s-c.yandex.net", "ydb.tech/last-applied": "some-body", diff --git a/internal/controllers/remotedatabasenodeset/controller.go b/internal/controllers/remotedatabasenodeset/controller.go index 9c3e87d2..dca15d5a 100644 --- a/internal/controllers/remotedatabasenodeset/controller.go +++ b/internal/controllers/remotedatabasenodeset/controller.go @@ -23,7 +23,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" @@ -118,8 +118,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C annotationFilter := func(mapObj client.Object) []reconcile.Request { requests := make([]reconcile.Request, 0) - an := mapObj.GetAnnotations() - primaryResourceName, exist := an[annotations.PrimaryResourceDatabase] + annotations := mapObj.GetAnnotations() + primaryResourceName, exist := annotations[ydbannotations.PrimaryResourceDatabase] if exist { databaseNodeSets := &v1alpha1.DatabaseNodeSetList{} if err := r.Client.List( diff --git a/internal/controllers/remotedatabasenodeset/controller_test.go b/internal/controllers/remotedatabasenodeset/controller_test.go index 5c1e3a92..fb96cd0c 100644 --- a/internal/controllers/remotedatabasenodeset/controller_test.go +++ b/internal/controllers/remotedatabasenodeset/controller_test.go @@ -26,7 +26,7 @@ import ( "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/database" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/databasenodeset" @@ -529,25 +529,25 @@ var _ = Describe("RemoteDatabaseNodeSet controller tests", func() { return err } - primaryResourceStorage, exist := remoteSecret.Annotations[annotations.PrimaryResourceStorage] + primaryResourceStorage, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceStorage] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceStorage, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceStorage, remoteSecret.Name) } if primaryResourceStorage != foundRemoteStorageNodeSet.Spec.StorageRef.Name { return fmt.Errorf("primaryResourceName %s does not equal storageRef name %s", primaryResourceStorage, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - primaryResourceDatabase, exist := remoteSecret.Annotations[annotations.PrimaryResourceDatabase] + primaryResourceDatabase, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabase] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceDatabase, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabase, remoteSecret.Name) } if primaryResourceDatabase != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name { return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceDatabase, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] + remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) @@ -620,22 +620,22 @@ var _ = Describe("RemoteDatabaseNodeSet controller tests", func() { return err } - _, exist := remoteSecret.Annotations[annotations.PrimaryResourceStorage] + _, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceStorage] if exist { - return fmt.Errorf("annotation %s still exist on remoteSecret %s", annotations.PrimaryResourceStorage, remoteSecret.Name) + return fmt.Errorf("annotation %s still exist on remoteSecret %s", ydbannotations.PrimaryResourceStorage, remoteSecret.Name) } - primaryResourceDatabase, exist := remoteSecret.Annotations[annotations.PrimaryResourceDatabase] + primaryResourceDatabase, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabase] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceDatabase, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabase, remoteSecret.Name) } if primaryResourceDatabase != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name { return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceDatabase, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] + remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) @@ -686,17 +686,17 @@ var _ = Describe("RemoteDatabaseNodeSet controller tests", func() { return err } - primaryResourceDatabase, exist := remoteSecret.Annotations[annotations.PrimaryResourceDatabase] + primaryResourceDatabase, exist := remoteSecret.Annotations[ydbannotations.PrimaryResourceDatabase] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.PrimaryResourceDatabase, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.PrimaryResourceDatabase, remoteSecret.Name) } if primaryResourceDatabase != foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name { return fmt.Errorf("primaryResourceName %s does not equal databaseRef name %s", primaryResourceDatabase, foundRemoteDatabaseNodeSet.Spec.DatabaseRef.Name) } - remoteRV, exist := remoteSecret.Annotations[annotations.RemoteResourceVersion] + remoteRV, exist := remoteSecret.Annotations[ydbannotations.RemoteResourceVersion] if !exist { - return fmt.Errorf("annotation %s does not exist on remoteSecret %s", annotations.RemoteResourceVersion, remoteSecret.Name) + return fmt.Errorf("annotation %s does not exist on remoteSecret %s", ydbannotations.RemoteResourceVersion, remoteSecret.Name) } if localSecret.GetResourceVersion() != remoteRV { return fmt.Errorf("localRV %s does not equal remoteRV %s", localSecret.GetResourceVersion(), remoteRV) diff --git a/internal/controllers/remotestoragenodeset/controller.go b/internal/controllers/remotestoragenodeset/controller.go index 2f471772..3bafd749 100644 --- a/internal/controllers/remotestoragenodeset/controller.go +++ b/internal/controllers/remotestoragenodeset/controller.go @@ -23,7 +23,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" @@ -118,8 +118,8 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager, remoteCluster *cluster.C annotationFilter := func(mapObj client.Object) []reconcile.Request { requests := make([]reconcile.Request, 0) - an := mapObj.GetAnnotations() - primaryResourceName, exist := an[annotations.PrimaryResourceStorage] + annotations := mapObj.GetAnnotations() + primaryResourceName, exist := annotations[ydbannotations.PrimaryResourceStorage] if exist { storageNodeSets := &v1alpha1.StorageNodeSetList{} if err := r.Client.List( diff --git a/internal/controllers/remotestoragenodeset/controller_test.go b/internal/controllers/remotestoragenodeset/controller_test.go index 1a6f485b..c801943e 100644 --- a/internal/controllers/remotestoragenodeset/controller_test.go +++ b/internal/controllers/remotestoragenodeset/controller_test.go @@ -25,7 +25,7 @@ import ( "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects" - annotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + annotations ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/remotestoragenodeset" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage" diff --git a/internal/controllers/remotestoragenodeset/remote_objects.go b/internal/controllers/remotestoragenodeset/remote_objects.go index 9e43953b..eec14fe4 100644 --- a/internal/controllers/remotestoragenodeset/remote_objects.go +++ b/internal/controllers/remotestoragenodeset/remote_objects.go @@ -16,7 +16,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ydblabels "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" @@ -277,7 +277,7 @@ func (r *Reconciler) removeUnusedRemoteObjects( // Remove annotation if no one another StorageNodeSet if !existInStorage { - patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, annotations.PrimaryResourceStorage)) + patch := []byte(fmt.Sprintf(`{"metadata": {"annotations": {"%s": null}}}`, ydbannotations.PrimaryResourceStorage)) updateErr := r.Client.Patch(ctx, localObj, client.RawPatch(types.StrategicMergePatchType, patch)) if updateErr != nil { r.Recorder.Event( @@ -297,7 +297,7 @@ func (r *Reconciler) removeUnusedRemoteObjects( } // Delete resource if annotation `ydb.tech/primary-resource-database` does not exist - _, existInDatabase := localObj.GetAnnotations()[annotations.PrimaryResourceDatabase] + _, existInDatabase := localObj.GetAnnotations()[ydbannotations.PrimaryResourceDatabase] if !existInDatabase { // Try to delete unused resource from local cluster deleteErr := r.Client.Delete(ctx, localObj) diff --git a/internal/controllers/storage/controller_test.go b/internal/controllers/storage/controller_test.go index 6eb7a796..53dd3b57 100644 --- a/internal/controllers/storage/controller_test.go +++ b/internal/controllers/storage/controller_test.go @@ -18,7 +18,7 @@ import ( "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources" @@ -123,7 +123,7 @@ var _ = Describe("Storage controller medium tests", func() { }, &foundStorage)).Should(Succeed()) foundConfigurationChecksumAnnotation := false - if podAnnotations[annotations.ConfigurationChecksum] == resources.GetSHA256Checksum(foundStorage.Spec.Configuration) { + if podAnnotations[ydbannotations.ConfigurationChecksum] == resources.GetSHA256Checksum(foundStorage.Spec.Configuration) { foundConfigurationChecksumAnnotation = true } Expect(foundConfigurationChecksumAnnotation).To(BeTrue()) diff --git a/internal/resources/database.go b/internal/resources/database.go index 168af2cf..672be046 100644 --- a/internal/resources/database.go +++ b/internal/resources/database.go @@ -7,7 +7,7 @@ import ( "k8s.io/client-go/rest" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/configuration/schema" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/metrics" @@ -35,10 +35,10 @@ func (b *DatabaseBuilder) NewLabels() labels.Labels { return l } -func (b *DatabaseBuilder) NewAnnotations() annotations.Annotations { - an := annotations.Common(b.Annotations) +func (b *DatabaseBuilder) NewAnnotations() ydbannotations.Annotations { + annotations := ydbannotations.Common(b.Annotations) - return an + return annotations } func (b *DatabaseBuilder) Unwrap() *api.Database { @@ -60,9 +60,9 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc statefulSetAnnotations := databaseAnnotations.Copy() statefulSetAnnotations.Merge(b.Spec.AdditionalAnnotations) if b.Spec.Configuration != "" { - statefulSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + statefulSetAnnotations.Merge(map[string]string{ydbannotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) } else { - statefulSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) + statefulSetAnnotations.Merge(map[string]string{ydbannotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) } grpcServiceLabels := databaseLabels.Copy() @@ -249,7 +249,7 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc func (b *DatabaseBuilder) getNodeSetBuilders( databaseLabels labels.Labels, - databaseAnnotations annotations.Annotations, + databaseAnnotations ydbannotations.Annotations, ) []ResourceBuilder { var nodeSetBuilders []ResourceBuilder @@ -266,9 +266,9 @@ func (b *DatabaseBuilder) getNodeSetBuilders( nodeSetAnnotations := databaseAnnotations.Copy() nodeSetAnnotations.Merge(nodeSetSpecInline.Annotations) if b.Spec.Configuration != "" { - nodeSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + nodeSetAnnotations.Merge(map[string]string{ydbannotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) } else { - nodeSetAnnotations.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) + nodeSetAnnotations.Merge(map[string]string{ydbannotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)}) } databaseNodeSetSpec := b.recastDatabaseNodeSetSpecInline(nodeSetSpecInline.DeepCopy()) diff --git a/internal/resources/database_statefulset.go b/internal/resources/database_statefulset.go index cc16a823..1925c8eb 100644 --- a/internal/resources/database_statefulset.go +++ b/internal/resources/database_statefulset.go @@ -17,7 +17,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr" ) @@ -148,7 +148,7 @@ func (b *DatabaseStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSp } if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateDNSPolicy]; ok { - for _, acceptedPolicy := range annotations.AcceptedDNSPolicy { + for _, acceptedPolicy := range ydbannotations.AcceptedDNSPolicy { if value == acceptedPolicy { podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) } diff --git a/internal/resources/databasenodeset.go b/internal/resources/databasenodeset.go index 2bbd4812..26413a30 100644 --- a/internal/resources/databasenodeset.go +++ b/internal/resources/databasenodeset.go @@ -8,7 +8,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" ) @@ -68,10 +68,10 @@ func (b *DatabaseNodeSetResource) NewLabels() labels.Labels { return l } -func (b *DatabaseNodeSetResource) NewAnnotations() annotations.Annotations { - an := annotations.Common(b.Annotations) +func (b *DatabaseNodeSetResource) NewAnnotations() ydbannotations.Annotations { + annotations := ydbannotations.Common(b.Annotations) - return an + return annotations } func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder { diff --git a/internal/resources/predicate.go b/internal/resources/predicate.go index 65e1a241..f9e8ca06 100644 --- a/internal/resources/predicate.go +++ b/internal/resources/predicate.go @@ -7,13 +7,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" ) func LastAppliedAnnotationPredicate() predicate.Predicate { return predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { - return !annotations.CompareLastAppliedAnnotation( + return !ydbannotations.CompareLastAppliedAnnotation( e.ObjectOld.GetAnnotations(), e.ObjectNew.GetAnnotations(), ) diff --git a/internal/resources/remotedatabasenodeset.go b/internal/resources/remotedatabasenodeset.go index f65dd835..8e1f41a3 100644 --- a/internal/resources/remotedatabasenodeset.go +++ b/internal/resources/remotedatabasenodeset.go @@ -12,7 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/apiutil" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ) @@ -161,26 +161,26 @@ func (b *RemoteDatabaseNodeSetResource) GetRemoteObjects( } func (b *RemoteDatabaseNodeSetResource) SetPrimaryResourceAnnotations(obj client.Object) { - an := make(map[string]string) + annotations := make(map[string]string) for key, value := range obj.GetAnnotations() { - an[key] = value + annotations[key] = value } - if _, exist := an[annotations.PrimaryResourceDatabase]; !exist { - an[annotations.PrimaryResourceDatabase] = b.Spec.DatabaseRef.Name + if _, exist := annotations[ydbannotations.PrimaryResourceDatabase]; !exist { + annotations[ydbannotations.PrimaryResourceDatabase] = b.Spec.DatabaseRef.Name } - obj.SetAnnotations(an) + obj.SetAnnotations(annotations) } func (b *RemoteDatabaseNodeSetResource) UnsetPrimaryResourceAnnotations(obj client.Object) { - an := make(map[string]string) + annotations := make(map[string]string) for key, value := range obj.GetAnnotations() { - if key != an[annotations.PrimaryResourceDatabase] { - an[key] = value + if key != annotations[ydbannotations.PrimaryResourceDatabase] { + annotations[key] = value } } - obj.SetAnnotations(an) + obj.SetAnnotations(annotations) } func (b *RemoteDatabaseNodeSetResource) CreateRemoteResourceStatus( diff --git a/internal/resources/remotestoragenodeset.go b/internal/resources/remotestoragenodeset.go index 193b916b..a627025f 100644 --- a/internal/resources/remotestoragenodeset.go +++ b/internal/resources/remotestoragenodeset.go @@ -12,7 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client/apiutil" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" . "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck ) @@ -62,7 +62,7 @@ func (b *RemoteStorageNodeSetResource) GetResourceBuilders() []ResourceBuilder { var resourceBuilders []ResourceBuilder nodeSetAnnotations := CopyDict(b.Annotations) - delete(nodeSetAnnotations, annotations.LastApplied) + delete(nodeSetAnnotations, ydbannotations.LastApplied) resourceBuilders = append(resourceBuilders, &StorageNodeSetBuilder{ @@ -145,26 +145,26 @@ func (b *RemoteStorageNodeSetResource) GetRemoteObjects( } func (b *RemoteStorageNodeSetResource) SetPrimaryResourceAnnotations(obj client.Object) { - an := make(map[string]string) + annotations := make(map[string]string) for key, value := range obj.GetAnnotations() { - an[key] = value + annotations[key] = value } - if _, exist := an[annotations.PrimaryResourceStorage]; !exist { - an[annotations.PrimaryResourceStorage] = b.Spec.StorageRef.Name + if _, exist := annotations[ydbannotations.PrimaryResourceStorage]; !exist { + annotations[ydbannotations.PrimaryResourceStorage] = b.Spec.StorageRef.Name } - obj.SetAnnotations(an) + obj.SetAnnotations(annotations) } func (b *RemoteStorageNodeSetResource) UnsetPrimaryResourceAnnotations(obj client.Object) { - an := make(map[string]string) + annotations := make(map[string]string) for key, value := range obj.GetAnnotations() { - if key != an[annotations.PrimaryResourceStorage] { - an[key] = value + if key != annotations[ydbannotations.PrimaryResourceStorage] { + annotations[key] = value } } - obj.SetAnnotations(an) + obj.SetAnnotations(annotations) } func (b *RemoteStorageNodeSetResource) CreateRemoteResourceStatus(remoteObj client.Object) { diff --git a/internal/resources/resource.go b/internal/resources/resource.go index ca245250..c2ce8482 100644 --- a/internal/resources/resource.go +++ b/internal/resources/resource.go @@ -25,7 +25,7 @@ import ( ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/connection" ) @@ -82,7 +82,7 @@ type ResourceBuilder interface { } var ( - annotator = patch.NewAnnotator(annotations.LastApplied) + annotator = patch.NewAnnotator(ydbannotations.LastApplied) patchMaker = patch.NewPatchMaker(annotator) ) @@ -263,23 +263,23 @@ func UpdateResource(oldObj, newObj client.Object) client.Object { } func CopyPrimaryResourceObjectAnnotation(obj client.Object, oldAnnotations map[string]string) { - an := CopyDict(obj.GetAnnotations()) + annotations := CopyDict(obj.GetAnnotations()) for key, value := range oldAnnotations { - if key == annotations.PrimaryResourceDatabase || - key == annotations.PrimaryResourceStorage { - an[key] = value + if key == ydbannotations.PrimaryResourceDatabase || + key == ydbannotations.PrimaryResourceStorage { + annotations[key] = value } } - obj.SetAnnotations(an) + obj.SetAnnotations(annotations) } func SetRemoteResourceVersionAnnotation(obj client.Object, resourceVersion string) { - an := make(map[string]string) + annotations := make(map[string]string) for key, value := range obj.GetAnnotations() { - an[key] = value + annotations[key] = value } - an[annotations.RemoteResourceVersion] = resourceVersion - obj.SetAnnotations(an) + annotations[ydbannotations.RemoteResourceVersion] = resourceVersion + obj.SetAnnotations(annotations) } func ConvertRemoteResourceToObject(remoteResource api.RemoteResource, namespace string) (client.Object, error) { diff --git a/internal/resources/storage.go b/internal/resources/storage.go index 6ae16ba2..6366d681 100644 --- a/internal/resources/storage.go +++ b/internal/resources/storage.go @@ -8,7 +8,7 @@ import ( "k8s.io/client-go/rest" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/metrics" ) @@ -38,11 +38,11 @@ func (b *StorageClusterBuilder) NewLabels() labels.Labels { return l } -func (b *StorageClusterBuilder) NewAnnotations() annotations.Annotations { - an := annotations.Common(b.Annotations) - an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) +func (b *StorageClusterBuilder) NewAnnotations() ydbannotations.Annotations { + annotations := ydbannotations.Common(b.Annotations) + annotations.Merge(map[string]string{ydbannotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) - return an + return annotations } func (b *StorageClusterBuilder) NewInitJobLabels() labels.Labels { @@ -56,15 +56,15 @@ func (b *StorageClusterBuilder) NewInitJobLabels() labels.Labels { return l } -func (b *StorageClusterBuilder) NewInitJobAnnotations() annotations.Annotations { - an := annotations.Common(b.Annotations) +func (b *StorageClusterBuilder) NewInitJobAnnotations() ydbannotations.Annotations { + annotations := ydbannotations.Common(b.Annotations) if b.Spec.InitJob != nil { - an.Merge(b.Spec.InitJob.AdditionalLabels) + annotations.Merge(b.Spec.InitJob.AdditionalLabels) } - an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) + annotations.Merge(map[string]string{ydbannotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)}) - return an + return annotations } func (b *StorageClusterBuilder) GetInitJobBuilder() ResourceBuilder { @@ -217,7 +217,7 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R func (b *StorageClusterBuilder) getNodeSetBuilders( storageLabels labels.Labels, - storageAnnotations annotations.Annotations, + storageAnnotations ydbannotations.Annotations, ) []ResourceBuilder { var nodeSetBuilders []ResourceBuilder diff --git a/internal/resources/storage_init_job.go b/internal/resources/storage_init_job.go index dadbf3e2..46db784f 100644 --- a/internal/resources/storage_init_job.go +++ b/internal/resources/storage_init_job.go @@ -11,7 +11,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" "github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr" ) @@ -80,7 +80,7 @@ func GetInitJobBuilder(storage *api.Storage) ResourceBuilder { } if storage.Spec.InitJob.AdditionalAnnotations != nil { jobAnnotations = CopyDict(storage.Spec.InitJob.AdditionalAnnotations) - jobAnnotations[annotations.ConfigurationChecksum] = GetSHA256Checksum(storage.Spec.Configuration) + jobAnnotations[ydbannotations.ConfigurationChecksum] = GetSHA256Checksum(storage.Spec.Configuration) } } @@ -148,7 +148,7 @@ func (b *StorageInitJobBuilder) buildInitJobPodTemplateSpec() corev1.PodTemplate } if value, ok := b.ObjectMeta.Annotations[api.AnnotationUpdateDNSPolicy]; ok { - for _, acceptedPolicy := range annotations.AcceptedDNSPolicy { + for _, acceptedPolicy := range ydbannotations.AcceptedDNSPolicy { if value == acceptedPolicy { podTemplate.Spec.DNSPolicy = corev1.DNSPolicy(value) } diff --git a/internal/resources/storagenodeset.go b/internal/resources/storagenodeset.go index 4e0b53dd..b87aaff8 100644 --- a/internal/resources/storagenodeset.go +++ b/internal/resources/storagenodeset.go @@ -8,7 +8,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1" - "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" + ydbannotations "github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations" "github.com/ydb-platform/ydb-kubernetes-operator/internal/labels" ) @@ -68,10 +68,10 @@ func (b *StorageNodeSetResource) NewLabels() labels.Labels { return l } -func (b *StorageNodeSetResource) NewAnnotations() annotations.Annotations { - an := annotations.Common(b.Annotations) +func (b *StorageNodeSetResource) NewAnnotations() ydbannotations.Annotations { + annotations := ydbannotations.Common(b.Annotations) - return an + return annotations } func (b *StorageNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {