bugfix: add node failure (#2395)

modify
This commit is contained in:
zhihui
2023-01-13 13:52:17 +08:00
committed by GitHub
parent 89299d9997
commit 430fcb6f0d
3 changed files with 59 additions and 45 deletions
+3 -44
View File
@@ -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())
+51
View File
@@ -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
}
+5 -1
View File
@@ -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)