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

fix: the concurrent calling issue #12

Merged
merged 1 commit into from
Jan 13, 2025
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
2 changes: 1 addition & 1 deletion kcl.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[package]
name = "kawesome"
version = "0.3.0"
version = "0.3.1"
28 changes: 19 additions & 9 deletions src/kawesome_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ import (
)

func main() {
server.Start(&Kawesome{})
server.Start(&KawesomeModGenerator{})
}

// Kawesome implements the Kusion Module generator interface.
// KawesomeModGenerator implements the Kusion Module generator interface.
type KawesomeModGenerator struct{}

// KawesomeCfgContext records the configuration context of Kawesome Module, which can be used for
// the unmarshalling of `devConfig` items and `platformConfig` items.
//
// Note: separating the definitions of `KawesomeModGenerator` and `KawesomeCfgContext` can ensure that
// the call of module generator is stateless, avoiding the issues concurrency.
//
// Note that as an example of a Kusion Module, Kawesome consists of two components, one of which
// As an example of a Kusion Module, Kawesome consists of two components, one of which
// is a 'Service', which is used to generate a Kubernetes Service resource, and the other is a
// 'RandomePassword', which is used to generate a Terraform random_password resource.
//
// Typically, these two resources are not particularly related, but here they are combined to primarily
// illustrate how to develop a Kusion Module.
type Kawesome struct {
type KawesomeCfgContext struct {
// Service is for service configs of kawesome module.
Service Service `yaml:"service,omitempty" json:"service,omitempty"`

Expand Down Expand Up @@ -62,7 +69,7 @@ type RandomPassword struct {

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

// Initiate a new KawesomeCfgContext instance.
k := &KawesomeCfgContext{}

// Get the complete kawesome module configs.
if err := k.CompleteConfig(request.DevConfig, request.PlatformConfig); err != nil {
logger.Debug("failed to get complete kawesome module configs: %v", err)
Expand Down Expand Up @@ -127,7 +137,7 @@ func (k *Kawesome) Generate(ctx context.Context, request *module.GeneratorReques
}

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

// ValidateConfig validates the completed kawesome configs are valid or not.
func (k *Kawesome) ValidateConfig() error {
func (k *KawesomeCfgContext) ValidateConfig() error {
if k.Service.Port < 1 || k.Service.Port > 65535 {
return errors.New("port must be between 1 and 65535")
}
Expand All @@ -184,7 +194,7 @@ func (k *Kawesome) ValidateConfig() error {
//
// Note that we will use the SDK provided by the kusion module framework to wrap the Kubernetes resource
// into Kusion resource.
func (k *Kawesome) GenerateServiceResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, error) {
func (k *KawesomeCfgContext) GenerateServiceResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, error) {
// Generate the unique application name with project, stack and app name.
appUniqueName := module.UniqueAppName(request.Project, request.Stack, request.App)
svcType := v1.ServiceTypeClusterIP
Expand Down Expand Up @@ -246,7 +256,7 @@ func (k *Kawesome) GenerateServiceResource(request *module.GeneratorRequest) (*k
//
// Note that we will use the SDK provided by the kusion module framework to wrap the Terraform resource
// into Kusion resource.
func (k *Kawesome) GenerateRandomPasswordResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, *kusionapiv1.Patcher, error) {
func (k *KawesomeCfgContext) GenerateRandomPasswordResource(request *module.GeneratorRequest) (*kusionapiv1.Resource, *kusionapiv1.Patcher, error) {
// Set the random_password provider config.
randomPasswordPvdCfg := module.ProviderConfig{
Source: "hashicorp/random",
Expand Down
2 changes: 1 addition & 1 deletion src/kawesome_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestKawesomeModule_Generate(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &Kawesome{}
o := &KawesomeModGenerator{}
got, err := o.Generate(context.Background(), tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
Loading