Skip to content

Commit

Permalink
If service is already owned by some other resource, do not try to tak…
Browse files Browse the repository at this point in the history
…e ownership of it (#627)

 If it later gets deleted because of cleanup, we will recreate it
  • Loading branch information
burmanm authored Mar 28, 2024
1 parent bd222e6 commit ebbb957
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions pkg/reconciliation/reconcile_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -72,30 +73,30 @@ 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 {
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 controllerutil.HasControllerReference(currentService) && currentService.GetOwnerReferences()[0].Kind == "CassandraDatacenter" && currentService.GetOwnerReferences()[0].Name != dc.GetName() {
// Some other CassandraDatacenter owns this service, so we should ignore it
logger.Info("Service is owned by another CassandraDatacenter, not updating it as part of this reconcile", "service", currentService, "datacenter", currentService.GetOwnerReferences()[0].Name)
continue
}

// if we found the service already, check if they need updating
if !utils.ResourcesHaveSameHash(currentService, desiredSvc) {
resourceVersion := currentService.GetResourceVersion()
Expand Down

0 comments on commit ebbb957

Please sign in to comment.