@@ -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