From b0e78f537266c957fefe397f34872b5306640fc2 Mon Sep 17 00:00:00 2001 From: Ivan Mikushin Date: Thu, 2 Feb 2023 14:06:47 -0800 Subject: [PATCH] Populate all imageRepository fields in TKR_DATA values (#4298) `imageRepository` fields in `TKR.spec.kubernetes.{etcd,pause,coredns, kube-vip}` can be empty. In this case, the clients should be defaulting to `TKR.spec.kubernetes.imageRepository`. This change allows to simplify ClusterClass JSON patches doing this: they can now assume imageRepository fields in TKR_DATA values are non-empty and have the correct value. Signed-off-by: Ivan Mikushin (cherry picked from commit 6cdbd30447cf85b9111b2aa50d85be82a7ac62c1) --- .../cluster/tkr-resolver/cluster/cluster.go | 19 ++++++++++++++++++- .../tkr-resolver/cluster/cluster_test.go | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tkr/webhook/cluster/tkr-resolver/cluster/cluster.go b/tkr/webhook/cluster/tkr-resolver/cluster/cluster.go index decebdb127..2e28e93e93 100644 --- a/tkr/webhook/cluster/tkr-resolver/cluster/cluster.go +++ b/tkr/webhook/cluster/tkr-resolver/cluster/cluster.go @@ -324,7 +324,7 @@ func tkrDataValue(customImageRepository string, tkr *runv1.TanzuKubernetesReleas osimage.SetRefLabels(ls, osImage.Spec.Image.Type, osImage.Spec.Image.Ref) return &TKRDataValue{ - KubernetesSpec: *withCustomImageRepository(customImageRepository, &tkr.Spec.Kubernetes), + KubernetesSpec: *withCustomImageRepository(customImageRepository, defaultImageRepository(&tkr.Spec.Kubernetes)), OSImageRef: osImage.Spec.Image.Ref, Labels: ls, } @@ -348,6 +348,23 @@ func withCustomImageRepository(customImageRepository string, k8sSpec *runv1.Kube return k8sSpec } +func defaultImageRepository(k8sSpec *runv1.KubernetesSpec) *runv1.KubernetesSpec { + if k8sSpec.ImageRepository != "" { + k8sSpec = k8sSpec.DeepCopy() + for _, imageInfo := range []*runv1.ContainerImageInfo{ + k8sSpec.CoreDNS, + k8sSpec.Etcd, + k8sSpec.Pause, + k8sSpec.KubeVIP, + } { + if imageInfo != nil && imageInfo.ImageRepository == "" { + imageInfo.ImageRepository = k8sSpec.ImageRepository + } + } + } + return k8sSpec +} + // getMap returns the map (creates it first if the map is nil). mp has to be a pointer to the variable holding the map, // so that we could save the newly created map. // Pre-reqs: mp != nil diff --git a/tkr/webhook/cluster/tkr-resolver/cluster/cluster_test.go b/tkr/webhook/cluster/tkr-resolver/cluster/cluster_test.go index 7f52c26aff..ebbe4c5159 100644 --- a/tkr/webhook/cluster/tkr-resolver/cluster/cluster_test.go +++ b/tkr/webhook/cluster/tkr-resolver/cluster/cluster_test.go @@ -418,6 +418,7 @@ var _ = Describe("cluster.Webhook", func() { cluster.Spec.Topology = &clusterv1.Topology{} cluster.Spec.Topology.Version = k8sVersionPrefix + customImageRepository = "" if rand.Intn(2) != 0 { customImageRepository = rand.String(10) } @@ -480,8 +481,19 @@ var _ = Describe("cluster.Webhook", func() { Expect(topology.GetVariable(cluster, VarTKRData, &tkrData)).To(Succeed()) Expect(tkrData).ToNot(BeNil()) Expect(tkrData).To(HaveKey(tkr.Spec.Kubernetes.Version)) - Expect(tkrData[tkr.Spec.Kubernetes.Version].Labels[runv1.LabelTKR]).To(Equal(tkr.Name)) - Expect(tkrData[tkr.Spec.Kubernetes.Version].KubernetesSpec).To(Equal(*withCustomImageRepository(customImageRepository, &tkr.Spec.Kubernetes))) + dataValue := tkrData[tkr.Spec.Kubernetes.Version] + Expect(dataValue.Labels[runv1.LabelTKR]).To(Equal(tkr.Name)) + Expect(dataValue.KubernetesSpec).To(Equal(*withCustomImageRepository(customImageRepository, defaultImageRepository(&tkr.Spec.Kubernetes)))) + for _, imageInfo := range []*runv1.ContainerImageInfo{ + dataValue.KubernetesSpec.Etcd, + dataValue.KubernetesSpec.Pause, + dataValue.KubernetesSpec.CoreDNS, + dataValue.KubernetesSpec.KubeVIP, + } { + if imageInfo != nil { + Expect(imageInfo.ImageRepository).ToNot(BeEmpty()) + } + } }) }) })