github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/tasks/dependent.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 tasks
    21  
    22  import (
    23  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/bootstrap/confirm"
    24  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/bootstrap/os"
    25  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/common"
    26  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/container"
    27  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/container/templates"
    28  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/connector"
    29  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/logger"
    30  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/prepare"
    31  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/core/task"
    32  	"github.com/kubesphere/kubekey/v3/cmd/kk/pkg/kubernetes"
    33  	"github.com/mitchellh/mapstructure"
    34  
    35  	"github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/builder"
    36  	"github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/constant"
    37  	"github.com/1aal/kubeblocks/pkg/gotemplate"
    38  )
    39  
    40  type InstallDependenciesModule struct {
    41  	common.KubeModule
    42  }
    43  
    44  func (i *InstallDependenciesModule) isNotReadyHosts() []connector.Host {
    45  	hosts := make([]connector.Host, 0)
    46  	for _, host := range i.Runtime.GetAllHosts() {
    47  		var result confirm.PreCheckResults
    48  		if v, ok := host.GetCache().Get(common.NodePreCheck); ok {
    49  			_ = mapstructure.Decode(v, &result)
    50  			if result.Socat == "" ||
    51  				result.Conntrack == "" ||
    52  				result.Curl == "" ||
    53  				result.Ipset == "" ||
    54  				result.Chronyd == "" ||
    55  				result.Ipvsadm == "" ||
    56  				result.Ebtables == "" {
    57  				hosts = append(hosts, host)
    58  			}
    59  		}
    60  	}
    61  	return hosts
    62  }
    63  
    64  // Init install dependencies module
    65  func (i *InstallDependenciesModule) Init() {
    66  	i.Name = "InstallDependenciesModule"
    67  	i.Desc = "install dependencies"
    68  
    69  	hosts := i.isNotReadyHosts()
    70  	if len(hosts) == 0 {
    71  		logger.Log.Info("All hosts are ready, skip install dependencies")
    72  		return
    73  	}
    74  
    75  	i.Tasks = []task.Interface{
    76  		&task.RemoteTask{
    77  			Name:     "GetOSData",
    78  			Desc:     "Get OS release",
    79  			Hosts:    i.Runtime.GetAllHosts(),
    80  			Action:   new(os.GetOSData),
    81  			Parallel: true,
    82  		},
    83  		&task.RemoteTask{
    84  			Name:     "InstallDependenciesModule",
    85  			Desc:     "check and install dependencies",
    86  			Hosts:    hosts,
    87  			Action:   new(InstallDependenciesTask),
    88  			Parallel: true,
    89  		},
    90  	}
    91  }
    92  
    93  type CheckNodeArchitectureModule struct {
    94  	common.KubeModule
    95  }
    96  
    97  // Init install dependencies module
    98  func (i *CheckNodeArchitectureModule) Init() {
    99  	i.Name = "CheckNodeArch"
   100  	i.Desc = "check and update host arch"
   101  	i.Tasks = []task.Interface{
   102  		&task.RemoteTask{
   103  			Name:     "CheckNodeArch",
   104  			Desc:     "check and update node arch",
   105  			Hosts:    i.Runtime.GetAllHosts(),
   106  			Action:   new(UpdateNodeTask),
   107  			Parallel: true,
   108  		},
   109  	}
   110  }
   111  
   112  type InstallCRIModule struct {
   113  	common.KubeModule
   114  	SandBoxImage string
   115  }
   116  
   117  func (i *InstallCRIModule) Init() {
   118  	i.Name = "InstallContainerModule"
   119  	i.Desc = "Install container manager"
   120  
   121  	syncContainerd := &task.RemoteTask{
   122  		Name:  "SyncContainerd",
   123  		Desc:  "Sync containerd binaries",
   124  		Hosts: i.Runtime.GetHostsByRole(common.K8s),
   125  		Prepare: &prepare.PrepareCollection{
   126  			&kubernetes.NodeInCluster{Not: true},
   127  			&container.ContainerdExist{Not: true},
   128  		},
   129  		Action:   new(container.SyncContainerd),
   130  		Parallel: true,
   131  		Retry:    2,
   132  	}
   133  
   134  	syncCrictlBinaries := &task.RemoteTask{
   135  		Name:  "SyncCrictlBinaries",
   136  		Desc:  "Sync crictl binaries",
   137  		Hosts: i.Runtime.GetHostsByRole(common.K8s),
   138  		Prepare: &prepare.PrepareCollection{
   139  			&kubernetes.NodeInCluster{Not: true},
   140  			&container.CrictlExist{Not: true},
   141  		},
   142  		Action:   new(container.SyncCrictlBinaries),
   143  		Parallel: true,
   144  		Retry:    2,
   145  	}
   146  
   147  	generateContainerdService := &task.RemoteTask{
   148  		Name:  "GenerateContainerdService",
   149  		Desc:  "Generate containerd service",
   150  		Hosts: i.Runtime.GetHostsByRole(common.K8s),
   151  		Prepare: &prepare.PrepareCollection{
   152  			&kubernetes.NodeInCluster{Not: true},
   153  			&container.ContainerdExist{Not: true},
   154  		},
   155  		Action: &builder.Template{
   156  			Template: constant.ContainerdService,
   157  			Dst:      constant.ContainerdServiceInstallPath,
   158  		},
   159  		Parallel: true,
   160  	}
   161  
   162  	generateContainerdConfig := &task.RemoteTask{
   163  		Name:  "GenerateContainerdConfig",
   164  		Desc:  "Generate containerd config",
   165  		Hosts: i.Runtime.GetHostsByRole(common.K8s),
   166  		Prepare: &prepare.PrepareCollection{
   167  			&kubernetes.NodeInCluster{Not: true},
   168  			&container.ContainerdExist{Not: true},
   169  		},
   170  		Action: &builder.Template{
   171  			Template: constant.ContainerdConfig,
   172  			Dst:      constant.ContainerdConfigInstallPath,
   173  			Values: gotemplate.TplValues{
   174  				"SandBoxImage": i.SandBoxImage,
   175  				"DataRoot":     templates.DataRoot(i.KubeConf),
   176  			}},
   177  		Parallel: true,
   178  	}
   179  
   180  	generateCrictlConfig := &task.RemoteTask{
   181  		Name:  "GenerateCrictlConfig",
   182  		Desc:  "Generate crictl config",
   183  		Hosts: i.Runtime.GetHostsByRole(common.K8s),
   184  		Prepare: &prepare.PrepareCollection{
   185  			&kubernetes.NodeInCluster{Not: true},
   186  			&container.ContainerdExist{Not: true},
   187  		},
   188  		Action: &builder.Template{
   189  			Template: constant.CRICtlConfig,
   190  			Dst:      constant.CRICtlConfigInstallPath,
   191  			Values: gotemplate.TplValues{
   192  				"Endpoint": i.KubeConf.Cluster.Kubernetes.ContainerRuntimeEndpoint,
   193  			}},
   194  		Parallel: true,
   195  	}
   196  
   197  	enableContainerd := &task.RemoteTask{
   198  		Name:  "EnableContainerd",
   199  		Desc:  "Enable containerd",
   200  		Hosts: i.Runtime.GetHostsByRole(common.K8s),
   201  		Prepare: &prepare.PrepareCollection{
   202  			&kubernetes.NodeInCluster{Not: true},
   203  			&container.ContainerdExist{Not: true},
   204  		},
   205  		Action:   new(container.EnableContainerd),
   206  		Parallel: true,
   207  	}
   208  
   209  	i.Tasks = []task.Interface{
   210  		syncContainerd,
   211  		syncCrictlBinaries,
   212  		generateContainerdService,
   213  		generateContainerdConfig,
   214  		generateCrictlConfig,
   215  		enableContainerd,
   216  	}
   217  }