github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/cluster-runtime/utils.go (about)

     1  // Copyright © 2022 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 clusterruntime
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"path/filepath"
    21  
    22  	"github.com/sealerio/sealer/common"
    23  	"github.com/sealerio/sealer/pkg/imagedistributor"
    24  	"github.com/sealerio/sealer/pkg/infradriver"
    25  
    26  	"github.com/sirupsen/logrus"
    27  )
    28  
    29  func getWorkerIPList(infraDriver infradriver.InfraDriver) []net.IP {
    30  	masters := make(map[string]bool)
    31  	for _, master := range infraDriver.GetHostIPListByRole(common.MASTER) {
    32  		masters[master.String()] = true
    33  	}
    34  	all := infraDriver.GetHostIPList()
    35  	workers := make([]net.IP, len(all)-len(masters))
    36  
    37  	index := 0
    38  	for _, ip := range all {
    39  		if !masters[ip.String()] {
    40  			workers[index] = ip
    41  			index++
    42  		}
    43  	}
    44  
    45  	return workers
    46  }
    47  
    48  // LoadToRegistry just load container image to local registry
    49  func LoadToRegistry(infraDriver infradriver.InfraDriver, distributor imagedistributor.Distributor) error {
    50  	regConfig := infraDriver.GetClusterRegistry()
    51  	// todo only support load image to local registry at present
    52  	if regConfig.LocalRegistry == nil {
    53  		return nil
    54  	}
    55  
    56  	deployHosts := infraDriver.GetHostIPListByRole(common.MASTER)
    57  	if len(deployHosts) < 1 {
    58  		return fmt.Errorf("local registry host can not be nil")
    59  	}
    60  	master0 := deployHosts[0]
    61  
    62  	logrus.Infof("start to apply with mode(%s)", common.ApplyModeLoadImage)
    63  	if !*regConfig.LocalRegistry.HA {
    64  		deployHosts = []net.IP{master0}
    65  	}
    66  
    67  	if err := distributor.DistributeRegistry(deployHosts, filepath.Join(infraDriver.GetClusterRootfsPath(), "registry")); err != nil {
    68  		return err
    69  	}
    70  
    71  	logrus.Infof("load image success")
    72  	return nil
    73  }
    74  
    75  func CheckNodeSSH(infraDriver infradriver.InfraDriver, clientHosts []net.IP) ([]net.IP, error) {
    76  	var failed []net.IP
    77  	for i := range clientHosts {
    78  		n := clientHosts[i]
    79  		logrus.Debug("checking ssh client of ", n)
    80  		err := infraDriver.CmdAsync(n, nil, "ls >> /dev/null")
    81  		if err != nil {
    82  			failed = append(failed, n)
    83  			logrus.Errorf("failed to connect node %s: %v", n.String(), err)
    84  		}
    85  	}
    86  
    87  	var retErr error
    88  	if len(failed) > 0 {
    89  		retErr = fmt.Errorf("failed to connect node: %v, maybe you have change its sshpasswd, if so, please correct passwd via cmd (kubectl -n kube-system edit cm sealer-clusterfile) or check other errors by yourself", failed)
    90  	}
    91  	return failed, retErr
    92  }