Skip to content

Commit 0a83ed5

Browse files
authored
Merge pull request kubernetes#77345 from rosti/omitempty-v1beta2
kubeadm: Fix omitempty in v1beta2
2 parents d5245b9 + 81e3adc commit 0a83ed5

File tree

5 files changed

+101
-29
lines changed

5 files changed

+101
-29
lines changed

cmd/kubeadm/app/apis/kubeadm/v1beta2/types.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ type ClusterConfiguration struct {
6565
metav1.TypeMeta `json:",inline"`
6666

6767
// Etcd holds configuration for etcd.
68-
Etcd Etcd `json:"etcd"`
68+
Etcd Etcd `json:"etcd,omitempty"`
6969

7070
// Networking holds configuration for the networking topology of the cluster.
71-
Networking Networking `json:"networking"`
71+
Networking Networking `json:"networking,omitempty"`
7272

7373
// KubernetesVersion is the target version of the control plane.
74-
KubernetesVersion string `json:"kubernetesVersion"`
74+
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
7575

7676
// ControlPlaneEndpoint sets a stable IP address or DNS name for the control plane; it
7777
// can be a valid IP address or a RFC-1123 DNS subdomain, both with optional TCP port.
@@ -84,7 +84,7 @@ type ClusterConfiguration struct {
8484
// control plane instances.
8585
// e.g. in environments with enforced node recycling, the ControlPlaneEndpoint
8686
// could be used for assigning a stable DNS to the control plane.
87-
ControlPlaneEndpoint string `json:"controlPlaneEndpoint"`
87+
ControlPlaneEndpoint string `json:"controlPlaneEndpoint,omitempty"`
8888

8989
// APIServer contains extra settings for the API server control plane component
9090
APIServer APIServer `json:"apiServer,omitempty"`
@@ -96,16 +96,16 @@ type ClusterConfiguration struct {
9696
Scheduler ControlPlaneComponent `json:"scheduler,omitempty"`
9797

9898
// DNS defines the options for the DNS add-on installed in the cluster.
99-
DNS DNS `json:"dns"`
99+
DNS DNS `json:"dns,omitempty"`
100100

101101
// CertificatesDir specifies where to store or look for all required certificates.
102-
CertificatesDir string `json:"certificatesDir"`
102+
CertificatesDir string `json:"certificatesDir,omitempty"`
103103

104104
// ImageRepository sets the container registry to pull images from.
105105
// If empty, `k8s.gcr.io` will be used by default; in case of kubernetes version is a CI build (kubernetes version starts with `ci/` or `ci-cross/`)
106106
// `gcr.io/kubernetes-ci-images` will be used as a default for control plane components and for kube-proxy, while `k8s.gcr.io`
107107
// will be used for all the other images.
108-
ImageRepository string `json:"imageRepository"`
108+
ImageRepository string `json:"imageRepository,omitempty"`
109109

110110
// UseHyperKubeImage controls if hyperkube should be used for Kubernetes components instead of their respective separate images
111111
UseHyperKubeImage bool `json:"useHyperKubeImage,omitempty"`
@@ -188,11 +188,11 @@ type ClusterStatus struct {
188188
// APIEndpoint struct contains elements of API server instance deployed on a node.
189189
type APIEndpoint struct {
190190
// AdvertiseAddress sets the IP address for the API server to advertise.
191-
AdvertiseAddress string `json:"advertiseAddress"`
191+
AdvertiseAddress string `json:"advertiseAddress,omitempty"`
192192

193193
// BindPort sets the secure port for the API Server to bind to.
194194
// Defaults to 6443.
195-
BindPort int32 `json:"bindPort"`
195+
BindPort int32 `json:"bindPort,omitempty"`
196196
}
197197

198198
// NodeRegistrationOptions holds fields that relate to registering a new control-plane or node to the cluster, either via "kubeadm init" or "kubeadm join"
@@ -209,7 +209,7 @@ type NodeRegistrationOptions struct {
209209
// 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
210210
// it will be defaulted to []v1.Taint{'node-role.kubernetes.io/master=""'}. If you don't want to taint your control-plane node, set this field to an
211211
// empty slice, i.e. `taints: {}` in the YAML file. This field is solely used for Node registration.
212-
Taints []v1.Taint `json:"taints,omitempty"`
212+
Taints []v1.Taint `json:"taints"`
213213

214214
// KubeletExtraArgs passes through extra arguments to the kubelet. The arguments here are passed to the kubelet command line via the environment file
215215
// kubeadm writes at runtime for the kubelet to source. This overrides the generic base-level configuration in the kubelet-config-1.X ConfigMap
@@ -220,11 +220,11 @@ type NodeRegistrationOptions struct {
220220
// Networking contains elements describing cluster's networking configuration
221221
type Networking struct {
222222
// ServiceSubnet is the subnet used by k8s services. Defaults to "10.96.0.0/12".
223-
ServiceSubnet string `json:"serviceSubnet"`
223+
ServiceSubnet string `json:"serviceSubnet,omitempty"`
224224
// PodSubnet is the subnet used by pods.
225-
PodSubnet string `json:"podSubnet"`
225+
PodSubnet string `json:"podSubnet,omitempty"`
226226
// DNSDomain is the dns domain used by k8s services. Defaults to "cluster.local".
227-
DNSDomain string `json:"dnsDomain"`
227+
DNSDomain string `json:"dnsDomain,omitempty"`
228228
}
229229

230230
// BootstrapToken describes one bootstrap token, stored as a Secret in the cluster
@@ -306,12 +306,12 @@ type JoinConfiguration struct {
306306
metav1.TypeMeta `json:",inline"`
307307

308308
// NodeRegistration holds fields that relate to registering the new control-plane node to the cluster
309-
NodeRegistration NodeRegistrationOptions `json:"nodeRegistration"`
309+
NodeRegistration NodeRegistrationOptions `json:"nodeRegistration,omitempty"`
310310

311311
// CACertPath is the path to the SSL certificate authority used to
312312
// secure comunications between node and control-plane.
313313
// Defaults to "/etc/kubernetes/pki/ca.crt".
314-
CACertPath string `json:"caCertPath"`
314+
CACertPath string `json:"caCertPath,omitempty"`
315315

316316
// Discovery specifies the options for the kubelet to use during the TLS Bootstrap process
317317
Discovery Discovery `json:"discovery"`
@@ -344,7 +344,7 @@ type Discovery struct {
344344
// TLSBootstrapToken is a token used for TLS bootstrapping.
345345
// If .BootstrapToken is set, this field is defaulted to .BootstrapToken.Token, but can be overridden.
346346
// If .File is set, this field **must be set** in case the KubeConfigFile does not contain any other authentication information
347-
TLSBootstrapToken string `json:"tlsBootstrapToken"`
347+
TLSBootstrapToken string `json:"tlsBootstrapToken,omitempty"`
348348

349349
// Timeout modifies the discovery timeout
350350
Timeout *metav1.Duration `json:"timeout,omitempty"`
@@ -372,7 +372,7 @@ type BootstrapTokenDiscovery struct {
372372
// UnsafeSkipCAVerification allows token-based discovery
373373
// without CA verification via CACertHashes. This can weaken
374374
// the security of kubeadm since other nodes can impersonate the control-plane.
375-
UnsafeSkipCAVerification bool `json:"unsafeSkipCAVerification"`
375+
UnsafeSkipCAVerification bool `json:"unsafeSkipCAVerification,omitempty"`
376376
}
377377

378378
// FileDiscovery is used to specify a file or URL to a kubeconfig file from which to load cluster information

cmd/kubeadm/app/cmd/upgrade/common_test.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,15 @@ func TestPrintConfiguration(t *testing.T) {
180180
expectedBytes: []byte(`[upgrade/config] Configuration used:
181181
apiServer: {}
182182
apiVersion: kubeadm.k8s.io/v1beta2
183-
certificatesDir: ""
184-
controlPlaneEndpoint: ""
185183
controllerManager: {}
186184
dns:
187185
type: CoreDNS
188186
etcd:
189187
local:
190188
dataDir: /some/path
191-
imageRepository: ""
192189
kind: ClusterConfiguration
193190
kubernetesVersion: v1.7.1
194-
networking:
195-
dnsDomain: ""
196-
podSubnet: ""
197-
serviceSubnet: ""
191+
networking: {}
198192
scheduler: {}
199193
`),
200194
},
@@ -217,8 +211,6 @@ func TestPrintConfiguration(t *testing.T) {
217211
expectedBytes: []byte(`[upgrade/config] Configuration used:
218212
apiServer: {}
219213
apiVersion: kubeadm.k8s.io/v1beta2
220-
certificatesDir: ""
221-
controlPlaneEndpoint: ""
222214
controllerManager: {}
223215
dns:
224216
type: CoreDNS
@@ -229,12 +221,9 @@ func TestPrintConfiguration(t *testing.T) {
229221
endpoints:
230222
- https://one-etcd-instance:2379
231223
keyFile: ""
232-
imageRepository: ""
233224
kind: ClusterConfiguration
234225
kubernetesVersion: v1.7.1
235226
networking:
236-
dnsDomain: ""
237-
podSubnet: ""
238227
serviceSubnet: 10.96.0.1/12
239228
scheduler: {}
240229
`),

cmd/kubeadm/app/util/config/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ go_test(
6868
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
6969
"//vendor/github.com/lithammer/dedent:go_default_library",
7070
"//vendor/github.com/pmezard/go-difflib/difflib:go_default_library",
71+
"//vendor/sigs.k8s.io/yaml:go_default_library",
7172
],
7273
)
7374

cmd/kubeadm/app/util/config/initconfiguration_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import (
2626

2727
"github.com/pmezard/go-difflib/difflib"
2828

29+
"k8s.io/api/core/v1"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
2932
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
33+
"sigs.k8s.io/yaml"
3034
)
3135

3236
func diff(expected, actual []byte) string {
@@ -283,3 +287,80 @@ apiVersion: foo.k8s.io/v1
283287
})
284288
}
285289
}
290+
291+
func TestDefaultTaintsMarshaling(t *testing.T) {
292+
tests := []struct {
293+
desc string
294+
cfg kubeadmapiv1beta2.InitConfiguration
295+
expectedTaintCnt int
296+
}{
297+
{
298+
desc: "Uninitialized nodeRegistration field produces a single taint (the master one)",
299+
cfg: kubeadmapiv1beta2.InitConfiguration{
300+
TypeMeta: metav1.TypeMeta{
301+
APIVersion: "kubeadm.k8s.io/v1beta2",
302+
Kind: constants.InitConfigurationKind,
303+
},
304+
},
305+
expectedTaintCnt: 1,
306+
},
307+
{
308+
desc: "Uninitialized taints field produces a single taint (the master one)",
309+
cfg: kubeadmapiv1beta2.InitConfiguration{
310+
TypeMeta: metav1.TypeMeta{
311+
APIVersion: "kubeadm.k8s.io/v1beta2",
312+
Kind: constants.InitConfigurationKind,
313+
},
314+
NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{},
315+
},
316+
expectedTaintCnt: 1,
317+
},
318+
{
319+
desc: "Forsing taints to an empty slice produces no taints",
320+
cfg: kubeadmapiv1beta2.InitConfiguration{
321+
TypeMeta: metav1.TypeMeta{
322+
APIVersion: "kubeadm.k8s.io/v1beta2",
323+
Kind: constants.InitConfigurationKind,
324+
},
325+
NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
326+
Taints: []v1.Taint{},
327+
},
328+
},
329+
expectedTaintCnt: 0,
330+
},
331+
{
332+
desc: "Custom taints are used",
333+
cfg: kubeadmapiv1beta2.InitConfiguration{
334+
TypeMeta: metav1.TypeMeta{
335+
APIVersion: "kubeadm.k8s.io/v1beta2",
336+
Kind: constants.InitConfigurationKind,
337+
},
338+
NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
339+
Taints: []v1.Taint{
340+
{Key: "taint1"},
341+
{Key: "taint2"},
342+
},
343+
},
344+
},
345+
expectedTaintCnt: 2,
346+
},
347+
}
348+
349+
for _, tc := range tests {
350+
t.Run(tc.desc, func(t *testing.T) {
351+
b, err := yaml.Marshal(tc.cfg)
352+
if err != nil {
353+
t.Fatalf("unexpected error while marshalling to YAML: %v", err)
354+
}
355+
356+
cfg, err := BytesToInitConfiguration(b)
357+
if err != nil {
358+
t.Fatalf("unexpected error of BytesToInitConfiguration: %v\nconfig: %s", err, string(b))
359+
}
360+
361+
if tc.expectedTaintCnt != len(cfg.NodeRegistration.Taints) {
362+
t.Fatalf("unexpected taints count\nexpected: %d\ngot: %d\ntaints: %v", tc.expectedTaintCnt, len(cfg.NodeRegistration.Taints), cfg.NodeRegistration.Taints)
363+
}
364+
})
365+
}
366+
}

cmd/kubeadm/app/util/config/testdata/defaulting/node/defaulted.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ kind: JoinConfiguration
1111
nodeRegistration:
1212
criSocket: /var/run/dockershim.sock
1313
name: thegopher
14+
taints: null

0 commit comments

Comments
 (0)