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: add context in the module grpc #1118

Merged
merged 1 commit into from
May 13, 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
47 changes: 25 additions & 22 deletions pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (g *appConfigurationGenerator) Generate(spec *v1.Spec) error {
wl := spec.Resources[1]

// call modules to generate customized resources
resources, patchers, err := g.callModules(projectModuleConfigs, g.dependencies)
resources, patchers, err := g.callModules(projectModuleConfigs)
if err != nil {
return err
}
Expand Down Expand Up @@ -253,11 +253,10 @@ func PatchWorkload(workload *v1.Resource, patcher *internalv1.Patcher) error {
type moduleConfig struct {
devConfig internalv1.Accessory
platformConfig v1.GenericConfig
ctx v1.GenericConfig
}

func (g *appConfigurationGenerator) callModules(
projectModuleConfigs map[string]v1.GenericConfig, dependencies *pkg.Dependencies,
) (resources []v1.Resource, patchers []internalv1.Patcher, err error) {
func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]v1.GenericConfig) (resources []v1.Resource, patchers []internalv1.Patcher, err error) {
pluginMap := make(map[string]*modules.Plugin)
defer func() {
for _, plugin := range pluginMap {
Expand All @@ -269,10 +268,10 @@ func (g *appConfigurationGenerator) callModules(
}()

// build module config index
if dependencies == nil {
if g.dependencies == nil {
return nil, nil, errors.New("dependencies should not be nil")
}
indexModuleConfig, err := buildModuleConfigIndex(g.app.Accessories, projectModuleConfigs, dependencies)
indexModuleConfig, err := g.buildModuleConfigIndex(projectModuleConfigs)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -337,30 +336,28 @@ func (g *appConfigurationGenerator) callModules(
return resources, patchers, nil
}

func buildModuleConfigIndex(
accessories map[string]internalv1.Accessory,
projectModuleConfigs map[string]v1.GenericConfig,
dependencies *pkg.Dependencies,
) (map[string]moduleConfig, error) {
func (g *appConfigurationGenerator) buildModuleConfigIndex(platformModuleConfigs map[string]v1.GenericConfig) (map[string]moduleConfig, error) {
indexModuleConfig := map[string]moduleConfig{}
for accName, accessory := range accessories {
for accName, accessory := range g.app.Accessories {
// parse accessory module key
key, err := parseModuleKey(accessory, dependencies)
key, err := parseModuleKey(accessory, g.dependencies)
if err != nil {
return nil, err
}
log.Info("build module index of accessory:%s module key: %s", accName, key)
indexModuleConfig[key] = moduleConfig{
devConfig: accessory,
platformConfig: projectModuleConfigs[key],
platformConfig: platformModuleConfigs[key],
ctx: g.ws.Context,
}
}
// append module configs only exist in platform configs
for key, platformConfig := range projectModuleConfigs {
for key, platformConfig := range platformModuleConfigs {
if _, ok := indexModuleConfig[key]; !ok {
indexModuleConfig[key] = moduleConfig{
devConfig: nil,
platformConfig: platformConfig,
ctx: g.ws.Context,
}
}
}
Expand Down Expand Up @@ -390,7 +387,7 @@ func parseModuleKey(accessory internalv1.Accessory, dependencies *pkg.Dependenci
}

func (g *appConfigurationGenerator) initModuleRequest(config moduleConfig) (*proto.GeneratorRequest, error) {
var workloadConfig, devConfig, platformConfig []byte
var workloadConfig, devConfig, platformConfig, ctx []byte
var err error
// Attention: we MUST yaml.v2 to serialize the object,
// because we have introduced MapSlice in the Workload which is supported only in the yaml.v2
Expand All @@ -409,14 +406,20 @@ func (g *appConfigurationGenerator) initModuleRequest(config moduleConfig) (*pro
return nil, fmt.Errorf("marshal platform module config failed. %w", err)
}
}
if config.ctx != nil {
if ctx, err = yaml.Marshal(config.ctx); err != nil {
return nil, fmt.Errorf("marshal context config failed. %w", err)
}
}

protoRequest := &proto.GeneratorRequest{
Project: g.project,
Stack: g.stack,
App: g.appName,
Workload: workloadConfig,
DevModuleConfig: devConfig,
PlatformModuleConfig: platformConfig,
Project: g.project,
Stack: g.stack,
App: g.appName,
Workload: workloadConfig,
DevConfig: devConfig,
PlatformConfig: platformConfig,
Context: ctx,
}
return protoRequest, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/modules/generators/app_configurations_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestAppConfigurationGenerator_CallModules(t *testing.T) {
killMock.UnPatch()
}()

resources, patchers, err := g.callModules(projectModuleConfigs, dependencies)
resources, patchers, err := g.callModules(projectModuleConfigs)
assert.NoError(t, err)
assert.NotEmpty(t, resources)
assert.Empty(t, patchers)
Expand All @@ -398,7 +398,7 @@ func TestAppConfigurationGenerator_CallModules(t *testing.T) {
pluginMock.UnPatch()
}()

_, _, err := g.callModules(projectModuleConfigs, dependencies)
_, _, err := g.callModules(projectModuleConfigs)
assert.Error(t, err)
})

Expand All @@ -412,7 +412,7 @@ func TestAppConfigurationGenerator_CallModules(t *testing.T) {
pluginMock.UnPatch()
killMock.UnPatch()
}()
_, _, err := g.callModules(projectModuleConfigs, dependencies)
_, _, err := g.callModules(projectModuleConfigs)
assert.Error(t, err)
})
}
35 changes: 22 additions & 13 deletions pkg/modules/proto/module.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/modules/proto/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ message GeneratorRequest {
// Workload represents the v1.Workload defined in the AppConfiguration
bytes workload = 4;
// DevModuleConfig is the developer's inputs of this module
bytes dev_module_config = 5;
bytes dev_config = 5;
// PlatformModuleConfig is the platform engineer's inputs of this module
bytes platform_module_config = 6;
bytes platform_config = 6;
// context contains workspace-level configurations, such as topologies, server endpoints, metadata, etc.
bytes context = 7;
}

// GeneratorResponse represents the generate result of the generator.
Expand Down
3 changes: 3 additions & 0 deletions pkg/modules/proto/scrip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
module.proto
Loading