Skip to content

Commit 90494eb

Browse files
committed
fix: the concurrent calling issue
1 parent 5229002 commit 90494eb

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

kcl.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[package]
22
name = "kawesome"
3-
version = "0.3.0"
3+
version = "0.3.1"

src/kawesome_generator.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@ import (
1919
)
2020

2121
func main() {
22-
server.Start(&Kawesome{})
22+
server.Start(&KawesomeModGenerator{})
2323
}
2424

25-
// Kawesome implements the Kusion Module generator interface.
25+
// KawesomeModGenerator implements the Kusion Module generator interface.
26+
type KawesomeModGenerator struct{}
27+
28+
// KawesomeCfgContext records the configuration context of Kawesome Module, which can be used for
29+
// the unmarshalling of `devConfig` items and `platformConfig` items.
30+
//
31+
// Note: separating the definitions of `KawesomeModGenerator` and `KawesomeCfgContext` can ensure that
32+
// the call of module generator is stateless, avoiding the issues concurrency.
2633
//
27-
// Note that as an example of a Kusion Module, Kawesome consists of two components, one of which
34+
// As an example of a Kusion Module, Kawesome consists of two components, one of which
2835
// is a 'Service', which is used to generate a Kubernetes Service resource, and the other is a
2936
// 'RandomePassword', which is used to generate a Terraform random_password resource.
3037
//
3138
// Typically, these two resources are not particularly related, but here they are combined to primarily
3239
// illustrate how to develop a Kusion Module.
33-
type Kawesome struct {
40+
type KawesomeCfgContext struct {
3441
// Service is for service configs of kawesome module.
3542
Service Service `yaml:"service,omitempty" json:"service,omitempty"`
3643

@@ -62,7 +69,7 @@ type RandomPassword struct {
6269

6370
// Generate implements the generation logic of kawesome module, including a Kubernetes Service and
6471
// a Terraform random_password resource.
65-
func (k *Kawesome) Generate(ctx context.Context, request *module.GeneratorRequest) (response *module.GeneratorResponse, err error) {
72+
func (*KawesomeModGenerator) Generate(ctx context.Context, request *module.GeneratorRequest) (response *module.GeneratorResponse, err error) {
6673
// Get the module logger with the generator context.
6774
logger := log.GetModuleLogger(ctx)
6875
logger.Info("Generating resources...")
@@ -90,6 +97,9 @@ func (k *Kawesome) Generate(ctx context.Context, request *module.GeneratorReques
9097
return nil, errors.New("port should be binded to a service workload")
9198
}
9299

100+
// Initiate a new KawesomeCfgContext instance.
101+
k := &KawesomeCfgContext{}
102+
93103
// Get the complete kawesome module configs.
94104
if err := k.CompleteConfig(request.DevConfig, request.PlatformConfig); err != nil {
95105
logger.Debug("failed to get complete kawesome module configs: %v", err)
@@ -127,7 +137,7 @@ func (k *Kawesome) Generate(ctx context.Context, request *module.GeneratorReques
127137
}
128138

129139
// CompleteConfig completes the kawesome module configs with both devModuleConfig and platformModuleConfig.
130-
func (k *Kawesome) CompleteConfig(devConfig kusionapiv1.Accessory, platformConfig kusionapiv1.GenericConfig) error {
140+
func (k *KawesomeCfgContext) CompleteConfig(devConfig kusionapiv1.Accessory, platformConfig kusionapiv1.GenericConfig) error {
131141
// Retrieve the config items the developers are concerned about.
132142
if devConfig != nil {
133143
devCfgYamlStr, err := yaml.Marshal(devConfig)
@@ -160,7 +170,7 @@ func (k *Kawesome) CompleteConfig(devConfig kusionapiv1.Accessory, platformConfi
160170
}
161171

162172
// ValidateConfig validates the completed kawesome configs are valid or not.
163-
func (k *Kawesome) ValidateConfig() error {
173+
func (k *KawesomeCfgContext) ValidateConfig() error {
164174
if k.Service.Port < 1 || k.Service.Port > 65535 {
165175
return errors.New("port must be between 1 and 65535")
166176
}
@@ -184,7 +194,7 @@ func (k *Kawesome) ValidateConfig() error {
184194
//
185195
// Note that we will use the SDK provided by the kusion module framework to wrap the Kubernetes resource
186196
// into Kusion resource.
187-
func (k *Kawesome) GenerateServiceResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, error) {
197+
func (k *KawesomeCfgContext) GenerateServiceResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, error) {
188198
// Generate the unique application name with project, stack and app name.
189199
appUniqueName := module.UniqueAppName(request.Project, request.Stack, request.App)
190200
svcType := v1.ServiceTypeClusterIP
@@ -246,7 +256,7 @@ func (k *Kawesome) GenerateServiceResource(request *module.GeneratorRequest) (*k
246256
//
247257
// Note that we will use the SDK provided by the kusion module framework to wrap the Terraform resource
248258
// into Kusion resource.
249-
func (k *Kawesome) GenerateRandomPasswordResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, *kusionapiv1.Patcher, error) {
259+
func (k *KawesomeCfgContext) GenerateRandomPasswordResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, *kusionapiv1.Patcher, error) {
250260
// Set the random_password provider config.
251261
randomPasswordPvdCfg := module.ProviderConfig{
252262
Source: "hashicorp/random",

src/kawesome_generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestKawesomeModule_Generate(t *testing.T) {
258258

259259
for _, tt := range tests {
260260
t.Run(tt.name, func(t *testing.T) {
261-
o := &Kawesome{}
261+
o := &KawesomeModGenerator{}
262262
got, err := o.Generate(context.Background(), tt.args.request)
263263
if (err != nil) != tt.wantErr {
264264
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)

0 commit comments

Comments
 (0)