github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/apply/processor/delete.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/pkg/clusterfile"
    21  
    22  	"github.com/alibaba/sealer/pkg/filesystem/cloudfilesystem"
    23  	"github.com/alibaba/sealer/pkg/filesystem/cloudimage"
    24  
    25  	"github.com/alibaba/sealer/utils"
    26  
    27  	"github.com/alibaba/sealer/pkg/plugin"
    28  
    29  	"github.com/alibaba/sealer/common"
    30  
    31  	"github.com/alibaba/sealer/pkg/filesystem"
    32  	"github.com/alibaba/sealer/pkg/runtime"
    33  	v2 "github.com/alibaba/sealer/types/api/v2"
    34  )
    35  
    36  type DeleteProcessor struct {
    37  	cloudImageMounter cloudimage.Interface
    38  	ClusterFile       clusterfile.Interface
    39  	Plugins           plugin.Plugins
    40  }
    41  
    42  func (d *DeleteProcessor) Reset(cluster *v2.Cluster) error {
    43  	runTime, err := runtime.NewDefaultRuntime(cluster, d.ClusterFile.GetKubeadmConfig())
    44  	if err != nil {
    45  		return fmt.Errorf("failed to init runtime, %v", err)
    46  	}
    47  
    48  	return runTime.Reset()
    49  }
    50  
    51  func (d *DeleteProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) {
    52  	var todoList []func(cluster *v2.Cluster) error
    53  	todoList = append(todoList,
    54  		d.InitPlugin,
    55  		d.GetPhasePluginFunc(plugin.PhasePreClean),
    56  		d.Reset,
    57  		d.GetPhasePluginFunc(plugin.PhasePostClean),
    58  		d.UnMountRootfs,
    59  		d.UnMountImage,
    60  		d.CleanFS,
    61  	)
    62  	return todoList, nil
    63  }
    64  
    65  func (d *DeleteProcessor) GetPhasePluginFunc(phase plugin.Phase) func(cluster *v2.Cluster) error {
    66  	return func(cluster *v2.Cluster) error {
    67  		return d.Plugins.Run(cluster.GetAllIPList(), phase)
    68  	}
    69  }
    70  
    71  func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error {
    72  	hosts := append(cluster.GetMasterIPList(), cluster.GetNodeIPList()...)
    73  	config := runtime.GetRegistryConfig(common.DefaultTheClusterRootfsDir(cluster.Name), runtime.GetMaster0Ip(cluster))
    74  	if utils.NotIn(config.IP, hosts) {
    75  		hosts = append(hosts, config.IP)
    76  	}
    77  	fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(cluster.Name))
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	return fs.UnMountRootfs(cluster, hosts)
    83  }
    84  
    85  func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error {
    86  	return d.cloudImageMounter.UnMountImage(cluster)
    87  }
    88  
    89  func (d *DeleteProcessor) InitPlugin(cluster *v2.Cluster) error {
    90  	d.Plugins = plugin.NewPlugins(cluster, d.ClusterFile.GetPlugins())
    91  	return d.Plugins.Load()
    92  }
    93  
    94  func (d *DeleteProcessor) CleanFS(cluster *v2.Cluster) error {
    95  	return cloudfilesystem.CleanFilesystem(cluster.Name)
    96  }
    97  
    98  func NewDeleteProcessor(clusterFile clusterfile.Interface) (Processor, error) {
    99  	mounter, err := filesystem.NewCloudImageMounter()
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	return &DeleteProcessor{
   105  		ClusterFile:       clusterFile,
   106  		cloudImageMounter: mounter,
   107  	}, nil
   108  }