Skip to content

Commit

Permalink
[bugfix]: add pre-check that If the cluster already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei committed Mar 31, 2023
1 parent a51d0ec commit f2eff17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/sealer/cmd/cluster/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ func runWithClusterfile(clusterFile string, runFlags *types.RunFlags) error {

func runClusterImage(imageEngine imageengine.Interface, cf clusterfile.Interface, imageSpec *imagev1.ImageSpec, mode string) error {
cluster := cf.GetCluster()
//check cluster
checkClusterFile, err := utils.CheckClusterIsExists(cluster)
if checkClusterFile != "" {
return err
}
infraDriver, err := infradriver.NewInfraDriver(&cluster)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions cmd/sealer/cmd/utils/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import (
"github.com/sealerio/sealer/cmd/sealer/cmd/types"
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/client/k8s"
"github.com/sealerio/sealer/pkg/clusterfile"
"github.com/sealerio/sealer/types/api/constants"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
netutils "github.com/sealerio/sealer/utils/net"
strUtils "github.com/sealerio/sealer/utils/strings"
)

const clusterExists = "exist"

func MergeClusterWithFlags(cluster v2.Cluster, mergeFlags *types.MergeFlags) (*v2.Cluster, error) {
if len(mergeFlags.CustomEnv) > 0 {
cluster.Spec.Env = append(cluster.Spec.Env, mergeFlags.CustomEnv...)
Expand Down Expand Up @@ -256,3 +259,15 @@ func GetClusterClient() *k8s.Client {
}
return nil
}

func CheckClusterIsExists(cluster v2.Cluster) (string, error) {
clusterFile, _, err := clusterfile.GetActualClusterFile()
if err == nil {
newClusterfile := clusterFile.GetCluster()
if cluster.Spec.Image != newClusterfile.Spec.Image {
return clusterExists, fmt.Errorf("the cluster image already exists, please uninstall the current cluster and install another version of the cluster image")
}
return clusterExists, fmt.Errorf("this cluster image has already been installed, please do not repeat the operation")
}
return "", nil
}

0 comments on commit f2eff17

Please sign in to comment.