Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize platform module config logic #1129

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/engine/api/generate/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/engine/api/builders"
"kusionstack.io/kusion/pkg/engine/api/generate/run"
"kusionstack.io/kusion/pkg/modules/generators"
"kusionstack.io/kusion/pkg/util/io"
"kusionstack.io/kusion/pkg/util/kfile"
)
Expand Down Expand Up @@ -99,6 +100,11 @@ func CopyDependentModules(workDir string) error {
var allErrs []error
for _, dep := range modFile.Deps {
if dep.Source.Oci != nil {
// ignore workload modules
if generators.IgnoreModules[dep.Name] {
continue
}

info := dep.Source.Oci
pkgDir := filepath.Join(absPkgPath, dep.FullName)
source := filepath.Join(pkgDir, "kusion-module-"+dep.FullName)
Expand Down
40 changes: 25 additions & 15 deletions pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,25 @@ type moduleConfig struct {
func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]v1.GenericConfig) (resources []v1.Resource, patcher *v1.Patcher, err error) {
pluginMap := make(map[string]*modules.Plugin)
defer func() {
if e := recover(); e != nil {
switch x := e.(type) {
case string:
err = fmt.Errorf("call modules panic:%s", e)
case error:
err = x
default:
err = errors.New("call modules unknown panic")
}
}
for _, plugin := range pluginMap {
pluginErr := plugin.KillPluginClient()
if pluginErr != nil {
err = fmt.Errorf("call modules failed %w. %s", err, pluginErr)
err = fmt.Errorf("kill modules failed %w. %s", err, pluginErr)
}
}
if err != nil {
log.Errorf(err.Error())
}
}()

// build module config index
Expand Down Expand Up @@ -388,7 +401,7 @@ func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]
}

// parse patcher
err = yaml.Unmarshal(response.Patcher, patcher)
err = yaml.Unmarshal(response.Patcher, &patcher)
if err != nil {
return nil, nil, err
}
Expand All @@ -406,28 +419,20 @@ func (g *appConfigurationGenerator) buildModuleConfigIndex(platformModuleConfigs
return nil, err
}
log.Info("build module index of accessory:%s module key: %s", accName, key)
moduleName := getModuleName(accessory)
indexModuleConfig[key] = moduleConfig{
devConfig: accessory,
platformConfig: platformModuleConfigs[key],
platformConfig: platformModuleConfigs[moduleName],
ctx: g.ws.Context,
}
}
// append module configs only exist in platform configs
for key, platformConfig := range platformModuleConfigs {
if _, ok := indexModuleConfig[key]; !ok {
indexModuleConfig[key] = moduleConfig{
devConfig: nil,
platformConfig: platformConfig,
ctx: g.ws.Context,
}
}
}
return indexModuleConfig, nil
}

// parseModuleKey returns the module key of the accessory in format of "org/module@version"
// example: "kusionstack/mysql@v0.1.0"
func parseModuleKey(accessory v1.Accessory, dependencies *pkg.Dependencies) (string, error) {
split := strings.Split(accessory["_type"].(string), ".")
moduleName := split[0]
moduleName := getModuleName(accessory)
// find module namespace and version
d, ok := dependencies.Deps[moduleName]
if !ok {
Expand All @@ -447,6 +452,11 @@ func parseModuleKey(accessory v1.Accessory, dependencies *pkg.Dependencies) (str
return key, nil
}

func getModuleName(accessory v1.Accessory) string {
split := strings.Split(accessory["_type"].(string), ".")
return split[0]
}

func (g *appConfigurationGenerator) initModuleRequest(config moduleConfig) (*proto.GeneratorRequest, error) {
var workloadConfig, devConfig, platformConfig, ctx []byte
var err error
Expand Down
Loading