From f12d66aa72b66211783c58d852f90fa3eb283103 Mon Sep 17 00:00:00 2001 From: liuhaoming Date: Thu, 25 Apr 2024 15:12:55 +0800 Subject: [PATCH] fix: mysql and postgres modules --- modules/mysql/example/dev/main.k | 2 +- modules/mysql/src/local_db.go | 10 +++++++++- modules/mysql/src/mysql.go | 13 ++++++++----- modules/postgres/src/local_db.go | 10 +++++++++- modules/postgres/src/postgres.go | 13 ++++++++----- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/modules/mysql/example/dev/main.k b/modules/mysql/example/dev/main.k index fc14d2e..f5c4d7f 100644 --- a/modules/mysql/example/dev/main.k +++ b/modules/mysql/example/dev/main.k @@ -3,7 +3,7 @@ import kam.v1.app_configuration as ac import kam.v1.workload as wl import kam.v1.workload.container as c import network as n -import mysql.mysql +import mysql.mysql quickstart: ac.AppConfiguration { workload: wl.Service { diff --git a/modules/mysql/src/local_db.go b/modules/mysql/src/local_db.go index 9ca48dd..59b886d 100644 --- a/modules/mysql/src/local_db.go +++ b/modules/mysql/src/local_db.go @@ -132,12 +132,20 @@ func (mysql *MySQL) generateLocalDeployment(request *module.GeneratorRequest) (* func (mysql *MySQL) generateLocalPodSpec(_ *module.GeneratorRequest) (v1.PodSpec, error) { image := dbEngine + ":" + mysql.Version secretName := mysql.DatabaseName + localSecretSuffix + + var portName string + if len(mysql.DatabaseName) > 15 { + portName = mysql.DatabaseName[:15] + } else { + portName = mysql.DatabaseName + } ports := []v1.ContainerPort{ { - Name: mysql.DatabaseName, + Name: portName, ContainerPort: int32(3306), }, } + volumes := []v1.Volume{ { Name: mysql.DatabaseName, diff --git a/modules/mysql/src/mysql.go b/modules/mysql/src/mysql.go index 1e82ef6..5097096 100644 --- a/modules/mysql/src/mysql.go +++ b/modules/mysql/src/mysql.go @@ -110,11 +110,12 @@ func (mysql *MySQL) Generate(_ context.Context, request *module.GeneratorRequest // Generate the MySQL intance resources based on the type and the cloud provider config. var resources []apiv1.Resource var patchers []apiv1.Patcher + var providerType string switch strings.ToLower(mysql.Type) { case LocalDBType: resources, patchers, err = mysql.GenerateLocalResources(request) case CloudDBType: - providerType, err := GetCloudProviderType(request.PlatformModuleConfig) + providerType, err = GetCloudProviderType(request.PlatformModuleConfig) if err != nil { return nil, err } @@ -122,8 +123,14 @@ func (mysql *MySQL) Generate(_ context.Context, request *module.GeneratorRequest switch strings.ToLower(providerType) { case "aws": resources, patchers, err = mysql.GenerateAWSResources(request) + if err != nil { + return nil, err + } case "alicloud": resources, patchers, err = mysql.GenerateAlicloudResources(request) + if err != nil { + return nil, err + } default: return nil, fmt.Errorf("unsupported cloud provider type: %s", providerType) } @@ -131,10 +138,6 @@ func (mysql *MySQL) Generate(_ context.Context, request *module.GeneratorRequest return nil, fmt.Errorf("unsupported mysql type: %s", mysql.Type) } - if err != nil { - return nil, err - } - return &module.GeneratorResponse{ Resources: resources, Patchers: patchers, diff --git a/modules/postgres/src/local_db.go b/modules/postgres/src/local_db.go index 7dcd57e..f8e87cf 100644 --- a/modules/postgres/src/local_db.go +++ b/modules/postgres/src/local_db.go @@ -134,12 +134,20 @@ func (postgres *PostgreSQL) generateLocalDeployment(request *module.GeneratorReq func (postgres *PostgreSQL) generateLocalPodSpec(_ *module.GeneratorRequest) (v1.PodSpec, error) { image := dbEngine + ":" + postgres.Version secretName := postgres.DatabaseName + localSecretSuffix + + var portName string + if len(postgres.DatabaseName) > 15 { + portName = postgres.DatabaseName[:15] + } else { + portName = postgres.DatabaseName + } ports := []v1.ContainerPort{ { - Name: postgres.DatabaseName, + Name: portName, ContainerPort: int32(5432), }, } + volumes := []v1.Volume{ { Name: postgres.DatabaseName, diff --git a/modules/postgres/src/postgres.go b/modules/postgres/src/postgres.go index 1f5a363..6754338 100644 --- a/modules/postgres/src/postgres.go +++ b/modules/postgres/src/postgres.go @@ -112,11 +112,12 @@ func (postgres *PostgreSQL) Generate(_ context.Context, request *module.Generato // Generate the PostgreSQL intance resources based on the type and the cloud provider config. var resources []apiv1.Resource var patchers []apiv1.Patcher + var providerType string switch strings.ToLower(postgres.Type) { case LocalDBType: resources, patchers, err = postgres.GenerateLocalResources(request) case CloudDBType: - providerType, err := GetCloudProviderType(request.PlatformModuleConfig) + providerType, err = GetCloudProviderType(request.PlatformModuleConfig) if err != nil { return nil, err } @@ -124,8 +125,14 @@ func (postgres *PostgreSQL) Generate(_ context.Context, request *module.Generato switch strings.ToLower(providerType) { case "aws": resources, patchers, err = postgres.GenerateAWSResources(request) + if err != nil { + return nil, err + } case "alicloud": resources, patchers, err = postgres.GenerateAlicloudResources(request) + if err != nil { + return nil, err + } default: return nil, fmt.Errorf("unsupported cloud provider type: %s", providerType) } @@ -133,10 +140,6 @@ func (postgres *PostgreSQL) Generate(_ context.Context, request *module.Generato return nil, fmt.Errorf("unsupported postgres type: %s", postgres.Type) } - if err != nil { - return nil, err - } - return &module.GeneratorResponse{ Resources: resources, Patchers: patchers,