Skip to content

Commit ee7d4ef

Browse files
committed
fix: automatically set CoreDNS version based on the Kubernetes version
1 parent 86f0228 commit ee7d4ef

File tree

7 files changed

+238
-91
lines changed

7 files changed

+238
-91
lines changed

api/v1alpha1/clusterconfig_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ type DNS struct {
322322

323323
type CoreDNS struct {
324324
// Image required for overriding Kubernetes DNS image details.
325+
// If the image version is not specified,
326+
// the default version based on the cluster's Kubernetes version will be used.
325327
// +kubebuilder:validation:Optional
326328
Image *Image `json:"image,omitempty"`
327329
}

api/v1alpha1/crds/caren.nutanix.com_awsclusterconfigs.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,10 @@ spec:
379379
description: CoreDNS defines the CoreDNS configuration for the cluster.
380380
properties:
381381
image:
382-
description: Image required for overriding Kubernetes DNS image details.
382+
description: |-
383+
Image required for overriding Kubernetes DNS image details.
384+
If the image version is not specified,
385+
the default version based on the cluster's Kubernetes version will be used.
383386
properties:
384387
repository:
385388
description: Repository is used to override the image repository to pull from.

api/v1alpha1/crds/caren.nutanix.com_dockerclusterconfigs.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ spec:
296296
description: CoreDNS defines the CoreDNS configuration for the cluster.
297297
properties:
298298
image:
299-
description: Image required for overriding Kubernetes DNS image details.
299+
description: |-
300+
Image required for overriding Kubernetes DNS image details.
301+
If the image version is not specified,
302+
the default version based on the cluster's Kubernetes version will be used.
300303
properties:
301304
repository:
302305
description: Repository is used to override the image repository to pull from.

api/v1alpha1/crds/caren.nutanix.com_genericclusterconfigs.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ spec:
5858
cluster.
5959
properties:
6060
image:
61-
description: Image required for overriding Kubernetes DNS
62-
image details.
61+
description: |-
62+
Image required for overriding Kubernetes DNS image details.
63+
If the image version is not specified,
64+
the default version based on the cluster's Kubernetes version will be used.
6365
properties:
6466
repository:
6567
description: Repository is used to override the image

api/v1alpha1/crds/caren.nutanix.com_nutanixclusterconfigs.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,10 @@ spec:
449449
description: CoreDNS defines the CoreDNS configuration for the cluster.
450450
properties:
451451
image:
452-
description: Image required for overriding Kubernetes DNS image details.
452+
description: |-
453+
Image required for overriding Kubernetes DNS image details.
454+
If the image version is not specified,
455+
the default version based on the cluster's Kubernetes version will be used.
453456
properties:
454457
repository:
455458
description: Repository is used to override the image repository to pull from.

pkg/handlers/generic/mutation/coredns/inject.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1616

1717
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
18+
corednsversions "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/versions"
1819
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1920
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches"
2021
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
@@ -53,7 +54,7 @@ func (h *coreDNSPatchHandler) Mutate(
5354
vars map[string]apiextensionsv1.JSON,
5455
holderRef runtimehooksv1.HolderReference,
5556
_ ctrlclient.ObjectKey,
56-
_ mutation.ClusterGetter,
57+
clusterGetter mutation.ClusterGetter,
5758
) error {
5859
log := ctrl.LoggerFrom(ctx).WithValues(
5960
"holderRef", holderRef,
@@ -65,11 +66,10 @@ func (h *coreDNSPatchHandler) Mutate(
6566
h.variableFieldPath...,
6667
)
6768
if err != nil {
68-
if variables.IsNotFoundError(err) {
69-
log.V(5).Info("coreDNSVar variable not defined")
70-
return nil
69+
if !variables.IsNotFoundError(err) {
70+
return err
7171
}
72-
return err
72+
log.V(5).Info("coreDNSVar variable not defined")
7373
}
7474

7575
log = log.WithValues(
@@ -81,34 +81,64 @@ func (h *coreDNSPatchHandler) Mutate(
8181
coreDNSVar,
8282
)
8383

84+
cluster, err := clusterGetter(ctx)
85+
if err != nil {
86+
log.Error(
87+
err,
88+
"failed to get cluster from CoreDNS mutation handler",
89+
)
90+
return err
91+
}
92+
8493
return patches.MutateIfApplicable(
8594
obj, vars, &holderRef, selectors.ControlPlane(), log,
8695
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error {
8796
log.WithValues(
8897
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
8998
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj),
90-
).Info("setting CoreDNS version if needed")
99+
).Info("setting CoreDNS version")
91100

92101
if obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration == nil {
93102
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration = &bootstrapv1.ClusterConfiguration{}
94103
}
95104

96-
if coreDNSVar.Image == nil {
97-
return nil
98-
}
99-
100105
dns := obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS
101106

102-
if coreDNSVar.Image.Tag != "" {
103-
dns.ImageTag = coreDNSVar.Image.Tag
104-
}
105-
106-
if coreDNSVar.Image.Repository != "" {
107-
dns.ImageRepository = coreDNSVar.Image.Repository
107+
// Set the CoreDNS image from the variable if it is defined.
108+
setFromVar(coreDNSVar.Image, &dns)
109+
110+
// Always set the default if the CoreDNS image version is not defined in the variable.
111+
if useDefaultVersion(coreDNSVar) {
112+
defaultCoreDNSVersion, found := corednsversions.GetCoreDNSVersion(
113+
cluster.Spec.Topology.Version,
114+
)
115+
if !found {
116+
log.Info("Default CoreDNS version not found for Kubernetes version")
117+
}
118+
dns.ImageTag = defaultCoreDNSVersion
108119
}
109120

110121
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS = dns
111122

112123
return nil
113124
})
114125
}
126+
127+
// setFromVar sets the CoreDNS image tag and repository from the variable if it is defined.
128+
// If the variable is not defined, the function just returns.
129+
func setFromVar(image *v1alpha1.Image, dns *bootstrapv1.DNS) {
130+
if image == nil {
131+
return
132+
}
133+
if image.Tag != "" {
134+
dns.ImageTag = image.Tag
135+
}
136+
if image.Repository != "" {
137+
dns.ImageRepository = image.Repository
138+
}
139+
}
140+
141+
// useDefaultVersion returns true if the CoreDNS version should be set to the default version.
142+
func useDefaultVersion(coreDNSVar v1alpha1.CoreDNS) bool {
143+
return coreDNSVar.Image == nil || coreDNSVar.Image.Tag == ""
144+
}

0 commit comments

Comments
 (0)