Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec:
bootstrap:
ref:
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
kind: EKSConfigTemplate
kind: NodeadmConfigTemplate
name: eks-quick-start-worker-configtemplate
infrastructure:
ref:
Expand Down Expand Up @@ -73,11 +73,18 @@ metadata:
spec:
template:
spec:
ami:
eksLookupType: AmazonLinux2023
cloudInit:
insecureSkipSecretsManager: true
instanceMetadataOptions:
httpPutResponseHopLimit: 2
httpTokens: required
instanceType: PLACEHOLDER
sshKeyName: ""
---
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
kind: EKSConfigTemplate
kind: NodeadmConfigTemplate
metadata:
labels:
cluster.x-k8s.io/provider: eks
Expand Down
13 changes: 10 additions & 3 deletions hack/examples/bases/eks/clusterclass/clusterclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spec:
ref:
name: "quick-start-worker-configtemplate"
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
kind: EKSConfigTemplate
kind: NodeadmConfigTemplate
infrastructure:
ref:
name: "quick-start-worker-machinetemplate"
Expand Down Expand Up @@ -58,10 +58,17 @@ metadata:
name: "quick-start-worker-machinetemplate"
spec:
template:
spec: {}
spec:
cloudInit:
insecureSkipSecretsManager: true
ami:
eksLookupType: AmazonLinux2023
instanceMetadataOptions:
httpTokens: required
httpPutResponseHopLimit: 2
---
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
kind: EKSConfigTemplate
kind: NodeadmConfigTemplate
metadata:
name: "quick-start-worker-configtemplate"
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ nameReference:
fieldSpecs:
- kind: ClusterClass
path: spec/controlPlane/ref/name
- kind: EKSConfigTemplate
- kind: NodeadmConfigTemplate
fieldSpecs:
- kind: ClusterClass
path: spec/workers/machineDeployments/template/bootstrap/ref/name
12 changes: 6 additions & 6 deletions pkg/handlers/eks/mutation/testutils/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ func NewEKSControlPlaneRequestItem(
)
}

func NewEKSConfigTemplateRequestItem(
func NewNodeadmConfigTemplateRequestItem(
uid types.UID,
existingSpec ...eksbootstrapv1.EKSConfigTemplateSpec,
existingSpec ...eksbootstrapv1.NodeadmConfigTemplateSpec,
) runtimehooksv1.GeneratePatchesRequestItem {
eksConfigTemplate := &eksbootstrapv1.EKSConfigTemplate{
nodeadmConfigTemplate := &eksbootstrapv1.NodeadmConfigTemplate{
TypeMeta: metav1.TypeMeta{
APIVersion: eksbootstrapv1.GroupVersion.String(),
Kind: "EKSConfigTemplate",
Kind: "NodeadmConfigTemplate",
},
}

switch len(existingSpec) {
case 0:
// Do nothing.
case 1:
eksConfigTemplate.Spec = existingSpec[0]
nodeadmConfigTemplate.Spec = existingSpec[0]
default:
panic("can only take at most one existing spec")
}

return request.NewRequestItem(
eksConfigTemplate,
nodeadmConfigTemplate,
&runtimehooksv1.HolderReference{
Kind: "MachineDeployment",
FieldPath: "spec.template.spec.bootstrap.configRef",
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/generic/mutation/ntp/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (h *ntpPatchHandler) Mutate(

if err := patches.MutateIfApplicable(
obj, vars, &holderRef,
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
log.WithValues(
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
"patchedObjectName", client.ObjectKeyFromObject(obj),
).Info("setting users in worker node EKS config template")
).Info("setting users in worker node NodeadmConfig template")
obj.Spec.Template.Spec.NTP = &eksbootstrapv1.NTP{
Enabled: ptr.To(true),
Servers: ntp.Servers,
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/generic/mutation/ntp/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ var _ = Describe("Generate NTP patches", func() {
},
},
{
Name: "NTP configuration is set for worker nodes with single server for EKSConfigTemplate",
RequestItem: testutils.NewEKSConfigTemplateRequestItem(""),
Name: "NTP configuration is set for worker nodes with single server for NodeadmConfigTemplate",
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem(""),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Expand Down
29 changes: 13 additions & 16 deletions pkg/handlers/generic/mutation/taints/inject_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ package taints

import (
"context"
"fmt"
"strings"

"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/ptr"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -48,6 +50,10 @@ func newTaintsWorkerPatchHandler(
}
}

type KubeletRegisterOptions struct {
RegisterWithTaints []v1.Taint `json:"registerWithTaints,omitempty"`
}

func (h *taintsWorkerPatchHandler) Mutate(
ctx context.Context,
obj *unstructured.Unstructured,
Expand Down Expand Up @@ -103,25 +109,16 @@ func (h *taintsWorkerPatchHandler) Mutate(

if err := patches.MutateIfApplicable(
obj, vars, &holderRef,
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
log.WithValues(
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj),
).Info("adding taints to worker node EKS config template")
if obj.Spec.Template.Spec.KubeletExtraArgs == nil {
obj.Spec.Template.Spec.KubeletExtraArgs = make(map[string]string, 1)
}

existingTaintsFlagValue := obj.Spec.Template.Spec.KubeletExtraArgs["register-with-taints"]

newTaintsFlagValue := toEKSConfigTaints(taintsVar)

if existingTaintsFlagValue != "" {
newTaintsFlagValue = existingTaintsFlagValue + "," + newTaintsFlagValue
}

obj.Spec.Template.Spec.KubeletExtraArgs["register-with-taints"] = newTaintsFlagValue
).Info("adding taints to worker NodeadmConfig template")
newTaints := toEKSConfigTaints(taintsVar)
kubeletOptions := ptr.Deref(obj.Spec.Template.Spec.Kubelet, eksbootstrapv1.KubeletOptions{})
kubeletOptions.Flags = append(kubeletOptions.Flags, fmt.Sprintf("--register-with-taints=%s", newTaints))
obj.Spec.Template.Spec.Kubelet = &kubeletOptions
return nil
}); err != nil {
return err
Expand Down
82 changes: 78 additions & 4 deletions pkg/handlers/generic/mutation/taints/inject_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"

eksbootstrapv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/bootstrap/eks/api/v1beta2"
"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/testutils/capitest"
Expand Down Expand Up @@ -59,7 +60,7 @@ var _ = Describe("Generate taints patches for Worker", func() {
}},
},
{
Name: "taints for workers set for EKSConfigTemplate",
Name: "taints for workers set for NodeadmConfigTemplate",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
v1alpha1.WorkerConfigVariableName,
Expand All @@ -77,15 +78,88 @@ var _ = Describe("Generate taints patches for Worker", func() {
},
),
},
RequestItem: testutils.NewEKSConfigTemplateRequestItem(""),
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem(""),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/kubeletExtraArgs",
Path: "/spec/template/spec/kubelet",
ValueMatcher: gomega.HaveKeyWithValue(
"register-with-taints", "key=value:NoExecute",
"flags",
gomega.ContainElement("--register-with-taints=key=value:NoExecute"),
),
}},
},
{
Name: "taints for workers set for NodeadmConfigTemplate with existing flags argument",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
v1alpha1.WorkerConfigVariableName,
[]v1alpha1.Taint{{
Key: "key",
Effect: v1alpha1.TaintEffectNoExecute,
Value: "value",
}},
VariableName,
),
capitest.VariableWithValue(
"builtin",
apiextensionsv1.JSON{
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
},
),
},
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem("", eksbootstrapv1.NodeadmConfigTemplateSpec{
Template: eksbootstrapv1.NodeadmConfigTemplateResource{
Spec: eksbootstrapv1.NodeadmConfigSpec{
Kubelet: &eksbootstrapv1.KubeletOptions{
Flags: []string{
"--max-pods=110",
},
},
},
},
}),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/kubelet/flags/1",
ValueMatcher: gomega.Equal("--register-with-taints=key=value:NoExecute"),
}},
},
{
Name: "taints for workers set for NodeadmConfigTemplate with existing flags with register-with-taints ",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
v1alpha1.WorkerConfigVariableName,
[]v1alpha1.Taint{{
Key: "key",
Effect: v1alpha1.TaintEffectNoExecute,
Value: "value",
}},
VariableName,
),
capitest.VariableWithValue(
"builtin",
apiextensionsv1.JSON{
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
},
),
},
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem("", eksbootstrapv1.NodeadmConfigTemplateSpec{
Template: eksbootstrapv1.NodeadmConfigTemplateResource{
Spec: eksbootstrapv1.NodeadmConfigSpec{
Kubelet: &eksbootstrapv1.KubeletOptions{
Flags: []string{
"--register-with-taints=key1=value1:NoSchedule",
},
},
},
},
}),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/kubelet/flags/1",
ValueMatcher: gomega.Equal("--register-with-taints=key=value:NoExecute"),
}},
},
}

// create test node for each case
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/generic/mutation/users/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func (h *usersPatchHandler) Mutate(

if err := patches.MutateIfApplicable(
obj, vars, &holderRef,
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
log.WithValues(
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj),
).Info("setting users in worker node EKS config template")
).Info("setting users in worker node NodeadmConfig template")
eksBootstrapUsers := make([]eksbootstrapv1.User, 0, len(bootstrapUsers))
for _, user := range bootstrapUsers {
var passwdFrom *eksbootstrapv1.PasswdSource
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/generic/mutation/users/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var _ = Describe("Generate Users patches", func() {
}},
},
{
Name: "users set for EKSConfigTemplate generic worker",
Name: "users set for NodeadmConfigTemplate generic worker",
Vars: []runtimehooksv1.Variable{
capitest.VariableWithValue(
v1alpha1.ClusterConfigVariableName,
Expand All @@ -210,7 +210,7 @@ var _ = Describe("Generate Users patches", func() {
},
),
},
RequestItem: testutils.NewEKSConfigTemplateRequestItem(""),
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem(""),
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
Operation: "add",
Path: "/spec/template/spec/users",
Expand Down
Loading