Skip to content

Commit

Permalink
migrate from databaseUsername to databaseAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek committed Feb 26, 2024
1 parent c3d2988 commit ba0040b
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 89 deletions.
19 changes: 6 additions & 13 deletions api/bases/placement.openstack.org_placementapis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,16 @@ spec:
added to to /etc/<service>/<service>.conf.d directory as custom.conf
file.
type: string
databaseAccount:
default: placement
description: DatabaseAccount - name of MariaDBAccount which will be
used to connect.
type: string
databaseInstance:
description: MariaDB instance name Right now required by the maridb-operator
to get the credentials from the instance to create the DB Might
not be required in future
type: string
databaseUser:
default: placement
description: 'DatabaseUser - optional username used for placement
DB, defaults to placement TODO: -> implement needs work in mariadb-operator,
right now only placement'
type: string
defaultConfigOverwrite:
additionalProperties:
type: string
Expand Down Expand Up @@ -264,16 +263,10 @@ spec:
type: object
passwordSelectors:
default:
database: PlacementDatabasePassword
service: PlacementPassword
description: PasswordSelectors - Selectors to identify the DB and
ServiceUser password from the Secret
properties:
database:
default: PlacementDatabasePassword
description: 'Database - Selector to get the Database user password
from the Secret TODO: not used, need change in mariadb-operator'
type: string
service:
default: PlacementPassword
description: Service - Selector to get the service user password
Expand Down Expand Up @@ -343,7 +336,7 @@ spec:
type: object
secret:
description: Secret containing OpenStack password information for
placement PlacementDatabasePassword, PlacementPassword
placement PlacementPassword
type: string
serviceUser:
default: placement
Expand Down
2 changes: 2 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.15.0 // indirect
github.com/onsi/gomega v1.31.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/ginkgo/v2 v2.14.0 h1:vSmGj2Z5YPb9JwCWT6z6ihcUvDhuXLc3sJiqd3jMKAY=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240216173409-86913e6d5885 h1:o7KZaxKt8Dr97ZJIBPW0P482gLyFEURKF89fizcJCBQ=
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240216173409-86913e6d5885/go.mod h1:bQwzyQtWCR9F0+IvWZ30J9d1lB6tcX3CNJ0Ten1smDw=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
14 changes: 4 additions & 10 deletions api/v1beta1/placementapi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ type PlacementAPISpec struct {

// +kubebuilder:validation:Optional
// +kubebuilder:default=placement
// DatabaseUser - optional username used for placement DB, defaults to placement
// TODO: -> implement needs work in mariadb-operator, right now only placement
DatabaseUser string `json:"databaseUser"`
// DatabaseAccount - name of MariaDBAccount which will be used to connect.
DatabaseAccount string `json:"databaseAccount"`

// +kubebuilder:validation:Required
// PlacementAPI Container Image URL (will be set to environmental default if empty)
Expand All @@ -70,11 +69,11 @@ type PlacementAPISpec struct {
Replicas *int32 `json:"replicas"`

// +kubebuilder:validation:Required
// Secret containing OpenStack password information for placement PlacementDatabasePassword, PlacementPassword
// Secret containing OpenStack password information for placement PlacementPassword
Secret string `json:"secret"`

// +kubebuilder:validation:Optional
// +kubebuilder:default={database: PlacementDatabasePassword, service: PlacementPassword}
// +kubebuilder:default={service: PlacementPassword}
// PasswordSelectors - Selectors to identify the DB and ServiceUser password from the Secret
PasswordSelectors PasswordSelector `json:"passwordSelectors"`

Expand Down Expand Up @@ -125,11 +124,6 @@ type APIOverrideSpec struct {

// PasswordSelector to identify the DB and AdminUser password from the Secret
type PasswordSelector struct {
// +kubebuilder:validation:Optional
// +kubebuilder:default="PlacementDatabasePassword"
// Database - Selector to get the Database user password from the Secret
// TODO: not used, need change in mariadb-operator
Database string `json:"database"`
// +kubebuilder:validation:Optional
// +kubebuilder:default="PlacementPassword"
// Service - Selector to get the service user password from the Secret
Expand Down
19 changes: 6 additions & 13 deletions config/crd/bases/placement.openstack.org_placementapis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,16 @@ spec:
added to to /etc/<service>/<service>.conf.d directory as custom.conf
file.
type: string
databaseAccount:
default: placement
description: DatabaseAccount - name of MariaDBAccount which will be
used to connect.
type: string
databaseInstance:
description: MariaDB instance name Right now required by the maridb-operator
to get the credentials from the instance to create the DB Might
not be required in future
type: string
databaseUser:
default: placement
description: 'DatabaseUser - optional username used for placement
DB, defaults to placement TODO: -> implement needs work in mariadb-operator,
right now only placement'
type: string
defaultConfigOverwrite:
additionalProperties:
type: string
Expand Down Expand Up @@ -264,16 +263,10 @@ spec:
type: object
passwordSelectors:
default:
database: PlacementDatabasePassword
service: PlacementPassword
description: PasswordSelectors - Selectors to identify the DB and
ServiceUser password from the Secret
properties:
database:
default: PlacementDatabasePassword
description: 'Database - Selector to get the Database user password
from the Secret TODO: not used, need change in mariadb-operator'
type: string
service:
default: PlacementPassword
description: Service - Selector to get the service user password
Expand Down Expand Up @@ -343,7 +336,7 @@ spec:
type: object
secret:
description: Secret containing OpenStack password information for
placement PlacementDatabasePassword, PlacementPassword
placement PlacementPassword
type: string
serviceUser:
default: placement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ spec:
displayName: Placement API
kind: PlacementAPI
name: placementapis.placement.openstack.org
specDescriptors:
- description: TLS - Parameters related to the TLS
displayName: TLS
path: tls
version: v1beta1
description: Placement Operator
displayName: Placement Operator
Expand Down
2 changes: 1 addition & 1 deletion config/samples/placement_v1beta1_placementapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
[DEFAULT]
debug = true
databaseInstance: openstack
databaseUser: placement
databaseAccount: placement
preserveJobs: false
replicas: 1
secret: placement-secret
Expand Down
2 changes: 1 addition & 1 deletion config/samples/placement_v1beta1_placementtls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spec:
[DEFAULT]
debug = true
databaseInstance: openstack
databaseUser: placement
databaseAccount: placement
preserveJobs: false
replicas: 1
secret: placement-secret
Expand Down
69 changes: 49 additions & 20 deletions controllers/placementapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
types.NamespacedName{Namespace: instance.Namespace, Name: instance.Spec.Secret},
[]string{
instance.Spec.PasswordSelectors.Service,
instance.Spec.PasswordSelectors.Database,
},
h.GetClient(),
&instance.Status.Conditions)
Expand All @@ -349,6 +348,32 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
// all our input checks out so report InputReady
instance.Status.Conditions.MarkTrue(condition.InputReadyCondition, condition.InputReadyMessage)

// ensure MariaDBAccount exists. This account record may be created by
// openstack-operator or the cloud operator up front without a specific
// MariaDBDatabase configured yet. Otherwise, a MariaDBAccount CR is
// created here with a generated username as well as a secret with
// generated password. The MariaDBAccount is created without being
// yet associated with any MariaDBDatabase.
_, _, err = mariadbv1.EnsureMariaDBAccount(
ctx, h, instance.Spec.DatabaseAccount,
instance.Namespace, false, placement.DatabaseName,
)

if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
mariadbv1.MariaDBAccountReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
mariadbv1.MariaDBAccountNotReadyMessage,
err.Error()))

return ctrl.Result{}, err
}
instance.Status.Conditions.MarkTrue(
mariadbv1.MariaDBAccountReadyCondition,
mariadbv1.MariaDBAccountReadyMessage,
)

db, result, err := r.ensureDB(ctx, h, instance)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -441,7 +466,7 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
}

err = r.ensureKeystoneServiceUser(ctx, h, instance)
if err != nil {
if (err != nil || result != ctrl.Result{}) {
return ctrl.Result{}, err
}

Expand All @@ -450,6 +475,7 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
// We can ignore RequeueAfter as we are watching the KeystoneEndpoint resource
return ctrl.Result{}, err
}

result, err = r.ensureDbSync(ctx, instance, h, serviceAnnotations)
if (err != nil || result != ctrl.Result{}) {
return result, err
Expand All @@ -466,6 +492,12 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, nil
}

// remove finalizers from unused MariaDBAccount records
err = mariadbv1.DeleteUnusedMariaDBAccountFinalizers(ctx, h, placement.DatabaseName, instance.Spec.DatabaseAccount, instance.Namespace)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -919,7 +951,7 @@ func (r *PlacementAPIReconciler) reconcileDelete(ctx context.Context, instance *
Log.Info("Reconciling Service delete")

// remove db finalizer before the placement one
db, err := mariadbv1.GetDatabaseByName(ctx, helper, placement.DatabaseName)
db, err := mariadbv1.GetDatabaseByNameAndAccount(ctx, helper, placement.DatabaseName, instance.Spec.DatabaseAccount, instance.Namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -974,23 +1006,16 @@ func (r *PlacementAPIReconciler) ensureDB(
h *helper.Helper,
instance *placementv1.PlacementAPI,
) (*mariadbv1.Database, ctrl.Result, error) {
// (ksambor) should we use NewDatabaseWithNamespace instead?
db := mariadbv1.NewDatabaseWithNamespace(
placement.DatabaseName,
instance.Spec.DatabaseUser,
instance.Spec.Secret,
map[string]string{
"dbName": instance.Spec.DatabaseInstance,
},
placement.DatabaseName,
instance.Namespace,
db := mariadbv1.NewDatabaseForAccount(
instance.Spec.DatabaseInstance, // mariadb/galera service to target
placement.DatabaseName, // name used in CREATE DATABASE in mariadb
placement.DatabaseName, // CR name for MariaDBDatabase
instance.Spec.DatabaseAccount, // CR name for MariaDBAccount
instance.Namespace, // namespace
)

// create or patch the DB
ctrlResult, err := db.CreateOrPatchDBByName(
ctx,
h,
instance.Spec.DatabaseInstance,
)
ctrlResult, err := db.CreateOrPatchAll(ctx, h)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.DBReadyCondition,
Expand Down Expand Up @@ -1216,15 +1241,19 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
if err != nil {
return err
}

databaseAccount := db.GetAccount()
dbSecret := db.GetSecret()

templateParameters := map[string]interface{}{
"ServiceUser": instance.Spec.ServiceUser,
"KeystoneInternalURL": keystoneInternalURL,
"KeystonePublicURL": keystonePublicURL,
"PlacementPassword": string(ospSecret.Data[instance.Spec.PasswordSelectors.Service]),
"log_file": "/var/log/placement/placement-api.log",
"DatabaseConnection": fmt.Sprintf("mysql+pymysql://%s:%s@%s/%s?read_default_file=/etc/my.cnf",
instance.Spec.DatabaseUser,
string(ospSecret.Data[instance.Spec.PasswordSelectors.Database]),
databaseAccount.Spec.UserName,
string(dbSecret.Data[mariadbv1.DatabasePasswordSelector]),
instance.Status.DatabaseHostname,
placement.DatabaseName,
),
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/go-logr/logr v1.4.1
github.com/google/uuid v1.6.0
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
github.com/onsi/ginkgo/v2 v2.14.0
github.com/onsi/gomega v1.30.0
github.com/onsi/ginkgo/v2 v2.15.0
github.com/onsi/gomega v1.31.1
github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240219094943-9bbb46c9afba
github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240216173409-86913e6d5885
github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20240216173409-86913e6d5885
Expand Down Expand Up @@ -87,3 +87,5 @@ replace github.com/openstack-k8s-operators/placement-operator/api => ./api
// mschuppert: map to latest commit from release-4.13 tag
// must consistent within modules and service operators
replace github.com/openshift/api => github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 //allow-merging

replace github.com/openstack-k8s-operators/mariadb-operator/api => github.com/zzzeek/mariadb-operator/api v0.3.1-0.20240223174203-130ff24a0c90 //allow-merging
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/onsi/ginkgo/v2 v2.14.0 h1:vSmGj2Z5YPb9JwCWT6z6ihcUvDhuXLc3sJiqd3jMKAY=
github.com/onsi/ginkgo/v2 v2.14.0/go.mod h1:JkUdW7JkN0V6rFvsHcJ478egV3XH9NxpD27Hal/PhZw=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/onsi/gomega v1.31.1/go.mod h1:y40C95dwAD1Nz36SsEnxvfFe8FFfNxzI5eJ0EYGyAy0=
github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 h1:rncLxJBpFGqBztyxCMwNRnMjhhIDOWHJowi6q8G6koI=
github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7/go.mod h1:ctXNyWanKEjGj8sss1KjjHQ3ENKFm33FFnS5BKaIPh4=
github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240219094943-9bbb46c9afba h1:E/4DVkBwTxAgea7NRtkStoz66cUXZacZmEJlGcr5/0o=
Expand All @@ -99,8 +99,6 @@ github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.2024021
github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.20240216173409-86913e6d5885/go.mod h1:8QsCFttAm+X6A8I8EQThGjNjeMAYt2hK7ivbvnR3434=
github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20240216173409-86913e6d5885 h1:ioJ2MO3vAcBkLM+0UBu5IuKW/DPXcyiNSOLq0Xvn+Nw=
github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20240216173409-86913e6d5885/go.mod h1:82nzS+DbBe1tzaMvNHH8FctmZzQ14ZAJysFGsMJiivo=
github.com/openstack-k8s-operators/mariadb-operator/api v0.3.1-0.20240220132409-f96d4d040f4e h1:6vqp5HZwcGvPH0MII/23iCd97T3/1HJZlONKW6LyNio=
github.com/openstack-k8s-operators/mariadb-operator/api v0.3.1-0.20240220132409-f96d4d040f4e/go.mod h1:PDqfLbP4ZWqQHAu1OtbjfpOGQUKSzLqRJChvE/9pcyQ=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -130,6 +128,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zzzeek/mariadb-operator/api v0.3.1-0.20240223174203-130ff24a0c90 h1:oFD6wDyzRjeh069l8JrqzZOpq9qFbM4ZMqs7hjwOXa0=
github.com/zzzeek/mariadb-operator/api v0.3.1-0.20240223174203-130ff24a0c90/go.mod h1:f9IIyWeoskWoeWaDFF3qmAJ2Kqyovfi0Ar/QUfk3qag=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Names struct {
ConfigMapName types.NamespacedName
DBSyncJobName types.NamespacedName
MariaDBDatabaseName types.NamespacedName
MariaDBAccount types.NamespacedName
DeploymentName types.NamespacedName
PublicServiceName types.NamespacedName
InternalServiceName types.NamespacedName
Expand All @@ -60,6 +61,9 @@ func CreateNames(placementAPIName types.NamespacedName) Names {
MariaDBDatabaseName: types.NamespacedName{
Namespace: placementAPIName.Namespace,
Name: placement.DatabaseName},
MariaDBAccount: types.NamespacedName{
Namespace: placementAPIName.Namespace,
Name: "placement"},
DeploymentName: types.NamespacedName{
Namespace: placementAPIName.Namespace,
Name: placementAPIName.Name},
Expand Down
Loading

0 comments on commit ba0040b

Please sign in to comment.