Skip to content

Commit 15f226c

Browse files
authored
chore(ops): backport reason constants and typo fixes to release-1.1 (#10216)
1 parent 0df5507 commit 15f226c

4 files changed

Lines changed: 131 additions & 19 deletions

File tree

apis/operations/v1alpha1/opsrequest_conditions.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ const (
5858
ReasonOpsCancelFailed = "CancelFailed"
5959
ReasonOpsCancelSucceed = "CancelSucceed"
6060
ReasonOpsCancelByController = "CancelByController"
61+
62+
// reasons emitted by NewXxxCondition constructors below. These were
63+
// previously inline string literals; centralized here so consumers
64+
// (Console, tests, lint analyzers in OPS-S1 Slice E) have a single
65+
// authoritative source. String values MUST remain stable — see
66+
// TestReasonConstantsStringDrift.
67+
ReasonValidateOpsRequestPassed = "ValidateOpsRequestPassed"
68+
ReasonOpsRequestProcessedSuccessfully = "OpsRequestProcessedSuccessfully"
69+
ReasonRestartStarted = "RestartStarted"
70+
ReasonStartToRebuildInstances = "StartToRebuildInstances"
71+
ReasonSwitchoverStarted = "SwitchoverStarted"
72+
ReasonVerticalScalingStarted = "VerticalScalingStarted"
73+
ReasonHorizontalScalingStarted = "HorizontalScalingStarted"
74+
ReasonVolumeExpansionStarted = "VolumeExpansionStarted"
75+
ReasonExposeStarted = "ExposeStarted"
76+
ReasonVersionUpgradeStarted = "VersionUpgradeStarted"
77+
ReasonStopStarted = "StopStarted"
78+
ReasonStartCluster = "StartCluster"
79+
ReasonReconfigureStarted = "ReconfigureStarted"
80+
ReasonReconfigureFailed = "ReconfigureFailed"
81+
ReasonBackupStarted = "BackupStarted"
82+
ReasonRestoreStarted = "RestoreStarted"
6183
)
6284

6385
func (r *OpsRequest) SetStatusCondition(condition metav1.Condition) {
@@ -130,7 +152,7 @@ func NewValidatePassedCondition(opsRequestName string) *metav1.Condition {
130152
return &metav1.Condition{
131153
Type: ConditionTypeValidated,
132154
Status: metav1.ConditionTrue,
133-
Reason: "ValidateOpsRequestPassed",
155+
Reason: ReasonValidateOpsRequestPassed,
134156
LastTransitionTime: metav1.Now(),
135157
Message: fmt.Sprintf("OpsRequest: %s is validated", opsRequestName),
136158
}
@@ -167,7 +189,7 @@ func NewSucceedCondition(ops *OpsRequest) *metav1.Condition {
167189
return &metav1.Condition{
168190
Type: ConditionTypeSucceed,
169191
Status: metav1.ConditionTrue,
170-
Reason: "OpsRequestProcessedSuccessfully",
192+
Reason: ReasonOpsRequestProcessedSuccessfully,
171193
LastTransitionTime: metav1.Now(),
172194
Message: fmt.Sprintf("Successfully processed the OpsRequest: %s in Cluster: %s",
173195
ops.Name, ops.Spec.GetClusterName()),
@@ -179,7 +201,7 @@ func NewRestartingCondition(ops *OpsRequest) *metav1.Condition {
179201
return &metav1.Condition{
180202
Type: ConditionTypeRestarting,
181203
Status: metav1.ConditionTrue,
182-
Reason: "RestartStarted",
204+
Reason: ReasonRestartStarted,
183205
LastTransitionTime: metav1.Now(),
184206
Message: fmt.Sprintf("Start to restart database in Cluster: %s", ops.Spec.GetClusterName()),
185207
}
@@ -190,7 +212,7 @@ func NewInstancesRebuildingCondition(ops *OpsRequest) *metav1.Condition {
190212
return &metav1.Condition{
191213
Type: ConditionTypeInstanceRebuilding,
192214
Status: metav1.ConditionTrue,
193-
Reason: "StartToRebuildInstances",
215+
Reason: ReasonStartToRebuildInstances,
194216
LastTransitionTime: metav1.Now(),
195217
Message: fmt.Sprintf("Start to rebuild the instances in Cluster: %s", ops.Spec.GetClusterName()),
196218
}
@@ -201,7 +223,7 @@ func NewSwitchoveringCondition(generation int64, message string) *metav1.Conditi
201223
return &metav1.Condition{
202224
Type: ConditionTypeSwitchover,
203225
Status: metav1.ConditionTrue,
204-
Reason: "SwitchoverStarted",
226+
Reason: ReasonSwitchoverStarted,
205227
LastTransitionTime: metav1.Now(),
206228
Message: message,
207229
ObservedGeneration: generation,
@@ -213,7 +235,7 @@ func NewVerticalScalingCondition(ops *OpsRequest) *metav1.Condition {
213235
return &metav1.Condition{
214236
Type: ConditionTypeVerticalScaling,
215237
Status: metav1.ConditionTrue,
216-
Reason: "VerticalScalingStarted",
238+
Reason: ReasonVerticalScalingStarted,
217239
LastTransitionTime: metav1.Now(),
218240
Message: fmt.Sprintf("Start to vertical scale resources in Cluster: %s", ops.Spec.GetClusterName()),
219241
}
@@ -224,7 +246,7 @@ func NewHorizontalScalingCondition(ops *OpsRequest) *metav1.Condition {
224246
return &metav1.Condition{
225247
Type: ConditionTypeHorizontalScaling,
226248
Status: metav1.ConditionTrue,
227-
Reason: "HorizontalScalingStarted",
249+
Reason: ReasonHorizontalScalingStarted,
228250
LastTransitionTime: metav1.Now(),
229251
Message: fmt.Sprintf("Start to horizontal scale replicas in Cluster: %s", ops.Spec.GetClusterName()),
230252
}
@@ -235,7 +257,7 @@ func NewVolumeExpandingCondition(ops *OpsRequest) *metav1.Condition {
235257
return &metav1.Condition{
236258
Type: ConditionTypeVolumeExpanding,
237259
Status: metav1.ConditionTrue,
238-
Reason: "VolumeExpansionStarted",
260+
Reason: ReasonVolumeExpansionStarted,
239261
LastTransitionTime: metav1.Now(),
240262
Message: fmt.Sprintf("Start to expand the volumes in Cluster: %s", ops.Spec.GetClusterName()),
241263
}
@@ -245,7 +267,7 @@ func NewExposingCondition(ops *OpsRequest) *metav1.Condition {
245267
return &metav1.Condition{
246268
Type: ConditionTypeExpose,
247269
Status: metav1.ConditionTrue,
248-
Reason: "ExposeStarted",
270+
Reason: ReasonExposeStarted,
249271
LastTransitionTime: metav1.Now(),
250272
Message: fmt.Sprintf("Start to expose the services in Cluster: %s", ops.Spec.GetClusterName()),
251273
}
@@ -256,7 +278,7 @@ func NewUpgradingCondition(ops *OpsRequest) *metav1.Condition {
256278
return &metav1.Condition{
257279
Type: ConditionTypeVersionUpgrading,
258280
Status: metav1.ConditionTrue,
259-
Reason: "VersionUpgradeStarted",
281+
Reason: ReasonVersionUpgradeStarted,
260282
LastTransitionTime: metav1.Now(),
261283
Message: fmt.Sprintf("Start to upgrade the version in Cluster: %s", ops.Spec.GetClusterName()),
262284
}
@@ -267,7 +289,7 @@ func NewStopCondition(ops *OpsRequest) *metav1.Condition {
267289
return &metav1.Condition{
268290
Type: ConditionTypeStop,
269291
Status: metav1.ConditionTrue,
270-
Reason: "StopStarted",
292+
Reason: ReasonStopStarted,
271293
LastTransitionTime: metav1.Now(),
272294
Message: fmt.Sprintf("Start to stop the Cluster: %s", ops.Spec.GetClusterName()),
273295
}
@@ -278,7 +300,7 @@ func NewStartCondition(ops *OpsRequest) *metav1.Condition {
278300
return &metav1.Condition{
279301
Type: ConditionTypeStart,
280302
Status: metav1.ConditionTrue,
281-
Reason: "StartCluster",
303+
Reason: ReasonStartCluster,
282304
LastTransitionTime: metav1.Now(),
283305
Message: fmt.Sprintf("Start the Cluster: %s", ops.Spec.GetClusterName()),
284306
}
@@ -289,7 +311,7 @@ func NewReconfigureCondition(ops *OpsRequest) *metav1.Condition {
289311
return &metav1.Condition{
290312
Type: ConditionTypeReconfigure,
291313
Status: metav1.ConditionTrue,
292-
Reason: "ReconfigureStarted",
314+
Reason: ReasonReconfigureStarted,
293315
LastTransitionTime: metav1.Now(),
294316
Message: fmt.Sprintf("Start to reconfigure in Cluster: %s, Component: %s",
295317
ops.Spec.GetClusterName(),
@@ -338,7 +360,7 @@ func NewReconfigureFailedCondition(ops *OpsRequest, err error) *metav1.Condition
338360
return &metav1.Condition{
339361
Type: appsv1alpha1.ReasonReconfigureFailed,
340362
Status: metav1.ConditionFalse,
341-
Reason: "ReconfigureFailed",
363+
Reason: ReasonReconfigureFailed,
342364
LastTransitionTime: metav1.Now(),
343365
Message: msg,
344366
}
@@ -349,7 +371,7 @@ func NewBackupCondition(ops *OpsRequest) *metav1.Condition {
349371
return &metav1.Condition{
350372
Type: ConditionTypeBackup,
351373
Status: metav1.ConditionTrue,
352-
Reason: "BackupStarted",
374+
Reason: ReasonBackupStarted,
353375
LastTransitionTime: metav1.Now(),
354376
Message: fmt.Sprintf("Start to backup the Cluster: %s", ops.Spec.GetClusterName()),
355377
}
@@ -360,7 +382,7 @@ func NewRestoreCondition(ops *OpsRequest) *metav1.Condition {
360382
return &metav1.Condition{
361383
Type: ConditionTypeBackup,
362384
Status: metav1.ConditionTrue,
363-
Reason: "RestoreStarted",
385+
Reason: ReasonRestoreStarted,
364386
LastTransitionTime: metav1.Now(),
365387
Message: fmt.Sprintf("Start to restore the Cluster: %s", ops.Spec.GetClusterName()),
366388
}

apis/operations/v1alpha1/opsrequest_conditions_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,96 @@ func TestNewAllCondition(t *testing.T) {
6363
NewReconfigureRunningCondition(opsRequest, appsv1alpha1.ReasonReconfigureRunning, "for_test", "")
6464
}
6565

66+
// TestReasonConstantsStringDrift pins the literal value of every Reason
67+
// constant emitted by the NewXxxCondition constructors. Console, Slock task
68+
// templates, fixtures, and the upcoming OPS-S1 reasoncatalog registry all
69+
// match on the string value, not the Go identifier, so a silent rename
70+
// would break consumers without a compile-time signal.
71+
//
72+
// Slice A intentionally only centralizes existing inline literals and does
73+
// NOT introduce the 9 OPS-S1 anchor reasons. Anchors land in Slice B with
74+
// their own catalog and tests.
75+
func TestReasonConstantsStringDrift(t *testing.T) {
76+
cases := []struct {
77+
name string
78+
got string
79+
want string
80+
}{
81+
// pre-existing constants — pinned for completeness
82+
{"ReasonClusterPhaseMismatch", ReasonClusterPhaseMismatch, "ClusterPhaseMismatch"},
83+
{"ReasonOpsTypeNotSupported", ReasonOpsTypeNotSupported, "OpsTypeNotSupported"},
84+
{"ReasonValidateFailed", ReasonValidateFailed, "ValidateFailed"},
85+
{"ReasonClusterNotFound", ReasonClusterNotFound, "ClusterNotFound"},
86+
{"ReasonOpsRequestFailed", ReasonOpsRequestFailed, "OpsRequestFailed"},
87+
{"ReasonOpsCanceling", ReasonOpsCanceling, "Canceling"},
88+
{"ReasonOpsCancelFailed", ReasonOpsCancelFailed, "CancelFailed"},
89+
{"ReasonOpsCancelSucceed", ReasonOpsCancelSucceed, "CancelSucceed"},
90+
{"ReasonOpsCancelByController", ReasonOpsCancelByController, "CancelByController"},
91+
92+
// constants centralized in Slice A — values must equal the
93+
// previously inline string literal, no behavior change allowed.
94+
{"ReasonValidateOpsRequestPassed", ReasonValidateOpsRequestPassed, "ValidateOpsRequestPassed"},
95+
{"ReasonOpsRequestProcessedSuccessfully", ReasonOpsRequestProcessedSuccessfully, "OpsRequestProcessedSuccessfully"},
96+
{"ReasonRestartStarted", ReasonRestartStarted, "RestartStarted"},
97+
{"ReasonStartToRebuildInstances", ReasonStartToRebuildInstances, "StartToRebuildInstances"},
98+
{"ReasonSwitchoverStarted", ReasonSwitchoverStarted, "SwitchoverStarted"},
99+
{"ReasonVerticalScalingStarted", ReasonVerticalScalingStarted, "VerticalScalingStarted"},
100+
{"ReasonHorizontalScalingStarted", ReasonHorizontalScalingStarted, "HorizontalScalingStarted"},
101+
{"ReasonVolumeExpansionStarted", ReasonVolumeExpansionStarted, "VolumeExpansionStarted"},
102+
{"ReasonExposeStarted", ReasonExposeStarted, "ExposeStarted"},
103+
{"ReasonVersionUpgradeStarted", ReasonVersionUpgradeStarted, "VersionUpgradeStarted"},
104+
{"ReasonStopStarted", ReasonStopStarted, "StopStarted"},
105+
{"ReasonStartCluster", ReasonStartCluster, "StartCluster"},
106+
{"ReasonReconfigureStarted", ReasonReconfigureStarted, "ReconfigureStarted"},
107+
{"ReasonReconfigureFailed", ReasonReconfigureFailed, "ReconfigureFailed"},
108+
{"ReasonBackupStarted", ReasonBackupStarted, "BackupStarted"},
109+
{"ReasonRestoreStarted", ReasonRestoreStarted, "RestoreStarted"},
110+
}
111+
for _, tc := range cases {
112+
if tc.got != tc.want {
113+
t.Errorf("Reason constant %s drifted: got %q, want %q (consumer match would break)", tc.name, tc.got, tc.want)
114+
}
115+
}
116+
}
117+
118+
// TestNewConditionReasonsMatchConstants verifies that the constructors
119+
// in opsrequest_conditions.go set Condition.Reason to the centralized
120+
// constants we just defined. Defends against an accidental in-place
121+
// edit that re-introduces an inline string and silently drifts from the
122+
// constant.
123+
func TestNewConditionReasonsMatchConstants(t *testing.T) {
124+
opsRequest := createTestOpsRequest("mysql-test", "mysql-restart", RestartType)
125+
opsRequest.Spec.Reconfigures = []Reconfigure{{ComponentOps: ComponentOps{ComponentName: "testReconfiguring"}}}
126+
127+
cases := []struct {
128+
name string
129+
got string
130+
want string
131+
}{
132+
{"NewValidatePassedCondition", NewValidatePassedCondition(opsRequest.Name).Reason, ReasonValidateOpsRequestPassed},
133+
{"NewSucceedCondition", NewSucceedCondition(opsRequest).Reason, ReasonOpsRequestProcessedSuccessfully},
134+
{"NewRestartingCondition", NewRestartingCondition(opsRequest).Reason, ReasonRestartStarted},
135+
{"NewInstancesRebuildingCondition", NewInstancesRebuildingCondition(opsRequest).Reason, ReasonStartToRebuildInstances},
136+
{"NewSwitchoveringCondition", NewSwitchoveringCondition(0, "test").Reason, ReasonSwitchoverStarted},
137+
{"NewVerticalScalingCondition", NewVerticalScalingCondition(opsRequest).Reason, ReasonVerticalScalingStarted},
138+
{"NewHorizontalScalingCondition", NewHorizontalScalingCondition(opsRequest).Reason, ReasonHorizontalScalingStarted},
139+
{"NewVolumeExpandingCondition", NewVolumeExpandingCondition(opsRequest).Reason, ReasonVolumeExpansionStarted},
140+
{"NewExposingCondition", NewExposingCondition(opsRequest).Reason, ReasonExposeStarted},
141+
{"NewUpgradingCondition", NewUpgradingCondition(opsRequest).Reason, ReasonVersionUpgradeStarted},
142+
{"NewStopCondition", NewStopCondition(opsRequest).Reason, ReasonStopStarted},
143+
{"NewStartCondition", NewStartCondition(opsRequest).Reason, ReasonStartCluster},
144+
{"NewReconfigureCondition", NewReconfigureCondition(opsRequest).Reason, ReasonReconfigureStarted},
145+
{"NewReconfigureFailedCondition", NewReconfigureFailedCondition(opsRequest, nil).Reason, ReasonReconfigureFailed},
146+
{"NewBackupCondition", NewBackupCondition(opsRequest).Reason, ReasonBackupStarted},
147+
{"NewRestoreCondition", NewRestoreCondition(opsRequest).Reason, ReasonRestoreStarted},
148+
}
149+
for _, tc := range cases {
150+
if tc.got != tc.want {
151+
t.Errorf("%s emitted Reason %q, want %q", tc.name, tc.got, tc.want)
152+
}
153+
}
154+
}
155+
66156
func TestSetStatusCondition(t *testing.T) {
67157
opsRequest := createTestOpsRequest("mysql-test", "mysql-restart", RestartType)
68158
progressingCondition := NewVerticalScalingCondition(opsRequest)

pkg/operations/custom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func validateAndGetCompSpec(cluster *appsv1.Cluster, opsDef *opsv1alpha1.OpsDefi
300300
}
301301
shardingSpec := cluster.Spec.GetShardingByName(componentName)
302302
if shardingSpec == nil {
303-
return nil, intctrlutil.NewFatalError(fmt.Sprintf(`cannot found the component "%s" in cluster "%s"`, componentName, cluster.Name))
303+
return nil, intctrlutil.NewFatalError(fmt.Sprintf(`cannot find the component "%s" in cluster "%s"`, componentName, cluster.Name))
304304
}
305305
if len(opsDef.Spec.PodInfoExtractors) == 0 {
306306
return nil, intctrlutil.NewFatalError(fmt.Sprintf(`podInfoExtractors cannot be empty in opsDef "%s" when the component "%s" is a shard component`, opsDef.Name, componentName))

pkg/operations/rebuild_instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (r rebuildInstanceOpsHandler) Action(reqCtx intctrlutil.RequestCtx, cli cli
102102
}
103103
isAvailable, _ := instanceIsAvailable(synthesizedComp, targetPod, "")
104104
if !opsRes.OpsRequest.Spec.Force && isAvailable {
105-
return intctrlutil.NewFatalError(fmt.Sprintf(`instance "%s" is availabled, can not rebuild it`, ins.Name))
105+
return intctrlutil.NewFatalError(fmt.Sprintf(`instance "%s" is available, can not rebuild it`, ins.Name))
106106
}
107107
instanceNames = append(instanceNames, ins.Name)
108108
}
@@ -459,7 +459,7 @@ func (r rebuildInstanceOpsHandler) checkProgressForScalingOutPods(reqCtx intctrl
459459
progressDetail := r.getInstanceProgressDetail(*compStatus, instance.Name)
460460
scalingOutPodName := r.getScalingOutPodNameFromMessage(progressDetail.Message)
461461
if _, ok := currPodSet[scalingOutPodName]; !ok {
462-
return 0, 0, nil, intctrlutil.NewFatalError(fmt.Sprintf(`the replicas of the component "%s" has been modifeied by another operation`, compSpec.Name))
462+
return 0, 0, nil, intctrlutil.NewFatalError(fmt.Sprintf(`the replicas of the component "%s" has been modified by another operation`, compSpec.Name))
463463
}
464464
pod := &corev1.Pod{}
465465
if exist, err := intctrlutil.CheckResourceExists(reqCtx.Ctx, cli,

0 commit comments

Comments
 (0)