github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/runtime/kubernetes/kubeadm/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 kubeadm
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"github.com/sealerio/sealer/utils"
    25  	"github.com/sealerio/sealer/utils/yaml"
    26  )
    27  
    28  var (
    29  	testClusterfile = `apiVersion: sealer.io/v2
    30  kind: KubeadmConfig
    31  metadata:
    32    name: default-kubernetes-config
    33  spec:
    34    localAPIEndpoint:
    35      advertiseAddress: 192.168.2.110
    36      bindPort: 6443
    37    nodeRegistration:
    38      criSocket: /var/run/dockershim.sock
    39    kubernetesVersion: v1.19.8
    40    controlPlaneEndpoint: "apiserver.cluster.local:6443"
    41    imageRepository: sea.hub:5000/library
    42    networking:
    43      podSubnet: 100.64.0.0/10
    44      serviceSubnet: 10.96.0.0/22
    45    apiServer:
    46      certSANs:
    47        - sealer.cloud
    48        - 127.0.0.1
    49        - Partial.custom.config
    50    clusterDomain: cluster.local
    51    nodeLeaseDurationSeconds: 99
    52    nodeStatusReportFrequency: 99s
    53    nodeStatusUpdateFrequency: 99s
    54  ---
    55  apiVersion: sealer.io/v2
    56  kind: Cluster
    57  metadata:
    58    name: default-kubernetes-cluster
    59  spec:
    60    image: kubernetes:v1.19.8
    61  ---
    62  apiVersion: sealer.io/v2
    63  kind: Infra
    64  metadata:
    65    name: alicloud
    66  spec:
    67    provider: ALI_CLOUD
    68    ssh:
    69      passwd: xxx
    70      port: 2222
    71    hosts:
    72      - count: 3
    73        role: [ master ]
    74        cpu: 4
    75        memory: 4
    76        systemDisk: 100
    77        dataDisk: [ 100,200 ]
    78      - count: 3
    79        role: [ node ]
    80        cpu: 4
    81        memory: 4
    82        systemDisk: 100
    83        dataDisk: [ 100, 200 ]
    84  ---
    85  apiVersion: kubelet.config.k8s.io/v1beta1
    86  kind: KubeletConfiguration
    87  authentication:
    88    anonymous:
    89      enabled: false
    90    webhook:
    91      cacheTTL: 2m0s
    92      enabled: true
    93    x509:
    94      clientCAFile: /etc/kubernetes/pki/ca.crt
    95  authorization:
    96    mode: Webhook
    97    webhook:
    98      cacheAuthorizedTTL: 5m0s
    99      cacheUnauthorizedTTL: 30s
   100  cgroupsPerQOS: true
   101  clusterDomain: cluster.local
   102  configMapAndSecretChangeDetectionStrategy: Watch
   103  containerLogMaxFiles: 5
   104  containerLogMaxSize: 10Mi
   105  contentType: application/vnd.kubernetes.protobuf
   106  cpuCFSQuota: true
   107  cpuCFSQuotaPeriod: 100ms
   108  cpuManagerPolicy: none
   109  cpuManagerReconcilePeriod: 10s
   110  enableControllerAttachDetach: true
   111  enableDebuggingHandlers: true
   112  enforceNodeAllocatable:
   113    - pods
   114  eventBurst: 10
   115  eventRecordQPS: 5
   116  evictionHard:
   117    imagefs.available: 15%
   118    memory.available: 100Mi
   119    nodefs.available: 10%
   120    nodefs.inodesFree: 5%
   121  evictionPressureTransitionPeriod: 5m0s
   122  failSwapOn: true
   123  fileCheckFrequency: 20s
   124  hairpinMode: promiscuous-bridge
   125  healthzBindAddress: 127.0.0.1
   126  healthzPort: 10248
   127  httpCheckFrequency: 20s
   128  imageGCHighThresholdPercent: 85
   129  imageGCLowThresholdPercent: 80
   130  imageMinimumGCAge: 2m0s
   131  iptablesDropBit: 15
   132  iptablesMasqueradeBit: 14
   133  kubeAPIBurst: 10
   134  kubeAPIQPS: 5
   135  makeIPTablesUtilChains: true
   136  maxOpenFiles: 1000000
   137  maxPods: 110
   138  nodeLeaseDurationSeconds: 40
   139  nodeStatusReportFrequency: 10s
   140  nodeStatusUpdateFrequency: 10s
   141  oomScoreAdj: -999
   142  podPidsLimit: -1
   143  port: 10250
   144  registryBurst: 10
   145  registryPullQPS: 5
   146  rotateCertificates: true
   147  runtimeRequestTimeout: 2m0s
   148  serializeImagePulls: true
   149  staticPodPath: /etc/kubernetes/manifests
   150  streamingConnectionIdleTimeout: 4h0m0s
   151  syncFrequency: 1m0s
   152  volumeStatsAggPeriod: 1m0s
   153  ---
   154  apiVersion: kubeadm.k8s.io/v1beta3
   155  kind: ClusterConfiguration
   156  networking:
   157    podSubnet: 100.64.0.0/10
   158    serviceSubnet: 10.96.0.0/22
   159  apiServer:
   160    certSANs:
   161      - default.raw.config
   162  ---
   163  apiVersion: kubeadm.k8s.io/v1beta3
   164  kind: InitConfiguration
   165  localAPIEndpoint:
   166    advertiseAddress: 127.0.0.1 
   167    bindPort: 6443
   168  nodeRegistration:
   169    criSocket: /var/run/dockershim.sock`
   170  )
   171  
   172  func TestKubeadmConfig_LoadFromClusterfile(t *testing.T) {
   173  	type fields struct {
   174  		KubeConfig *KubeadmConfig
   175  	}
   176  	type args struct {
   177  		kubeadmconfig []byte
   178  	}
   179  	tests := []struct {
   180  		name    string
   181  		fields  fields
   182  		args    args
   183  		wantErr bool
   184  	}{
   185  		{
   186  			name:   "test kubeadm config from Clusterfile",
   187  			fields: fields{&KubeadmConfig{}},
   188  			args: args{
   189  				[]byte(testClusterfile),
   190  			},
   191  			wantErr: false,
   192  		},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			k := tt.fields.KubeConfig
   197  			testfile := "test-Clusterfile"
   198  			err := os.WriteFile(testfile, tt.args.kubeadmconfig, 0644)
   199  			if err != nil {
   200  				t.Errorf("WriteFile %s error = %v, wantErr %v", testfile, err, tt.wantErr)
   201  			}
   202  			defer func() {
   203  				err = os.Remove(testfile)
   204  				if err != nil {
   205  					t.Errorf("Remove %s error = %v, wantErr %v", testfile, err, tt.wantErr)
   206  				}
   207  			}()
   208  			KubeadmConfig, err := LoadKubeadmConfigs(testfile, utils.DecodeCRDFromFile)
   209  			if err != nil {
   210  				t.Errorf("err: %v", err)
   211  				return
   212  			}
   213  			if err := k.LoadFromClusterfile(KubeadmConfig); (err != nil) != tt.wantErr {
   214  				t.Errorf("LoadFromClusterfile() error = %v, wantErr %v", err, tt.wantErr)
   215  			}
   216  			logrus.Infof("k.InitConfiguration.Kind: %v", k.InitConfiguration.Kind)
   217  			out, err := yaml.MarshalWithDelimiter(k.InitConfiguration, k.ClusterConfiguration,
   218  				k.JoinConfiguration, k.KubeletConfiguration, k.KubeProxyConfiguration)
   219  			if (err != nil) != tt.wantErr {
   220  				t.Errorf("MarshalConfigsToYaml() error = %v, wantErr %v", err, tt.wantErr)
   221  			}
   222  			fmt.Println(string(out))
   223  		})
   224  	}
   225  }