Skip to content

Commit

Permalink
feat: update network, mysql and postgres modules (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 authored Jun 6, 2024
1 parent 5eb5e95 commit 3027f49
Show file tree
Hide file tree
Showing 33 changed files with 399 additions and 622 deletions.
8 changes: 4 additions & 4 deletions modules/mysql/example/dev/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name = "example"

[dependencies]
kam = { git = "https://github.com/KusionStack/kam.git", tag = "0.1.0" }
network = { oci = "oci://ghcr.io/kusionstack/network", tag = "0.1.0" }
mysql = { oci = "oci://ghcr.io/kusionstack/mysql", tag = "0.1.0" }
kam = { git = "https://github.com/KusionStack/kam.git", tag = "0.2.0" }
service = {oci = "oci://ghcr.io/kusionstack/service", tag = "0.1.0" }
network = { oci = "oci://ghcr.io/kusionstack/network", tag = "0.2.0" }
mysql = { oci = "oci://ghcr.io/kusionstack/mysql", tag = "0.2.0" }

[profile]
entries = ["main.k"]

8 changes: 4 additions & 4 deletions modules/mysql/example/dev/main.k
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# The configuration codes in perspective of developers.
import kam.v1.app_configuration as ac
import kam.v1.workload as wl
import kam.v1.workload.container as c
import service
import service.container as c
import network as n
import mysql.mysql
import mysql

quickstart: ac.AppConfiguration {
workload: wl.Service {
workload: service.Service {
containers: {
quickstart: c.Container {
image: "kusionstack/kusion-quickstart:latest"
Expand Down
2 changes: 1 addition & 1 deletion modules/mysql/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[package]
name = "mysql"
version = "0.1.0"
version = "0.2.0"
42 changes: 14 additions & 28 deletions modules/mysql/src/alicloud_rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"kusionstack.io/kusion-module-framework/pkg/module"
apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/modules"
)

Expand All @@ -19,7 +19,7 @@ var (
alicloudRDSAccount = "alicloud_rds_account"
)

var defaultAlicloudProviderCfg = apiv1.ProviderConfig{
var defaultAlicloudProviderCfg = module.ProviderConfig{
Source: "aliyun/alicloud",
Version: "1.209.1",
}
Expand All @@ -32,9 +32,8 @@ type alicloudServerlessConfig struct {
}

// GenerateAlicloudResources generates Alicloud provided MySQL database instance.
func (mysql *MySQL) GenerateAlicloudResources(request *module.GeneratorRequest) ([]apiv1.Resource, []apiv1.Patcher, error) {
func (mysql *MySQL) GenerateAlicloudResources(request *module.GeneratorRequest) ([]apiv1.Resource, *apiv1.Patcher, error) {
var resources []apiv1.Resource
var patchers []apiv1.Patcher

// Set the Alicloud provider with the default provider config.
alicloudProviderCfg := defaultAlicloudProviderCfg
Expand Down Expand Up @@ -98,19 +97,18 @@ func (mysql *MySQL) GenerateAlicloudResources(request *module.GeneratorRequest)

// Build Kubernetes Secret with the hostAddress, username and password of the Alicloud provided MySQL instance,
// and inject the credentials as the environment variable patcher.
dbSecret, pathcer, err := mysql.GenerateDBSecret(request, hostAddress, mysql.Username, password)
dbSecret, patcher, err := mysql.GenerateDBSecret(request, hostAddress, mysql.Username, password)
if err != nil {
return nil, nil, err
}
resources = append(resources, *dbSecret)
patchers = append(patchers, *pathcer)

return resources, patchers, nil
return resources, patcher, nil
}

// generateAlicloudDBInstance generates alicloud_db_instance resource
// for the Alicloud provided MySQL database instance.
func (mysql *MySQL) generateAlicloudDBInstance(alicloudProviderCfg apiv1.ProviderConfig,
func (mysql *MySQL) generateAlicloudDBInstance(alicloudProviderCfg module.ProviderConfig,
region string,
) (*apiv1.Resource, string, error) {
resAttrs := map[string]interface{}{
Expand Down Expand Up @@ -146,12 +144,8 @@ func (mysql *MySQL) generateAlicloudDBInstance(alicloudProviderCfg apiv1.Provide
return nil, "", err
}

resExts, err := module.TerraformProviderExtensions(alicloudProviderCfg, map[string]any{"region": region}, alicloudDBInstance)
if err != nil {
return nil, "", err
}

resource, err := module.WrapTFResourceToKusionResource(id, resAttrs, resExts, nil)
alicloudProviderCfg.ProviderMeta = map[string]any{"region": region}
resource, err := module.WrapTFResourceToKusionResource(alicloudProviderCfg, alicloudDBInstance, id, resAttrs, nil)
if err != nil {
return nil, "", err
}
Expand All @@ -161,7 +155,7 @@ func (mysql *MySQL) generateAlicloudDBInstance(alicloudProviderCfg apiv1.Provide

// generateAlicloudDBConnection generates alicloud_db_connection resource
// for the Alicloud provided MySQL database instance.
func (mysql *MySQL) generateAlicloudDBConnection(alicloudProviderCfg apiv1.ProviderConfig,
func (mysql *MySQL) generateAlicloudDBConnection(alicloudProviderCfg module.ProviderConfig,
region, dbInstanceID string,
) (*apiv1.Resource, string, error) {
resAttrs := map[string]interface{}{
Expand All @@ -173,12 +167,8 @@ func (mysql *MySQL) generateAlicloudDBConnection(alicloudProviderCfg apiv1.Provi
return nil, "", err
}

resExts, err := module.TerraformProviderExtensions(alicloudProviderCfg, map[string]any{"region": region}, alicloudDBConnection)
if err != nil {
return nil, "", err
}

resource, err := module.WrapTFResourceToKusionResource(id, resAttrs, resExts, nil)
alicloudProviderCfg.ProviderMeta = map[string]any{"region": region}
resource, err := module.WrapTFResourceToKusionResource(alicloudProviderCfg, alicloudDBConnection, id, resAttrs, nil)
if err != nil {
return nil, "", err
}
Expand All @@ -188,7 +178,7 @@ func (mysql *MySQL) generateAlicloudDBConnection(alicloudProviderCfg apiv1.Provi

// generateAlicloudRDSAccount generates alicloud_rds_account resource
// for the Alicloud provided MySQL database instance.
func (mysql *MySQL) generateAlicloudRDSAccount(alicloudProviderCfg apiv1.ProviderConfig,
func (mysql *MySQL) generateAlicloudRDSAccount(alicloudProviderCfg module.ProviderConfig,
region, accountName, randomPasswordID, dbInstanceID string,
) (*apiv1.Resource, error) {
resAttrs := map[string]interface{}{
Expand All @@ -203,12 +193,8 @@ func (mysql *MySQL) generateAlicloudRDSAccount(alicloudProviderCfg apiv1.Provide
return nil, err
}

resExts, err := module.TerraformProviderExtensions(alicloudProviderCfg, map[string]any{"region": region}, alicloudRDSAccount)
if err != nil {
return nil, err
}

resource, err := module.WrapTFResourceToKusionResource(id, resAttrs, resExts, nil)
alicloudProviderCfg.ProviderMeta = map[string]any{"region": region}
resource, err := module.WrapTFResourceToKusionResource(alicloudProviderCfg, alicloudRDSAccount, id, resAttrs, nil)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions modules/mysql/src/alicloud_rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
"kusionstack.io/kusion-module-framework/pkg/module"
"kusionstack.io/kusion/pkg/apis/core/v1/workload"
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
)

func TestMySQLModule_GenerateAlicloudResources(t *testing.T) {
r := &module.GeneratorRequest{
Project: "test-project",
Stack: "test-stack",
App: "test-app",
Workload: &workload.Workload{
Header: workload.Header{
Workload: &v1.Workload{
Header: v1.Header{
Type: "Service",
},
Service: &workload.Service{},
Service: &v1.Service{},
},
}

Expand Down
30 changes: 10 additions & 20 deletions modules/mysql/src/aws_rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"

"kusionstack.io/kusion-module-framework/pkg/module"
apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/modules"
)

Expand All @@ -18,7 +18,7 @@ var (
awsDBInstance = "aws_db_instance"
)

var defaultAWSProviderCfg = apiv1.ProviderConfig{
var defaultAWSProviderCfg = module.ProviderConfig{
Source: "hashicorp/aws",
Version: "5.0.1",
}
Expand All @@ -36,9 +36,8 @@ type awsSecurityGroupTraffic struct {
}

// GenerateAWSResources generates the AWS provided MySQL database instance.
func (mysql *MySQL) GenerateAWSResources(request *module.GeneratorRequest) ([]apiv1.Resource, []apiv1.Patcher, error) {
func (mysql *MySQL) GenerateAWSResources(request *module.GeneratorRequest) ([]apiv1.Resource, *apiv1.Patcher, error) {
var resources []apiv1.Resource
var pathcers []apiv1.Patcher

// Set the AWS provider with the default provider config.
awsProviderCfg := defaultAWSProviderCfg
Expand Down Expand Up @@ -83,13 +82,12 @@ func (mysql *MySQL) GenerateAWSResources(request *module.GeneratorRequest) ([]ap
return nil, nil, err
}
resources = append(resources, *dbSecret)
pathcers = append(pathcers, *patcher)

return resources, pathcers, nil
return resources, patcher, nil
}

// generateAWSSecurityGroup generates aws_security_group resource for the AWS provided MySQL database instance.
func (mysql *MySQL) generateAWSSecurityGroup(awsProviderCfg apiv1.ProviderConfig, region string) (*apiv1.Resource, string, error) {
func (mysql *MySQL) generateAWSSecurityGroup(awsProviderCfg module.ProviderConfig, region string) (*apiv1.Resource, string, error) {
// SecurityIPs should be in the format of IP address or Classes Inter-Domain
// Routing (CIDR) mode.
for _, ip := range mysql.SecurityIPs {
Expand Down Expand Up @@ -122,12 +120,8 @@ func (mysql *MySQL) generateAWSSecurityGroup(awsProviderCfg apiv1.ProviderConfig
return nil, "", err
}

resExts, err := module.TerraformProviderExtensions(awsProviderCfg, map[string]any{"region": region}, awsSecurityGroup)
if err != nil {
return nil, "", err
}

resource, err := module.WrapTFResourceToKusionResource(id, resAttrs, resExts, nil)
awsProviderCfg.ProviderMeta = map[string]any{"region": region}
resource, err := module.WrapTFResourceToKusionResource(awsProviderCfg, awsSecurityGroup, id, resAttrs, nil)
if err != nil {
return nil, "", err
}
Expand All @@ -136,7 +130,7 @@ func (mysql *MySQL) generateAWSSecurityGroup(awsProviderCfg apiv1.ProviderConfig
}

// generateAWSDBInstance generates aws_db_instance resource for the AWS provided MySQL database instance.
func (mysql *MySQL) generateAWSDBInstance(awsProviderCfg apiv1.ProviderConfig, region, randomPasswordID, awsSecurityGroupID string) (*apiv1.Resource, string, error) {
func (mysql *MySQL) generateAWSDBInstance(awsProviderCfg module.ProviderConfig, region, randomPasswordID, awsSecurityGroupID string) (*apiv1.Resource, string, error) {
resAttrs := map[string]interface{}{
"allocated_storage": mysql.Size,
"engine": dbEngine,
Expand All @@ -161,12 +155,8 @@ func (mysql *MySQL) generateAWSDBInstance(awsProviderCfg apiv1.ProviderConfig, r
return nil, "", err
}

resExts, err := module.TerraformProviderExtensions(awsProviderCfg, map[string]any{"region": region}, awsDBInstance)
if err != nil {
return nil, "", err
}

resource, err := module.WrapTFResourceToKusionResource(id, resAttrs, resExts, nil)
awsProviderCfg.ProviderMeta = map[string]any{"region": region}
resource, err := module.WrapTFResourceToKusionResource(awsProviderCfg, awsDBInstance, id, resAttrs, nil)
if err != nil {
return nil, "", err
}
Expand Down
8 changes: 4 additions & 4 deletions modules/mysql/src/aws_rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
"kusionstack.io/kusion-module-framework/pkg/module"
"kusionstack.io/kusion/pkg/apis/core/v1/workload"
v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
)

func TestMySQLModule_GenerateAWSResources(t *testing.T) {
r := &module.GeneratorRequest{
Project: "test-project",
Stack: "test-stack",
App: "test-app",
Workload: &workload.Workload{
Header: workload.Header{
Workload: &v1.Workload{
Header: v1.Header{
Type: "Service",
},
Service: &workload.Service{},
Service: &v1.Service{},
},
}

Expand Down
54 changes: 23 additions & 31 deletions modules/mysql/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,51 @@ go 1.22.1
require (
github.com/bytedance/mockey v1.2.10
github.com/stretchr/testify v1.9.0
k8s.io/api v0.29.3
k8s.io/apimachinery v0.29.3
kusionstack.io/kusion v0.10.1-0.20240326060146-3f01e9416ff6
kusionstack.io/kusion-module-framework v0.0.0-20240326063408-807a5a4e4682
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
kusionstack.io/kusion v0.12.0-rc.1.0.20240606063448-01e877d0c7ef
kusionstack.io/kusion-module-framework v0.2.0
)

require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-github/v50 v50.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-plugin v1.6.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/arch v0.1.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
kcl-lang.io/kcl-plugin v0.5.0 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 3027f49

Please sign in to comment.