github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/cluster-runtime/uninstall.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  	"net"
    19  
    20  	"github.com/sealerio/sealer/common"
    21  	containerruntime "github.com/sealerio/sealer/pkg/container-runtime"
    22  	"github.com/sealerio/sealer/pkg/registry"
    23  )
    24  
    25  func (i *Installer) UnInstall() error {
    26  	masters := i.infraDriver.GetHostIPListByRole(common.MASTER)
    27  	master0 := masters[0]
    28  	workers := getWorkerIPList(i.infraDriver)
    29  	all := append(masters, workers...)
    30  
    31  	_, err := CheckNodeSSH(i.infraDriver, all)
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	if err := i.runClusterHook(master0, PreUnInstallCluster); err != nil {
    37  		return err
    38  	}
    39  
    40  	if err := i.runHostHook(PreCleanHost, all); err != nil {
    41  		return err
    42  	}
    43  
    44  	kubeRuntimeInstaller, err := getClusterRuntimeInstaller(i.clusterRuntimeType, i.infraDriver,
    45  		containerruntime.Info{}, registry.Info{}, i.KubeadmConfig)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	if err = kubeRuntimeInstaller.Reset(); err != nil {
    51  		return err
    52  	}
    53  
    54  	crInfo, err := i.containerRuntimeInstaller.GetInfo()
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	if i.regConfig.LocalRegistry != nil {
    60  		if *i.regConfig.LocalRegistry.HA {
    61  			installer := registry.NewInstaller(masters, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor)
    62  			err = installer.Clean()
    63  			if err != nil {
    64  				return err
    65  			}
    66  		}
    67  
    68  		installer := registry.NewInstaller([]net.IP{master0}, i.regConfig.LocalRegistry, i.infraDriver, i.Distributor)
    69  		err = installer.Clean()
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  
    75  	registryConfigurator, err := registry.NewConfigurator(nil, crInfo, i.regConfig, i.infraDriver, i.Distributor)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if err = registryConfigurator.UninstallFrom(masters, workers); err != nil {
    81  		return err
    82  	}
    83  
    84  	if err = i.containerRuntimeInstaller.UnInstallFrom(all); err != nil {
    85  		return err
    86  	}
    87  
    88  	if err = i.runHostHook(PostCleanHost, all); err != nil {
    89  		return err
    90  	}
    91  
    92  	if err = i.runClusterHook(master0, PostUnInstallCluster); err != nil {
    93  		return err
    94  	}
    95  
    96  	// delete HostAlias
    97  	if err := i.infraDriver.DeleteClusterHostAliases(all); err != nil {
    98  		return err
    99  	}
   100  
   101  	if err = i.Distributor.Restore(i.infraDriver.GetClusterBasePath(), all); err != nil {
   102  		return err
   103  	}
   104  
   105  	return nil
   106  }