Skip to content

Not possible to represent absence of Taints when marshalling kubeadm configuration #1358

@dlipovetsky

Description

@dlipovetsky

Versions

kubeadm API v1alpha3 and v1beta1 (releases 1.11, 1.12, 1.13)

What happened?

I wanted to use a Go program to generate kubeadm configuration, such that no taints are applied to the node. I imported the kubeadm types, initialized an InitConfiguration struct, and set NodeRegistration.Taints to an empty slice. I then marshalled the struct YAML and wrote this to a configuration file. I observed that NodeRegistration.Taints was not present in the file. When I passed the configuration file to kubeadm init, I observed that kubeadm applied the default set of taints to the node.

What you expected to happen?

The Taints field is explained in the code as follows:

Taints specifies the taints the Node API object should be registered with. If this field is unset, i.e. nil, in the kubeadm init process it will be defaulted to []v1.Taint{'node-role.kubernetes.io/master=""'}. If you don't want to taint your master node, set this field to an empty slice, i.e. taints: {} in the YAML file. This field is solely used for Node registration.

I want to use the kubeadm types in a Go program and set the Taints field to an empty slice. However, when I marshal the struct, the field is omitted (because it is tagged omitempty).

How to reproduce it (as minimally and precisely as possible)?

Here is a Go program that generates InitConfiguration with NodeRegistration.Taints set to an empty slice, then marshals the configuration to YAML, then unmarshals it into a fresh InitConfiguration struct.. Note that, in the fresh configuration struct, NodeRegistration.Taints is nil, not an empty slice.

package main import (	"fmt"	"log"	"k8s.io/api/core/v1"	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"	kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1"	"sigs.k8s.io/yaml" ) func main() {	// Create an InitConfiguration that sets taints to an empty slice	srcConfig := kubeadm.InitConfiguration{	TypeMeta: metav1.TypeMeta{	Kind: "InitConfiguration",	APIVersion: "kubeadm.k8s.io/v1beta1",	},	NodeRegistration: kubeadm.NodeRegistrationOptions{	// empty slice means no taints should be applied	Taints: []v1.Taint{},	},	}	b, err := yaml.Marshal(srcConfig)	if err != nil {	log.Fatal(err)	}	dstConfig := kubeadm.InitConfiguration{}	err = yaml.Unmarshal(b, dstConfig)	if err != nil {	log.Fatal(err)	}	// Taints is not an empty slice	fmt.Println(dstConfig.NodeRegistration.Taints == nil) } 

Output:

true 

Metadata

Metadata

Assignees

Labels

area/UXkind/api-changeCategorizes issue or PR as related to adding, removing, or otherwise changing an APIkind/bugCategorizes issue or PR as related to a bug.priority/important-longtermImportant over the long term, but may not be staffed and/or may need multiple releases to complete.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions