github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/apply/processor/scale.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 processor
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/alibaba/sealer/common"
    21  	"github.com/alibaba/sealer/pkg/clusterfile"
    22  	"github.com/alibaba/sealer/pkg/config"
    23  	"github.com/alibaba/sealer/pkg/plugin"
    24  
    25  	"github.com/alibaba/sealer/pkg/filesystem/cloudfilesystem"
    26  
    27  	"github.com/alibaba/sealer/utils"
    28  
    29  	"github.com/alibaba/sealer/pkg/filesystem"
    30  	"github.com/alibaba/sealer/pkg/runtime"
    31  	v2 "github.com/alibaba/sealer/types/api/v2"
    32  )
    33  
    34  type ScaleProcessor struct {
    35  	fileSystem      cloudfilesystem.Interface
    36  	ClusterFile     clusterfile.Interface
    37  	Runtime         runtime.Interface
    38  	KubeadmConfig   *runtime.KubeadmConfig
    39  	Config          config.Interface
    40  	Plugins         plugin.Plugins
    41  	MastersToJoin   []string
    42  	MastersToDelete []string
    43  	NodesToJoin     []string
    44  	NodesToDelete   []string
    45  	IsScaleUp       bool
    46  }
    47  
    48  func (s *ScaleProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) {
    49  	var todoList []func(cluster *v2.Cluster) error
    50  	if s.IsScaleUp {
    51  		todoList = append(todoList,
    52  			s.PreProcess,
    53  			s.GetPhasePluginFunc(plugin.PhaseOriginally),
    54  			s.RunConfig,
    55  			s.MountRootfs,
    56  			s.GetPhasePluginFunc(plugin.PhasePreJoin),
    57  			s.Join,
    58  			s.GetPhasePluginFunc(plugin.PhasePreGuest), //taint plugin,label plugin, or clusterCheck plugin
    59  			s.GetPhasePluginFunc(plugin.PhasePostJoin),
    60  		)
    61  		return todoList, nil
    62  	}
    63  
    64  	todoList = append(todoList,
    65  		s.PreProcess,
    66  		s.GetPhasePluginFunc(plugin.PhasePreClean),
    67  		s.Delete,
    68  		s.GetPhasePluginFunc(plugin.PhasePostClean),
    69  		s.UnMountRootfs,
    70  	)
    71  	return todoList, nil
    72  }
    73  
    74  func (s *ScaleProcessor) PreProcess(cluster *v2.Cluster) error {
    75  	runTime, err := runtime.NewDefaultRuntime(cluster, s.KubeadmConfig)
    76  	if err != nil {
    77  		return fmt.Errorf("failed to init runtime, %v", err)
    78  	}
    79  	s.Runtime = runTime
    80  	s.Config = config.NewConfiguration(cluster)
    81  	if s.IsScaleUp {
    82  		if err = utils.SaveClusterInfoToFile(cluster, cluster.Name); err != nil {
    83  			return err
    84  		}
    85  	}
    86  	return s.initPlugin(cluster)
    87  }
    88  
    89  func (s *ScaleProcessor) initPlugin(cluster *v2.Cluster) error {
    90  	s.Plugins = plugin.NewPlugins(cluster, s.ClusterFile.GetPlugins())
    91  	return s.Plugins.Load()
    92  }
    93  
    94  func (s *ScaleProcessor) GetPhasePluginFunc(phase plugin.Phase) func(cluster *v2.Cluster) error {
    95  	return func(cluster *v2.Cluster) error {
    96  		if s.IsScaleUp {
    97  			return s.Plugins.Run(append(s.MastersToJoin, s.NodesToJoin...), phase)
    98  		}
    99  		return s.Plugins.Run(append(s.MastersToDelete, s.NodesToDelete...), phase)
   100  	}
   101  }
   102  
   103  func (s *ScaleProcessor) RunConfig(cluster *v2.Cluster) error {
   104  	return s.Config.Dump(s.ClusterFile.GetConfigs())
   105  }
   106  
   107  func (s *ScaleProcessor) MountRootfs(cluster *v2.Cluster) error {
   108  	return s.fileSystem.MountRootfs(cluster, append(s.MastersToJoin, s.NodesToJoin...), true)
   109  }
   110  
   111  func (s *ScaleProcessor) UnMountRootfs(cluster *v2.Cluster) error {
   112  	return s.fileSystem.UnMountRootfs(cluster, append(s.MastersToDelete, s.NodesToDelete...))
   113  }
   114  
   115  func (s *ScaleProcessor) Join(cluster *v2.Cluster) error {
   116  	err := s.Runtime.JoinMasters(s.MastersToJoin)
   117  	if err != nil {
   118  		return err
   119  	}
   120  	return s.Runtime.JoinNodes(s.NodesToJoin)
   121  }
   122  
   123  func (s *ScaleProcessor) Delete(cluster *v2.Cluster) error {
   124  	err := s.Runtime.DeleteMasters(s.MastersToDelete)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	return s.Runtime.DeleteNodes(s.NodesToDelete)
   129  }
   130  
   131  func NewScaleProcessor(kubeadmConfig *runtime.KubeadmConfig, clusterFile clusterfile.Interface, masterToJoin, masterToDelete, nodeToJoin, nodeToDelete []string) (Processor, error) {
   132  	fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(clusterFile.GetCluster().Name))
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	var up bool
   138  	// only scale up or scale down at a time
   139  	if len(masterToJoin) > 0 || len(nodeToJoin) > 0 {
   140  		up = true
   141  	}
   142  
   143  	return &ScaleProcessor{
   144  		MastersToDelete: masterToDelete,
   145  		MastersToJoin:   masterToJoin,
   146  		NodesToDelete:   nodeToDelete,
   147  		NodesToJoin:     nodeToJoin,
   148  		KubeadmConfig:   kubeadmConfig,
   149  		ClusterFile:     clusterFile,
   150  		IsScaleUp:       up,
   151  		fileSystem:      fs,
   152  	}, nil
   153  }