Skip to content

Commit

Permalink
resolve conversation and enhanced runtime logic
Browse files Browse the repository at this point in the history
Signed-off-by: starComingup <[email protected]>
  • Loading branch information
starComingup committed Sep 15, 2022
1 parent a487276 commit 28bd67e
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apply/driver/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion apply/processor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions apply/processor/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
16 changes: 9 additions & 7 deletions apply/processor/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion apply/processor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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!")
}
8 changes: 4 additions & 4 deletions cmd/sealer/cmd/cluster/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
5 changes: 2 additions & 3 deletions cmd/sealer/cmd/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions pkg/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 9 additions & 4 deletions pkg/runtime/k0s/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions pkg/runtime/k0s/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ type Runtime struct {
RegConfig *registry.Config
}

var ForceDelete bool

func (k *Runtime) Init() error {
return k.init()
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions pkg/runtime/kubernetes/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ import (
"golang.org/x/sync/errgroup"
)

var ForceDelete bool

type Config struct {
Vlog int
VIP string
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 28bd67e

Please sign in to comment.