From 8ca40b6ff66c9bd0a56825e8f5bb1ea6ac9b8a19 Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Wed, 27 Mar 2024 17:46:19 +0200 Subject: [PATCH] If service is already owned by some other resource, do not try to take ownership of it. If it later gets deleted because of cleanup, we will recreate it --- CHANGELOG.md | 1 + pkg/reconciliation/reconcile_services.go | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 516fcccce..10c6a945d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Changelog for Cass Operator, new PRs should update the `main / unreleased` secti * [CHANGE] [#618](https://github.com/k8ssandra/cass-operator/issues/618) Update dependencies to support controller-runtime 0.17.2, modify required parts. * [ENHANCEMENT] [#532](https://github.com/k8ssandra/cass-operator/issues/532) Instead of rejecting updates/creates with deprecated fields, return kubectl warnings. * [BUGFIX] [#622](https://github.com/k8ssandra/cass-operator/issues/622) Fix sanitization of DC label +* [BUGFIX] [#626](https://github.com/k8ssandra/cass-operator/issues/626) If multiple datacenters are present in the same namespace, the seed-service ownership is constantly updated between these two resources ## v1.19.0 diff --git a/pkg/reconciliation/reconcile_services.go b/pkg/reconciliation/reconcile_services.go index 9e9f67196..bf278af21 100644 --- a/pkg/reconciliation/reconcile_services.go +++ b/pkg/reconciliation/reconcile_services.go @@ -9,6 +9,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "github.com/k8ssandra/cass-operator/pkg/utils" ) @@ -72,29 +73,27 @@ func (rc *ReconciliationContext) CheckHeadlessServices() result.ReconcileResult for idx := range services { desiredSvc := services[idx] - // Set CassandraDatacenter dc as the owner and controller - err := setControllerReference(dc, desiredSvc, rc.Scheme) - if err != nil { - logger.Error(err, "Could not set controller reference for headless service") - return result.Error(err) + // If the service is already owned by some other resource, do not try to take ownership - otherwise resources will fight for control. + if !controllerutil.HasControllerReference(desiredSvc) { + // Set CassandraDatacenter dc as the owner and controller + if err := setControllerReference(dc, desiredSvc, rc.Scheme); err != nil { + logger.Error(err, "Could not set controller reference for headless service") + return result.Error(err) + } } // See if the service already exists nsName := types.NamespacedName{Name: desiredSvc.Name, Namespace: desiredSvc.Namespace} currentService := &corev1.Service{} - err = client.Get(rc.Ctx, nsName, currentService) - - if err != nil && errors.IsNotFound(err) { + if err := client.Get(rc.Ctx, nsName, currentService); err != nil && errors.IsNotFound(err) { // if it's not found, put the service in the slice to be created when Apply is called createNeeded = append(createNeeded, desiredSvc) - } else if err != nil { // if we hit a k8s error, log it and error out logger.Error(err, "Could not get headless seed service", "name", nsName, ) return result.Error(err) - } else { // if we found the service already, check if they need updating if !utils.ResourcesHaveSameHash(currentService, desiredSvc) {