Skip to content

Commit

Permalink
fix: panic when resetting a non-existent cluster (#3782) (#3788) (#3790)
Browse files Browse the repository at this point in the history
Co-authored-by: fengxsong <fengxsong@outlook.com>
  • Loading branch information
sealos-ci-robot and fengxsong authored Aug 29, 2023
1 parent b923bf0 commit 9067cec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
17 changes: 15 additions & 2 deletions pkg/apply/processor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ func (d *DeleteProcessor) PreProcess(cluster *v2.Cluster) error {
func (d *DeleteProcessor) UndoBootstrap(cluster *v2.Cluster) error {
logger.Info("Executing pipeline Bootstrap in DeleteProcessor")
hosts := append(cluster.GetMasterIPAndPortList(), cluster.GetNodeIPAndPortList()...)
bs := bootstrap.New(d.ClusterFile.GetCluster())
var cls *v2.Cluster
if v := d.ClusterFile.GetCluster(); v != nil {
cls = v
} else {
cls = cluster
}
bs := bootstrap.New(cls)
return bs.Delete(hosts...)
}

Expand All @@ -96,7 +102,14 @@ func (d *DeleteProcessor) UnMountRootfs(cluster *v2.Cluster) error {
if err != nil {
return err
}
return fs.UnMountRootfs(d.ClusterFile.GetCluster(), hosts)
var cls *v2.Cluster
if v := d.ClusterFile.GetCluster(); v != nil {
cls = v
} else {
cls = cluster
}

return fs.UnMountRootfs(cls, hosts)
}

func (d *DeleteProcessor) UnMountImage(cluster *v2.Cluster) error {
Expand Down
8 changes: 7 additions & 1 deletion pkg/apply/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package apply

import (
"errors"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -32,6 +33,7 @@ func NewApplierFromResetArgs(cmd *cobra.Command, args *ResetArgs) (applydrivers.
clusterPath := constants.Clusterfile(args.ClusterName)
cf := clusterfile.NewClusterFile(clusterPath)
err := cf.Process()
// incase we want to reset force
if err != nil && err != clusterfile.ErrClusterFileNotExists {
return nil, err
}
Expand All @@ -58,7 +60,7 @@ func (r *ClusterArgs) resetArgs(cmd *cobra.Command, args *ResetArgs) error {
}
override := getSSHFromCommand(cmd)
if override != nil {
r.cluster.Spec.SSH = *override
ssh.OverSSHConfig(&r.cluster.Spec.SSH, override)
}

if len(args.Cluster.Masters) > 0 {
Expand All @@ -73,6 +75,10 @@ func (r *ClusterArgs) resetArgs(cmd *cobra.Command, args *ResetArgs) error {
}
r.cluster.Spec.Hosts = r.hosts
}

if r.cluster.ObjectMeta.CreationTimestamp.IsZero() && len(r.cluster.Spec.Hosts) == 0 {
return errors.New("must specified '--masters' or '--nodes' when clusterfile is not exists")
}
logger.Debug("cluster info: %v", r.cluster)
return nil
}

0 comments on commit 9067cec

Please sign in to comment.