Skip to content

Commit

Permalink
feat: update grpc interface (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Mar 19, 2024
1 parent 5e34746 commit 1726f10
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 51 deletions.
13 changes: 13 additions & 0 deletions pkg/apis/core/v1/patcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v1

import v1 "k8s.io/api/core/v1"

// Patcher contains fields should be patched into the workload corresponding fields
type Patcher struct {
// Environments represent the environment variables patched to all containers in the workload.
Environments []v1.EnvVar `json:"environments" yaml:"environments"`
// Labels represent the labels patched to both the workload and pod.
Labels map[string]string `json:"labels" yaml:"labels"`
// Annotations represent the annotations patched to both the workload and pod.
Annotations map[string]string `json:"annotations" yaml:"annotations"`
}
14 changes: 0 additions & 14 deletions pkg/apis/core/v1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package v1

import (
"encoding/json"

v1 "k8s.io/api/core/v1"
)

type Resources []Resource
Expand All @@ -24,22 +22,10 @@ type Resource struct {
// DependsOn contains all resources this resource depends on
DependsOn []string `json:"dependsOn,omitempty" yaml:"dependsOn,omitempty"`

// Patcher contains fields should be patched into the workload corresponding fields
Patcher *Patcher `json:"patcher,omitempty" yaml:"patcher,omitempty"`

// Extensions specifies arbitrary metadata of this resource
Extensions map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}

type Patcher struct {
// Environments represent the environment variables patched to all containers in the workload.
Environments []v1.EnvVar `json:"environments" yaml:"environments"`
// Labels represent the labels patched to both the workload and pod.
Labels map[string]string `json:"labels" yaml:"labels"`
// Annotations represent the annotations patched to both the workload and pod.
Annotations map[string]string `json:"annotations" yaml:"annotations"`
}

func (r *Resource) ResourceKey() string {
return r.ID
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/mod/mod_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type InitOptions struct {
}

var example = i18n.T(`# Create a kusion module template in the current directory
kusion mod init my-app
kusion mod init my-module
# Init a kusion module at the specified Path
kusion mod init my-app ./modules
kusion mod init my-module ./modules
# Init a module from a remote git template repository
kusion mod init my-app --template https://github.com/<user>/<repo>`)
kusion mod init my-module --template https://github.com/<user>/<repo>`)
var short = i18n.T("Create a kusion module along with common files and directories in the current directory")

const (
Expand Down
41 changes: 23 additions & 18 deletions pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,22 @@ func (g *appConfigurationGenerator) Generate(spec *v1.Intent) error {
if spec.Resources == nil || len(spec.Resources) < 2 {
return fmt.Errorf("workload is not generated")
}
workload := spec.Resources[1]
wl := spec.Resources[1]

// call modules to generate customized resources
resources, err := g.callModules(projectModuleConfigs)
resources, patchers, err := g.callModules(projectModuleConfigs)
if err != nil {
return err
}

// patch workload with resource patchers
for i, r := range resources {
if r.Patcher != nil {
if err = patchWorkload(&workload, r.Patcher); err != nil {
return err
}
resources[i] = r
for _, p := range patchers {
if err = patchWorkload(&wl, &p); err != nil {
return err
}
}

// append the generated resources to the spec
spec.Resources = append(spec.Resources, resources...)

// The OrderedResourcesGenerator should be executed after all resources are generated.
Expand Down Expand Up @@ -245,9 +243,7 @@ func patchWorkload(workload *v1.Resource, patcher *v1.Patcher) error {
return nil
}

func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]v1.GenericConfig) ([]v1.Resource, error) {
var resources []v1.Resource

func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]v1.GenericConfig) (resources []v1.Resource, patchers []v1.Patcher, err error) {
pluginMap := make(map[string]*modules.Plugin)
defer func() {
for _, plugin := range pluginMap {
Expand All @@ -266,10 +262,10 @@ func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]
if pluginMap[t] == nil {
plugin, err := modules.NewPlugin(t)
if err != nil {
return nil, err
return nil, nil, err
}
if plugin == nil {
return nil, fmt.Errorf("init plugin for module %s failed", t)
return nil, nil, fmt.Errorf("init plugin for module %s failed", t)
}
pluginMap[t] = plugin
}
Expand All @@ -278,31 +274,40 @@ func (g *appConfigurationGenerator) callModules(projectModuleConfigs map[string]
// prepare the request
protoRequest, err := g.initModuleRequest(t, config)
if err != nil {
return nil, err
return nil, nil, err
}

// invoke the plugin
log.Infof("invoke module:%s with request:%s", t, protoRequest.String())
response, err := plugin.Module.Generate(context.Background(), protoRequest)
if err != nil {
return nil, fmt.Errorf("invoke kusion module: %s failed. %w", t, err)
return nil, nil, fmt.Errorf("invoke kusion module: %s failed. %w", t, err)
}
if response == nil {
return nil, fmt.Errorf("empty response from module %s", t)
return nil, nil, fmt.Errorf("empty response from module %s", t)
}

// parse module result
for _, res := range response.Resources {
temp := &v1.Resource{}
err = yaml.Unmarshal(res, temp)
if err != nil {
return nil, err
return nil, nil, err
}
resources = append(resources, *temp)
}
// parse patcher
for _, patcher := range response.Patchers {
temp := &v1.Patcher{}
err = yaml.Unmarshal(patcher, temp)
if err != nil {
return nil, nil, err
}
patchers = append(patchers, *temp)
}
}

return resources, nil
return resources, patchers, nil
}

func (g *appConfigurationGenerator) initModuleRequest(key string, platformModuleConfig v1.GenericConfig) (*proto.GeneratorRequest, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func mockPlugin() {
mockey.Mock(modules.NewPlugin).To(func(key string) (*modules.Plugin, error) {
return &modules.Plugin{Module: &fakeModule{}}, nil
}).Build()
mockey.Mock((*modules.Plugin).KillPluginClient).To(func() {}).Build()
mockey.Mock((*modules.Plugin).KillPluginClient).Return().Build()
}

func TestAppConfigurationGenerator_Generate_CustomNamespace(t *testing.T) {
Expand Down
28 changes: 19 additions & 9 deletions pkg/modules/proto/module.pb.go

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

1 change: 1 addition & 0 deletions pkg/modules/proto/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ message GeneratorRequest {
message GeneratorResponse {
// Resources is a v1.Resource array, which represents the generated resources by this module.
repeated bytes resources = 1;
repeated bytes patchers = 2;
}

service Module {
Expand Down
12 changes: 6 additions & 6 deletions pkg/modules/proto/module_grpc.pb.go

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

0 comments on commit 1726f10

Please sign in to comment.