Skip to content

Commit

Permalink
Restart services when their NodeSelector changes
Browse files Browse the repository at this point in the history
Add a hash of a service's Spec.NodeSelector to the inputs that
determine when the corresponding pod should be recreated. This
ensures pods are restarted whenever the NodeSelector is modified.
  • Loading branch information
ASBishop committed Mar 1, 2024
1 parent c46d63a commit df33f3e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions controllers/cinderapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,20 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
// all cert input checks out so report InputReady
instance.Status.Conditions.MarkTrue(condition.TLSInputReadyCondition, condition.InputReadyMessage)

//
// Hash the nodeSelector so the pod is recreated when it changes
//
err = cinder.AddNodeSelectorHash(instance.Spec.NodeSelector, &configVars)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ServiceConfigReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}

//
// Create secrets required as input for the Service and calculate an overall hash of hashes
//
Expand Down
14 changes: 14 additions & 0 deletions controllers/cinderbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ func (r *CinderBackupReconciler) reconcileNormal(ctx context.Context, instance *
// all cert input checks out so report InputReady
instance.Status.Conditions.MarkTrue(condition.TLSInputReadyCondition, condition.InputReadyMessage)

//
// Hash the nodeSelector so the pod is recreated when it changes
//
err = cinder.AddNodeSelectorHash(instance.Spec.NodeSelector, &configVars)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ServiceConfigReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}

//
// Create secrets required as input for the Service and calculate an overall hash of hashes
//
Expand Down
14 changes: 14 additions & 0 deletions controllers/cinderscheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ func (r *CinderSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
// all cert input checks out so report InputReady
instance.Status.Conditions.MarkTrue(condition.TLSInputReadyCondition, condition.InputReadyMessage)

//
// Hash the nodeSelector so the pod is recreated when it changes
//
err = cinder.AddNodeSelectorHash(instance.Spec.NodeSelector, &configVars)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ServiceConfigReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}

//
// Create ConfigMaps required as input for the Service and calculate an overall hash of hashes
//
Expand Down
14 changes: 14 additions & 0 deletions controllers/cindervolume_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ func (r *CinderVolumeReconciler) reconcileNormal(ctx context.Context, instance *
return ctrl.Result{}, err
}

//
// Hash the nodeSelector so the pod is recreated when it changes
//
err = cinder.AddNodeSelectorHash(instance.Spec.NodeSelector, &configVars)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ServiceConfigReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}

//
// create hash over all the different input resources to identify if any those changed
// and a restart/recreate is required.
Expand Down
11 changes: 11 additions & 0 deletions pkg/cinder/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cinder
import (
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -47,3 +49,12 @@ func GetPodAffinity(componentName string) *corev1.Affinity {
corev1.LabelHostname,
)
}

// AddNodeSelectorHash - Adds a hash of a nodeSelector map to the envVars.
func AddNodeSelectorHash(nodeSelector map[string]string, envVars *map[string]env.Setter) error {
hash, err := util.ObjectHash(nodeSelector)
if err != nil {
(*envVars)["NodeSelectorHash"] = env.SetValue(hash)
}
return err
}

0 comments on commit df33f3e

Please sign in to comment.