diff --git a/apply/driver/local.go b/apply/driver/local.go index d768af353b9..83b179fb1d7 100644 --- a/apply/driver/local.go +++ b/apply/driver/local.go @@ -234,7 +234,7 @@ func (applier *Applier) Upgrade(upgradeImgName string) error { } func (applier *Applier) upgrade() error { - runtimeInterface, err := processor.RuntimeChoose(platform.DefaultMountClusterImageDir(applier.ClusterDesired.Name), applier.ClusterDesired, applier.ClusterFile.GetKubeadmConfig()) + runtimeInterface, err := processor.ChooseRuntime(platform.DefaultMountClusterImageDir(applier.ClusterDesired.Name), applier.ClusterDesired, applier.ClusterFile.GetKubeadmConfig()) if err != nil { return fmt.Errorf("failed to init runtime: %v", err) } diff --git a/apply/processor/create.go b/apply/processor/create.go index 958b556b998..8f28c63fee2 100644 --- a/apply/processor/create.go +++ b/apply/processor/create.go @@ -111,7 +111,7 @@ func (c *CreateProcessor) MountImage(cluster *v2.Cluster) error { return err } //TODO split kubeadm config from cluster file. - runTime, err := RuntimeChoose(platform.DefaultMountClusterImageDir(cluster.Name), cluster, c.ClusterFile.GetKubeadmConfig()) + runTime, err := ChooseRuntime(platform.DefaultMountClusterImageDir(cluster.Name), cluster, c.ClusterFile.GetKubeadmConfig()) if err != nil { return fmt.Errorf("failed to init runtime: %v", err) } diff --git a/apply/processor/delete.go b/apply/processor/delete.go index fd718a9a8cb..5d6abfb4588 100644 --- a/apply/processor/delete.go +++ b/apply/processor/delete.go @@ -38,7 +38,7 @@ type DeleteProcessor struct { } func (d *DeleteProcessor) Reset(cluster *v2.Cluster) error { - runTime, err := RuntimeChoose(platform.DefaultMountClusterImageDir(cluster.Name), cluster, d.ClusterFile.GetKubeadmConfig()) + runTime, err := ChooseRuntime(platform.DefaultMountClusterImageDir(cluster.Name), cluster, d.ClusterFile.GetKubeadmConfig()) if err != nil { return fmt.Errorf("failed to init runtime: %v", err) } diff --git a/apply/processor/gen.go b/apply/processor/gen.go index 31dae2d1ddd..5abadb67a01 100644 --- a/apply/processor/gen.go +++ b/apply/processor/gen.go @@ -189,7 +189,7 @@ func (g *GenerateProcessor) MountImage(cluster *v2.Cluster) error { if err = g.ImageMounter.MountImage(cluster); err != nil { return err } - runt, err := RuntimeChoose(platform.DefaultMountClusterImageDir(cluster.Name), cluster, nil) + runt, err := ChooseRuntime(platform.DefaultMountClusterImageDir(cluster.Name), cluster, nil) if err != nil { return err } @@ -202,7 +202,7 @@ func (g *GenerateProcessor) UnmountImage(cluster *v2.Cluster) error { } func (g *GenerateProcessor) ApplyRegistry(cluster *v2.Cluster) error { - runt, err := RuntimeChoose(platform.DefaultMountClusterImageDir(cluster.Name), cluster, nil) + runt, err := ChooseRuntime(platform.DefaultMountClusterImageDir(cluster.Name), cluster, nil) if err != nil { return err } diff --git a/apply/processor/scale.go b/apply/processor/scale.go index 59597f0db83..dae103b38a1 100644 --- a/apply/processor/scale.go +++ b/apply/processor/scale.go @@ -71,14 +71,9 @@ func (s *ScaleProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error } func (s *ScaleProcessor) PreProcess(cluster *v2.Cluster) error { - runTime, err := RuntimeChoose(platform.DefaultMountClusterImageDir(cluster.Name), cluster, s.ClusterFile.GetKubeadmConfig()) - if err != nil { - return fmt.Errorf("failed to init default runtime: %v", err) - } - s.Runtime = runTime s.Config = config.NewConfiguration(platform.DefaultMountClusterImageDir(cluster.Name)) if s.IsScaleUp { - if err = clusterfile.SaveToDisk(cluster, cluster.Name); err != nil { + if err := clusterfile.SaveToDisk(cluster, cluster.Name); err != nil { return err } } @@ -127,7 +122,8 @@ func (s *ScaleProcessor) Delete(cluster *v2.Cluster) error { } func NewScaleProcessor(kubeadmConfig *kubeadm.KubeadmConfig, clusterFile clusterfile.Interface, masterToJoin, masterToDelete, nodeToJoin, nodeToDelete []net.IP) (Processor, error) { - fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(clusterFile.GetCluster().Name)) + cluster := clusterFile.GetCluster() + fs, err := filesystem.NewFilesystem(common.DefaultTheClusterRootfsDir(cluster.Name)) if err != nil { return nil, err } @@ -138,7 +134,13 @@ func NewScaleProcessor(kubeadmConfig *kubeadm.KubeadmConfig, clusterFile cluster up = true } + runTime, err := ChooseRuntime(platform.DefaultMountClusterImageDir(cluster.Name), &cluster, clusterFile.GetKubeadmConfig()) + if err != nil { + return nil, fmt.Errorf("failed to init default runtime: %v", err) + } + return &ScaleProcessor{ + Runtime: runTime, MastersToDelete: masterToDelete, MastersToJoin: masterToJoin, NodesToDelete: nodeToDelete, diff --git a/apply/processor/utils.go b/apply/processor/utils.go index 1f96cbf9add..1c390a1deea 100644 --- a/apply/processor/utils.go +++ b/apply/processor/utils.go @@ -20,13 +20,17 @@ import ( "github.com/sealerio/sealer/pkg/runtime/kubernetes" "github.com/sealerio/sealer/pkg/runtime/kubernetes/kubeadm" v2 "github.com/sealerio/sealer/types/api/v2" + "github.com/sirupsen/logrus" ) -func RuntimeChoose(rootfs string, cluster *v2.Cluster, config *kubeadm.KubeadmConfig) (runtime.Interface, error) { +func ChooseRuntime(rootfs string, cluster *v2.Cluster, config *kubeadm.KubeadmConfig) (runtime.Interface, error) { metadata, err := runtime.LoadMetadata(rootfs) if err != nil { return nil, err } + + hintInfo(metadata.ClusterRuntime) + switch metadata.ClusterRuntime { case runtime.K8s: return kubernetes.NewDefaultRuntime(cluster, config) @@ -37,3 +41,13 @@ func RuntimeChoose(rootfs string, cluster *v2.Cluster, config *kubeadm.KubeadmCo return kubernetes.NewDefaultRuntime(cluster, config) } } + +// hintInfo will hint end-user which cluster runtime will load and move to using new version ClusterImage. +func hintInfo(clusterRuntime runtime.ClusterRuntime) { + if clusterRuntime != "" { + logrus.Infof("using %s as a cluster runtime.", clusterRuntime) + return + } + logrus.Infof("using k8s as a default cluster runtime.") + logrus.Infof("the old sealer version is deprecated, using new version of sealer and ClusterImage is encouraged!") +} diff --git a/cmd/sealer/cmd/cluster/apply.go b/cmd/sealer/cmd/cluster/apply.go index 850055f4d24..6ce4cfec7d8 100644 --- a/cmd/sealer/cmd/cluster/apply.go +++ b/cmd/sealer/cmd/cluster/apply.go @@ -15,10 +15,10 @@ package cluster import ( - "github.com/sealerio/sealer/pkg/runtime/kubernetes" - "github.com/spf13/cobra" - "github.com/sealerio/sealer/apply" + "github.com/sealerio/sealer/pkg/runtime" + + "github.com/spf13/cobra" ) var clusterFile string @@ -44,6 +44,6 @@ func NewApplyCmd() *cobra.Command { }, } applyCmd.Flags().StringVarP(&clusterFile, "Clusterfile", "f", "Clusterfile", "Clusterfile path to apply a Kubernetes cluster") - applyCmd.Flags().BoolVar(&kubernetes.ForceDelete, "force", false, "force to delete the specified cluster if set true") + applyCmd.Flags().BoolVar(&runtime.ForceDelete, "force", false, "force to delete the specified cluster if set true") return applyCmd } diff --git a/cmd/sealer/cmd/cluster/delete.go b/cmd/sealer/cmd/cluster/delete.go index b22c1533ca2..505cc10e678 100644 --- a/cmd/sealer/cmd/cluster/delete.go +++ b/cmd/sealer/cmd/cluster/delete.go @@ -19,9 +19,8 @@ import ( "github.com/sealerio/sealer/apply" "github.com/sealerio/sealer/common" - "github.com/sealerio/sealer/pkg/clusterfile" - "github.com/sealerio/sealer/pkg/runtime/kubernetes" + "github.com/sealerio/sealer/pkg/runtime" "github.com/spf13/cobra" ) @@ -100,7 +99,7 @@ func NewDeleteCmd() *cobra.Command { deleteCmd.Flags().StringVarP(&deleteClusterFile, "Clusterfile", "f", "", "delete a kubernetes cluster with Clusterfile Annotations") deleteCmd.Flags().StringVarP(&deleteClusterName, "cluster", "c", "", "delete a kubernetes cluster with cluster name") deleteCmd.Flags().StringSliceVarP(&deleteArgs.CustomEnv, "env", "e", []string{}, "set custom environment variables") - deleteCmd.Flags().BoolVar(&kubernetes.ForceDelete, "force", false, "We also can input an --force flag to delete cluster by force") + deleteCmd.Flags().BoolVar(&runtime.ForceDelete, "force", false, "We also can input an --force flag to delete cluster by force") deleteCmd.Flags().BoolP("all", "a", false, "this flags is for delete nodes, if this is true, empty all node ip") return deleteCmd } diff --git a/pkg/runtime/interface.go b/pkg/runtime/interface.go index 022ba96dae4..957ced70a7d 100644 --- a/pkg/runtime/interface.go +++ b/pkg/runtime/interface.go @@ -36,3 +36,5 @@ type Interface interface { // GetClusterMetadata read the rootfs/Metadata file to get some install info for cluster. GetClusterMetadata() (*Metadata, error) } + +var ForceDelete bool diff --git a/pkg/runtime/k0s/reset.go b/pkg/runtime/k0s/reset.go index ea4895f1fff..3791627deb7 100644 --- a/pkg/runtime/k0s/reset.go +++ b/pkg/runtime/k0s/reset.go @@ -48,12 +48,17 @@ func (k *Runtime) resetNodes(nodes []net.IP) error { } func (k *Runtime) resetMasters(nodes []net.IP) error { + eg, _ := errgroup.WithContext(context.Background()) for _, node := range nodes { - if err := k.resetNode(node); err != nil { - return fmt.Errorf("failed to reset master %s: %v", node, err) - } + node := node + eg.Go(func() error { + if err := k.resetNode(node); err != nil { + return fmt.Errorf("failed to reset master %s: %v", node, err) + } + return nil + }) } - return nil + return eg.Wait() } func (k *Runtime) resetNode(node net.IP) error { diff --git a/pkg/runtime/k0s/runtime.go b/pkg/runtime/k0s/runtime.go index 82c14c4020a..6834b90ca93 100644 --- a/pkg/runtime/k0s/runtime.go +++ b/pkg/runtime/k0s/runtime.go @@ -41,8 +41,6 @@ type Runtime struct { RegConfig *registry.Config } -var ForceDelete bool - func (k *Runtime) Init() error { return k.init() } @@ -242,7 +240,7 @@ func (k *Runtime) Command(role string) []string { } func (k *Runtime) confirmDeleteNodes() error { - if !ForceDelete { + if !runtime.ForceDelete { if pass, err := utils.ConfirmOperation("Are you sure to delete these nodes? "); err != nil { return err } else if !pass { diff --git a/pkg/runtime/kubernetes/runtime.go b/pkg/runtime/kubernetes/runtime.go index cb432174fad..5945c214ff7 100644 --- a/pkg/runtime/kubernetes/runtime.go +++ b/pkg/runtime/kubernetes/runtime.go @@ -41,8 +41,6 @@ import ( "golang.org/x/sync/errgroup" ) -var ForceDelete bool - type Config struct { Vlog int VIP string @@ -142,7 +140,7 @@ func (k *Runtime) DeleteNodes(nodesIPList []net.IP) error { } func (k *Runtime) confirmDeleteNodes() error { - if !ForceDelete { + if !runtime.ForceDelete { if pass, err := utils.ConfirmOperation("Are you sure to delete these nodes? "); err != nil { return err } else if !pass {