-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracking.go
152 lines (126 loc) · 5.56 KB
/
tracking.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
/*
Copyright 2021 The Kubernetes 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 hooks has helper functions for Runtime Hooks.
package hooks
import (
"context"
"strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
tlog "github.com/verrazzano/cluster-api-provider-ocne/internal/log"
"github.com/verrazzano/cluster-api-provider-ocne/util/patch"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
)
// MarkAsPending adds to the object's PendingHooksAnnotation the intent to execute a hook after an operation completes.
// Usually this function is called when an operation is starting in order to track the intent to call an After<operation> hook later in the process.
func MarkAsPending(ctx context.Context, c client.Client, obj client.Object, hooks ...runtimecatalog.Hook) error {
hookNames := []string{}
for _, hook := range hooks {
hookNames = append(hookNames, runtimecatalog.HookName(hook))
}
patchHelper, err := patch.NewHelper(obj, c)
if err != nil {
return errors.Wrapf(err, "failed to mark %q hook(s) as pending: failed to create patch helper for %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
}
// Read the annotation of the objects and add the hook to the comma separated list
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[runtimev1.PendingHooksAnnotation] = addToCommaSeparatedList(annotations[runtimev1.PendingHooksAnnotation], hookNames...)
obj.SetAnnotations(annotations)
if err := patchHelper.Patch(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to mark %q hook(s) as pending: failed to patch %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
}
return nil
}
// IsPending returns true if there is an intent to call a hook being tracked in the object's PendingHooksAnnotation.
func IsPending(hook runtimecatalog.Hook, obj client.Object) bool {
hookName := runtimecatalog.HookName(hook)
annotations := obj.GetAnnotations()
if annotations == nil {
return false
}
return isInCommaSeparatedList(annotations[runtimev1.PendingHooksAnnotation], hookName)
}
// MarkAsDone removes the intent to call a Hook from the object's PendingHooksAnnotation.
// Usually this func is called after all the registered extensions for the Hook returned an answer without requests
// to hold on to the object's lifecycle (retryAfterSeconds).
func MarkAsDone(ctx context.Context, c client.Client, obj client.Object, hooks ...runtimecatalog.Hook) error {
hookNames := []string{}
for _, hook := range hooks {
hookNames = append(hookNames, runtimecatalog.HookName(hook))
}
patchHelper, err := patch.NewHelper(obj, c)
if err != nil {
return errors.Wrapf(err, "failed to mark %q hook(s) as done: failed to create patch helper for %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
}
// Read the annotation of the objects and add the hook to the comma separated list
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[runtimev1.PendingHooksAnnotation] = removeFromCommaSeparatedList(annotations[runtimev1.PendingHooksAnnotation], hookNames...)
if annotations[runtimev1.PendingHooksAnnotation] == "" {
delete(annotations, runtimev1.PendingHooksAnnotation)
}
obj.SetAnnotations(annotations)
if err := patchHelper.Patch(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to mark %q hook(s) as done: failed to patch %s", strings.Join(hookNames, ","), tlog.KObj{Obj: obj})
}
return nil
}
// IsOkToDelete returns true if object has the OkToDeleteAnnotation in the annotations of the object, false otherwise.
func IsOkToDelete(obj client.Object) bool {
annotations := obj.GetAnnotations()
if annotations == nil {
return false
}
if _, ok := annotations[runtimev1.OkToDeleteAnnotation]; ok {
return true
}
return false
}
// MarkAsOkToDelete adds the OkToDeleteAnnotation annotation to the object and patches it.
func MarkAsOkToDelete(ctx context.Context, c client.Client, obj client.Object) error {
patchHelper, err := patch.NewHelper(obj, c)
if err != nil {
return errors.Wrapf(err, "failed to mark %s as ok to delete: failed to create patch helper", tlog.KObj{Obj: obj})
}
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations[runtimev1.OkToDeleteAnnotation] = ""
obj.SetAnnotations(annotations)
if err := patchHelper.Patch(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to mark %s as ok to delete: failed to patch", tlog.KObj{Obj: obj})
}
return nil
}
func addToCommaSeparatedList(list string, items ...string) string {
set := sets.NewString(strings.Split(list, ",")...)
set.Insert(items...)
return strings.Join(set.List(), ",")
}
func isInCommaSeparatedList(list, item string) bool {
set := sets.NewString(strings.Split(list, ",")...)
return set.Has(item)
}
func removeFromCommaSeparatedList(list string, items ...string) string {
set := sets.NewString(strings.Split(list, ",")...)
set.Delete(items...)
return strings.Join(set.List(), ",")
}