@@ -21,7 +21,7 @@ type PolicySetBuilder struct {
21
21
// created policySet object.
22
22
Object * policiesv1beta1.PolicySet
23
23
// api client to interact with the cluster.
24
- apiClient * clients. Settings
24
+ apiClient runtimeclient. Client
25
25
// used to store latest error message upon defining or mutating policySet definition.
26
26
errorMsg string
27
27
}
@@ -33,8 +33,21 @@ func NewPolicySetBuilder(
33
33
"Initializing new policy set structure with the following params: name: %s, nsname: %s, policy: %v" ,
34
34
name , nsname , policy )
35
35
36
+ if apiClient == nil {
37
+ glog .V (100 ).Infof ("The apiClient is nil" )
38
+
39
+ return nil
40
+ }
41
+
42
+ err := apiClient .AttachScheme (policiesv1beta1 .AddToScheme )
43
+ if err != nil {
44
+ glog .V (100 ).Info ("Failed to add PolicySet scheme to client schemes" )
45
+
46
+ return nil
47
+ }
48
+
36
49
builder := PolicySetBuilder {
37
- apiClient : apiClient ,
50
+ apiClient : apiClient . Client ,
38
51
Definition : & policiesv1beta1.PolicySet {
39
52
ObjectMeta : metav1.ObjectMeta {
40
53
Name : name ,
@@ -72,13 +85,20 @@ func PullPolicySet(apiClient *clients.Settings, name, nsname string) (*PolicySet
72
85
glog .V (100 ).Infof ("Pulling existing policySet name %s under namespace %s from cluster" , name , nsname )
73
86
74
87
if apiClient == nil {
75
- glog .V (100 ).Infof ("The apiClient is empty" )
88
+ glog .V (100 ).Infof ("The apiClient is nil" )
89
+
90
+ return nil , fmt .Errorf ("policyset's 'apiClient' cannot be nil" )
91
+ }
92
+
93
+ err := apiClient .AttachScheme (policiesv1beta1 .AddToScheme )
94
+ if err != nil {
95
+ glog .V (100 ).Info ("Failed to add PolicySet scheme to client schemes" )
76
96
77
- return nil , fmt . Errorf ( "policyset's 'apiClient' cannot be empty" )
97
+ return nil , err
78
98
}
79
99
80
100
builder := PolicySetBuilder {
81
- apiClient : apiClient ,
101
+ apiClient : apiClient . Client ,
82
102
Definition : & policiesv1beta1.PolicySet {
83
103
ObjectMeta : metav1.ObjectMeta {
84
104
Name : name ,
@@ -196,21 +216,30 @@ func (builder *PolicySetBuilder) Update(force bool) (*PolicySetBuilder, error) {
196
216
return builder , err
197
217
}
198
218
219
+ if ! builder .Exists () {
220
+ glog .V (100 ).Infof (
221
+ "PolicySet %s does not exist in namespace %s" , builder .Definition .Name , builder .Definition .Namespace )
222
+
223
+ return nil , fmt .Errorf ("cannot update non-existent policySet" )
224
+ }
225
+
199
226
glog .V (100 ).Infof ("Updating the policySet object: %s in namespace: %s" ,
200
227
builder .Definition .Name , builder .Definition .Namespace )
201
228
229
+ builder .Definition .ResourceVersion = builder .Object .ResourceVersion
202
230
err := builder .apiClient .Update (context .TODO (), builder .Definition )
203
231
204
232
if err != nil {
205
233
if force {
206
234
glog .V (100 ).Infof (
207
- msg .FailToUpdateNotification ("policySet" , builder .Definition .Name ))
235
+ msg .FailToUpdateNotification ("policySet" , builder .Definition .Name , builder . Definition . Namespace ))
208
236
209
237
builder , err := builder .Delete ()
238
+ builder .Definition .ResourceVersion = ""
210
239
211
240
if err != nil {
212
241
glog .V (100 ).Infof (
213
- msg .FailToUpdateError ("policySet" , builder .Definition .Name ))
242
+ msg .FailToUpdateError ("policySet" , builder .Definition .Name , builder . Definition . Namespace ))
214
243
215
244
return nil , err
216
245
}
@@ -219,9 +248,7 @@ func (builder *PolicySetBuilder) Update(force bool) (*PolicySetBuilder, error) {
219
248
}
220
249
}
221
250
222
- if err == nil {
223
- builder .Object = builder .Definition
224
- }
251
+ builder .Object = builder .Definition
225
252
226
253
return builder , err
227
254
}
0 commit comments