Skip to content

Commit

Permalink
Store hash to determine when pod spec changes
Browse files Browse the repository at this point in the history
Comparing the desired pod spec to the current pod spec is not reliable as
the spec can be mutated by k8s e.g to add volume mount for a service account
or to add the default nodeselector from the namespace/cluster on OpenShift

Instead store a hash of the original pod spec as an annotation and compare
this to the hash of the current desired pod spec to determine if the spec
has changed.
  • Loading branch information
olliewalsh committed Dec 3, 2024
1 parent 1e3929e commit 73dfdc9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions controllers/client/openstackclient_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
Expand Down Expand Up @@ -307,17 +308,23 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ

spec := openstackclient.ClientPodSpec(ctx, instance, helper, configVarsHash)

podSpecHash, err := util.ObjectHash(spec)
if err != nil {
return ctrl.Result{}, err
}

podSpecAnnotation := labels.GetGroupLabel("openstackclient") + "/pod-spec-hash"

op, err := controllerutil.CreateOrPatch(ctx, r.Client, osclient, func() error {
isPodUpdate := !osclient.ObjectMeta.CreationTimestamp.IsZero()
if !isPodUpdate {
currentPodSpecHash, hasPodSpecHash := osclient.Annotations[podSpecAnnotation]
if !isPodUpdate || (hasPodSpecHash && currentPodSpecHash != podSpecHash) {
osclient.Spec = spec
} else {
osclient.Spec.Containers[0].Env = spec.Containers[0].Env
osclient.Spec.NodeSelector = spec.NodeSelector
osclient.Spec.Containers[0].Image = instance.Spec.ContainerImage
}

osclient.Labels = util.MergeStringMaps(osclient.Labels, clientLabels)
osclient.Annotations = util.MergeStringMaps(map[string]string{
podSpecAnnotation: podSpecHash,
}, osclient.Annotations)

err = controllerutil.SetControllerReference(instance, osclient, r.Scheme)
if err != nil {
Expand Down

0 comments on commit 73dfdc9

Please sign in to comment.