Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Populate all imageRepository fields in TKR_DATA values (#4298)
Browse files Browse the repository at this point in the history
`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 <imikushin@vmware.com>
(cherry picked from commit 6cdbd30)
  • Loading branch information
imikushin authored and Ivan Mikushin committed Feb 15, 2023
1 parent 97ec281 commit b0e78f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 18 additions & 1 deletion tkr/webhook/cluster/tkr-resolver/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions tkr/webhook/cluster/tkr-resolver/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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())
}
}
})
})
})
Expand Down

0 comments on commit b0e78f5

Please sign in to comment.