@@ -19,18 +19,25 @@ import (
19
19
)
20
20
21
21
func main () {
22
- server .Start (& Kawesome {})
22
+ server .Start (& KawesomeModGenerator {})
23
23
}
24
24
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.
26
33
//
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
28
35
// is a 'Service', which is used to generate a Kubernetes Service resource, and the other is a
29
36
// 'RandomePassword', which is used to generate a Terraform random_password resource.
30
37
//
31
38
// Typically, these two resources are not particularly related, but here they are combined to primarily
32
39
// illustrate how to develop a Kusion Module.
33
- type Kawesome struct {
40
+ type KawesomeCfgContext struct {
34
41
// Service is for service configs of kawesome module.
35
42
Service Service `yaml:"service,omitempty" json:"service,omitempty"`
36
43
@@ -62,7 +69,7 @@ type RandomPassword struct {
62
69
63
70
// Generate implements the generation logic of kawesome module, including a Kubernetes Service and
64
71
// 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 ) {
66
73
// Get the module logger with the generator context.
67
74
logger := log .GetModuleLogger (ctx )
68
75
logger .Info ("Generating resources..." )
@@ -90,6 +97,9 @@ func (k *Kawesome) Generate(ctx context.Context, request *module.GeneratorReques
90
97
return nil , errors .New ("port should be binded to a service workload" )
91
98
}
92
99
100
+ // Initiate a new KawesomeCfgContext instance.
101
+ k := & KawesomeCfgContext {}
102
+
93
103
// Get the complete kawesome module configs.
94
104
if err := k .CompleteConfig (request .DevConfig , request .PlatformConfig ); err != nil {
95
105
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
127
137
}
128
138
129
139
// 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 {
131
141
// Retrieve the config items the developers are concerned about.
132
142
if devConfig != nil {
133
143
devCfgYamlStr , err := yaml .Marshal (devConfig )
@@ -160,7 +170,7 @@ func (k *Kawesome) CompleteConfig(devConfig kusionapiv1.Accessory, platformConfi
160
170
}
161
171
162
172
// ValidateConfig validates the completed kawesome configs are valid or not.
163
- func (k * Kawesome ) ValidateConfig () error {
173
+ func (k * KawesomeCfgContext ) ValidateConfig () error {
164
174
if k .Service .Port < 1 || k .Service .Port > 65535 {
165
175
return errors .New ("port must be between 1 and 65535" )
166
176
}
@@ -184,7 +194,7 @@ func (k *Kawesome) ValidateConfig() error {
184
194
//
185
195
// Note that we will use the SDK provided by the kusion module framework to wrap the Kubernetes resource
186
196
// 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 ) {
188
198
// Generate the unique application name with project, stack and app name.
189
199
appUniqueName := module .UniqueAppName (request .Project , request .Stack , request .App )
190
200
svcType := v1 .ServiceTypeClusterIP
@@ -246,7 +256,7 @@ func (k *Kawesome) GenerateServiceResource(request *module.GeneratorRequest) (*k
246
256
//
247
257
// Note that we will use the SDK provided by the kusion module framework to wrap the Terraform resource
248
258
// 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 ) {
250
260
// Set the random_password provider config.
251
261
randomPasswordPvdCfg := module.ProviderConfig {
252
262
Source : "hashicorp/random" ,
0 commit comments