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. If it later gets deleted because of cleanup, we will recreate it
  • Loading branch information
burmanm committed Mar 27, 2024
1 parent bd222e6 commit 8ca40b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 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
19 changes: 9 additions & 10 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,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) {
Expand Down

0 comments on commit 8ca40b6

Please sign in to comment.