github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/runtime/k0s/reset.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 k0s
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"net"
    21  
    22  	"golang.org/x/sync/errgroup"
    23  )
    24  
    25  func (k *Runtime) reset(mastersToDelete, workersToDelete []net.IP) error {
    26  	if err := k.resetNodes(workersToDelete); err != nil {
    27  		return err
    28  	}
    29  
    30  	if err := k.resetMasters(mastersToDelete); err != nil {
    31  		return err
    32  	}
    33  	return nil
    34  }
    35  
    36  func (k *Runtime) resetNodes(nodes []net.IP) error {
    37  	eg, _ := errgroup.WithContext(context.Background())
    38  	for _, node := range nodes {
    39  		node := node
    40  		eg.Go(func() error {
    41  			if err := k.infra.CmdAsync(node, nil, "k0s stop",
    42  				"umount $(df -HT | grep '/var/lib/k0s/kubelet/pods' | awk '{print $7}')",
    43  				"k0s reset",
    44  				"rm -rf /etc/k0s/",
    45  				"rm -rf /usr/bin/k0s",
    46  				"rm -rf /usr/bin/kube* && rm -rf ~/.kube/",
    47  				"rm -rf /etc/cni && rm -rf /opt/cni"); err != nil {
    48  				return fmt.Errorf("failed to reset node %s: %v", node, err)
    49  			}
    50  			return nil
    51  		})
    52  	}
    53  	return eg.Wait()
    54  }
    55  
    56  func (k *Runtime) resetMasters(nodes []net.IP) error {
    57  	eg, _ := errgroup.WithContext(context.Background())
    58  	for _, node := range nodes {
    59  		node := node
    60  		eg.Go(func() error {
    61  			if err := k.infra.CmdAsync(node, nil, "k0s stop",
    62  				"k0s reset",
    63  				"rm -rf /etc/k0s/",
    64  				"rm -rf /usr/bin/k0s",
    65  				"rm -rf /usr/bin/kube* && rm -rf ~/.kube/",
    66  				"rm -rf /etc/cni && rm -rf /opt/cni",
    67  				"rm -rf .kube/config"); err != nil {
    68  				return fmt.Errorf("failed to reset master %s: %v", node, err)
    69  			}
    70  			return nil
    71  		})
    72  	}
    73  	return eg.Wait()
    74  }