Skip to content

Commit 5b6b54b

Browse files
committed
Store hash to determine when pod spec changes
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 and compare this to the hash of the current desired pod spec to determine if the spec has changed.
1 parent 6b7917b commit 5b6b54b

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

apis/bases/client.openstack.org_openstackclients.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ spec:
8282
- type
8383
type: object
8484
type: array
85+
hash:
86+
additionalProperties:
87+
type: string
88+
type: object
8589
observedGeneration:
8690
format: int64
8791
type: integer

apis/client/v1beta1/openstackclient_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type OpenStackClientStatus struct {
6868

6969
//ObservedGeneration - the most recent generation observed for this object.
7070
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
71+
72+
// Map of hashes to track e.g. pod spec
73+
Hash map[string]string `json:"hash,omitempty"`
7174
}
7275

7376
// +kubebuilder:object:root=true

apis/client/v1beta1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/client.openstack.org_openstackclients.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ spec:
8282
- type
8383
type: object
8484
type: array
85+
hash:
86+
additionalProperties:
87+
type: string
88+
type: object
8589
observedGeneration:
8690
format: int64
8791
type: integer

controllers/client/openstackclient_controller.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,19 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
307307

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

310+
podSpecHash, err := util.ObjectHash(spec)
311+
if err != nil {
312+
return ctrl.Result{}, err
313+
}
314+
315+
podSpecHashName := "podSpec"
316+
310317
op, err := controllerutil.CreateOrPatch(ctx, r.Client, osclient, func() error {
311318
isPodUpdate := !osclient.ObjectMeta.CreationTimestamp.IsZero()
312-
if !isPodUpdate {
319+
currentPodSpecHash := instance.Status.Hash[podSpecHashName]
320+
if !isPodUpdate || currentPodSpecHash != podSpecHash {
313321
osclient.Spec = spec
314-
} else {
315-
osclient.Spec.Containers[0].Env = spec.Containers[0].Env
316-
osclient.Spec.NodeSelector = spec.NodeSelector
317-
osclient.Spec.Containers[0].Image = instance.Spec.ContainerImage
318322
}
319-
320323
osclient.Labels = util.MergeStringMaps(osclient.Labels, clientLabels)
321324

322325
err = controllerutil.SetControllerReference(instance, osclient, r.Scheme)
@@ -360,6 +363,8 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
360363
return ctrl.Result{}, err
361364
}
362365

366+
instance.Status.Hash, _ = util.SetHash(instance.Status.Hash, podSpecHashName, podSpecHash)
367+
363368
if op != controllerutil.OperationResultNone {
364369
util.LogForObject(
365370
helper,

0 commit comments

Comments
 (0)