From d0ee04b6878cf282d5beead7c8b6a3cc246e532c Mon Sep 17 00:00:00 2001 From: Andrew Gouin Date: Mon, 29 Jan 2024 15:00:07 -0700 Subject: [PATCH] Apply strategic overrides last (#405) --- internal/fullnode/build_pods.go | 35 ++++++++------------------------ internal/fullnode/pod_builder.go | 27 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/internal/fullnode/build_pods.go b/internal/fullnode/build_pods.go index bd4bd919..f86371c9 100644 --- a/internal/fullnode/build_pods.go +++ b/internal/fullnode/build_pods.go @@ -14,9 +14,8 @@ const ( // BuildPods creates the final state of pods given the crd. func BuildPods(crd *cosmosv1.CosmosFullNode, cksums ConfigChecksums) ([]diff.Resource[*corev1.Pod], error) { var ( - builder = NewPodBuilder(crd) - overrides = crd.Spec.InstanceOverrides - pods []diff.Resource[*corev1.Pod] + builder = NewPodBuilder(crd) + pods []diff.Resource[*corev1.Pod] ) candidates := podCandidates(crd) for i := int32(0); i < crd.Spec.Replicas; i++ { @@ -24,33 +23,15 @@ func BuildPods(crd *cosmosv1.CosmosFullNode, cksums ConfigChecksums) ([]diff.Res if err != nil { return nil, err } - if _, shouldSnapshot := candidates[pod.Name]; shouldSnapshot { + + if pod == nil { continue } - if len(crd.Spec.ChainSpec.Versions) > 0 { - instanceHeight := uint64(0) - if height, ok := crd.Status.Height[pod.Name]; ok { - instanceHeight = height - } - var image string - for _, version := range crd.Spec.ChainSpec.Versions { - if instanceHeight < version.UpgradeHeight { - break - } - image = version.Image - } - if image != "" { - setChainContainerImage(pod, image) - } - } - if o, ok := overrides[pod.Name]; ok { - if o.DisableStrategy != nil { - continue - } - if o.Image != "" { - setChainContainerImage(pod, o.Image) - } + + if _, shouldSnapshot := candidates[pod.Name]; shouldSnapshot { + continue } + pod.Annotations[configChecksumAnnotation] = cksums[client.ObjectKeyFromObject(pod)] pods = append(pods, diff.Adapt(pod, i)) } diff --git a/internal/fullnode/pod_builder.go b/internal/fullnode/pod_builder.go index 2d95ccfc..6776e55b 100644 --- a/internal/fullnode/pod_builder.go +++ b/internal/fullnode/pod_builder.go @@ -180,9 +180,36 @@ func podReadinessProbes(crd *cosmosv1.CosmosFullNode) []*corev1.Probe { // Build assigns the CosmosFullNode crd as the owner and returns a fully constructed pod. func (b PodBuilder) Build() (*corev1.Pod, error) { pod := b.pod.DeepCopy() + + if len(b.crd.Spec.ChainSpec.Versions) > 0 { + instanceHeight := uint64(0) + if height, ok := b.crd.Status.Height[pod.Name]; ok { + instanceHeight = height + } + var image string + for _, version := range b.crd.Spec.ChainSpec.Versions { + if instanceHeight < version.UpgradeHeight { + break + } + image = version.Image + } + if image != "" { + setChainContainerImage(pod, image) + } + } + if o, ok := b.crd.Spec.InstanceOverrides[pod.Name]; ok { + if o.DisableStrategy != nil { + return nil, nil + } + if o.Image != "" { + setChainContainerImage(pod, o.Image) + } + } + if err := kube.ApplyStrategicMergePatch(pod, podPatch(b.crd)); err != nil { return nil, err } + kube.NormalizeMetadata(&pod.ObjectMeta) return pod, nil }