This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
/
db_types.go
298 lines (251 loc) · 8.7 KB
/
db_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Copyright 2019 The KubeCarrier Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DBSpec defines the desired state of DB
type DBSpec struct {
// RootPassword is root account password for this data. Leave blank for auto-generation
RootPassword string `json:"rootPassword,omitempty"`
// DatabaseName of the created database at start up
// +kubebuilder:validation:MinLength=1
DatabaseName string `json:"databaseName"`
// DatabaseUser for created database
DatabaseUser string `json:"databaseUser"`
// DatabasePassword for the created database. Leave blank for auto-generation
DatabasePassword string `json:"databasePassword,omitempty"`
// Config is E2E tests params
Config Config `json:"config,omitempty"`
}
// OperationFlagType represents a enable/disable flag
type OperationFlagType string
func (o OperationFlagType) Enabled() bool {
return o == OperationFlagEnabled
}
// Values of OperationFlagType.
const (
OperationFlagEnabled OperationFlagType = "Enabled"
OperationFlagDisabled OperationFlagType = "Disabled"
)
// Config defines the e2e tests params
type Config struct {
// ReadyAfterSeconds represents duration after which operator will mark DB as Ready
ReadyAfterSeconds int `json:"readyAfterSeconds,omitempty"`
// DeletionAfterSeconds represents duration after which operator will remove finalizer
DeletionAfterSeconds int `json:"deletionAfterSeconds,omitempty"`
// CreateEnable control whether create operation enabled or not
// +kubebuilder:default:=Enabled
Create OperationFlagType `json:"create,omitempty"`
// UpdateEnable control whether update operation enabled or not
// +kubebuilder:default:=Enabled
Update OperationFlagType `json:"update,omitempty"`
// DeleteEnable control whether delete operation enabled or not
// +kubebuilder:default:=Enabled
Delete OperationFlagType `json:"delete,omitempty"`
}
// DBStatus defines the observed state of DB
type DBStatus struct {
// ObservedGeneration is the most recent generation observed for this FakeDB by the controller.
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
// Conditions represents the latest available observations of a FakeDB's current state.
Conditions []DBCondition `json:"conditions,omitempty"`
// DEPRECATED.
// Phase represents the current lifecycle state of this object.
// Consider this field DEPRECATED, it will be removed as soon as there
// is a mechanism to map conditions to strings when printing the property.
// This is only for display purpose, for everything else use conditions.
Phase DBPhaseType `json:"phase,omitempty"`
// Connection is the connection string for FakeDB
Connection *Connection `json:"connection,omitempty"`
}
// Connection defines necessary endpoints and credential for DB usage
type Connection struct {
// Endpoint for this database
Endpoint string `json:"endpoint"`
// Database name
Name string `json:"name"`
// Username for this database
Username string `json:"username"`
}
func (c Connection) String() string {
return fmt.Sprintf("%s-%s:%s", c.Endpoint, c.Username, c.Name)
}
// DBPhaseType represents all conditions as a single string for printing by using kubectl commands.
type DBPhaseType string
// Values of DBPhaseType.
const (
DBPhaseReady DBPhaseType = "Ready"
DBPhaseNotReady DBPhaseType = "NotReady"
DBPhaseUnknown DBPhaseType = "Unknown"
DBPhaseTerminating DBPhaseType = "Terminating"
)
const (
DBTerminatingReason = "Deleting"
)
// DBConditionType represents a DBCondition value.
type DBConditionType string
const (
// DBReady represents a DB condition is in ready state.
DBReady DBConditionType = "Ready"
)
// DBCondition contains details for the current condition of this DB.
type DBCondition struct {
// Type is the type of the DB condition, currently ('Ready').
Type DBConditionType `json:"type"`
// Status is the status of the condition, one of ('True', 'False', 'Unknown').
Status ConditionStatus `json:"status"`
// LastTransitionTime is the last time the condition transits from one status to another.
LastTransitionTime metav1.Time `json:"lastTransitionTime"`
// Reason is the (brief) reason for the condition's last transition.
Reason string `json:"reason"`
// Message is the human readable message indicating details about last transition.
Message string `json:"message"`
}
// True returns whether .Status == "True"
func (c DBCondition) True() bool {
return c.Status == ConditionTrue
}
// updatePhase updates the phase property based on the current conditions.
// this method should be called every time the conditions are updated.
func (s *DBStatus) updatePhase() {
for _, condition := range s.Conditions {
if condition.Type != DBReady {
continue
}
switch condition.Status {
case ConditionTrue:
s.Phase = DBPhaseReady
case ConditionFalse:
if condition.Reason == DBTerminatingReason {
s.Phase = DBPhaseTerminating
} else {
s.Phase = DBPhaseNotReady
}
case ConditionUnknown:
s.Phase = DBPhaseUnknown
}
return
}
s.Phase = DBPhaseUnknown
}
// GetCondition returns the Condition of the given condition type, if it exists.
func (s *DBStatus) GetCondition(t DBConditionType) (condition DBCondition, exists bool) {
for _, cond := range s.Conditions {
if cond.Type == t {
condition = cond
exists = true
return
}
}
return
}
// SetCondition replaces or adds the given condition.
func (s *DBStatus) SetCondition(condition DBCondition) {
defer s.updatePhase()
if condition.LastTransitionTime.IsZero() {
condition.LastTransitionTime = metav1.Now()
}
for i := range s.Conditions {
if s.Conditions[i].Type == condition.Type {
// Only update the LastTransitionTime when the Status is changed.
if s.Conditions[i].Status != condition.Status {
s.Conditions[i].LastTransitionTime = condition.LastTransitionTime
}
s.Conditions[i].Status = condition.Status
s.Conditions[i].Reason = condition.Reason
s.Conditions[i].Message = condition.Message
return
}
}
s.Conditions = append(s.Conditions, condition)
}
// DB is core element in e2e operator
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="Connection",type="string",JSONPath=".status.connection"
// +kubebuilder:resource:categories=all
type DB struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec DBSpec `json:"spec,omitempty"`
Status DBStatus `json:"status,omitempty"`
}
// +kubebuilder:object:root=true
// DBList contains a list of DB
type DBList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DB `json:"items"`
}
// IsReady returns if the DB is ready.
func (s *DB) IsReady() bool {
if s.Generation != s.Status.ObservedGeneration {
return false
}
for _, condition := range s.Status.Conditions {
if condition.Type == DBReady &&
condition.Status == ConditionTrue {
return true
}
}
return false
}
func (s *DB) SetReadyCondition() bool {
if !s.IsReady() {
s.Status.ObservedGeneration = s.Generation
s.Status.SetCondition(DBCondition{
Type: DBReady,
Status: ConditionTrue,
Reason: "DeploymentReady",
Message: "the DB is ready",
})
return true
}
return false
}
func (s *DB) SetUnReadyCondition() bool {
readyCondition, _ := s.Status.GetCondition(DBReady)
if readyCondition.Status != ConditionFalse {
s.Status.ObservedGeneration = s.Generation
s.Status.SetCondition(DBCondition{
Type: DBReady,
Status: ConditionFalse,
Reason: "DBUnready",
Message: "the DB is not ready",
})
return true
}
return false
}
func (s *DB) SetTerminatingCondition() bool {
readyCondition, _ := s.Status.GetCondition(DBReady)
if readyCondition.Status != ConditionFalse ||
readyCondition.Status == ConditionFalse && readyCondition.Reason != DBTerminatingReason {
s.Status.ObservedGeneration = s.Generation
s.Status.SetCondition(DBCondition{
Type: DBReady,
Status: ConditionFalse,
Reason: DBTerminatingReason,
Message: "DB is being deleted",
})
return true
}
return false
}
func init() {
SchemeBuilder.Register(&DB{}, &DBList{})
}