From e227e1f75aeb469d43298bca8aecdcfc9f809f8a Mon Sep 17 00:00:00 2001 From: liu-hm19 Date: Mon, 8 Jan 2024 11:39:29 +0800 Subject: [PATCH] refactor: update database accessory and related samples (#43) ## What type of PR is this? /kind feature ## What this PR does / why we need it: This PR refactors the database accessory model and updates related samples. ## Which issue(s) this PR fixes: Fixes # ## Special notes for your reviewer: ### Does this PR introduce a user-facing change? ```release-note ``` ### Additional documentation e.g., design docs, usage docs, etc.: ```docs ``` --- models/samples/pgadmin/base/base.k | 46 ++++++++++++ models/samples/pgadmin/prod/kcl.yaml | 6 ++ models/samples/pgadmin/prod/main.k | 10 +++ models/samples/pgadmin/prod/stack.yaml | 1 + models/samples/pgadmin/project.yaml | 4 ++ models/samples/wordpress/base/base.k | 23 ++---- models/samples/wordpress/prod/main.k | 35 ++++++++- models/schema/v1/accessories/database.k | 94 ------------------------- models/schema/v1/accessories/mysql.k | 30 ++++++++ models/schema/v1/accessories/postgres.k | 30 ++++++++ models/schema/v1/app_configuration.k | 10 ++- 11 files changed, 172 insertions(+), 117 deletions(-) create mode 100644 models/samples/pgadmin/base/base.k create mode 100644 models/samples/pgadmin/prod/kcl.yaml create mode 100644 models/samples/pgadmin/prod/main.k create mode 100644 models/samples/pgadmin/prod/stack.yaml create mode 100644 models/samples/pgadmin/project.yaml delete mode 100644 models/schema/v1/accessories/database.k create mode 100644 models/schema/v1/accessories/mysql.k create mode 100644 models/schema/v1/accessories/postgres.k diff --git a/models/samples/pgadmin/base/base.k b/models/samples/pgadmin/base/base.k new file mode 100644 index 0000000..9f7a59a --- /dev/null +++ b/models/samples/pgadmin/base/base.k @@ -0,0 +1,46 @@ +import models.schema.v1 as ac +import models.schema.v1.workload as wl +import models.schema.v1.workload.container as c +import models.schema.v1.workload.network as n +import models.schema.v1.accessories.postgres +import models.schema.v1.workload.secret as sec + +# base.k declares reusable configurations for all stacks. +pgadmin: ac.AppConfiguration { + workload: wl.Service { + containers: { + pgadmin: c.Container { + image: "dpage/pgadmin4:latest" + env: { + "PGADMIN_DEFAULT_EMAIL": "admin@admin.com" + "PGADMIN_DEFAULT_PASSWORD": "secret://pgadmin-secret/pgadmin-default-password" + "PGADMIN_PORT": "80" + } + resources = { + "cpu": "500m" + "memory": "512Mi" + } + } + } + secrets: { + "pgadmin-secret": sec.Secret { + type: "opaque" + data: { + "pgadmin-default-password": "*******" + } + } + } + replicas: 1 + ports: [ + n.Port { + port: 80 + } + ] + } + database: { + pgadmin: postgres.PostgreSQL { + type: "cloud" + version: "14.0" + } + } +} diff --git a/models/samples/pgadmin/prod/kcl.yaml b/models/samples/pgadmin/prod/kcl.yaml new file mode 100644 index 0000000..2236899 --- /dev/null +++ b/models/samples/pgadmin/prod/kcl.yaml @@ -0,0 +1,6 @@ +kcl_cli_configs: + file: + - ../base/base.k + - ./main.k + disable_none: false + \ No newline at end of file diff --git a/models/samples/pgadmin/prod/main.k b/models/samples/pgadmin/prod/main.k new file mode 100644 index 0000000..bb4d3ae --- /dev/null +++ b/models/samples/pgadmin/prod/main.k @@ -0,0 +1,10 @@ +import models.schema.v1 as ac + +# main.k declares customized configurations for prod stack. + +pgadmin: ac.AppConfiguration { + workload.containers.pgadmin: { + # prod stack has different image + image = "dpage/pgadmin4:8.0" + } +} \ No newline at end of file diff --git a/models/samples/pgadmin/prod/stack.yaml b/models/samples/pgadmin/prod/stack.yaml new file mode 100644 index 0000000..b5a631c --- /dev/null +++ b/models/samples/pgadmin/prod/stack.yaml @@ -0,0 +1 @@ +name: prod diff --git a/models/samples/pgadmin/project.yaml b/models/samples/pgadmin/project.yaml new file mode 100644 index 0000000..41d0698 --- /dev/null +++ b/models/samples/pgadmin/project.yaml @@ -0,0 +1,4 @@ +name: pgadmin +generator: + type: AppConfiguration + \ No newline at end of file diff --git a/models/samples/wordpress/base/base.k b/models/samples/wordpress/base/base.k index d65baae..0872f5d 100644 --- a/models/samples/wordpress/base/base.k +++ b/models/samples/wordpress/base/base.k @@ -1,11 +1,8 @@ import models.schema.v1 as ac -import models.schema.v1.trait as t import models.schema.v1.workload as wl import models.schema.v1.workload.container as c -import models.schema.v1.workload.secret as sec import models.schema.v1.workload.network as n -import models.schema.v1.monitoring as m -import models.schema.v1.accessories.database as db +import models.schema.v1.accessories.mysql # base.k declares reusable configurations for all stacks. wordpress: ac.AppConfiguration { @@ -31,18 +28,10 @@ wordpress: ac.AppConfiguration { } ] } - # Declare dependent database resource - database: db.Database { - type: "alicloud" - engine: "MySQL" - version: "5.7" - size: 20 - instanceType: "mysql.n2.serverless.1c" - category: "serverless_basic" - - # SubnetID defines the virtual subnet ID associated with the VPC that the rds - # instance will be created in. The rds instance won't be created in user's own VPC - # if this field is not provided. - subnetID: "your_subnet_id" + database: { + wordpress: mysql.MySQL { + type: "cloud" + version: "8.0" + } } } diff --git a/models/samples/wordpress/prod/main.k b/models/samples/wordpress/prod/main.k index 221a2df..ff28bd3 100644 --- a/models/samples/wordpress/prod/main.k +++ b/models/samples/wordpress/prod/main.k @@ -1,10 +1,39 @@ import models.schema.v1 as ac +import models.schema.v1.workload as wl +import models.schema.v1.workload.container as c +import models.schema.v1.workload.network as n +import models.schema.v1.accessories.mysql # main.k declares customized configurations for prod stack. wordpress: ac.AppConfiguration { - workload.containers.wordpress: { - # prod stack has different image - image = "wordpress:4.8-apache" + workload: wl.Service { + containers: { + wordpress: c.Container { + image = "wordpress:6.3" + env = { + "WORDPRESS_DB_HOST": "$(KUSION_DB_HOST_WORDPRESS)" + "WORDPRESS_DB_USER": "$(KUSION_DB_USERNAME_WORDPRESS)" + "WORDPRESS_DB_PASSWORD": "$(KUSION_DB_PASSWORD_WORDPRESS)" + "WORDPRESS_DB_NAME": "mysql" + } + resources = { + "cpu": "500m" + "memory": "512Mi" + } + } + } + replicas = 1 + ports = [ + n.Port { + port: 80 + } + ] + } + database = { + wordpress: mysql.MySQL { + type: "cloud" + version: "8.0" + } } } diff --git a/models/schema/v1/accessories/database.k b/models/schema/v1/accessories/database.k deleted file mode 100644 index 35af56e..0000000 --- a/models/schema/v1/accessories/database.k +++ /dev/null @@ -1,94 +0,0 @@ -schema Database: - """ As an important supporting accessory, Database describes the attributes - to locally deploy or create a cloud provider managed database instance for - the workload. - - Attributes - ---------- - type: str, default is Undefined, required. - Type defines the local deployment mode or the specific cloud vendor that - provides the relational database service (rds). - engine: str, default is Undefined, required. - Engine defines the database engine to use. - version: str, default is Undefined, required. - Version defines the database engine version to use. - instanceType: str, default is Undefined, optional. - InstanceType defines the type of the database which is required when - creating an rds instance provided by the cloud vendor. - size: int, default is 10, optional. - Size defines the allocated storage size of the rds instance provided by - the cloud vendor in GB. - category: str, default is "Basic", optional. - Category defines the edition of the rds instance provided by the cloud - vendor. - username: str, default is "root", optional. - Username defines the operation account for the database. - securityIPs: [str], default is ["0.0.0.0/0"], optional. - SecurityIPs defines the list of IP addresses allowed to access the rds - instance provided by the cloud vendor. - subnetID: str, default is Undefined, optional. - SubnetID defines the virtual subnet ID associated with the VPC that the rds - instance will be created in. The rds instance won't be created in user's own VPC - if this field is not provided. - privateRouting: bool, default is True, optional. - PrivateRouting defines whether the host address of the rds instance for the workload - to connect with is via public network or private network of the cloud vendor. - extraMap: {str:str}, default is Undefined, optional. - ExtraMap defines the diversified rds configuration items from different - cloud vendors. - - Examples - -------- - Instantiate an aws rds with mysql 5.7. - - import models.schema.v1.accessories.database as db - - database: db.Database { - type: "aws" - engine: "mysql" - version: "5.7" - instanceType: "db.t3.micro" - } - """ - - # The local deployment mode or the specific cloud vendor that provides the - # relational database service (rds). - type: str - - # The database engine to use. - engine: str - - # The database engine version to use. - version: str - - # The type of the database which is required when creating an rds instance - # provided by the cloud vendor. - instanceType?: str - - # The allocated storage size of the rds instance provided by the cloud vendor - # in GB. - size?: int = 10 - - # The edition of the rds instance provided by the cloud vendor. - category?: str = "Basic" - - # The operation account for the database. - username?: str = "root" - - # The list of IP addresses allowed to access the rds instance provided by the - # cloud vendor. - securityIPs?: [str] = ["0.0.0.0/0"] - - # The virtual subnet ID associated with the VPC that the rds instance will be - # created in. - subnetID?: str - - # Whether the host address of the rds instance for the workload to connect with - # is via public network or private network of the cloud vendor. - privateRouting?: bool = True - - # The diversified rds configuration items from different cloud vendors. - extraMap?: {str:str} - - check: - instanceType if type != "local", "instanceType is required for cloud provider managed database" \ No newline at end of file diff --git a/models/schema/v1/accessories/mysql.k b/models/schema/v1/accessories/mysql.k new file mode 100644 index 0000000..10b91bb --- /dev/null +++ b/models/schema/v1/accessories/mysql.k @@ -0,0 +1,30 @@ +schema MySQL: + """ MySQL describes the attributes to locally deploy or create a cloud provider + managed mysql database instance for the workload. + + Attributes + ---------- + type: "local" | "cloud", defaults to Undefined, required. + Type defines whether the mysql database is deployed locally or provided by + cloud vendor. + version: str, defaults to Undefined, required. + Version defines the mysql version to use. + + Examples + -------- + Instantiate a local mysql database with version of 5.7. + + import models.schema.v1.accessories.mysql + + mysql: mysql.MySQL { + type: "local" + version: "5.7" + } + """ + + # The deployment mode of the mysql database. + type: "local" | "cloud" + + # The mysql database version to use. + version: str + \ No newline at end of file diff --git a/models/schema/v1/accessories/postgres.k b/models/schema/v1/accessories/postgres.k new file mode 100644 index 0000000..a4e79a9 --- /dev/null +++ b/models/schema/v1/accessories/postgres.k @@ -0,0 +1,30 @@ +schema PostgreSQL: + """ PostgreSQL describes the attributes to locally deploy or create a cloud provider + managed postgresql database instance for the workload. + + Attributes + ---------- + type: "local" | "cloud", defaults to Undefined, required. + Type defines whether the postgresql database is deployed locally or provided by + cloud vendor. + version: str, defaults to Undefined, required. + Version defines the mysql version to use. + + Examples + -------- + Instantiate a local postgresql database with image version of 14.0. + + import models.schema.v1.accessories.postgres + + postgres: postgres.PostgreSQL { + type: "local" + version: "14.0" + } + """ + + # The deployment mode of the postgresql database. + type: "local" | "cloud" + + # The postgresql database version to use. + version: str + \ No newline at end of file diff --git a/models/schema/v1/app_configuration.k b/models/schema/v1/app_configuration.k index dad3ecc..fb1d0ff 100644 --- a/models/schema/v1/app_configuration.k +++ b/models/schema/v1/app_configuration.k @@ -1,7 +1,8 @@ import models.schema.v1.workload as wl import models.schema.v1.trait as t -import models.schema.v1.accessories.database as db import models.schema.v1.monitoring as m +import models.schema.v1.accessories.mysql +import models.schema.v1.accessories.postgres schema AppConfiguration: """ AppConfiguration is a developer-centric definition that describes how to run an Application. @@ -15,6 +16,9 @@ schema AppConfiguration: includes Service and Job. monitoring: m.Prometheus, default is Undefined, optional. Monitoring specifies how to scrape Prometheus metrics for the workload. + database: {str:mysql.MySQL | postgres.PostgreSQL}, defualt is Undefined, optional. + Database describes a locally deployed or a cloud provider managed database instance + for the workload. Currently supported database profile includes MySQL and PostgreSQL. opsRule: t.OpsRule, default is Undefined, optional. OpsRule specifies collection of rules that will be checked for Day-2 operation. database: db.Database, default is Undefined, optional. @@ -52,8 +56,8 @@ schema AppConfiguration: # OpsRule specifies collection of rules that will be checked for Day-2 operation. opsRule?: t.OpsRule - # Database describes a locally deployed or a cloud provider managed database instance for the workload. - database?: db.Database + # Database describes a locally deployed or a cloud provider managed database instance for the workload. + database?: {str: mysql.MySQL | postgres.PostgreSQL} ###### Other metadata info # Labels and annotations can be used to attach arbitrary metadata as key-value pairs to resources.