Skip to content

Commit 07c7ebe

Browse files
committed
fixup! refactor: Make logic more readable
1 parent ca8eaec commit 07c7ebe

File tree

1 file changed

+15
-27
lines changed
  • pkg/handlers/generic/mutation/coredns

1 file changed

+15
-27
lines changed

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (h *coreDNSPatchHandler) Mutate(
7474
if !variables.IsNotFoundError(err) {
7575
return err
7676
}
77-
log.V(5).Info("coreDNSVar variable not defined")
77+
log.V(5).Info("coreDNS variable not defined")
7878
}
7979

8080
log = log.WithValues(
@@ -90,7 +90,7 @@ func (h *coreDNSPatchHandler) Mutate(
9090
if err != nil {
9191
log.Error(
9292
err,
93-
"failed to get cluster from CoreDNS mutation handler",
93+
"failed to get cluster for CoreDNS mutation handler",
9494
)
9595
return err
9696
}
@@ -107,43 +107,31 @@ func (h *coreDNSPatchHandler) Mutate(
107107
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration = &bootstrapv1.ClusterConfiguration{}
108108
}
109109

110-
dns := obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS
110+
dns := &obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS
111111

112112
// Set the CoreDNS image from the variable if it is defined.
113-
setFromVar(coreDNSVar.Image, &dns)
113+
if coreDNSVar.Image != nil {
114+
if coreDNSVar.Image.Tag != "" {
115+
dns.ImageTag = coreDNSVar.Image.Tag
116+
}
117+
if coreDNSVar.Image.Repository != "" {
118+
dns.ImageRepository = coreDNSVar.Image.Repository
119+
}
120+
}
114121

115-
// Always set the default if the CoreDNS image version is not defined in the variable.
116-
if useDefaultVersion(coreDNSVar) {
122+
// If the CoreDNS image tag is still not set, set the image tag to the default CoreDNS version based on the
123+
// Kubernetes version.
124+
if dns.ImageTag == "" {
117125
defaultCoreDNSVersion, found := corednsversions.GetCoreDNSVersion(
118126
cluster.Spec.Topology.Version,
119127
)
120128
if !found {
121129
return ErrDefaultCoreDNSVersionNotFound
122130
}
131+
123132
dns.ImageTag = defaultCoreDNSVersion
124133
}
125134

126-
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS = dns
127-
128135
return nil
129136
})
130137
}
131-
132-
// setFromVar sets the CoreDNS image tag and repository from the variable if it is defined.
133-
// If the variable is not defined, the function just returns.
134-
func setFromVar(image *v1alpha1.Image, dns *bootstrapv1.DNS) {
135-
if image == nil {
136-
return
137-
}
138-
if image.Tag != "" {
139-
dns.ImageTag = image.Tag
140-
}
141-
if image.Repository != "" {
142-
dns.ImageRepository = image.Repository
143-
}
144-
}
145-
146-
// useDefaultVersion returns true if the CoreDNS version should be set to the default version.
147-
func useDefaultVersion(coreDNSVar v1alpha1.CoreDNS) bool {
148-
return coreDNSVar.Image == nil || coreDNSVar.Image.Tag == ""
149-
}

0 commit comments

Comments
 (0)