github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/runtime/runtime.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  	"sync"
    20  
    21  	"github.com/alibaba/sealer/logger"
    22  	"github.com/alibaba/sealer/utils"
    23  
    24  	v2 "github.com/alibaba/sealer/types/api/v2"
    25  )
    26  
    27  type Interface interface {
    28  	// Init exec kubeadm init
    29  	Init(cluster *v2.Cluster) error
    30  	Upgrade() error
    31  	Reset() error
    32  	JoinMasters(newMastersIPList []string) error
    33  	JoinNodes(newNodesIPList []string) error
    34  	DeleteMasters(mastersIPList []string) error
    35  	DeleteNodes(nodesIPList []string) error
    36  	GetClusterMetadata() (*Metadata, error)
    37  	UpdateCert(certs []string) error
    38  }
    39  
    40  type Metadata struct {
    41  	Version string `json:"version"`
    42  	Arch    string `json:"arch"`
    43  	Variant string `json:"variant"`
    44  	//KubeVersion is a SemVer constraint specifying the version of Kubernetes required.
    45  	KubeVersion string `json:"kubeVersion"`
    46  	NydusFlag   bool   `json:"NydusFlag"`
    47  }
    48  
    49  type KubeadmRuntime struct {
    50  	*sync.Mutex
    51  	*v2.Cluster
    52  	*KubeadmConfig
    53  	*Config
    54  }
    55  
    56  var ForceDelete bool
    57  
    58  func (k *KubeadmRuntime) Init(cluster *v2.Cluster) error {
    59  	return k.init(cluster)
    60  }
    61  
    62  func (k *KubeadmRuntime) Upgrade() error {
    63  	return k.upgrade()
    64  }
    65  
    66  func (k *KubeadmRuntime) Reset() error {
    67  	logger.Info("Start to delete cluster: master %s, node %s", k.Cluster.GetMasterIPList(), k.Cluster.GetNodeIPList())
    68  	if err := k.confirmDeleteNodes(); err != nil {
    69  		return err
    70  	}
    71  	return k.reset()
    72  }
    73  
    74  func (k *KubeadmRuntime) JoinMasters(newMastersIPList []string) error {
    75  	if len(newMastersIPList) != 0 {
    76  		logger.Info("%s will be added as master", newMastersIPList)
    77  	}
    78  	return k.joinMasters(newMastersIPList)
    79  }
    80  
    81  func (k *KubeadmRuntime) JoinNodes(newNodesIPList []string) error {
    82  	if len(newNodesIPList) != 0 {
    83  		logger.Info("%s will be added as worker", newNodesIPList)
    84  	}
    85  	return k.joinNodes(newNodesIPList)
    86  }
    87  
    88  func (k *KubeadmRuntime) DeleteMasters(mastersIPList []string) error {
    89  	if len(mastersIPList) != 0 {
    90  		logger.Info("master %s will be deleted", mastersIPList)
    91  		if err := k.confirmDeleteNodes(); err != nil {
    92  			return err
    93  		}
    94  	}
    95  	return k.deleteMasters(mastersIPList)
    96  }
    97  
    98  func (k *KubeadmRuntime) DeleteNodes(nodesIPList []string) error {
    99  	if len(nodesIPList) != 0 {
   100  		logger.Info("worker %s will be deleted", nodesIPList)
   101  		if err := k.confirmDeleteNodes(); err != nil {
   102  			return err
   103  		}
   104  	}
   105  	return k.deleteNodes(nodesIPList)
   106  }
   107  
   108  func (k *KubeadmRuntime) confirmDeleteNodes() error {
   109  	if !ForceDelete {
   110  		if pass, err := utils.ConfirmOperation("Are you sure to delete these nodes? "); err != nil {
   111  			return err
   112  		} else if !pass {
   113  			return fmt.Errorf("exit the operation of delete these nodes")
   114  		}
   115  	}
   116  	return nil
   117  }
   118  
   119  func (k *KubeadmRuntime) GetClusterMetadata() (*Metadata, error) {
   120  	return k.getClusterMetadata()
   121  }
   122  
   123  func (k *KubeadmRuntime) UpdateCert(certs []string) error {
   124  	return k.updateCert(certs)
   125  }
   126  
   127  // NewDefaultRuntime arg "clusterfileKubeConfig" is the Clusterfile path/name, runtime need read kubeadm config from it
   128  // Mount image is required before new Runtime.
   129  func NewDefaultRuntime(cluster *v2.Cluster, clusterfileKubeConfig *KubeadmConfig) (Interface, error) {
   130  	return newKubeadmRuntime(cluster, clusterfileKubeConfig)
   131  }