Skip to content

Commit

Permalink
feat: optimize platform module config logic (#1129)
Browse files Browse the repository at this point in the history
feat: optimize platform module config logic

- load workspace module config with the module name
- ignore modules that only exist in the workspace config
- optimize some error logs in `callModules`
  • Loading branch information
SparkYuan committed May 21, 2024
1 parent 46465fc commit bd90c5a
Showing 1 changed file with 25 additions and 15 deletions.
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

0 comments on commit bd90c5a

Please sign in to comment.