Skip to content

Commit

Permalink
feat: refactor application and image spec
Browse files Browse the repository at this point in the history
Signed-off-by: kakzhou719 <kakazhou719@163.com>
  • Loading branch information
kakaZhou719 committed Jun 28, 2023
1 parent f8ea340 commit d82bd22
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 112 deletions.
50 changes: 23 additions & 27 deletions build/kubefile/parser/kubefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import (
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/pkg/errors"
"github.com/sealerio/sealer/build/kubefile/command"
v1 "github.com/sealerio/sealer/pkg/define/application/v1"
"github.com/sealerio/sealer/pkg/define/application/version"
v12 "github.com/sealerio/sealer/pkg/define/image/v1"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -73,9 +71,9 @@ type KubefileResult struct {
// APP myapp local://app.yaml
Applications map[string]version.VersionedApplication

// ApplicationConfigs structured APPCMDS instruction and register it to this map
// AppCmdsMap structured APPCMDS instruction and register it to this map
// APPCMDS myapp ["kubectl apply -f app.yaml"]
ApplicationConfigs map[string]*v12.ApplicationConfig
AppCmdsMap map[string][]string

legacyContext LegacyContext
}
Expand All @@ -102,10 +100,10 @@ func (kp *KubefileParser) ParseKubefile(rwc io.Reader) (*KubefileResult, error)
func (kp *KubefileParser) generateResult(mainNode *Node) (*KubefileResult, error) {
var (
result = &KubefileResult{
Applications: map[string]version.VersionedApplication{},
ApplicationConfigs: map[string]*v12.ApplicationConfig{},
GlobalEnv: map[string]string{},
AppEnvMap: map[string]map[string]string{},
Applications: map[string]version.VersionedApplication{},
AppCmdsMap: map[string][]string{},
GlobalEnv: map[string]string{},
AppEnvMap: map[string]map[string]string{},
legacyContext: LegacyContext{
files: []string{},
directories: []string{},
Expand Down Expand Up @@ -181,27 +179,29 @@ func (kp *KubefileParser) generateResult(mainNode *Node) (*KubefileResult, error
}

// check result validation
// if no app type detected and no ApplicationConfigs exist for this app, will return error.
// if no app type detected and no AppCmds exist for this app, will return error.
for name, registered := range result.Applications {
if registered.Type() != "" {
continue
}

if _, ok := result.ApplicationConfigs[name]; !ok {
if _, ok := result.AppCmdsMap[name]; !ok {
return nil, fmt.Errorf("app %s need to specify APPCMDS if no app type detected", name)
}
}

// register app with all env list.
// register app with app env list.
for appName, appEnv := range result.AppEnvMap {
app := result.Applications[appName]
result.Applications[appName] = &v1.Application{
NameVar: app.Name(),
TypeVar: app.Type(),
FilesVar: app.Files(),
VersionVar: app.Version(),
AppEnv: appEnv,
}
app.SetEnv(appEnv)
result.Applications[appName] = app
}

// register app with app cmds.
for appName, appCmds := range result.AppCmdsMap {
app := result.Applications[appName]
app.SetCmds(appCmds)
result.Applications[appName] = app
}

return result, nil
Expand Down Expand Up @@ -326,12 +326,7 @@ func (kp *KubefileParser) processAppCmds(node *Node, result *KubefileResult) err
return fmt.Errorf("the specified app name(%s) for `APPCMDS` should be exist", appName)
}

result.ApplicationConfigs[appName] = &v12.ApplicationConfig{
Name: appName,
Launch: &v12.ApplicationConfigLaunch{
CMDs: appCmds,
},
}
result.AppCmdsMap[appName] = appCmds
return nil
}

Expand Down Expand Up @@ -481,9 +476,10 @@ func (kp *KubefileParser) processFrom(node *Node, result *KubefileResult) error
theApp := app
result.Applications[app.Name()] = theApp
}
for _, appConfig := range imageSpec.ImageExtension.Launch.AppConfigs {
result.ApplicationConfigs[appConfig.Name] = appConfig
}
//
//for _, appConfig := range imageSpec.ImageExtension.Launch.AppConfigs {
// result.ApplicationConfigs[appConfig.Name] = appConfig
//}

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/sealer/cmd/cluster/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (i AppInstaller) prepareMaterials(appImageName string, mode string, ignoreC
}

func NewApplicationInstaller(appSpec *v2.Application, extension imagev1.ImageExtension, imageEngine imageengine.Interface) (*AppInstaller, error) {
v2App, err := application.NewV2Application(appSpec, extension)
v2App, err := application.NewAppDriver(appSpec, extension)
if err != nil {
return nil, fmt.Errorf("failed to parse application:%v ", err)
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (k KubeInstaller) Install(kubeImageName string, options KubeInstallOptions)
}()

// new merge image extension with app
v2App, err := application.NewV2Application(utils.ConstructApplication(k.cf.GetApplication(), cmds, appNames, cluster.Spec.Env), k.imageSpec.ImageExtension)
v2App, err := application.NewAppDriver(utils.ConstructApplication(k.cf.GetApplication(), cmds, appNames, cluster.Spec.Env), k.imageSpec.ImageExtension)
if err != nil {
return fmt.Errorf("failed to parse application from Clusterfile:%v ", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/sealer/cmd/cluster/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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, cluster.Spec.Env), imageSpec.ImageExtension)
v2App, err := application.NewAppDriver(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
2 changes: 1 addition & 1 deletion cmd/sealer/cmd/cluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,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, cluster.Spec.Env), imageSpec.ImageExtension)
v2App, err := application.NewAppDriver(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
6 changes: 3 additions & 3 deletions cmd/sealer/cmd/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ func buildImageExtensionOnResult(result *parser.KubefileResult, imageType string
extension.Launch.Cmds = result.RawCmds
extension.Launch.AppNames = result.LaunchedAppNames

for _, appConfig := range result.ApplicationConfigs {
extension.Launch.AppConfigs = append(extension.Launch.AppConfigs, appConfig)
}
//for _, appConfig := range result.ApplicationConfigs {
// extension.Launch.AppConfigs = append(extension.Launch.AppConfigs, appConfig)
//}

return extension
}
Expand Down
Loading

0 comments on commit d82bd22

Please sign in to comment.