1
16
17 package config
18
19 import (
20 "bytes"
21 "fmt"
22 "os"
23 "path/filepath"
24 "testing"
25
26 v1 "k8s.io/api/core/v1"
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 "sigs.k8s.io/yaml"
29
30 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
31 kubeadmapiv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
32 "k8s.io/kubernetes/cmd/kubeadm/app/constants"
33 )
34
35 func TestLoadInitConfigurationFromFile(t *testing.T) {
36 certDir := "/tmp/foo"
37 clusterCfg := []byte(fmt.Sprintf(`
38 apiVersion: %s
39 kind: ClusterConfiguration
40 certificatesDir: %s
41 kubernetesVersion: %s`, kubeadmapiv1.SchemeGroupVersion.String(), certDir, constants.MinimumControlPlaneVersion.String()))
42
43
44 tmpdir, err := os.MkdirTemp("", "")
45 if err != nil {
46 t.Fatalf("Couldn't create tmpdir: %v", err)
47 }
48 defer os.RemoveAll(tmpdir)
49
50
51 var tests = []struct {
52 name string
53 fileContents []byte
54 expectErr bool
55 validate func(*testing.T, *kubeadm.InitConfiguration)
56 }{
57 {
58 name: "v1beta3.partial1",
59 fileContents: cfgFiles["InitConfiguration_v1beta3"],
60 },
61 {
62 name: "v1beta3.partial2",
63 fileContents: bytes.Join([][]byte{
64 cfgFiles["InitConfiguration_v1beta3"],
65 clusterCfg,
66 }, []byte(constants.YAMLDocumentSeparator)),
67 validate: func(t *testing.T, cfg *kubeadm.InitConfiguration) {
68 if cfg.ClusterConfiguration.CertificatesDir != certDir {
69 t.Errorf("CertificatesDir from ClusterConfiguration holds the wrong value, Expected: %v. Actual: %v", certDir, cfg.ClusterConfiguration.CertificatesDir)
70 }
71 },
72 },
73 {
74 name: "v1beta3.full",
75 fileContents: bytes.Join([][]byte{
76 cfgFiles["InitConfiguration_v1beta3"],
77 cfgFiles["ClusterConfiguration_v1beta3"],
78 cfgFiles["Kube-proxy_componentconfig"],
79 cfgFiles["Kubelet_componentconfig"],
80 }, []byte(constants.YAMLDocumentSeparator)),
81 },
82 }
83
84 for _, rt := range tests {
85 t.Run(rt.name, func(t2 *testing.T) {
86 cfgPath := filepath.Join(tmpdir, rt.name)
87 err := os.WriteFile(cfgPath, rt.fileContents, 0644)
88 if err != nil {
89 t.Errorf("Couldn't create file: %v", err)
90 return
91 }
92
93 opts := LoadOrDefaultConfigurationOptions{
94 SkipCRIDetect: true,
95 }
96
97 obj, err := LoadInitConfigurationFromFile(cfgPath, opts)
98 if rt.expectErr {
99 if err == nil {
100 t.Error("Unexpected success")
101 }
102 } else {
103 if err != nil {
104 t.Errorf("Error reading file: %v", err)
105 return
106 }
107
108 if obj == nil {
109 t.Error("Unexpected nil return value")
110 }
111 }
112
113 if rt.validate != nil {
114 rt.validate(t, obj)
115 }
116 })
117 }
118 }
119
120 func TestDefaultTaintsMarshaling(t *testing.T) {
121 tests := []struct {
122 desc string
123 cfg kubeadmapiv1.InitConfiguration
124 expectedTaintCnt int
125 }{
126 {
127 desc: "Uninitialized nodeRegistration field produces expected taints",
128 cfg: kubeadmapiv1.InitConfiguration{
129 TypeMeta: metav1.TypeMeta{
130 APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
131 Kind: constants.InitConfigurationKind,
132 },
133 },
134 expectedTaintCnt: 1,
135 },
136 {
137 desc: "Uninitialized taints field produces expected taints",
138 cfg: kubeadmapiv1.InitConfiguration{
139 TypeMeta: metav1.TypeMeta{
140 APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
141 Kind: constants.InitConfigurationKind,
142 },
143 },
144 expectedTaintCnt: 1,
145 },
146 {
147 desc: "Forsing taints to an empty slice produces no taints",
148 cfg: kubeadmapiv1.InitConfiguration{
149 TypeMeta: metav1.TypeMeta{
150 APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
151 Kind: constants.InitConfigurationKind,
152 },
153 NodeRegistration: kubeadmapiv1.NodeRegistrationOptions{
154 Taints: []v1.Taint{},
155 },
156 },
157 expectedTaintCnt: 0,
158 },
159 {
160 desc: "Custom taints are used",
161 cfg: kubeadmapiv1.InitConfiguration{
162 TypeMeta: metav1.TypeMeta{
163 APIVersion: kubeadmapiv1.SchemeGroupVersion.String(),
164 Kind: constants.InitConfigurationKind,
165 },
166 NodeRegistration: kubeadmapiv1.NodeRegistrationOptions{
167 Taints: []v1.Taint{
168 {Key: "taint1"},
169 {Key: "taint2"},
170 },
171 },
172 },
173 expectedTaintCnt: 2,
174 },
175 }
176
177 for _, tc := range tests {
178 t.Run(tc.desc, func(t *testing.T) {
179 b, err := yaml.Marshal(tc.cfg)
180 if err != nil {
181 t.Fatalf("unexpected error while marshalling to YAML: %v", err)
182 }
183
184 cfg, err := BytesToInitConfiguration(b, true)
185 if err != nil {
186 t.Fatalf("unexpected error of BytesToInitConfiguration: %v\nconfig: %s", err, string(b))
187 }
188
189 if tc.expectedTaintCnt != len(cfg.NodeRegistration.Taints) {
190 t.Fatalf("unexpected taints count\nexpected: %d\ngot: %d\ntaints: %v", tc.expectedTaintCnt, len(cfg.NodeRegistration.Taints), cfg.NodeRegistration.Taints)
191 }
192 })
193 }
194 }
195
View as plain text