-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support configuring CoreDNS image (#950)
**What problem does this PR solve?**: New API to support setting CoreDNS image repository and image tag. ``` spec: topology: variables: - name: clusterConfig value: dns: coreDNS: image: repository: my-registry.io/my-org/my-repo tag: "v1.11.3_custom.0" ``` **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> 1. New unit tests. 2. Brought up a Nutanix cluster with and verified the expected image was set in the CoreDNS Deployment. ``` dns: coreDNS: image: repository: docker.io/dkoshkin tag: "v1.11.3" ``` **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
- Loading branch information
Showing
13 changed files
with
554 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
+++ | ||
title = "etcd" | ||
+++ | ||
|
||
This customization will be available when the | ||
[provider-specific cluster configuration patch]({{< ref "..">}}) is included in the `ClusterClass`. | ||
|
||
The DNS configuration can then be manipulated via the cluster variables. | ||
If the `dns` property is not specified, then the customization will be skipped. | ||
|
||
## CoreDNS | ||
|
||
The CoreDNS configuration can then be manipulated via the cluster variables. | ||
If the `dns.coreDNS` property is not specified, then the customization will be skipped. | ||
|
||
### Example | ||
|
||
To change the repository and tag for the container image for the CoreDNS pod, specify the following configuration: | ||
|
||
> Note do not include "coredns" in the repository, kubeadm already appends it. | ||
```yaml | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
name: <NAME> | ||
spec: | ||
topology: | ||
variables: | ||
- name: clusterConfig | ||
value: | ||
dns: | ||
coreDNS: | ||
image: | ||
repository: my-registry.io/my-org/my-repo | ||
tag: "v1.11.3_custom.0" | ||
``` | ||
Applying this configuration will result in the following value being set: | ||
- `KubeadmControlPlaneTemplate`: | ||
|
||
- ```yaml | ||
spec: | ||
kubeadmConfigSpec: | ||
clusterConfiguration: | ||
dns: | ||
imageRepository: "my-registry.io/my-org/my-repo" | ||
imageTag: "v1.11.3_custom.0" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2023 Nutanix. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package coredns | ||
|
||
import ( | ||
"context" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" | ||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches" | ||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors" | ||
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables" | ||
) | ||
|
||
const ( | ||
// VariableName is the external patch variable name. | ||
VariableName = "coreDNS" | ||
) | ||
|
||
type coreDNSPatchHandler struct { | ||
variableName string | ||
variableFieldPath []string | ||
} | ||
|
||
func NewPatch() *coreDNSPatchHandler { | ||
return newKubernetesDNSPatchHandlerPatchHandler( | ||
v1alpha1.ClusterConfigVariableName, v1alpha1.DNSVariableName, VariableName, | ||
) | ||
} | ||
|
||
func newKubernetesDNSPatchHandlerPatchHandler( | ||
variableName string, | ||
variableFieldPath ...string, | ||
) *coreDNSPatchHandler { | ||
return &coreDNSPatchHandler{ | ||
variableName: variableName, | ||
variableFieldPath: variableFieldPath, | ||
} | ||
} | ||
|
||
func (h *coreDNSPatchHandler) Mutate( | ||
ctx context.Context, | ||
obj *unstructured.Unstructured, | ||
vars map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
_ ctrlclient.ObjectKey, | ||
_ mutation.ClusterGetter, | ||
) error { | ||
log := ctrl.LoggerFrom(ctx).WithValues( | ||
"holderRef", holderRef, | ||
) | ||
|
||
coreDNSVar, err := variables.Get[v1alpha1.CoreDNS]( | ||
vars, | ||
h.variableName, | ||
h.variableFieldPath..., | ||
) | ||
if err != nil { | ||
if variables.IsNotFoundError(err) { | ||
log.V(5).Info("coreDNSVar variable not defined") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
log = log.WithValues( | ||
"variableName", | ||
h.variableName, | ||
"variableFieldPath", | ||
h.variableFieldPath, | ||
"variableValue", | ||
coreDNSVar, | ||
) | ||
|
||
return patches.MutateIfApplicable( | ||
obj, vars, &holderRef, selectors.ControlPlane(), log, | ||
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("setting CoreDNS version if needed") | ||
|
||
if obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration == nil { | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration = &bootstrapv1.ClusterConfiguration{} | ||
} | ||
|
||
if coreDNSVar.Image == nil { | ||
return nil | ||
} | ||
|
||
dns := obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS | ||
|
||
if coreDNSVar.Image.Tag != "" { | ||
dns.ImageTag = coreDNSVar.Image.Tag | ||
} | ||
|
||
if coreDNSVar.Image.Repository != "" { | ||
dns.ImageRepository = coreDNSVar.Image.Repository | ||
} | ||
|
||
obj.Spec.Template.Spec.KubeadmConfigSpec.ClusterConfiguration.DNS = dns | ||
|
||
return nil | ||
}) | ||
} |
Oops, something went wrong.