Skip to content
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Use the status connectionStatus field to check the status of the MySQL connectio
### RelationalDatabaseProvider Spec Fields

- kind (required): The type of database provider, which can be either mysql or postgresql.
- scope (required): Defines the scope of the database request, which influences the environment setup. Valid values are production, development, and custom. Defaults to development if not specified.
- selector (required): Defines the selector of the database request, which influences the environment setup. Valid values are production, development, and custom. Defaults to development if not specified.
- connections (required): A list of `connection` objects that detail the connection parameters to MySQL or PostgreSQL databases. At least one connection must be defined.

- connection Fields
Expand Down Expand Up @@ -116,7 +116,7 @@ metadata:
name: example-mysql-provider
spec:
kind: mysql
scope: development
selector: development
mysqlConnections:
- name: primary-db
hostname: primary-db.example.com
Expand All @@ -130,12 +130,12 @@ spec:

## DatabaseRequest CRD Documentation

The `DatabaseRequest` Custom Resource Definition (CRD) provides a mechanism for requesting database instances within a Kubernetes cluster. This resource allows lagoon to specify their requirements for a database, including the type, scope, and optional parameters such as seeding data, additional users, and database connection references. This documentation outlines the structure and functionality of the `DatabaseRequest` resource to facilitate its usage.
The `DatabaseRequest` Custom Resource Definition (CRD) provides a mechanism for requesting database instances within a Kubernetes cluster. This resource allows lagoon to specify their requirements for a database, including the type, selector, and optional parameters such as seeding data, additional users, and database connection references. This documentation outlines the structure and functionality of the `DatabaseRequest` resource to facilitate its usage.

### DatabaseRequest Spec Fields

- name (required): Defines the intended service name for the database. This field is required and must be unique within the namespace.
- scope (required): Defines the intended use of the requested database. It helps in configuring the database appropriately for its intended environment. Valid options are production, development, and custom. The default value is development.
- selector (required): Defines the intended use of the requested database. It helps in configuring the database appropriately for its intended environment. Valid options are production, development, and custom. The default value is development.
- type (required): Specifies the type of database requested. Supported types are mysql, mariadb, postgres, and mongodb.
- seed (optional): A reference to a local Kubernetes secret within the same namespace that contains data used for seeding the database. This field is optional and intended for initial database setup.
- additionalUsers (optional): Specifies the creation of additional database users.
Expand All @@ -162,7 +162,7 @@ kind: DatabaseRequest
metadata:
name: example-database-request
spec:
scope: development
selector: development
type: mysql
additionalUsers:
type: read-write
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/databaserequest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ type DatabaseRequestSpec struct {

//+kubebuilder:required
//+kubebuilder:validation:Required
// Scope is the scope of the database request, this is used to select a provider from a pool of scopes
Scope string `json:"scope"`
// Selector is the name of the database request, this is used to select a provider from a pool of providers with the same selector
Selector string `json:"selector"`

//+kubebuilder:required
//+kubebuilder:validation:Required
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/mongodbprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ type MongoDBConnection struct {
type MongoDBProviderSpec struct {
//+kubebuilder:required
//+kubebuilder:validation:Required
// Scope is the scope of the database request, this is used to select a provider from a pool of scopes
Scope string `json:"scope"`
// Selector is the name of the database request, this is used to select a provider from a pool of providers with the same selector
Selector string `json:"selector"`

//+kubebuilder:validation:MinItems=1
// Connections defines the connection to a relational database
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/relationaldatabaseprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ type RelationalDatabaseProviderSpec struct {

//+kubebuilder:required
//+kubebuilder:validation:Required
// Scope is the scope of the database request, this is used to select a provider from a pool of scopes
Scope string `json:"scope"`
// Selector is the name of the database request, this is used to select a provider from a pool of providers with the same selector
Selector string `json:"selector"`

//+kubebuilder:validation:MinItems=1
// Connections defines the connection to a relational database
Expand Down
11 changes: 6 additions & 5 deletions config/crd/bases/crd.lagoon.sh_databaserequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ spec:
Name is used for the service name and the prefix in the secret data
for example mariadb-0
type: string
scope:
description: Scope is the scope of the database request, this is used
to select a provider from a pool of scopes
type: string
seed:
description: |-
Seed is the seed for the database request
Expand All @@ -156,6 +152,11 @@ spec:
type: string
type: object
x-kubernetes-map-type: atomic
selector:
description: Selector is the name of the database request, this is
used to select a provider from a pool of providers with the same
selector
type: string
type:
description: |-
Type is the type of the database request
Expand All @@ -168,7 +169,7 @@ spec:
type: string
required:
- name
- scope
- selector
- type
type: object
status:
Expand Down
9 changes: 5 additions & 4 deletions config/crd/bases/crd.lagoon.sh_mongodbproviders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ spec:
type: object
minItems: 1
type: array
scope:
description: Scope is the scope of the database request, this is used
to select a provider from a pool of scopes
selector:
description: Selector is the name of the database request, this is
used to select a provider from a pool of providers with the same
selector
type: string
required:
- connections
- scope
- selector
type: object
status:
description: MongoDBProviderStatus defines the observed state of MongoDBProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ spec:
type: object
minItems: 1
type: array
scope:
description: Scope is the scope of the database request, this is used
to select a provider from a pool of scopes
selector:
description: Selector is the name of the database request, this is
used to select a provider from a pool of providers with the same
selector
type: string
type:
description: |-
Expand All @@ -114,7 +115,7 @@ spec:
type: string
required:
- connections
- scope
- selector
- type
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ metadata:
name: databaserequest-mysql-scope-sample
spec:
name: first-mysql-scope-db
scope: vip-development
selector: vip-development
type: mysql
2 changes: 1 addition & 1 deletion config/samples/crd_v1alpha1_databaserequest_mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ metadata:
name: databaserequest-mysql-sample
spec:
name: first-mysql-db
scope: development
selector: development
type: mysql
2 changes: 1 addition & 1 deletion config/samples/crd_v1alpha1_databaserequest_postgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ metadata:
name: databaserequest-postgres-sample
spec:
name: first-postgres-db
scope: development
selector: development
type: postgres
2 changes: 1 addition & 1 deletion config/samples/crd_v1alpha1_databaserequest_seed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ spec:
seed:
name: seed-mysql-secret
namespace: default
scope: development
selector: development
type: mysql
2 changes: 1 addition & 1 deletion config/samples/crd_v1alpha1_mongodbprovider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
app.kubernetes.io/managed-by: kustomize
name: mongodbprovider-sample
spec:
scope: development
selector: development
connections:
- name: primary-test-mongodb-connection
hostname: mongodb-service.mongodb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
name: relationaldatabaseprovider-mysql-scope-sample
spec:
type: mysql
scope: vip-development
selector: vip-development
connections:
- name: primary-test-mysql-connection
hostname: mysql-service.mysql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
name: relationaldatabaseprovider-mysql-sample
spec:
type: mysql
scope: development
selector: development
connections:
- name: primary-test-mysql-connection
hostname: mysql-service.mysql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
name: relationaldatabaseprovider-postgres-sample
spec:
type: postgres
scope: development
selector: development
connections:
- name: primary-test-postgres-connection
hostname: postgres-service.postgres
Expand Down
24 changes: 12 additions & 12 deletions internal/controller/databaserequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var (
Name: "databaserequest_reconcile_error_total",
Help: "The total number of reconciled database requests errors",
},
[]string{"name", "namespace", "scope", "type", "username", "databasename", "error"},
[]string{"name", "namespace", "selector", "type", "username", "databasename", "error"},
)

// promDatabaseRequestReconcileStatus is the status of the reconciled database requests
Expand All @@ -76,7 +76,7 @@ var (
Name: "databaserequest_reconcile_status",
Help: "The status of the reconciled database requests",
},
[]string{"name", "namespace", "scope", "type", "username", "databasename"},
[]string{"name", "namespace", "selector", "type", "username", "databasename"},
)
)

Expand Down Expand Up @@ -125,7 +125,7 @@ func (r *DatabaseRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
logger = logger.WithValues("scope", databaseRequest.Spec.Scope, "type", databaseRequest.Spec.Type)
logger = logger.WithValues("selector", databaseRequest.Spec.Selector, "type", databaseRequest.Spec.Type)
log.IntoContext(ctx, logger)

if databaseRequest.DeletionTimestamp != nil && !databaseRequest.DeletionTimestamp.IsZero() {
Expand Down Expand Up @@ -315,7 +315,7 @@ func (r *DatabaseRequestReconciler) handleSeed(
ctx,
databaseRequest.Spec.Seed,
databaseRequest.Spec.Type,
databaseRequest.Spec.Scope,
databaseRequest.Spec.Selector,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -567,7 +567,7 @@ func promLabels(databaseRequest *crdv1alpha1.DatabaseRequest, withError string)
labels := prometheus.Labels{
"name": databaseRequest.Name,
"namespace": databaseRequest.Namespace,
"scope": databaseRequest.Spec.Scope,
"selector": databaseRequest.Spec.Selector,
"type": databaseRequest.Spec.Type,
"username": username,
"databasename": databaseName,
Expand Down Expand Up @@ -676,8 +676,8 @@ func (r *DatabaseRequestReconciler) relationalDatabaseOperation(

// get the database provider, for info and drop we use the reference which is already set on the database request
// if not we error out.
// For create we list all database providers and check if the scope matches and if
// there are more than one provider with the same scope, we select the one with lower load.
// For create we list all database providers and check if the selector matches and if
// there are more than one provider with the same selector, we select the one with lower load.
databaseProvider := &crdv1alpha1.RelationalDatabaseProvider{}
connectionName := ""
if operation == create {
Expand Down Expand Up @@ -830,7 +830,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseInfoFromSeed(
ctx context.Context,
seed *v1.SecretReference,
dbType string,
scope string,
selector string,
) (*seedDatabaseInfo, error) {
dbInfo, err := r.seedDatabase(ctx, seed)
if err != nil {
Expand All @@ -852,7 +852,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseInfoFromSeed(
var connection *crdv1alpha1.Connection
var databaseProviderRef *crdv1alpha1.DatabaseConnectionReference
for _, dbProvider := range dbProviders.Items {
if dbProvider.Spec.Scope == scope && dbProvider.Spec.Type == dbType {
if dbProvider.Spec.Selector == selector && dbProvider.Spec.Type == dbType {
for _, dbConnection := range dbProvider.Spec.Connections {
if dbConnection.Enabled && dbConnection.Hostname == dbInfo.hostName &&
dbConnection.Port == dbInfo.port {
Expand Down Expand Up @@ -904,7 +904,7 @@ func (r *DatabaseRequestReconciler) relationalDatabaseInfoFromSeed(
return &seedDatabaseInfo{dbInfo: dbInfo, conn: conn, databaseProviderRef: databaseProviderRef}, nil
}

// findRelationalDatabaseProvider finds the relational database provider with the same scope and the lower load
// findRelationalDatabaseProvider finds the relational database provider with the same selector and the lower load
// returns the provider, connection name and an error
func (r *DatabaseRequestReconciler) findRelationalDatabaseProvider(
ctx context.Context,
Expand All @@ -917,13 +917,13 @@ func (r *DatabaseRequestReconciler) findRelationalDatabaseProvider(
)
}

// find the provider with the same scope
// find the provider with the same selector
// set load to the max int value to find the provider with the lower
load := int(^uint(0) >> 1)
var provider *crdv1alpha1.RelationalDatabaseProvider
var connName string
for _, dbProvider := range dbProviders.Items {
if dbProvider.Spec.Scope == databaseRequest.Spec.Scope && dbProvider.Spec.Type == databaseRequest.Spec.Type {
if dbProvider.Spec.Selector == databaseRequest.Spec.Selector && dbProvider.Spec.Type == databaseRequest.Spec.Type {
log.FromContext(ctx).Info("Found provider", "provider", dbProvider.Name)
for _, dbConnection := range dbProvider.Spec.Connections {
if dbConnection.Enabled {
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/databaserequest_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ var _ = Describe("DatabaseRequest Controller", func() {
Name: dbMySQLProviderResource,
},
Spec: crdv1alpha1.RelationalDatabaseProviderSpec{
Type: "mysql",
Scope: "development",
Type: "mysql",
Selector: "development",
Connections: []crdv1alpha1.Connection{
{
Name: "test-connection",
Expand Down Expand Up @@ -107,7 +107,7 @@ var _ = Describe("DatabaseRequest Controller", func() {
Namespace: "default",
},
Spec: crdv1alpha1.DatabaseRequestSpec{
Scope: "development",
Selector: "development",
Type: "mysql",
Name: "mysql-test-database",
DropDatabaseOnDelete: false,
Expand Down
Loading