/
placementbinding.go
330 lines (248 loc) · 9.59 KB
/
placementbinding.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
package ocm
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/msg"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
// PlacementBindingBuilder type definition.
type PlacementBindingBuilder struct {
// placementBinding Definition, used to create the placementBinding object.
Definition *policiesv1.PlacementBinding
// created placementBinding object.
Object *policiesv1.PlacementBinding
// api client to interact with the cluster.
apiClient *clients.Settings
// used to store latest error message upon defining or mutating placementBinding definition.
errorMsg string
}
// NewPlacementBindingBuilder creates a new instance of PlacementBindingBuilder.
func NewPlacementBindingBuilder(
apiClient *clients.Settings,
name,
nsname string,
placementRef policiesv1.PlacementSubject,
subject policiesv1.Subject) *PlacementBindingBuilder {
glog.V(100).Infof(
"Initializing new placement binding structure with the following params: name: %s, nsname: %s",
name, nsname)
builder := PlacementBindingBuilder{
apiClient: apiClient,
Definition: &policiesv1.PlacementBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
PlacementRef: placementRef,
Subjects: []policiesv1.Subject{subject},
},
}
if name == "" {
builder.errorMsg = "placementBinding's 'name' cannot be empty"
}
if nsname == "" {
builder.errorMsg = "placementBinding's 'nsname' cannot be empty"
}
if placementRefErr := validatePlacementRef(placementRef); placementRefErr != "" {
builder.errorMsg = placementRefErr
}
if subjectErr := validateSubject(subject); subjectErr != "" {
builder.errorMsg = subjectErr
}
return &builder
}
// PullPlacementBinding pulls existing placementBinding into Builder struct.
func PullPlacementBinding(apiClient *clients.Settings, name, nsname string) (*PlacementBindingBuilder, error) {
glog.V(100).Infof("Pulling existing placementBinding name %s under namespace %s from cluster", name, nsname)
if apiClient == nil {
glog.V(100).Info("The apiClient is empty")
return nil, fmt.Errorf("placementBinding's 'apiClient' cannot be empty")
}
builder := PlacementBindingBuilder{
apiClient: apiClient,
Definition: &policiesv1.PlacementBinding{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: nsname,
},
},
}
if name == "" {
glog.V(100).Infof("The name of the placementBinding is empty")
return nil, fmt.Errorf("placementBinding's 'name' cannot be empty")
}
if nsname == "" {
glog.V(100).Infof("The namespace of the placementBinding is empty")
return nil, fmt.Errorf("placementBinding's 'namespace' cannot be empty")
}
if !builder.Exists() {
return nil, fmt.Errorf("placementBinding object %s doesn't exist in namespace %s", name, nsname)
}
builder.Definition = builder.Object
return &builder, nil
}
// Exists checks whether the given placementBinding exists.
func (builder *PlacementBindingBuilder) Exists() bool {
if valid, _ := builder.validate(); !valid {
return false
}
glog.V(100).Infof("Checking if placementBinding %s exists in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
var err error
builder.Object, err = builder.Get()
return err == nil || !k8serrors.IsNotFound(err)
}
// Get returns a placementBinding object if found.
func (builder *PlacementBindingBuilder) Get() (*policiesv1.PlacementBinding, error) {
if valid, err := builder.validate(); !valid {
return nil, err
}
glog.V(100).Infof("Getting placementBinding %s in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
placementBinding := &policiesv1.PlacementBinding{}
err := builder.apiClient.Get(context.TODO(), runtimeclient.ObjectKey{
Name: builder.Definition.Name,
Namespace: builder.Definition.Namespace,
}, placementBinding)
if err != nil {
return nil, err
}
return placementBinding, err
}
// Create makes a placementBinding in the cluster and stores the created object in struct.
func (builder *PlacementBindingBuilder) Create() (*PlacementBindingBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Creating the placementBinding %s in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
var err error
if !builder.Exists() {
err = builder.apiClient.Create(context.TODO(), builder.Definition)
if err == nil {
builder.Object = builder.Definition
}
}
return builder, err
}
// Delete removes a placementBinding from a cluster.
func (builder *PlacementBindingBuilder) Delete() (*PlacementBindingBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Deleting the placementBinding %s in namespace %s",
builder.Definition.Name, builder.Definition.Namespace)
if !builder.Exists() {
return builder, fmt.Errorf("placementBinding cannot be deleted because it does not exist")
}
err := builder.apiClient.Delete(context.TODO(), builder.Definition)
if err != nil {
return builder, fmt.Errorf("can not delete placementBinding: %w", err)
}
builder.Object = nil
return builder, nil
}
// Update renovates the existing placementBinding object with the placementBinding definition in builder.
func (builder *PlacementBindingBuilder) Update(force bool) (*PlacementBindingBuilder, error) {
if valid, err := builder.validate(); !valid {
return builder, err
}
glog.V(100).Infof("Updating the placementBinding object: %s in namespace: %s",
builder.Definition.Name, builder.Definition.Namespace)
err := builder.apiClient.Update(context.TODO(), builder.Definition)
if err != nil {
if force {
glog.V(100).Infof(
"Failed to update the placementBinding object %s. "+
"Note: Force flag set, executed delete/create methods instead", builder.Definition.Name)
builder, err := builder.Delete()
if err != nil {
glog.V(100).Infof(
"Failed to update the placementBinding object %s, "+
"due to error in delete function", builder.Definition.Name)
return nil, err
}
return builder.Create()
}
}
if err == nil {
builder.Object = builder.Definition
}
return builder, err
}
// WithAdditionalSubject appends a subject to the subjects list in the PlacementBinding definition.
func (builder *PlacementBindingBuilder) WithAdditionalSubject(subject policiesv1.Subject) *PlacementBindingBuilder {
if valid, _ := builder.validate(); !valid {
return builder
}
glog.V(100).Infof("Adding Subject %s to PlacementBinding %s", subject.Name, builder.Definition.Name)
if err := validateSubject(subject); err != "" {
builder.errorMsg = err
return builder
}
builder.Definition.Subjects = append(builder.Definition.Subjects, subject)
return builder
}
// validatePlacementRef validates all the fields of the PlacementRef and returns an errorMsg based on the validation.
// The errorMsg will be empty for valid Subjects.
func validatePlacementRef(placementRef policiesv1.PlacementSubject) string {
apiGroup := placementRef.APIGroup
if apiGroup != "apps.open-cluster-management.io" && apiGroup != "cluster.open-cluster-management.io" {
glog.V(100).Info("The APIGroup of the PlacementRef of the PlacementBinding is invalid")
return "placementBinding's 'PlacementRef.APIGroup' must be a valid option"
}
kind := placementRef.Kind
if kind != "PlacementRule" && kind != "Placement" {
glog.V(100).Info("The Kind of the PlacementRef of the PlacementBinding is invalid")
return "placementBinding's 'PlacementRef.Kind' must be a valid option"
}
if placementRef.Name == "" {
glog.V(100).Info("The Name of the PlacementRef of the PlacementBinding is empty")
return "placementBinding's 'PlacementRef.Name' cannot be empty"
}
return ""
}
// validateSubject validates the fields of the Subject and returns an errorMsg based on the validation. The errorMsg
// will be empty for valid Subjects.
func validateSubject(subject policiesv1.Subject) string {
if subject.APIGroup != "policy.open-cluster-management.io" {
glog.V(100).Info("The APIGroup of the PlacementBinding subject is invalid")
return "placementBinding's 'Subject.APIGroup' must be 'policy.open-cluster-management.io'"
}
if subject.Kind != "Policy" && subject.Kind != "PolicySet" {
glog.V(100).Info("The Kind of the subject of the PlacementBinding is invalid")
return "placementBinding's 'Subject.Kind' must be a valid option"
}
if subject.Name == "" {
glog.V(100).Info("The Name of the subject of the PlacementBinding is empty")
return "placementBinding's 'Subject.Name' cannot be empty"
}
return ""
}
// validate will check that the builder and builder definition are properly initialized before
// accessing any member fields.
func (builder *PlacementBindingBuilder) validate() (bool, error) {
resourceCRD := "PlacementBinding"
if builder == nil {
glog.V(100).Infof("The %s builder is uninitialized", resourceCRD)
return false, fmt.Errorf("error: received nil %s builder", resourceCRD)
}
if builder.Definition == nil {
glog.V(100).Infof("The %s is undefined", resourceCRD)
builder.errorMsg = msg.UndefinedCrdObjectErrString(resourceCRD)
}
if builder.apiClient == nil {
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD)
builder.errorMsg = fmt.Sprintf("%s builder cannot have nil apiClient", resourceCRD)
}
if builder.errorMsg != "" {
glog.V(100).Infof("The %s builder has error message: %s", resourceCRD, builder.errorMsg)
return false, fmt.Errorf(builder.errorMsg)
}
return true, nil
}