Skip to content

Commit

Permalink
feat: support merge cmd env with image extension (sealerio#2207)
Browse files Browse the repository at this point in the history
* feat: support merge cmd env with image extension

Signed-off-by: kakzhou719 <kakazhou719@163.com>

feat: support merge cmd env with image extension

Signed-off-by: kakzhou719 <kakazhou719@163.com>

feat:support env and appenv instruction

* feat:support env and appenv instruction

---------

Co-authored-by: kakzhou719 <wb-zl958011@alibaba-inc.com>
  • Loading branch information
kakaZhou719 and kakzhou719 authored May 16, 2023
1 parent a593435 commit d83ead0
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 51 deletions.
22 changes: 13 additions & 9 deletions cmd/sealer/cmd/cluster/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
imagev1 "github.com/sealerio/sealer/pkg/define/image/v1"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sealerio/sealer/pkg/infradriver"
"github.com/sealerio/sealer/utils/strings"

"github.com/pkg/errors"
Expand Down Expand Up @@ -102,7 +101,8 @@ func NewApplyCmd() *cobra.Command {
}

if imageSpec.ImageExtension.Type == imagev1.AppInstaller {
app := utils.ConstructApplication(cf.GetApplication(), desiredCluster.Spec.CMD, desiredCluster.Spec.APPNames)
app := utils.ConstructApplication(cf.GetApplication(), desiredCluster.Spec.CMD,
desiredCluster.Spec.APPNames, desiredCluster.Spec.Env)

return runApplicationImage(&RunApplicationImageRequest{
ImageName: imageName,
Expand Down Expand Up @@ -138,7 +138,8 @@ func NewApplyCmd() *cobra.Command {
}

// install application
app := utils.ConstructApplication(cf.GetApplication(), desiredCluster.Spec.CMD, desiredCluster.Spec.APPNames)
//TODO use flag env to construct application directly. at present ,sealer use cluster.env to construct application
app := utils.ConstructApplication(cf.GetApplication(), desiredCluster.Spec.CMD, desiredCluster.Spec.APPNames, desiredCluster.Spec.Env)
return runApplicationImage(&RunApplicationImageRequest{
ImageName: imageName,
Application: app,
Expand Down Expand Up @@ -191,8 +192,11 @@ func applyClusterWithNew(cf clusterfile.Interface, applyMode string,
return fmt.Errorf("failed to merge cluster with apply args: %v", err)
}

// merge image extension
mergedWithExt := utils.MergeClusterWithImageExtension(cluster, imageSpec.ImageExtension)

// set merged cluster
cf.SetCluster(*cluster)
cf.SetCluster(*mergedWithExt)
return runClusterImage(imageEngine, cf, imageSpec, applyMode, applyFlags.IgnoreCache)
}

Expand All @@ -218,12 +222,12 @@ func applyClusterWithExisted(cf clusterfile.Interface, client *k8s.Client,
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 false, err
}
// merge image extension
mergedWithExt := utils.MergeClusterWithImageExtension(&desiredCluster, imageSpec.ImageExtension)

cf.SetCluster(*mergedWithExt)

if err := scaleUpCluster(imageSpec.Name, mj, nj, infraDriver, imageEngine, cf, applyFlags.IgnoreCache); err != nil {
if err := scaleUpCluster(mj, nj, imageSpec, cf, imageEngine, applyFlags.IgnoreCache); err != nil {
return false, err
}
return true, nil
Expand Down
32 changes: 23 additions & 9 deletions cmd/sealer/cmd/cluster/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import (
"fmt"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"
"github.com/sealerio/sealer/cmd/sealer/cmd/utils"
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/clusterfile"
imagecommon "github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sealerio/sealer/pkg/infradriver"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var joinFlags *types.ScaleUpFlags
Expand Down Expand Up @@ -85,19 +83,35 @@ func NewJoinCmd() *cobra.Command {
if err != nil {
return err
}
cf.SetCluster(cluster)

infraDriver, err := infradriver.NewInfraDriver(&cluster)
// get image extension
imageName := cluster.Spec.Image
imageEngine, err := imageengine.NewImageEngine(options.EngineGlobalConfigurations{})
if err != nil {
return err
}

imageEngine, err := imageengine.NewImageEngine(imagecommon.EngineGlobalConfigurations{})
id, err := imageEngine.Pull(&options.PullOptions{
Quiet: false,
PullPolicy: "missing",
Image: imageName,
Platform: "local",
})
if err != nil {
return err
}

return scaleUpCluster(cluster.Spec.Image, mj, nj, infraDriver, imageEngine, cf, joinFlags.IgnoreCache)
imageSpec, err := imageEngine.Inspect(&options.InspectOptions{ImageNameOrID: id})
if err != nil {
return fmt.Errorf("failed to get sealer image extension: %s", err)
}

// merge image extension
mergedWithExt := utils.MergeClusterWithImageExtension(&cluster, imageSpec.ImageExtension)

cf.SetCluster(*mergedWithExt)

return scaleUpCluster(mj, nj, imageSpec, cf, imageEngine, joinFlags.IgnoreCache)
},
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/sealer/cmd/cluster/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func rollbackCluster(cf clusterfile.Interface, imageEngine imageengine.Interface
}

cluster := cf.GetCluster()
infraDriver, err := infradriver.NewInfraDriver(&cluster)
// merge image extension
mergedWithExt := utils.MergeClusterWithImageExtension(&cluster, imageSpec.ImageExtension)

infraDriver, err := infradriver.NewInfraDriver(mergedWithExt)
if err != nil {
return err
}
Expand Down Expand Up @@ -190,7 +193,7 @@ func rollbackCluster(cf clusterfile.Interface, imageEngine imageengine.Interface
appNames := infraDriver.GetClusterLaunchApps()

// merge to application between v2.ClusterSpec, v2.Application and image extension
v2App, err := application.NewV2Application(utils.ConstructApplication(cf.GetApplication(), cmds, appNames), imageSpec.ImageExtension)
v2App, err := application.NewV2Application(utils.ConstructApplication(cf.GetApplication(), cmds, appNames, cluster.Spec.Env), imageSpec.ImageExtension)
if err != nil {
return fmt.Errorf("failed to parse application from Clusterfile:%v ", err)
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/sealer/cmd/cluster/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewRunCmd() *cobra.Command {
}

if imageSpec.ImageExtension.Type == imagev1.AppInstaller {
app := utils.ConstructApplication(nil, runFlags.Cmds, runFlags.AppNames)
app := utils.ConstructApplication(nil, runFlags.Cmds, runFlags.AppNames, runFlags.CustomEnv)

return runApplicationImage(&RunApplicationImageRequest{
ImageName: args[0],
Expand Down Expand Up @@ -221,7 +221,7 @@ func runWithClusterfile(clusterFile string, runFlags *types.RunFlags) error {
}

if imageSpec.ImageExtension.Type == imagev1.AppInstaller {
app := utils.ConstructApplication(cf.GetApplication(), cluster.Spec.CMD, cluster.Spec.APPNames)
app := utils.ConstructApplication(cf.GetApplication(), cluster.Spec.CMD, cluster.Spec.APPNames, runFlags.CustomEnv)

return runApplicationImage(&RunApplicationImageRequest{
ImageName: imageName,
Expand All @@ -241,7 +241,11 @@ func runWithClusterfile(clusterFile string, runFlags *types.RunFlags) error {
func runClusterImage(imageEngine imageengine.Interface, cf clusterfile.Interface,
imageSpec *imagev1.ImageSpec, mode string, ignoreCache bool) error {
cluster := cf.GetCluster()
infraDriver, err := infradriver.NewInfraDriver(&cluster)

// merge image extension
mergedWithExt := utils.MergeClusterWithImageExtension(&cluster, imageSpec.ImageExtension)

infraDriver, err := infradriver.NewInfraDriver(mergedWithExt)
if err != nil {
return err
}
Expand Down Expand Up @@ -327,7 +331,7 @@ func runClusterImage(imageEngine imageengine.Interface, cf clusterfile.Interface

// TODO valid construct application
// merge to application between v2.ClusterSpec, v2.Application and image extension
v2App, err := application.NewV2Application(utils.ConstructApplication(cf.GetApplication(), cmds, appNames), imageSpec.ImageExtension)
v2App, err := application.NewV2Application(utils.ConstructApplication(cf.GetApplication(), cmds, appNames, cluster.Spec.Env), imageSpec.ImageExtension)
if err != nil {
return fmt.Errorf("failed to parse application from Clusterfile:%v ", err)
}
Expand Down
49 changes: 33 additions & 16 deletions cmd/sealer/cmd/cluster/scale-up.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import (
"net"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/sealerio/sealer/cmd/sealer/cmd/types"
"github.com/sealerio/sealer/cmd/sealer/cmd/utils"
"github.com/sealerio/sealer/common"
clusterruntime "github.com/sealerio/sealer/pkg/cluster-runtime"
"github.com/sealerio/sealer/pkg/clusterfile"
imagecommon "github.com/sealerio/sealer/pkg/define/options"
imagev1 "github.com/sealerio/sealer/pkg/define/image/v1"
"github.com/sealerio/sealer/pkg/define/options"
"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"
)

var scaleUpFlags *types.ScaleUpFlags
Expand Down Expand Up @@ -87,19 +87,33 @@ func NewScaleUpCmd() *cobra.Command {
if err != nil {
return err
}
cf.SetCluster(cluster)

infraDriver, err := infradriver.NewInfraDriver(&cluster)
imageEngine, err := imageengine.NewImageEngine(options.EngineGlobalConfigurations{})
if err != nil {
return err
}

imageEngine, err := imageengine.NewImageEngine(imagecommon.EngineGlobalConfigurations{})
id, err := imageEngine.Pull(&options.PullOptions{
Quiet: false,
PullPolicy: "missing",
Image: cluster.Spec.Image,
Platform: "local",
})
if err != nil {
return err
}

return scaleUpCluster(cluster.Spec.Image, mj, nj, infraDriver, imageEngine, cf, scaleUpFlags.IgnoreCache)
imageSpec, err := imageEngine.Inspect(&options.InspectOptions{ImageNameOrID: id})
if err != nil {
return fmt.Errorf("failed to get sealer image extension: %s", err)
}

// merge image extension
mergedWithExt := utils.MergeClusterWithImageExtension(&cluster, imageSpec.ImageExtension)

cf.SetCluster(*mergedWithExt)

return scaleUpCluster(mj, nj, imageSpec, cf, imageEngine, scaleUpFlags.IgnoreCache)
},
}

Expand All @@ -116,15 +130,23 @@ func NewScaleUpCmd() *cobra.Command {
return scaleUpFlagsCmd
}

func scaleUpCluster(clusterImageName string, scaleUpMasterIPList, scaleUpNodeIPList []net.IP,
infraDriver infradriver.InfraDriver, imageEngine imageengine.Interface,
cf clusterfile.Interface, ignoreCache bool) error {
func scaleUpCluster(scaleUpMasterIPList, scaleUpNodeIPList []net.IP, imageSpec *imagev1.ImageSpec,
cf clusterfile.Interface, imageEngine imageengine.Interface, ignoreCache bool) error {
logrus.Infof("start to scale up cluster")

var (
newHosts = append(scaleUpMasterIPList, scaleUpNodeIPList...)
)

cluster := cf.GetCluster()

infraDriver, err := infradriver.NewInfraDriver(&cluster)
if err != nil {
return err
}

clusterImageName := infraDriver.GetClusterImageName()

clusterHostsPlatform, err := infraDriver.GetHostsPlatform(newHosts)
if err != nil {
return err
Expand Down Expand Up @@ -172,11 +194,6 @@ func scaleUpCluster(clusterImageName string, scaleUpMasterIPList, scaleUpNodeIPL
runtimeConfig.KubeadmConfig = *cf.GetKubeadmConfig()
}

imageSpec, err := imageEngine.Inspect(&imagecommon.InspectOptions{ImageNameOrID: clusterImageName})
if err != nil {
return fmt.Errorf("failed to get sealer image extension: %s", err)
}

installer, err := clusterruntime.NewInstaller(infraDriver, *runtimeConfig,
clusterruntime.GetClusterInstallInfo(imageSpec.ImageExtension.Labels, runtimeConfig.ContainerRuntimeConfig))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/sealer/cmd/cluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func upgradeCluster(cf clusterfile.Interface, imageEngine imageengine.Interface,
}

cluster := cf.GetCluster()
infraDriver, err := infradriver.NewInfraDriver(&cluster)
infraDriver, err := infradriver.NewInfraDriver(utils.MergeClusterWithImageExtension(&cluster, imageSpec.ImageExtension))
if err != nil {
return err
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func upgradeCluster(cf clusterfile.Interface, imageEngine imageengine.Interface,
appNames := infraDriver.GetClusterLaunchApps()

// merge to application between v2.ClusterSpec, v2.Application and image extension
v2App, err := application.NewV2Application(utils.ConstructApplication(cf.GetApplication(), cmds, appNames), imageSpec.ImageExtension)
v2App, err := application.NewV2Application(utils.ConstructApplication(cf.GetApplication(), cmds, appNames, cluster.Spec.Env), imageSpec.ImageExtension)
if err != nil {
return fmt.Errorf("failed to parse application from Clusterfile:%v ", err)
}
Expand Down
12 changes: 11 additions & 1 deletion cmd/sealer/cmd/utils/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

//ConstructApplication merge flags to v2.Application
func ConstructApplication(app *v2.Application, cmds, appNames []string) *v2.Application {
func ConstructApplication(app *v2.Application, cmds, appNames, globalEnvs []string) *v2.Application {
var newApp *v2.Application

if app != nil {
Expand All @@ -42,5 +42,15 @@ func ConstructApplication(app *v2.Application, cmds, appNames []string) *v2.Appl
newApp.Spec.LaunchApps = appNames
}

// add appEnvs from flag to application object.
if len(globalEnvs) > 0 {
var appConfigList []v2.ApplicationConfig
for _, appConfig := range newApp.Spec.Configs {
appConfig.Env = append(globalEnvs, appConfig.Env...)
appConfigList = append(appConfigList, appConfig)
}
newApp.Spec.Configs = appConfigList
}

return newApp
}
Loading

0 comments on commit d83ead0

Please sign in to comment.