Skip to content

Commit

Permalink
Fix the issue of passing [] taints to kubeadm
Browse files Browse the repository at this point in the history
Cluster-api needs to pass [] taints to Kubeadm so that kubeadm will
not taint the control plane. However, a [] taints will be omitted by
cluster-api during Marshing, thus kubeadm gets a nil instead of [].

For more info: kubernetes-sigs#7149
  • Loading branch information
d8660091 authored and Prow Bot committed Nov 10, 2022
1 parent e8c7576 commit 6c39f58
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions bootstrap/kubeadm/api/v1beta1/kubeadm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -312,6 +313,41 @@ type NodeRegistrationOptions struct {
IgnorePreflightErrors []string `json:"ignorePreflightErrors,omitempty"`
}

// MarshalJSON marshals NodeRegistrationOptions.Taints in a way that an empty slice is preserved.
func (n *NodeRegistrationOptions) MarshalJSON() ([]byte, error) {
// Marshal an empty Taints slice array with omitempty so it's preserved.
if n.Taints != nil && len(n.Taints) == 0 {
return json.Marshal(struct {
Name string `json:"name,omitempty"`
CRISocket string `json:"criSocket,omitempty"`
Taints []corev1.Taint `json:"taints"`
KubeletExtraArgs map[string]string `json:"kubeletExtraArgs,omitempty"`
IgnorePreflightErrors []string `json:"ignorePreflightErrors,omitempty"`
}{
Name: n.Name,
CRISocket: n.CRISocket,
Taints: n.Taints,
KubeletExtraArgs: n.KubeletExtraArgs,
IgnorePreflightErrors: n.IgnorePreflightErrors,
})
}

// If Taints is nil or not empty we can use omitempty.
return json.Marshal(struct {
Name string `json:"name,omitempty"`
CRISocket string `json:"criSocket,omitempty"`
Taints []corev1.Taint `json:"taints,omitempty"`
KubeletExtraArgs map[string]string `json:"kubeletExtraArgs,omitempty"`
IgnorePreflightErrors []string `json:"ignorePreflightErrors,omitempty"`
}{
Name: n.Name,
CRISocket: n.CRISocket,
Taints: n.Taints,
KubeletExtraArgs: n.KubeletExtraArgs,
IgnorePreflightErrors: n.IgnorePreflightErrors,
})
}

// Networking contains elements describing cluster's networking configuration.
type Networking struct {
// ServiceSubnet is the subnet used by k8s services.
Expand Down

0 comments on commit 6c39f58

Please sign in to comment.