github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/create.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package infrastructure
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  
    26  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
    27  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
    28  	"github.com/spf13/cobra"
    29  	versionutil "k8s.io/apimachinery/pkg/util/version"
    30  	"k8s.io/cli-runtime/pkg/genericiooptions"
    31  	"k8s.io/kubectl/pkg/util/templates"
    32  
    33  	"github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/constant"
    34  	"github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/tasks"
    35  	"github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/types"
    36  	"github.com/1aal/kubeblocks/pkg/cli/util"
    37  	"github.com/1aal/kubeblocks/pkg/configuration/container"
    38  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    39  )
    40  
    41  type createOptions struct {
    42  	clusterOptions
    43  	version types.InfraVersionInfo
    44  
    45  	criType      string
    46  	debug        bool
    47  	sandBoxImage string
    48  
    49  	securityEnhancement bool
    50  	outputKubeconfig    string
    51  }
    52  
    53  var createExamples = templates.Examples(`
    54  	# Create kubernetes cluster with specified config yaml
    55  	kbcli infra create -c cluster.yaml
    56  
    57      # example cluster.yaml
    58  	cat cluster.yaml
    59  metadata:
    60    name: kb-k8s-test-cluster
    61  user:
    62    name: user1
    63    privateKeyPath: ~/.ssh/test.pem
    64  nodes:
    65    - name: kb-infra-node-0
    66      address: 1.1.1.1
    67      internalAddress: 10.128.0.19
    68    - name: kb-infra-node-1
    69      address: 1.1.1.2
    70      internalAddress: 10.128.0.20
    71    - name: kb-infra-node-2
    72      address: 1.1.1.3
    73      internalAddress: 10.128.0.21
    74      options:
    75        hugePageFeature:
    76          hugePageSize: 10GB
    77  roleGroup:
    78    etcd:
    79      - kb-infra-node-0
    80      - kb-infra-node-1
    81      - kb-infra-node-2
    82    master:
    83      - kb-infra-node-0
    84    worker:
    85      - kb-infra-node-1
    86      - kb-infra-node-2
    87  
    88  kubernetes:
    89    containerManager: containerd
    90    # apis/kubeadm/types.Networking
    91    networking:
    92      plugin: cilium
    93      dnsDomain: cluster.local
    94      podSubnet: 10.233.64.0/18
    95      serviceSubnet: 10.233.0.0/18
    96    controlPlaneEndpoint:
    97      domain: lb.kubeblocks.local
    98      port: 6443
    99    cri:
   100      containerRuntimeType: "containerd"
   101      containerRuntimeEndpoint: "unix:///run/containerd/containerd.sock"
   102      sandBoxImage: "k8s.gcr.io/pause:3.8"
   103  addons:
   104    - name: openebs
   105      namespace: kube-blocks
   106      sources:
   107        chart:
   108          name: openebs
   109          version: 3.7.0
   110          repo: https://openebs.github.io/charts
   111          options:
   112            values:
   113              - "localprovisioner.basePath=/mnt/disks"
   114              - "localprovisioner.hostpathClass.isDefaultClass=true"
   115  `)
   116  
   117  func (o *createOptions) Run() error {
   118  	const minKubernetesVersion = "v1.24.0"
   119  
   120  	v, err := versionutil.ParseSemantic(o.version.KubernetesVersion)
   121  	if err != nil {
   122  		return err
   123  	}
   124  	c, err := v.Compare(minKubernetesVersion)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	if c < 0 {
   129  		return cfgcore.MakeError("kubernetes version must be greater than %s", minKubernetesVersion)
   130  	}
   131  
   132  	o.Cluster.Kubernetes.AutoDefaultFill()
   133  	o.version = o.Version
   134  	o.checkAndSetDefaultVersion()
   135  	cluster, err := createClusterWithOptions(buildTemplateParams(o))
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	yes, err := o.confirm(fmt.Sprintf("install kubernetes using version: %v", o.version.KubernetesVersion))
   141  	if err != nil {
   142  		return err
   143  	}
   144  	if !yes {
   145  		return nil
   146  	}
   147  
   148  	runtime := &common.KubeRuntime{
   149  		BaseRuntime: connector.NewBaseRuntime(o.clusterName, connector.NewDialer(), o.debug, false),
   150  		Cluster:     cluster,
   151  		ClusterName: o.clusterName,
   152  	}
   153  
   154  	syncClusterNodeRole(cluster, runtime)
   155  	checkAndUpdateZone()
   156  	pipelineRunner := tasks.NewPipelineRunner("CreateCluster", NewCreatePipeline(o), runtime)
   157  	if err := pipelineRunner.Do(o.IOStreams.Out); err != nil {
   158  		return err
   159  	}
   160  	fmt.Fprintf(o.IOStreams.Out, "Kubernetes Installation is complete.\n\n")
   161  	return nil
   162  }
   163  
   164  func checkAndUpdateZone() {
   165  	const ZoneName = "KKZONE"
   166  	if location, _ := util.GetIPLocation(); location == "CN" {
   167  		os.Setenv(ZoneName, "cn")
   168  	}
   169  	fmt.Printf("current zone: %s\n", os.Getenv(ZoneName))
   170  }
   171  
   172  func NewCreateKubernetesCmd(streams genericiooptions.IOStreams) *cobra.Command {
   173  	o := &createOptions{
   174  		clusterOptions: clusterOptions{
   175  			IOStreams: streams,
   176  		}}
   177  	o.checkAndSetDefaultVersion()
   178  	cmd := &cobra.Command{
   179  		Use:     "create",
   180  		Short:   "create kubernetes cluster.",
   181  		Example: createExamples,
   182  		Run: func(cmd *cobra.Command, args []string) {
   183  			util.CheckErr(o.Complete())
   184  			util.CheckErr(o.Validate())
   185  			util.CheckErr(o.Run())
   186  		},
   187  	}
   188  	o.buildCreateInfraFlags(cmd)
   189  	o.Version = o.version
   190  	return cmd
   191  }
   192  
   193  func (o *createOptions) buildCreateInfraFlags(cmd *cobra.Command) {
   194  	buildCommonFlags(cmd, &o.clusterOptions)
   195  	cmd.Flags().StringVarP(&o.version.KubernetesVersion, "version", "", o.version.KubernetesVersion, fmt.Sprintf("Specify install kubernetes version. default version is %s", o.version.KubernetesVersion))
   196  	cmd.Flags().StringVarP(&o.sandBoxImage, "sandbox-image", "", constant.DefaultSandBoxImage, "Specified sandbox-image will not be used by the cri. [option]")
   197  	cmd.Flags().StringVarP(&o.criType, "container-runtime", "", string(container.ContainerdType), "Specify kubernetes container runtime. default is containerd")
   198  	cmd.Flags().BoolVarP(&o.debug, "debug", "", false, "set debug mode")
   199  	cmd.Flags().StringVarP(&o.outputKubeconfig, "output-kubeconfig", "", tasks.GetDefaultConfig(), "Specified output kubeconfig. [option]")
   200  }
   201  
   202  func (o *createOptions) checkAndSetDefaultVersion() {
   203  	if o.version.KubernetesVersion == "" {
   204  		o.version.KubernetesVersion = constant.DefaultK8sVersion
   205  	}
   206  	if o.version.EtcdVersion == "" {
   207  		o.version.EtcdVersion = constant.DefaultEtcdVersion
   208  	}
   209  	if o.version.ContainerVersion == "" {
   210  		o.version.ContainerVersion = constant.DefaultContainerdVersion
   211  	}
   212  	if o.version.HelmVersion == "" {
   213  		o.version.HelmVersion = constant.DefaultHelmVersion
   214  	}
   215  	if o.version.CRICtlVersion == "" {
   216  		o.version.CRICtlVersion = constant.DefaultCRICtlVersion
   217  	}
   218  	if o.version.CniVersion == "" {
   219  		o.version.CniVersion = constant.DefaultCniVersion
   220  	}
   221  	if o.version.RuncVersion == "" {
   222  		o.version.RuncVersion = constant.DefaultRuncVersion
   223  	}
   224  }