/
workstatus.go
330 lines (283 loc) · 11.7 KB
/
workstatus.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Copyright 2021 The Karmada 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 helper
import (
"context"
"encoding/json"
"fmt"
"reflect"
"sort"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/events"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/names"
)
const (
// FullyAppliedSuccessReason defines the success reason for the FullyApplied condition.
FullyAppliedSuccessReason = "FullyAppliedSuccess"
// FullyAppliedFailedReason defines the failure reason for the FullyApplied condition.
FullyAppliedFailedReason = "FullyAppliedFailed"
// FullyAppliedSuccessMessage defines the success message for the FullyApplied condition.
FullyAppliedSuccessMessage = "All works have been successfully applied"
// FullyAppliedFailedMessage defines the failure message for the FullyApplied condition.
FullyAppliedFailedMessage = "Failed to apply all works, see status.aggregatedStatus for details"
)
// AggregateResourceBindingWorkStatus will collect all work statuses with current ResourceBinding objects,
// then aggregate status info to current ResourceBinding status.
func AggregateResourceBindingWorkStatus(
c client.Client,
binding *workv1alpha2.ResourceBinding,
resourceTemplate *unstructured.Unstructured,
eventRecorder record.EventRecorder,
) error {
workList, err := GetWorksByBindingNamespaceName(c, binding.Namespace, binding.Name)
if err != nil {
return err
}
aggregatedStatuses, err := assembleWorkStatus(workList.Items, resourceTemplate)
if err != nil {
return err
}
fullyAppliedCondition := generateFullyAppliedCondition(binding.Spec, aggregatedStatuses)
err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
currentBindingStatus := binding.Status.DeepCopy()
binding.Status.AggregatedStatus = aggregatedStatuses
// set binding status with the newest condition
meta.SetStatusCondition(&binding.Status.Conditions, fullyAppliedCondition)
if reflect.DeepEqual(binding.Status, *currentBindingStatus) {
klog.V(4).Infof("New aggregatedStatuses are equal with old resourceBinding(%s/%s) AggregatedStatus, no update required.",
binding.Namespace, binding.Name)
return nil
}
updateErr := c.Status().Update(context.TODO(), binding)
if updateErr == nil {
return nil
}
updated := &workv1alpha2.ResourceBinding{}
if err = c.Get(context.TODO(), client.ObjectKey{Namespace: binding.Namespace, Name: binding.Name}, updated); err == nil {
binding = updated
} else {
klog.Errorf("Failed to get updated binding %s/%s: %v", binding.Namespace, binding.Name, err)
}
return updateErr
})
if err != nil {
eventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonAggregateStatusFailed, err.Error())
eventRecorder.Event(resourceTemplate, corev1.EventTypeWarning, events.EventReasonAggregateStatusFailed, err.Error())
return err
}
msg := fmt.Sprintf("Update resourceBinding(%s/%s) with AggregatedStatus successfully.", binding.Namespace, binding.Name)
eventRecorder.Event(binding, corev1.EventTypeNormal, events.EventReasonAggregateStatusSucceed, msg)
eventRecorder.Event(resourceTemplate, corev1.EventTypeNormal, events.EventReasonAggregateStatusSucceed, msg)
return nil
}
// AggregateClusterResourceBindingWorkStatus will collect all work statuses with current ClusterResourceBinding objects,
// then aggregate status info to current ClusterResourceBinding status.
func AggregateClusterResourceBindingWorkStatus(
c client.Client,
binding *workv1alpha2.ClusterResourceBinding,
resourceTemplate *unstructured.Unstructured,
eventRecorder record.EventRecorder,
) error {
workList, err := GetWorksByBindingNamespaceName(c, "", binding.Name)
if err != nil {
return err
}
aggregatedStatuses, err := assembleWorkStatus(workList.Items, resourceTemplate)
if err != nil {
return err
}
fullyAppliedCondition := generateFullyAppliedCondition(binding.Spec, aggregatedStatuses)
err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
currentBindingStatus := binding.Status.DeepCopy()
binding.Status.AggregatedStatus = aggregatedStatuses
// set binding status with the newest condition
meta.SetStatusCondition(&binding.Status.Conditions, fullyAppliedCondition)
if reflect.DeepEqual(binding.Status, *currentBindingStatus) {
klog.Infof("New aggregatedStatuses are equal with old clusterResourceBinding(%s) AggregatedStatus, no update required.", binding.Name)
return nil
}
updateErr := c.Status().Update(context.TODO(), binding)
if updateErr == nil {
return nil
}
updated := &workv1alpha2.ClusterResourceBinding{}
if err = c.Get(context.TODO(), client.ObjectKey{Name: binding.Name}, updated); err == nil {
binding = updated
} else {
klog.Errorf("Failed to get updated binding %s/%s: %v", binding.Namespace, binding.Name, err)
}
return updateErr
})
if err != nil {
eventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonAggregateStatusFailed, err.Error())
eventRecorder.Event(resourceTemplate, corev1.EventTypeWarning, events.EventReasonAggregateStatusFailed, err.Error())
return err
}
msg := fmt.Sprintf("Update clusterResourceBinding(%s) with AggregatedStatus successfully.", binding.Name)
eventRecorder.Event(binding, corev1.EventTypeNormal, events.EventReasonAggregateStatusSucceed, msg)
eventRecorder.Event(resourceTemplate, corev1.EventTypeNormal, events.EventReasonAggregateStatusSucceed, msg)
return nil
}
func generateFullyAppliedCondition(spec workv1alpha2.ResourceBindingSpec, aggregatedStatuses []workv1alpha2.AggregatedStatusItem) metav1.Condition {
clusterNames := ObtainBindingSpecExistingClusters(spec)
if worksFullyApplied(aggregatedStatuses, clusterNames) {
return util.NewCondition(workv1alpha2.FullyApplied, FullyAppliedSuccessReason, FullyAppliedSuccessMessage, metav1.ConditionTrue)
}
return util.NewCondition(workv1alpha2.FullyApplied, FullyAppliedFailedReason, FullyAppliedFailedMessage, metav1.ConditionFalse)
}
// assemble workStatuses from workList which list by selector and match with workload.
func assembleWorkStatus(works []workv1alpha1.Work, workload *unstructured.Unstructured) ([]workv1alpha2.AggregatedStatusItem, error) {
statuses := make([]workv1alpha2.AggregatedStatusItem, 0)
for _, work := range works {
if !work.DeletionTimestamp.IsZero() {
continue
}
identifierIndex, err := GetManifestIndex(work.Spec.Workload.Manifests, workload)
if err != nil {
klog.Errorf("Failed to get manifestIndex of workload in work.Spec.Workload.Manifests. Error: %v.", err)
return nil, err
}
clusterName, err := names.GetClusterName(work.Namespace)
if err != nil {
klog.Errorf("Failed to get clusterName from work namespace %s. Error: %v.", work.Namespace, err)
return nil, err
}
// if sync work to member cluster failed, then set status back to resource binding.
var applied bool
var appliedMsg string
if cond := meta.FindStatusCondition(work.Status.Conditions, workv1alpha1.WorkApplied); cond != nil {
switch cond.Status {
case metav1.ConditionTrue:
applied = true
case metav1.ConditionUnknown:
fallthrough
case metav1.ConditionFalse:
applied = false
appliedMsg = cond.Message
default: // should not happen unless the condition api changed.
panic("unexpected status")
}
}
if !applied {
aggregatedStatus := workv1alpha2.AggregatedStatusItem{
ClusterName: clusterName,
Applied: applied,
AppliedMessage: appliedMsg,
Health: workv1alpha2.ResourceUnknown,
}
statuses = append(statuses, aggregatedStatus)
continue
}
// resources with no status,only record whether the propagation is successful in work
aggregatedStatus := workv1alpha2.AggregatedStatusItem{
ClusterName: clusterName,
Applied: applied,
Health: workv1alpha2.ResourceUnknown,
}
for i := range work.Status.ManifestStatuses {
equal, err := equalIdentifier(&work.Status.ManifestStatuses[i].Identifier, identifierIndex, workload)
if err != nil {
return nil, err
}
if equal {
aggregatedStatus.Status = work.Status.ManifestStatuses[i].Status
aggregatedStatus.Health = workv1alpha2.ResourceHealth(work.Status.ManifestStatuses[i].Health)
break
}
}
statuses = append(statuses, aggregatedStatus)
}
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].ClusterName < statuses[j].ClusterName
})
return statuses, nil
}
// GetManifestIndex get the index of clusterObj in manifest list, if not exist return -1.
func GetManifestIndex(manifests []workv1alpha1.Manifest, clusterObj *unstructured.Unstructured) (int, error) {
for index, rawManifest := range manifests {
manifest := &unstructured.Unstructured{}
if err := manifest.UnmarshalJSON(rawManifest.Raw); err != nil {
return -1, err
}
if manifest.GetAPIVersion() == clusterObj.GetAPIVersion() &&
manifest.GetKind() == clusterObj.GetKind() &&
manifest.GetNamespace() == clusterObj.GetNamespace() &&
manifest.GetName() == clusterObj.GetName() {
return index, nil
}
}
return -1, fmt.Errorf("no such manifest exist")
}
func equalIdentifier(targetIdentifier *workv1alpha1.ResourceIdentifier, ordinal int, workload *unstructured.Unstructured) (bool, error) {
groupVersion, err := schema.ParseGroupVersion(workload.GetAPIVersion())
if err != nil {
return false, err
}
if targetIdentifier.Ordinal == ordinal &&
targetIdentifier.Group == groupVersion.Group &&
targetIdentifier.Version == groupVersion.Version &&
targetIdentifier.Kind == workload.GetKind() &&
targetIdentifier.Namespace == workload.GetNamespace() &&
targetIdentifier.Name == workload.GetName() {
return true, nil
}
return false, nil
}
// worksFullyApplied checks if all works are applied according the scheduled result and collected status.
func worksFullyApplied(aggregatedStatuses []workv1alpha2.AggregatedStatusItem, targetClusters sets.Set[string]) bool {
// short path: not scheduled
if len(targetClusters) == 0 {
return false
}
// short path: lack of status
if len(targetClusters) != len(aggregatedStatuses) {
return false
}
for _, aggregatedStatusItem := range aggregatedStatuses {
if !aggregatedStatusItem.Applied {
return false
}
if !targetClusters.Has(aggregatedStatusItem.ClusterName) {
return false
}
}
return true
}
// IsResourceApplied checks whether resource has been dispatched to member cluster or not
func IsResourceApplied(workStatus *workv1alpha1.WorkStatus) bool {
return meta.IsStatusConditionTrue(workStatus.Conditions, workv1alpha1.WorkApplied)
}
// BuildStatusRawExtension builds raw JSON by a status map.
func BuildStatusRawExtension(status interface{}) (*runtime.RawExtension, error) {
statusJSON, err := json.Marshal(status)
if err != nil {
klog.Errorf("Failed to marshal status. Error: %v.", statusJSON)
return nil, err
}
return &runtime.RawExtension{
Raw: statusJSON,
}, nil
}