From 430fcb6f0da4dad3fc8d9e42d23e1892b7b60e9e Mon Sep 17 00:00:00 2001 From: zhihui <82888636+mond77@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:52:17 +0800 Subject: [PATCH] bugfix: add node failure (#2395) modify --- pkg/apply/processor/create.go | 47 ++--------------------------- pkg/apply/processor/interface.go | 51 ++++++++++++++++++++++++++++++++ pkg/apply/processor/scale.go | 6 +++- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/pkg/apply/processor/create.go b/pkg/apply/processor/create.go index 9eaee0bcc..afe26cbde 100644 --- a/pkg/apply/processor/create.go +++ b/pkg/apply/processor/create.go @@ -16,11 +16,9 @@ package processor import ( "context" - "errors" "fmt" "golang.org/x/sync/errgroup" - "k8s.io/apimachinery/pkg/util/sets" "github.com/labring/sealos/pkg/bootstrap" "github.com/labring/sealos/pkg/buildah" @@ -33,7 +31,6 @@ import ( "github.com/labring/sealos/pkg/runtime" v2 "github.com/labring/sealos/pkg/types/v1beta1" "github.com/labring/sealos/pkg/utils/logger" - "github.com/labring/sealos/pkg/utils/rand" "github.com/labring/sealos/pkg/utils/yaml" ) @@ -87,51 +84,13 @@ func (c *CreateProcessor) Check(cluster *v2.Cluster) error { return nil } -func (c *CreateProcessor) CheckImageType(cluster *v2.Cluster) error { - imageTypes := sets.NewString() - for _, image := range cluster.Spec.Image { - oci, err := c.Buildah.InspectImage(image) - if err != nil { - return err - } - if oci.Config.Labels != nil { - imageTypes.Insert(oci.Config.Labels[v2.ImageTypeKey]) - } else { - imageTypes.Insert(string(v2.AppImage)) - } - } - if !imageTypes.Has(string(v2.RootfsImage)) { - return errors.New("can't apply ApplicationImage, kubernetes cluster not found, need to run a BaseImage") - } - return nil -} - func (c *CreateProcessor) PreProcess(cluster *v2.Cluster) error { logger.Info("Executing pipeline PreProcess in CreateProcessor.") - err := c.Buildah.Pull(cluster.Spec.Image, buildah.WithPlatformOption(buildah.DefaultPlatform()), - buildah.WithPullPolicyOption(buildah.PullIfMissing.String())) - if err != nil { + + if err := MountClusterImages(cluster, c.Buildah); err != nil { return err } - if err = c.CheckImageType(cluster); err != nil { - return err - } - for _, img := range cluster.Spec.Image { - bderInfo, err := c.Buildah.Create(rand.Generator(8), img) - if err != nil { - return err - } - mount := &v2.MountImage{ - Name: bderInfo.Container, - ImageName: img, - MountPoint: bderInfo.MountPoint, - } - if err = OCIToImageMount(mount, c.Buildah); err != nil { - return err - } - cluster.Status.Mounts = append(cluster.Status.Mounts, *mount) - } - if err = SyncClusterStatus(cluster, c.Buildah, false); err != nil { + if err := SyncClusterStatus(cluster, c.Buildah, false); err != nil { return err } runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig()) diff --git a/pkg/apply/processor/interface.go b/pkg/apply/processor/interface.go index f92fa8ff7..67011979e 100644 --- a/pkg/apply/processor/interface.go +++ b/pkg/apply/processor/interface.go @@ -20,6 +20,7 @@ import ( v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/util/sets" "github.com/labring/sealos/pkg/buildah" "github.com/labring/sealos/pkg/constants" @@ -30,6 +31,7 @@ import ( "github.com/labring/sealos/pkg/utils/file" "github.com/labring/sealos/pkg/utils/logger" "github.com/labring/sealos/pkg/utils/maps" + "github.com/labring/sealos/pkg/utils/rand" "github.com/labring/sealos/pkg/utils/strings" ) @@ -142,3 +144,52 @@ func MirrorRegistry(cluster *v2.Cluster, mounts []v2.MountImage) error { mirror := registry.New(constants.NewData(cluster.GetName()).RootFSPath(), sshClient, mounts) return mirror.MirrorTo(context.Background(), registries...) } + +func CheckImageType(cluster *v2.Cluster, bd buildah.Interface) error { + imageTypes := sets.NewString() + for _, image := range cluster.Spec.Image { + oci, err := bd.InspectImage(image) + if err != nil { + return err + } + if oci.Config.Labels != nil { + imageTypes.Insert(oci.Config.Labels[v2.ImageTypeKey]) + } else { + imageTypes.Insert(string(v2.AppImage)) + } + } + if !imageTypes.Has(string(v2.RootfsImage)) { + return errors.New("can't apply ApplicationImage, kubernetes cluster not found, need to run a BaseImage") + } + return nil +} + +func MountClusterImages(cluster *v2.Cluster, bd buildah.Interface) error { + err := bd.Pull(cluster.Spec.Image, buildah.WithPlatformOption(buildah.DefaultPlatform()), + buildah.WithPullPolicyOption(buildah.PullIfMissing.String())) + if err != nil { + return err + } + if err := CheckImageType(cluster, bd); err != nil { + return err + } + if cluster.Status.Mounts != nil { + return nil + } + for _, img := range cluster.Spec.Image { + bderInfo, err := bd.Create(rand.Generator(8), img) + if err != nil { + return err + } + mount := &v2.MountImage{ + Name: bderInfo.Container, + ImageName: img, + MountPoint: bderInfo.MountPoint, + } + if err = OCIToImageMount(mount, bd); err != nil { + return err + } + cluster.Status.Mounts = append(cluster.Status.Mounts, *mount) + } + return nil +} diff --git a/pkg/apply/processor/scale.go b/pkg/apply/processor/scale.go index 999a4c776..0f0eb1e43 100644 --- a/pkg/apply/processor/scale.go +++ b/pkg/apply/processor/scale.go @@ -165,7 +165,8 @@ func (c *ScaleProcessor) PreProcess(cluster *v2.Cluster) error { if err != nil { return err } - if err = SyncClusterStatus(cluster, c.Buildah, false); err != nil { + //cluster status might be overwriten by inappropriate usage, add mounts if loss. + if err = MountClusterImages(cluster, c.Buildah); err != nil { return err } if c.IsScaleUp { @@ -180,6 +181,9 @@ func (c *ScaleProcessor) PreProcess(cluster *v2.Cluster) error { return err } } + if err = SyncClusterStatus(cluster, c.Buildah, false); err != nil { + return err + } runTime, err := runtime.NewDefaultRuntime(cluster, c.ClusterFile.GetKubeadmConfig()) if err != nil { return fmt.Errorf("failed to init runtime: %v", err)