From e074bdabb77e4bca562e53c53cf941112d89b48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=85=9C=E6=98=9F?= Date: Thu, 9 Mar 2023 19:49:13 +0800 Subject: [PATCH] bugfix: fix apply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 煜星 --- cmd/sealer/cmd/cluster/apply.go | 38 +++++++++++++++++------------- cmd/sealer/cmd/cluster/run.go | 21 ++++++++++------- cmd/sealer/cmd/cluster/upgrade.go | 1 + pkg/imageengine/buildah/inspect.go | 3 +++ 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/cmd/sealer/cmd/cluster/apply.go b/cmd/sealer/cmd/cluster/apply.go index 4f6124f6199..6f7edab69b0 100644 --- a/cmd/sealer/cmd/cluster/apply.go +++ b/cmd/sealer/cmd/cluster/apply.go @@ -117,7 +117,7 @@ func NewApplyCmd() *cobra.Command { // NOTE: in some scenarios, we do not need to prepare the app file repeatedly, // such as the cluster and the apps in the same image - var ignorePrepareAppMaterials bool + var skipPrepareAppMaterials bool // ensure that the cluster reaches the final state firstly if imageSpec.ImageExtension.Type == imagev1.KubeInstaller { client := utils.GetClusterClient() @@ -127,25 +127,26 @@ func NewApplyCmd() *cobra.Command { return applyClusterWithNew(cf, applyMode, imageEngine, imageSpec) } - if err := applyClusterWithExisted(cf, client, imageEngine, imageSpec); err != nil { + clusterUpdated, err := applyClusterWithExisted(cf, client, imageEngine, imageSpec) + if err != nil { return err } // NOTE: we should continue to apply application after the cluster is applied successfully // And it's not needed to prepare the app file repeatedly - ignorePrepareAppMaterials = true + skipPrepareAppMaterials = clusterUpdated } // install application app := utils.ConstructApplication(cf.GetApplication(), desiredCluster.Spec.CMD, desiredCluster.Spec.APPNames) return runApplicationImage(&RunApplicationImageRequest{ - ImageName: imageName, - Application: app, - Envs: desiredCluster.Spec.Env, - ImageEngine: imageEngine, - Extension: imageSpec.ImageExtension, - Configs: cf.GetConfigs(), - RunMode: applyMode, - IgnorePrepareAppMaterials: ignorePrepareAppMaterials, + ImageName: imageName, + Application: app, + Envs: desiredCluster.Spec.Env, + ImageEngine: imageEngine, + Extension: imageSpec.ImageExtension, + Configs: cf.GetConfigs(), + RunMode: applyMode, + SkipPrepareAppMaterials: skipPrepareAppMaterials, }) }, } @@ -193,31 +194,34 @@ func applyClusterWithNew(cf clusterfile.Interface, applyMode string, } func applyClusterWithExisted(cf clusterfile.Interface, client *k8s.Client, - imageEngine imageengine.Interface, imageSpec *imagev1.ImageSpec) error { + imageEngine imageengine.Interface, imageSpec *imagev1.ImageSpec) (bool, error) { desiredCluster := cf.GetCluster() currentCluster, err := utils.GetCurrentCluster(client) if err != nil { - return errors.Wrap(err, "failed to get current cluster") + return false, errors.Wrap(err, "failed to get current cluster") } mj, md := strings.Diff(currentCluster.GetMasterIPList(), desiredCluster.GetMasterIPList()) nj, nd := strings.Diff(currentCluster.GetNodeIPList(), desiredCluster.GetNodeIPList()) if len(mj) == 0 && len(md) == 0 && len(nj) == 0 && len(nd) == 0 { logrus.Infof("No need scale, completed") - return nil + return false, nil } if len(md) > 0 || len(nd) > 0 { logrus.Warnf("scale down not supported: %v, %v, skip them", md, nd) } if len(md) > 0 { - return fmt.Errorf("make sure all masters' ip exist in your clusterfile: %s", applyFlags.ClusterFile) + return false, fmt.Errorf("make sure all masters' ip exist in your clusterfile: %s", applyFlags.ClusterFile) } infraDriver, err := infradriver.NewInfraDriver(&desiredCluster) if err != nil { - return err + return false, err } - return scaleUpCluster(imageSpec.Name, mj, nj, infraDriver, imageEngine, cf) + if err := scaleUpCluster(imageSpec.Name, mj, nj, infraDriver, imageEngine, cf); err != nil { + return false, err + } + return true, nil } diff --git a/cmd/sealer/cmd/cluster/run.go b/cmd/sealer/cmd/cluster/run.go index 2124bd8faed..34bc060aad0 100644 --- a/cmd/sealer/cmd/cluster/run.go +++ b/cmd/sealer/cmd/cluster/run.go @@ -385,14 +385,14 @@ func loadToRegistry(infraDriver infradriver.InfraDriver, distributor imagedistri } type RunApplicationImageRequest struct { - ImageName string - Application *v2.Application - Envs []string - ImageEngine imageengine.Interface - Extension imagev1.ImageExtension - Configs []v1.Config - RunMode string - IgnorePrepareAppMaterials bool + ImageName string + Application *v2.Application + Envs []string + ImageEngine imageengine.Interface + Extension imagev1.ImageExtension + Configs []v1.Config + RunMode string + SkipPrepareAppMaterials bool } func runApplicationImage(request *RunApplicationImageRequest) error { @@ -415,12 +415,15 @@ func runApplicationImage(request *RunApplicationImageRequest) error { } infraDriver.AddClusterEnv(request.Envs) - if !request.IgnorePrepareAppMaterials { + if !request.SkipPrepareAppMaterials { if err := prepareMaterials(infraDriver, request.ImageEngine, v2App, request.ImageName, request.RunMode, request.Configs); err != nil { return err } } + if request.RunMode == common.ApplyModeLoadImage { + return nil + } if err = v2App.Launch(infraDriver); err != nil { return err diff --git a/cmd/sealer/cmd/cluster/upgrade.go b/cmd/sealer/cmd/cluster/upgrade.go index 81b78437d14..07a16db9799 100644 --- a/cmd/sealer/cmd/cluster/upgrade.go +++ b/cmd/sealer/cmd/cluster/upgrade.go @@ -29,6 +29,7 @@ import ( "github.com/sealerio/sealer/pkg/imagedistributor" "github.com/sealerio/sealer/pkg/imageengine" "github.com/sealerio/sealer/pkg/infradriver" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "sigs.k8s.io/yaml" diff --git a/pkg/imageengine/buildah/inspect.go b/pkg/imageengine/buildah/inspect.go index 50b1aa3a9f7..4ebd54f0ddf 100644 --- a/pkg/imageengine/buildah/inspect.go +++ b/pkg/imageengine/buildah/inspect.go @@ -16,6 +16,7 @@ package buildah import ( "fmt" + "sort" "strings" "github.com/containers/buildah" @@ -134,10 +135,12 @@ func handleImageLabelOutput(labels map[string]string) map[string]string { } if len(supportedCNI) != 0 { + sort.Strings(supportedCNI) supportedCNIJSON, _ := json.Marshal(supportedCNI) result[command.LabelSupportedKubeCNIAlpha] = string(supportedCNIJSON) } if len(supportedCSI) != 0 { + sort.Strings(supportedCSI) supportedCSIJSON, _ := json.Marshal(supportedCSI) result[command.LabelSupportedKubeCSIAlpha] = string(supportedCSIJSON) }