forked from maistra/istio-operator
/
readiness.go
307 lines (277 loc) · 9.96 KB
/
readiness.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
299
300
301
302
303
304
305
306
307
package controlplane
import (
"context"
"fmt"
"reflect"
"strings"
maistrav2 "github.com/acostamanowarrior/istio-operator/pkg/apis/maistra/v2"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/acostamanowarrior/istio-operator/pkg/apis/maistra/status"
maistrav1 "github.com/acostamanowarrior/istio-operator/pkg/apis/maistra/v1"
"github.com/acostamanowarrior/istio-operator/pkg/controller/common"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
const statusAnnotationReadyComponentCount = "readyComponentCount"
const statusAnnotationAlwaysReadyComponents = "alwaysReadyComponents"
func (r *controlPlaneInstanceReconciler) UpdateReadiness(ctx context.Context) error {
log := common.LogFromContext(ctx)
update, err := r.updateReadinessStatus(ctx)
if update {
statusErr := r.PostStatus(ctx)
if statusErr != nil {
// original error is more important than the status update error
if err == nil {
// if there's no original error, we can return the status update error
return statusErr
}
// otherwise, we must log the status update error and return the original error
log.Error(statusErr, "Error updating status")
}
}
return err
}
func (r *controlPlaneInstanceReconciler) updateReadinessStatus(ctx context.Context) (bool, error) {
log := common.LogFromContext(ctx)
log.Info("Updating ServiceMeshControlPlane readiness state")
readyComponents, unreadyComponents, err := r.calculateComponentReadiness(ctx)
if err != nil {
condition := status.Condition{
Type: status.ConditionTypeReady,
Status: status.ConditionStatusUnknown,
Reason: status.ConditionReasonProbeError,
Message: fmt.Sprintf("Error collecting ready state: %s", err),
}
r.Status.SetCondition(condition)
r.EventRecorder.Event(r.Instance, corev1.EventTypeWarning, eventReasonNotReady, condition.Message)
return true, nil
}
readyCondition := r.Status.GetCondition(status.ConditionTypeReady)
updateStatus := false
reconciledCondition := r.Status.GetCondition(status.ConditionTypeReconciled)
if reconciledCondition.Status != status.ConditionStatusTrue {
if !readyCondition.Matches(reconciledCondition.Status, reconciledCondition.Reason, reconciledCondition.Message) {
r.Status.SetCondition(status.Condition{
Type: status.ConditionTypeReady,
Status: reconciledCondition.Status,
Reason: reconciledCondition.Reason,
Message: reconciledCondition.Message,
})
updateStatus = true
}
} else {
if len(unreadyComponents) > 0 {
message := fmt.Sprintf("The following components are not fully available: %s", unreadyComponents.List())
if !readyCondition.Matches(status.ConditionStatusFalse, status.ConditionReasonComponentsNotReady, message) {
r.Status.SetCondition(status.Condition{
Type: status.ConditionTypeReady,
Status: status.ConditionStatusFalse,
Reason: status.ConditionReasonComponentsNotReady,
Message: message,
})
r.EventRecorder.Event(r.Instance, corev1.EventTypeWarning, eventReasonNotReady, message)
updateStatus = true
}
} else {
message := "All component deployments are Available"
if !readyCondition.Matches(status.ConditionStatusTrue, status.ConditionReasonComponentsReady, message) {
r.Status.SetCondition(status.Condition{
Type: status.ConditionTypeReady,
Status: status.ConditionStatusTrue,
Reason: status.ConditionReasonComponentsReady,
Message: message,
})
r.EventRecorder.Event(r.Instance, corev1.EventTypeNormal, eventReasonReady, message)
updateStatus = true
}
}
}
readyComponentCount := fmt.Sprintf("%d/%d", len(readyComponents), len(r.Status.ComponentStatus))
if r.Status.GetAnnotation(statusAnnotationReadyComponentCount) != readyComponentCount {
r.Status.SetAnnotation(statusAnnotationReadyComponentCount, readyComponentCount)
updateStatus = true
}
allComponents := sets.NewString()
for _, comp := range r.Status.ComponentStatus {
allComponents.Insert(comp.Resource)
}
readinessMap := maistrav2.ReadinessMap{
"ready": readyComponents.List(),
"unready": unreadyComponents.List(),
"pending": allComponents.Difference(readyComponents).Difference(unreadyComponents).List(),
}
if !reflect.DeepEqual(r.Status.Readiness.Components, readinessMap) {
r.Status.Readiness.Components = readinessMap
updateStatus = true
}
return updateStatus, nil
}
type isReadyFunc func(runtime.Object) bool
// keep this in sync with kinds in calculateComponentReadiness()
var kindsWithReadiness = sets.NewString("Deployment", "StatefulSet", "DaemonSet")
func (r *controlPlaneInstanceReconciler) hasReadiness(kind string) bool {
return kindsWithReadiness.Has(kind)
}
func (r *controlPlaneInstanceReconciler) calculateComponentReadiness(ctx context.Context) (readyComponents, unreadyComponents sets.String, err error) {
readyComponents = sets.NewString()
unreadyComponents = sets.NewString()
var readinessMap map[string]bool
readinessMap, err = r.calculateComponentReadinessMap(ctx)
if err != nil {
return
}
for component, ready := range readinessMap {
if ready {
readyComponents.Insert(component)
} else {
unreadyComponents.Insert(component)
}
}
return
}
func (r *controlPlaneInstanceReconciler) calculateComponentReadinessMap(ctx context.Context) (map[string]bool, error) {
log := common.LogFromContext(ctx)
readinessMap := map[string]bool{}
typesToCheck := []struct {
list runtime.Object
ready isReadyFunc
}{
// keep this in sync with kindsWithReadiness
{
list: &appsv1.DeploymentList{},
ready: func(obj runtime.Object) bool {
deployment := obj.(*appsv1.Deployment)
if deployment.Status.ReadyReplicas < deployment.Status.Replicas || deployment.Status.ObservedGeneration < deployment.Generation {
return false
}
for _, condition := range deployment.Status.Conditions {
if condition.Type == appsv1.DeploymentAvailable {
return condition.Status == corev1.ConditionTrue
}
}
return false
},
},
{
list: &appsv1.StatefulSetList{},
ready: func(obj runtime.Object) bool {
statefulSet := obj.(*appsv1.StatefulSet)
return statefulSet.Status.ReadyReplicas >= statefulSet.Status.Replicas
},
},
{
list: &appsv1.DaemonSetList{},
ready: func(obj runtime.Object) bool {
daemonSet := obj.(*appsv1.DaemonSet)
return r.daemonSetReady(daemonSet)
},
},
}
namespaces, err := r.getNamespacesToCheck()
if err != nil {
return nil, err
}
log.V(2).Info("Calculating readiness", "namespaces", namespaces)
for _, check := range typesToCheck {
err := r.calculateReadinessForType(ctx, namespaces, check.list, check.ready, readinessMap)
if err != nil {
return readinessMap, err
}
}
alwaysReadyComponents := r.Status.GetAnnotation(statusAnnotationAlwaysReadyComponents)
if alwaysReadyComponents != "" {
for _, c := range strings.Split(alwaysReadyComponents, ",") {
readinessMap[c] = true
}
}
log.V(2).Info("Readiness calculated", "readinessMap", readinessMap)
return readinessMap, nil
}
func (r *controlPlaneInstanceReconciler) isCNIReady(ctx context.Context) (bool, error) {
if !r.cniConfig.Enabled {
return true, nil
}
labelSelector := map[string]string{"istio": "cni"}
daemonSets := &appsv1.DaemonSetList{}
operatorNamespace := common.GetOperatorNamespace()
if err := r.Client.List(ctx, daemonSets, client.InNamespace(operatorNamespace), client.MatchingLabels(labelSelector)); err != nil {
return true, err
}
for _, ds := range daemonSets.Items {
if !r.daemonSetReady(&ds) {
return false, nil
}
}
return true, nil
}
func (r *controlPlaneInstanceReconciler) calculateReadinessForType(ctx context.Context, namespaces []string, list runtime.Object, isReady isReadyFunc, readinessMap map[string]bool) error {
log := common.LogFromContext(ctx)
selector := map[string]string{common.OwnerKey: r.Instance.GetNamespace()}
for _, ns := range namespaces {
err := r.Client.List(ctx, list, client.InNamespace(ns), client.MatchingLabels(selector))
if err != nil {
return err
}
items, err := meta.ExtractList(list)
if err != nil {
return err
}
for _, obj := range items {
log.V(3).Info("Readiness check found object", "object", obj)
metaObject, err := meta.Accessor(obj)
if err != nil {
return err
}
if component, ok := metaObject.GetLabels()[common.KubernetesAppComponentKey]; ok {
ready, exists := readinessMap[component]
readinessMap[component] = (ready || !exists) && isReady(obj)
} else {
// resource was most likely created by user, not by the operator; we can safely ignore it
log.V(3).Info("skipping resource for readiness check: resource has no component label", obj.GetObjectKind(), metaObject.GetName())
}
}
}
return nil
}
func (r *controlPlaneInstanceReconciler) getNamespacesToCheck() ([]string, error) {
// gather namespaces in SMCP
namespaces := sets.NewString(r.Instance.Namespace)
if gateways := r.Instance.Spec.Gateways; gateways != nil {
if gw := gateways.ClusterIngress; gw != nil {
if gw.Namespace != "" {
namespaces.Insert(gw.Namespace)
}
}
if gw := gateways.ClusterEgress; gw != nil {
if gw.Namespace != "" {
namespaces.Insert(gw.Namespace)
}
}
for _, gw := range gateways.IngressGateways {
if gw.Namespace != "" {
namespaces.Insert(gw.Namespace)
}
}
for _, gw := range gateways.EgressGateways {
if gw.Namespace != "" {
namespaces.Insert(gw.Namespace)
}
}
}
// ensure we only check namespaces that are part of the mesh
smmr := &maistrav1.ServiceMeshMemberRoll{}
if err := r.Client.Get(context.TODO(), client.ObjectKey{Namespace: r.Instance.Namespace, Name: common.MemberRollName}, smmr); err != nil {
if !apierrors.IsNotFound(err) {
return nil, err
}
}
namespaces = namespaces.Intersection(common.GetMeshNamespaces(r.Instance.Namespace, smmr))
return namespaces.List(), nil
}
func (r *controlPlaneInstanceReconciler) daemonSetReady(ds *appsv1.DaemonSet) bool {
return ds.Status.NumberUnavailable == 0
}