-
Notifications
You must be signed in to change notification settings - Fork 151
/
cluster_status_conditions.go
159 lines (139 loc) · 6.53 KB
/
cluster_status_conditions.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
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package apps
import (
"fmt"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
const (
ReasonPreCheckSucceed = "PreCheckSucceed" // ReasonPreCheckSucceed preChecks succeeded for provisioning started
ReasonPreCheckFailed = "PreCheckFailed" // ReasonPreCheckFailed preChecks failed for provisioning started
ReasonApplyResourcesFailed = "ApplyResourcesFailed" // ReasonApplyResourcesFailed applies resources failed to create or change the cluster
ReasonApplyResourcesSucceed = "ApplyResourcesSucceed" // ReasonApplyResourcesSucceed applies resources succeeded to create or change the cluster
ReasonReplicasNotReady = "ReplicasNotReady" // ReasonReplicasNotReady the pods of components are not ready
ReasonAllReplicasReady = "AllReplicasReady" // ReasonAllReplicasReady the pods of components are ready
ReasonComponentsNotReady = "ComponentsNotReady" // ReasonComponentsNotReady the components of cluster are not ready
ReasonClusterReady = "ClusterReady" // ReasonClusterReady the components of cluster are ready, the component phase is running
)
func setProvisioningStartedCondition(conditions *[]metav1.Condition, clusterName string, clusterGeneration int64, err error) {
var condition metav1.Condition
if err == nil {
condition = newProvisioningStartedCondition(clusterName, clusterGeneration)
} else {
condition = newFailedProvisioningStartedCondition(err)
}
meta.SetStatusCondition(conditions, condition)
}
// newProvisioningStartedCondition creates the provisioning started condition in cluster conditions.
func newProvisioningStartedCondition(clusterName string, clusterGeneration int64) metav1.Condition {
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeProvisioningStarted,
ObservedGeneration: clusterGeneration,
Status: metav1.ConditionTrue,
Message: fmt.Sprintf("The operator has started the provisioning of Cluster: %s", clusterName),
Reason: ReasonPreCheckSucceed,
}
}
func getConditionReasonWithError(defaultReason string, err error) string {
if err == nil {
return defaultReason
}
controllerErr := intctrlutil.UnwrapControllerError(err)
if controllerErr != nil {
defaultReason = string(controllerErr.Type)
}
return defaultReason
}
// newApplyResourcesCondition creates a condition when applied resources succeed.
func newFailedProvisioningStartedCondition(err error) metav1.Condition {
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeProvisioningStarted,
Status: metav1.ConditionFalse,
Message: err.Error(),
Reason: getConditionReasonWithError(ReasonPreCheckFailed, err),
}
}
func setApplyResourceCondition(conditions *[]metav1.Condition, clusterGeneration int64, err error) {
condition := newApplyResourcesCondition(clusterGeneration)
// ignore requeue error
if err != nil && !intctrlutil.IsRequeueError(err) {
condition = newFailedApplyResourcesCondition(err)
}
meta.SetStatusCondition(conditions, condition)
}
// newApplyResourcesCondition creates a condition when applied resources succeed.
func newApplyResourcesCondition(clusterGeneration int64) metav1.Condition {
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeApplyResources,
ObservedGeneration: clusterGeneration,
Status: metav1.ConditionTrue,
Message: "Successfully applied for resources",
Reason: ReasonApplyResourcesSucceed,
}
}
// newApplyResourcesCondition creates a condition when applied resources succeed.
func newFailedApplyResourcesCondition(err error) metav1.Condition {
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeApplyResources,
Status: metav1.ConditionFalse,
Message: err.Error(),
Reason: getConditionReasonWithError(ReasonApplyResourcesFailed, err),
}
}
// newAllReplicasPodsReadyConditions creates a condition when all pods of components are ready
func newAllReplicasPodsReadyConditions() metav1.Condition {
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeReplicasReady,
Status: metav1.ConditionTrue,
Message: "all pods of components are ready, waiting for the probe detection successful",
Reason: ReasonAllReplicasReady,
}
}
// newReplicasNotReadyCondition creates a condition when pods of components are not ready
func newReplicasNotReadyCondition(notReadyComponentNames map[string]struct{}) metav1.Condition {
cNameSlice := maps.Keys(notReadyComponentNames)
slices.Sort(cNameSlice)
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeReplicasReady,
Status: metav1.ConditionFalse,
Message: fmt.Sprintf("pods are not ready in Components: %v, refer to related component message in Cluster.status.components", cNameSlice),
Reason: ReasonReplicasNotReady,
}
}
// newClusterReadyCondition creates a condition when all components of cluster are running
func newClusterReadyCondition(clusterName string) metav1.Condition {
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeReady,
Status: metav1.ConditionTrue,
Message: fmt.Sprintf("Cluster: %s is ready, current phase is Running", clusterName),
Reason: ReasonClusterReady,
}
}
// newComponentsNotReadyCondition creates a condition when components of cluster are not ready
func newComponentsNotReadyCondition(notReadyComponentNames map[string]struct{}) metav1.Condition {
cNameSlice := maps.Keys(notReadyComponentNames)
slices.Sort(cNameSlice)
return metav1.Condition{
Type: appsv1alpha1.ConditionTypeReady,
Status: metav1.ConditionFalse,
Message: fmt.Sprintf("pods are unavailable in Components: %v, refer to related component message in Cluster.status.components", cNameSlice),
Reason: ReasonComponentsNotReady,
}
}