From 43bc7ad89d92e9834e67d62dd01fcf0dd657154a Mon Sep 17 00:00:00 2001 From: Vivek Koppuru Date: Mon, 24 Jan 2022 00:46:44 -0800 Subject: [PATCH 12/34] Add node labels support for bottlerocket --- .../internal/bottlerocket/bootstrap.go | 9 ++++++++ .../internal/bottlerocket/bottlerocket.go | 23 ++++++++++++++++++- .../controllers/kubeadmconfig_controller.go | 9 ++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/bootstrap/kubeadm/internal/bottlerocket/bootstrap.go b/bootstrap/kubeadm/internal/bottlerocket/bootstrap.go index 4e1de77db..3ebb9240a 100644 --- a/bootstrap/kubeadm/internal/bottlerocket/bootstrap.go +++ b/bootstrap/kubeadm/internal/bottlerocket/bootstrap.go @@ -42,6 +42,11 @@ no-proxy = [{{stringsJoin .NoProxyEndpoints "," }}] data = "{{.RegistryMirrorCACert}}" trusted=true {{- end -}} +` + nodeLabelsTemplate = `{{ define "nodeLabelSettings" -}} +[settings.kubernetes.node-labels] +{{.NodeLabels}} +{{- end -}} ` bottlerocketNodeInitSettingsTemplate = `{{template "bootstrapHostContainerSettings" .}} @@ -60,5 +65,9 @@ trusted=true {{- if (ne .RegistryMirrorCACert "")}} {{template "registryMirrorCACertSettings" .}} {{- end -}} + +{{- if (ne .NodeLabels "")}} +{{template "nodeLabelSettings" .}} +{{- end -}} ` ) diff --git a/bootstrap/kubeadm/internal/bottlerocket/bottlerocket.go b/bootstrap/kubeadm/internal/bottlerocket/bottlerocket.go index 9e4f8d4a5..0ba4318c5 100644 --- a/bootstrap/kubeadm/internal/bottlerocket/bottlerocket.go +++ b/bootstrap/kubeadm/internal/bottlerocket/bottlerocket.go @@ -11,7 +11,7 @@ import ( bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" "github.com/pkg/errors" - yaml "gopkg.in/yaml.v2" + "gopkg.in/yaml.v2" ) const ( @@ -26,6 +26,7 @@ type BottlerocketConfig struct { BottlerocketBootstrap bootstrapv1.BottlerocketBootstrap ProxyConfiguration bootstrapv1.ProxyConfiguration RegistryMirrorConfiguration bootstrapv1.RegistryMirrorConfiguration + KubeletExtraArgs map[string]string } type BottlerocketSettingsInput struct { @@ -37,6 +38,7 @@ type BottlerocketSettingsInput struct { NoProxyEndpoints []string RegistryMirrorEndpoint string RegistryMirrorCACert string + NodeLabels string } type HostPath struct { @@ -99,6 +101,9 @@ func generateNodeUserData(kind string, tpl string, data interface{}) ([]byte, er if _, err := tm.Parse(registryMirrorCACertTemplate); err != nil { return nil, errors.Wrapf(err, "failed to parse registry mirror ca cert %s template", kind) } + if _, err := tm.Parse(nodeLabelsTemplate); err != nil { + return nil, errors.Wrapf(err, "failed to parse node labels %s template", kind) + } t, err := tm.Parse(tpl) if err != nil { return nil, errors.Wrapf(err, "failed to parse %s template", kind) @@ -133,6 +138,7 @@ func getBottlerocketNodeUserData(bootstrapContainerUserData []byte, users []boot PauseContainerSource: fmt.Sprintf("%s:%s", config.Pause.ImageRepository, config.Pause.ImageTag), HTTPSProxyEndpoint: config.ProxyConfiguration.HTTPSProxy, RegistryMirrorEndpoint: config.RegistryMirrorConfiguration.Endpoint, + NodeLabels: parseNodeLabels(config.KubeletExtraArgs["node-labels"]), // empty string if it does not exist } if len(config.ProxyConfiguration.NoProxy) > 0 { for _, noProxy := range config.ProxyConfiguration.NoProxy { @@ -150,6 +156,21 @@ func getBottlerocketNodeUserData(bootstrapContainerUserData []byte, users []boot return bottlerocketNodeUserData, nil } +func parseNodeLabels(nodeLabels string) string { + if nodeLabels == "" { + return "" + } + nodeLabelsToml := "" + nodeLabelsList := strings.Split(nodeLabels, ",") + for _, nodeLabel := range nodeLabelsList { + keyVal := strings.Split(nodeLabel, "=") + if len(keyVal) == 2 { + nodeLabelsToml += fmt.Sprintf("\"%v\" = \"%v\"\n", keyVal[0], keyVal[1]) + } + } + return nodeLabelsToml +} + // Parses through all the users and return list of all user's authorized ssh keys func getAllAuthorizedKeys(users []bootstrapv1.User) string { var sshAuthorizedKeys []string diff --git a/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go b/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go index b1fb19751..8a8c04c92 100644 --- a/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go +++ b/bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go @@ -474,6 +474,9 @@ func (r *KubeadmConfigReconciler) handleClusterNotInitialized(ctx context.Contex if scope.Config.Spec.ClusterConfiguration.RegistryMirror.Endpoint != "" { bottlerocketConfig.RegistryMirrorConfiguration = scope.Config.Spec.ClusterConfiguration.RegistryMirror } + if scope.Config.Spec.InitConfiguration.NodeRegistration.KubeletExtraArgs != nil { + bottlerocketConfig.KubeletExtraArgs = scope.Config.Spec.InitConfiguration.NodeRegistration.KubeletExtraArgs + } } clusterdata, err := kubeadmtypes.MarshalClusterConfigurationForVersion(scope.Config.Spec.ClusterConfiguration, parsedVersion) @@ -669,6 +672,9 @@ func (r *KubeadmConfigReconciler) joinWorker(ctx context.Context, scope *Scope) if scope.Config.Spec.JoinConfiguration.RegistryMirror.Endpoint != "" { bottlerocketConfig.RegistryMirrorConfiguration = scope.Config.Spec.JoinConfiguration.RegistryMirror } + if scope.Config.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs != nil { + bottlerocketConfig.KubeletExtraArgs = scope.Config.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs + } bootstrapJoinData, err = bottlerocket.NewNode(nodeInput, bottlerocketConfig) if err != nil { scope.Error(err, "Failed to create a worker bottlerocket join configuration") @@ -788,6 +794,9 @@ func (r *KubeadmConfigReconciler) joinControlplane(ctx context.Context, scope *S if scope.Config.Spec.ClusterConfiguration.RegistryMirror.Endpoint != "" { bottlerocketConfig.RegistryMirrorConfiguration = scope.Config.Spec.ClusterConfiguration.RegistryMirror } + if scope.Config.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs != nil { + bottlerocketConfig.KubeletExtraArgs = scope.Config.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs + } bootstrapJoinData, err = bottlerocket.NewJoinControlPlane(controlPlaneJoinInput, bottlerocketConfig) if err != nil { scope.Error(err, "Failed to generate cloud init for bottlerocket bootstrap control plane") -- 2.40.0