github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/runtime/kubeadm_config_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "testing" 22 23 "github.com/alibaba/sealer/logger" 24 25 "github.com/alibaba/sealer/utils" 26 ) 27 28 const ( 29 testKubeadmConfigYaml = ` 30 apiVersion: kubeadm.k8s.io/v1beta2 31 kind: InitConfiguration 32 localAPIEndpoint: 33 # advertiseAddress: 192.168.2.110 34 bindPort: 6443 35 #nodeRegistration: 36 # criSocket: /var/run/dockershim.sock 37 38 --- 39 apiVersion: kubeadm.k8s.io/v1beta2 40 kind: ClusterConfiguration 41 #kubernetesVersion: v1.19.8 42 #controlPlaneEndpoint: "apiserver.cluster.local:6443" 43 imageRepository: sea.hub:5000/library 44 networking: 45 # dnsDomain: cluster.local 46 podSubnet: 100.64.0.0/10 47 serviceSubnet: 10.96.0.0/22 48 apiServer: 49 certSANs: 50 - 127.0.0.1 51 - apiserver.cluster.local 52 - 192.168.2.110 53 - aliyun-inc.com 54 - 10.0.0.2 55 - 10.103.97.2 56 extraArgs: 57 etcd-servers: https://192.168.2.110:2379 58 feature-gates: TTLAfterFinished=true,EphemeralContainers=true 59 audit-policy-file: "/etc/kubernetes/audit-policy.yml" 60 audit-log-path: "/var/log/kubernetes/audit.log" 61 audit-log-format: json 62 audit-log-maxbackup: '"10"' 63 audit-log-maxsize: '"100"' 64 audit-log-maxage: '"7"' 65 enable-aggregator-routing: '"true"' 66 extraVolumes: 67 - name: "audit" 68 hostPath: "/etc/kubernetes" 69 mountPath: "/etc/kubernetes" 70 pathType: DirectoryOrCreate 71 - name: "audit-log" 72 hostPath: "/var/log/kubernetes" 73 mountPath: "/var/log/kubernetes" 74 pathType: DirectoryOrCreate 75 - name: localtime 76 hostPath: /etc/localtime 77 mountPath: /etc/localtime 78 readOnly: true 79 pathType: File 80 controllerManager: 81 extraArgs: 82 feature-gates: TTLAfterFinished=true,EphemeralContainers=true 83 experimental-cluster-signing-duration: 876000h 84 extraVolumes: 85 - hostPath: /etc/localtime 86 mountPath: /etc/localtime 87 name: localtime 88 readOnly: true 89 pathType: File 90 scheduler: 91 extraArgs: 92 feature-gates: TTLAfterFinished=true,EphemeralContainers=true 93 extraVolumes: 94 - hostPath: /etc/localtime 95 mountPath: /etc/localtime 96 name: localtime 97 readOnly: true 98 pathType: File 99 etcd: 100 local: 101 extraArgs: 102 listen-metrics-urls: http://0.0.0.0:2381 103 104 --- 105 apiVersion: kubeproxy.config.k8s.io/v1alpha1 106 kind: KubeProxyConfiguration 107 mode: "ipvs" 108 ipvs: 109 excludeCIDRs: 110 - "10.103.97.2/32" 111 112 --- 113 apiVersion: kubelet.config.k8s.io/v1beta1 114 kind: KubeletConfiguration 115 authentication: 116 anonymous: 117 enabled: false 118 webhook: 119 cacheTTL: 2m0s 120 enabled: true 121 x509: 122 clientCAFile: /etc/kubernetes/pki/ca.crt 123 authorization: 124 mode: Webhook 125 webhook: 126 cacheAuthorizedTTL: 5m0s 127 cacheUnauthorizedTTL: 30s 128 cgroupDriver: 129 cgroupsPerQOS: true 130 clusterDomain: cluster.local 131 configMapAndSecretChangeDetectionStrategy: Watch 132 containerLogMaxFiles: 5 133 containerLogMaxSize: 10Mi 134 contentType: application/vnd.kubernetes.protobuf 135 cpuCFSQuota: true 136 cpuCFSQuotaPeriod: 100ms 137 cpuManagerPolicy: none 138 cpuManagerReconcilePeriod: 10s 139 enableControllerAttachDetach: true 140 enableDebuggingHandlers: true 141 enforceNodeAllocatable: 142 - pods 143 eventBurst: 10 144 eventRecordQPS: 5 145 evictionHard: 146 imagefs.available: 15% 147 memory.available: 100Mi 148 nodefs.available: 10% 149 nodefs.inodesFree: 5% 150 evictionPressureTransitionPeriod: 5m0s 151 failSwapOn: true 152 fileCheckFrequency: 20s 153 hairpinMode: promiscuous-bridge 154 healthzBindAddress: 127.0.0.1 155 healthzPort: 10248 156 httpCheckFrequency: 20s 157 imageGCHighThresholdPercent: 85 158 imageGCLowThresholdPercent: 80 159 imageMinimumGCAge: 2m0s 160 iptablesDropBit: 15 161 iptablesMasqueradeBit: 14 162 kubeAPIBurst: 10 163 kubeAPIQPS: 5 164 makeIPTablesUtilChains: true 165 maxOpenFiles: 1000000 166 maxPods: 110 167 nodeLeaseDurationSeconds: 40 168 nodeStatusReportFrequency: 10s 169 nodeStatusUpdateFrequency: 10s 170 oomScoreAdj: -999 171 podPidsLimit: -1 172 port: 10250 173 registryBurst: 10 174 registryPullQPS: 5 175 rotateCertificates: true 176 runtimeRequestTimeout: 2m0s 177 serializeImagePulls: true 178 staticPodPath: /etc/kubernetes/manifests 179 streamingConnectionIdleTimeout: 4h0m0s 180 syncFrequency: 1m0s 181 volumeStatsAggPeriod: 1m0s 182 ` 183 testClusterfile = `apiVersion: sealer.cloud/v2 184 kind: KubeadmConfig 185 metadata: 186 name: default-kubernetes-config 187 spec: 188 localAPIEndpoint: 189 advertiseAddress: 192.168.2.110 190 bindPort: 6443 191 nodeRegistration: 192 criSocket: /var/run/dockershim.sock 193 kubernetesVersion: v1.19.8 194 controlPlaneEndpoint: "apiserver.cluster.local:6443" 195 imageRepository: sea.hub:5000/library 196 networking: 197 podSubnet: 100.64.0.0/10 198 serviceSubnet: 10.96.0.0/22 199 apiServer: 200 certSANs: 201 - sealer.cloud 202 - 127.0.0.1 203 - Partial.custom.config 204 clusterDomain: cluster.local 205 nodeLeaseDurationSeconds: 99 206 nodeStatusReportFrequency: 99s 207 nodeStatusUpdateFrequency: 99s 208 --- 209 apiVersion: sealer.cloud/v2 210 kind: Cluster 211 metadata: 212 name: default-kubernetes-cluster 213 spec: 214 image: kubernetes:v1.19.8 215 --- 216 apiVersion: sealer.cloud/v2 217 kind: Infra 218 metadata: 219 name: alicloud 220 spec: 221 provider: ALI_CLOUD 222 ssh: 223 passwd: xxx 224 port: 2222 225 hosts: 226 - count: 3 227 role: [ master ] 228 cpu: 4 229 memory: 4 230 systemDisk: 100 231 dataDisk: [ 100,200 ] 232 - count: 3 233 role: [ node ] 234 cpu: 4 235 memory: 4 236 systemDisk: 100 237 dataDisk: [ 100, 200 ] 238 --- 239 apiVersion: kubelet.config.k8s.io/v1beta1 240 kind: KubeletConfiguration 241 authentication: 242 anonymous: 243 enabled: false 244 webhook: 245 cacheTTL: 2m0s 246 enabled: true 247 x509: 248 clientCAFile: /etc/kubernetes/pki/ca.crt 249 authorization: 250 mode: Webhook 251 webhook: 252 cacheAuthorizedTTL: 5m0s 253 cacheUnauthorizedTTL: 30s 254 cgroupsPerQOS: true 255 clusterDomain: cluster.local 256 configMapAndSecretChangeDetectionStrategy: Watch 257 containerLogMaxFiles: 5 258 containerLogMaxSize: 10Mi 259 contentType: application/vnd.kubernetes.protobuf 260 cpuCFSQuota: true 261 cpuCFSQuotaPeriod: 100ms 262 cpuManagerPolicy: none 263 cpuManagerReconcilePeriod: 10s 264 enableControllerAttachDetach: true 265 enableDebuggingHandlers: true 266 enforceNodeAllocatable: 267 - pods 268 eventBurst: 10 269 eventRecordQPS: 5 270 evictionHard: 271 imagefs.available: 15% 272 memory.available: 100Mi 273 nodefs.available: 10% 274 nodefs.inodesFree: 5% 275 evictionPressureTransitionPeriod: 5m0s 276 failSwapOn: true 277 fileCheckFrequency: 20s 278 hairpinMode: promiscuous-bridge 279 healthzBindAddress: 127.0.0.1 280 healthzPort: 10248 281 httpCheckFrequency: 20s 282 imageGCHighThresholdPercent: 85 283 imageGCLowThresholdPercent: 80 284 imageMinimumGCAge: 2m0s 285 iptablesDropBit: 15 286 iptablesMasqueradeBit: 14 287 kubeAPIBurst: 10 288 kubeAPIQPS: 5 289 makeIPTablesUtilChains: true 290 maxOpenFiles: 1000000 291 maxPods: 110 292 nodeLeaseDurationSeconds: 40 293 nodeStatusReportFrequency: 10s 294 nodeStatusUpdateFrequency: 10s 295 oomScoreAdj: -999 296 podPidsLimit: -1 297 port: 10250 298 registryBurst: 10 299 registryPullQPS: 5 300 rotateCertificates: true 301 runtimeRequestTimeout: 2m0s 302 serializeImagePulls: true 303 staticPodPath: /etc/kubernetes/manifests 304 streamingConnectionIdleTimeout: 4h0m0s 305 syncFrequency: 1m0s 306 volumeStatsAggPeriod: 1m0s 307 --- 308 apiVersion: kubeadm.k8s.io/v1beta2 309 kind: ClusterConfiguration 310 networking: 311 podSubnet: 100.64.0.0/10 312 serviceSubnet: 10.96.0.0/22 313 apiServer: 314 certSANs: 315 - default.raw.config 316 --- 317 apiVersion: kubeadm.k8s.io/v1beta2 318 kind: InitConfiguration 319 localAPIEndpoint: 320 advertiseAddress: 127.0.0.1 321 bindPort: 6443 322 nodeRegistration: 323 criSocket: /var/run/dockershim.sock` 324 ) 325 326 func TestKubeadmConfig_LoadFromClusterfile(t *testing.T) { 327 type fields struct { 328 KubeConfig *KubeadmConfig 329 } 330 type args struct { 331 kubeadmconfig []byte 332 } 333 tests := []struct { 334 name string 335 fields fields 336 args args 337 wantErr bool 338 }{ 339 { 340 name: "test kubeadm config from Clusterfile", 341 fields: fields{&KubeadmConfig{}}, 342 args: args{ 343 []byte(testClusterfile), 344 }, 345 wantErr: false, 346 }, 347 } 348 for _, tt := range tests { 349 t.Run(tt.name, func(t *testing.T) { 350 k := tt.fields.KubeConfig 351 testfile := "test-Clusterfile" 352 err := ioutil.WriteFile(testfile, tt.args.kubeadmconfig, 0644) 353 if err != nil { 354 t.Errorf("WriteFile %s error = %v, wantErr %v", testfile, err, tt.wantErr) 355 } 356 defer func() { 357 err = os.Remove(testfile) 358 if err != nil { 359 t.Errorf("Remove %s error = %v, wantErr %v", testfile, err, tt.wantErr) 360 } 361 }() 362 KubeadmConfig, err := LoadKubeadmConfigs(testfile, DecodeCRDFromFile) 363 if err != nil { 364 t.Errorf("err: %v", err) 365 return 366 } 367 if err := k.LoadFromClusterfile(KubeadmConfig); (err != nil) != tt.wantErr { 368 t.Errorf("LoadFromClusterfile() error = %v, wantErr %v", err, tt.wantErr) 369 } 370 logger.Info("k.InitConfiguration.Kind", k.InitConfiguration.Kind) 371 out, err := utils.MarshalYamlConfigs(k.InitConfiguration, k.ClusterConfiguration, 372 k.JoinConfiguration, k.KubeletConfiguration, k.KubeProxyConfiguration) 373 if (err != nil) != tt.wantErr { 374 t.Errorf("MarshalConfigsToYaml() error = %v, wantErr %v", err, tt.wantErr) 375 } 376 fmt.Println(string(out)) 377 }) 378 } 379 } 380 381 func TestKubeadmConfig_Merge(t *testing.T) { 382 type fields struct { 383 kubeadmConfig *KubeadmConfig 384 } 385 type args struct { 386 defaultKubeadmConfig []byte 387 } 388 tests := []struct { 389 name string 390 fields fields 391 args args 392 want []byte 393 wantErr bool 394 }{ 395 { 396 name: "test kubeadm config merge", 397 fields: fields{&KubeadmConfig{}}, 398 args: args{ 399 []byte(testKubeadmConfigYaml), 400 }, 401 }, 402 } 403 for _, tt := range tests { 404 t.Run(tt.name, func(t *testing.T) { 405 k := tt.fields.kubeadmConfig 406 /* err := k.LoadFromClusterfile("test-kubeConfig.yml") 407 if (err != nil) != tt.wantErr { 408 t.Errorf("LoadFromClusterfile() error = %v, wantErr %v", err, tt.wantErr) 409 return 410 }*/ 411 testfile := "test-kubeadm.yml" 412 err := ioutil.WriteFile(testfile, tt.args.defaultKubeadmConfig, 0644) 413 if (err != nil) != tt.wantErr { 414 t.Errorf("WriteFile %s error = %v, wantErr %v", testfile, err, tt.wantErr) 415 return 416 } 417 defer func() { 418 err = os.Remove(testfile) 419 if err != nil { 420 t.Errorf("remove file %s error = %v, wantErr %v", testfile, err, tt.wantErr) 421 return 422 } 423 }() 424 err = k.Merge(testfile) 425 if (err != nil) != tt.wantErr { 426 t.Errorf("Merge() error = %v, wantErr %v", err, tt.wantErr) 427 return 428 } 429 }) 430 } 431 }