Skip to content

Commit 69fbe67

Browse files
committed
add conditions for mariadbdatabase
by adding conditions, we allow the mariadbaccount to start its work before the mariadbdatabase is fully completed. this also brings MariaDBDatabase into the same condition style as other controllers in play.
1 parent 6fb96fd commit 69fbe67

File tree

8 files changed

+215
-6
lines changed

8 files changed

+215
-6
lines changed

api/bases/mariadb.openstack.org_mariadbdatabases.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,49 @@ spec:
5858
properties:
5959
completed:
6060
type: boolean
61+
conditions:
62+
description: Deployment Conditions
63+
items:
64+
description: Condition defines an observation of a API resource
65+
operational state.
66+
properties:
67+
lastTransitionTime:
68+
description: Last time the condition transitioned from one status
69+
to another. This should be when the underlying condition changed.
70+
If that is not known, then using the time when the API field
71+
changed is acceptable.
72+
format: date-time
73+
type: string
74+
message:
75+
description: A human readable message indicating details about
76+
the transition.
77+
type: string
78+
reason:
79+
description: The reason for the condition's last transition
80+
in CamelCase.
81+
type: string
82+
severity:
83+
description: Severity provides a classification of Reason code,
84+
so the current situation is immediately understandable and
85+
could act accordingly. It is meant for situations where Status=False
86+
and it should be indicated if it is just informational, warning
87+
(next reconciliation might fix it) or an error (e.g. DB create
88+
issue and no actions to automatically resolve the issue can/should
89+
be done). For conditions where Status=Unknown or Status=True
90+
the Severity should be SeverityNone.
91+
type: string
92+
status:
93+
description: Status of the condition, one of True, False, Unknown.
94+
type: string
95+
type:
96+
description: Type of condition in CamelCase.
97+
type: string
98+
required:
99+
- lastTransitionTime
100+
- status
101+
- type
102+
type: object
103+
type: array
61104
hash:
62105
additionalProperties:
63106
type: string

api/v1beta1/conditions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ const (
7777

7878
MariaDBServerReadyMessage = "MariaDB / Galera server ready"
7979

80+
MariaDBServerNotBootstrappedMessage = "MariaDB / Galera server not bootstrapped"
81+
8082
MariaDBAccountReadyInitMessage = "MariaDBAccount create / drop not started"
8183

8284
MariaDBAccountReadyMessage = "MariaDBAccount creation complete"

api/v1beta1/mariadbdatabase_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
)
2223

@@ -44,6 +45,9 @@ type MariaDBDatabaseSpec struct {
4445

4546
// MariaDBDatabaseStatus defines the observed state of MariaDBDatabase
4647
type MariaDBDatabaseStatus struct {
48+
// Deployment Conditions
49+
Conditions condition.Conditions `json:"conditions,omitempty" optional:"true"`
50+
4751
Completed bool `json:"completed,omitempty"`
4852
// Map of hashes to track e.g. job status
4953
Hash map[string]string `json:"hash,omitempty"`

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/mariadb.openstack.org_mariadbdatabases.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,49 @@ spec:
5858
properties:
5959
completed:
6060
type: boolean
61+
conditions:
62+
description: Deployment Conditions
63+
items:
64+
description: Condition defines an observation of a API resource
65+
operational state.
66+
properties:
67+
lastTransitionTime:
68+
description: Last time the condition transitioned from one status
69+
to another. This should be when the underlying condition changed.
70+
If that is not known, then using the time when the API field
71+
changed is acceptable.
72+
format: date-time
73+
type: string
74+
message:
75+
description: A human readable message indicating details about
76+
the transition.
77+
type: string
78+
reason:
79+
description: The reason for the condition's last transition
80+
in CamelCase.
81+
type: string
82+
severity:
83+
description: Severity provides a classification of Reason code,
84+
so the current situation is immediately understandable and
85+
could act accordingly. It is meant for situations where Status=False
86+
and it should be indicated if it is just informational, warning
87+
(next reconciliation might fix it) or an error (e.g. DB create
88+
issue and no actions to automatically resolve the issue can/should
89+
be done). For conditions where Status=Unknown or Status=True
90+
the Severity should be SeverityNone.
91+
type: string
92+
status:
93+
description: Status of the condition, one of True, False, Unknown.
94+
type: string
95+
type:
96+
description: Type of condition in CamelCase.
97+
type: string
98+
required:
99+
- lastTransitionTime
100+
- status
101+
- type
102+
type: object
103+
type: array
61104
hash:
62105
additionalProperties:
63106
type: string

controllers/mariadbaccount_controller.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func (r *MariaDBAccountReconciler) reconcileCreate(
167167
instance.Name, instance.ObjectMeta.Labels["mariaDBDatabaseName"]))
168168

169169
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
170-
} else if err == nil && !mariadbDatabase.Status.Completed {
171-
// found but not in completed status.
170+
} else if err == nil && !mariadbDatabase.Status.Conditions.IsTrue(databasev1beta1.MariaDBDatabaseReadyCondition) {
171+
// found but database not ready
172172

173173
// for the create case, need to wait for the MariaDBDatabase to exists before we can continue;
174174
// requeue
@@ -356,8 +356,8 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
356356
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
357357

358358
return ctrl.Result{}, nil
359-
} else if err == nil && !mariadbDatabase.Status.Completed {
360-
// found but not in completed status.
359+
} else if err == nil && !mariadbDatabase.Status.Conditions.IsTrue(databasev1beta1.MariaDBDatabaseReadyCondition) {
360+
// found but database is not ready
361361

362362
// for the delete case, the database doesn't exist. so
363363
// that means we don't, either. remove finalizer from
@@ -427,6 +427,14 @@ func (r *MariaDBAccountReconciler) reconcileDelete(
427427

428428
if !dbGalera.Status.Bootstrapped {
429429
log.Info("DB bootstrap not complete. Requeue...")
430+
431+
instance.Status.Conditions.MarkFalse(
432+
databasev1beta1.MariaDBServerReadyCondition,
433+
databasev1beta1.ReasonDBWaitingInitialized,
434+
condition.SeverityInfo,
435+
databasev1beta1.MariaDBServerNotBootstrappedMessage,
436+
)
437+
430438
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
431439
}
432440

controllers/mariadbdatabase_controller.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/client"
2929
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3030

31+
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
3132
helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
3233
job "github.com/openstack-k8s-operators/lib-common/modules/common/job"
3334
databasev1beta1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
@@ -73,13 +74,49 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
7374

7475
// Always patch the instance status when exiting this function so we can persist any changes.
7576
defer func() {
77+
if instance.Status.Conditions.AllSubConditionIsTrue() {
78+
// update the Ready condition based on the sub conditions
79+
80+
instance.Status.Conditions.MarkTrue(
81+
condition.ReadyCondition, condition.ReadyMessage)
82+
} else {
83+
// something is not ready so reset the Ready condition
84+
instance.Status.Conditions.MarkUnknown(
85+
condition.ReadyCondition, condition.InitReason, condition.ReadyInitMessage)
86+
// and recalculate it based on the state of the rest of the conditions
87+
instance.Status.Conditions.Set(
88+
instance.Status.Conditions.Mirror(condition.ReadyCondition))
89+
}
90+
91+
if instance.Status.Conditions.IsTrue(condition.ReadyCondition) {
92+
// database creation finished... okay to set to completed
93+
instance.Status.Completed = true
94+
}
95+
7696
err := helper.PatchInstance(ctx, instance)
97+
7798
if err != nil {
7899
_err = err
79100
return
80101
}
102+
81103
}()
82104

105+
if instance.Status.Conditions == nil {
106+
instance.Status.Conditions = condition.Conditions{}
107+
108+
// initialize conditions used later as Status=Unknown
109+
cl := condition.CreateList(
110+
condition.UnknownCondition(databasev1beta1.MariaDBServerReadyCondition, condition.InitReason, databasev1beta1.MariaDBServerReadyInitMessage),
111+
condition.UnknownCondition(databasev1beta1.MariaDBDatabaseReadyCondition, condition.InitReason, databasev1beta1.MariaDBDatabaseReadyInitMessage),
112+
)
113+
114+
instance.Status.Conditions.Init(&cl)
115+
116+
// Register overall status immediately to have an early feedback e.g. in the cli
117+
return ctrl.Result{}, nil
118+
}
119+
83120
// Fetch the Galera instance from which we'll pull the credentials
84121
dbGalera, err := r.getDatabaseObject(ctx, instance)
85122

@@ -133,6 +170,14 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
133170

134171
if !dbGalera.Status.Bootstrapped {
135172
log.Info("DB bootstrap not complete. Requeue...")
173+
174+
instance.Status.Conditions.MarkFalse(
175+
databasev1beta1.MariaDBServerReadyCondition,
176+
databasev1beta1.ReasonDBWaitingInitialized,
177+
condition.SeverityInfo,
178+
databasev1beta1.MariaDBServerNotBootstrappedMessage,
179+
)
180+
136181
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
137182
}
138183

@@ -141,6 +186,11 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
141186
dbContainerImage = dbGalera.Spec.ContainerImage
142187
serviceAccount = dbGalera.RbacResourceName()
143188

189+
instance.Status.Conditions.MarkTrue(
190+
databasev1beta1.MariaDBServerReadyCondition,
191+
databasev1beta1.MariaDBServerReadyMessage,
192+
)
193+
144194
// Define a new Job object (hostname, password, containerImage)
145195
jobDef, err := mariadb.DbDatabaseJob(instance, dbName, dbSecret, dbContainerImage, serviceAccount)
146196
if err != nil {
@@ -173,8 +223,10 @@ func (r *MariaDBDatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Requ
173223
log.Info("Job hash added", "Job", jobDef.Name, "Hash", instance.Status.Hash[databasev1beta1.DbCreateHash])
174224
}
175225

176-
// database creation finished... okay to set to completed
177-
instance.Status.Completed = true
226+
instance.Status.Conditions.MarkTrue(
227+
databasev1beta1.MariaDBDatabaseReadyCondition,
228+
databasev1beta1.MariaDBDatabaseReadyMessage,
229+
)
178230

179231
return ctrl.Result{}, nil
180232
}

tests/kuttl/tests/database_create/02-assert.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,53 @@ metadata:
1212
name: kuttldb-latin1-db-create
1313
status:
1414
succeeded: 1
15+
---
16+
apiVersion: mariadb.openstack.org/v1beta1
17+
kind: MariaDBDatabase
18+
metadata:
19+
name: kuttldb-utf8
20+
labels:
21+
dbName: openstack
22+
spec:
23+
secret: osp-secret
24+
name: kuttldb_utf8
25+
status:
26+
conditions:
27+
- message: Setup complete
28+
reason: Ready
29+
status: "True"
30+
type: Ready
31+
- message: MariaDBDatabase ready
32+
reason: Ready
33+
status: "True"
34+
type: MariaDBDatabaseReady
35+
- message: MariaDB / Galera server ready
36+
reason: Ready
37+
status: "True"
38+
type: MariaDBServerReady
39+
completed: true
40+
---
41+
apiVersion: mariadb.openstack.org/v1beta1
42+
kind: MariaDBDatabase
43+
metadata:
44+
name: kuttldb-latin1
45+
labels:
46+
dbName: openstack
47+
spec:
48+
secret: osp-secret
49+
name: kuttldb_latin1
50+
status:
51+
conditions:
52+
- message: Setup complete
53+
reason: Ready
54+
status: "True"
55+
type: Ready
56+
- message: MariaDBDatabase ready
57+
reason: Ready
58+
status: "True"
59+
type: MariaDBDatabaseReady
60+
- message: MariaDB / Galera server ready
61+
reason: Ready
62+
status: "True"
63+
type: MariaDBServerReady
64+
completed: true

0 commit comments

Comments
 (0)