@@ -9,28 +9,27 @@ import (
9
9
"github.com/openshift-kni/eco-goinfra/pkg/clients"
10
10
"github.com/openshift-kni/eco-goinfra/pkg/msg"
11
11
k8serrors "k8s.io/apimachinery/pkg/api/errors"
12
+ runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
12
13
13
- mcov1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
14
+ mcv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
14
15
corev1 "k8s.io/api/core/v1"
15
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
17
"k8s.io/apimachinery/pkg/util/wait"
17
18
)
18
19
19
20
const (
20
- fiveScds time.Duration = 5 * time .Second
21
- isTrue = "True"
22
- machineConfigPool = "MachineConfigPool"
21
+ fiveScds time.Duration = 5 * time .Second
23
22
)
24
23
25
24
// MCPBuilder provides struct for MachineConfigPool object which contains connection to cluster
26
25
// and MachineConfigPool definitions.
27
26
type MCPBuilder struct {
28
27
// MachineConfigPool definition. Used to create MachineConfigPool object with minimum set of required elements.
29
- Definition * mcov1 .MachineConfigPool
28
+ Definition * mcv1 .MachineConfigPool
30
29
// Created MachineConfigPool object on the cluster.
31
- Object * mcov1 .MachineConfigPool
30
+ Object * mcv1 .MachineConfigPool
32
31
// api client to interact with the cluster.
33
- apiClient * clients. Settings
32
+ apiClient runtimeclient. Client
34
33
// errorMsg is processed before MachineConfigPool object is created.
35
34
errorMsg string
36
35
}
@@ -43,9 +42,22 @@ func NewMCPBuilder(apiClient *clients.Settings, mcpName string) *MCPBuilder {
43
42
glog .V (100 ).Infof (
44
43
"Initializing new MCPBuilder structure with the following params: %s" , mcpName )
45
44
45
+ if apiClient == nil {
46
+ glog .V (100 ).Info ("The apiClient of the MachineConfigPool is nil" )
47
+
48
+ return nil
49
+ }
50
+
51
+ err := apiClient .AttachScheme (mcv1 .Install )
52
+ if err != nil {
53
+ glog .V (100 ).Info ("Failed to add machineconfig v1 scheme to client schemes" )
54
+
55
+ return nil
56
+ }
57
+
46
58
builder := & MCPBuilder {
47
- apiClient : apiClient ,
48
- Definition : & mcov1 .MachineConfigPool {
59
+ apiClient : apiClient . Client ,
60
+ Definition : & mcv1 .MachineConfigPool {
49
61
ObjectMeta : metav1.ObjectMeta {
50
62
Name : mcpName ,
51
63
},
@@ -55,7 +67,7 @@ func NewMCPBuilder(apiClient *clients.Settings, mcpName string) *MCPBuilder {
55
67
if mcpName == "" {
56
68
glog .V (100 ).Infof ("The name of the MachineConfigPool is empty" )
57
69
58
- builder .errorMsg = "MachineConfigPool 'name' cannot be empty"
70
+ builder .errorMsg = "machineconfigpool 'name' cannot be empty"
59
71
}
60
72
61
73
return builder
@@ -65,9 +77,22 @@ func NewMCPBuilder(apiClient *clients.Settings, mcpName string) *MCPBuilder {
65
77
func Pull (apiClient * clients.Settings , name string ) (* MCPBuilder , error ) {
66
78
glog .V (100 ).Infof ("Pulling existing machineconfigpool name %s from cluster" , name )
67
79
80
+ if apiClient == nil {
81
+ glog .V (100 ).Info ("The apiClient of the MachineConfigPool is nil" )
82
+
83
+ return nil , fmt .Errorf ("machineconfigpool 'apiClient' cannot be nil" )
84
+ }
85
+
86
+ err := apiClient .AttachScheme (mcv1 .Install )
87
+ if err != nil {
88
+ glog .V (100 ).Info ("Failed to add machineconfig v1 scheme to client schemes" )
89
+
90
+ return nil , err
91
+ }
92
+
68
93
builder := MCPBuilder {
69
- apiClient : apiClient ,
70
- Definition : & mcov1 .MachineConfigPool {
94
+ apiClient : apiClient . Client ,
95
+ Definition : & mcv1 .MachineConfigPool {
71
96
ObjectMeta : metav1.ObjectMeta {
72
97
Name : name ,
73
98
},
@@ -77,7 +102,7 @@ func Pull(apiClient *clients.Settings, name string) (*MCPBuilder, error) {
77
102
if name == "" {
78
103
glog .V (100 ).Infof ("The name of the machineconfigpool is empty" )
79
104
80
- builder . errorMsg = "machineconfigpool 'name' cannot be empty"
105
+ return nil , fmt . Errorf ( "machineconfigpool 'name' cannot be empty" )
81
106
}
82
107
83
108
if ! builder .Exists () {
@@ -89,6 +114,26 @@ func Pull(apiClient *clients.Settings, name string) (*MCPBuilder, error) {
89
114
return & builder , nil
90
115
}
91
116
117
+ // Get returns the MachineConfigPool object if found.
118
+ func (builder * MCPBuilder ) Get () (* mcv1.MachineConfigPool , error ) {
119
+ if valid , err := builder .validate (); ! valid {
120
+ return nil , err
121
+ }
122
+
123
+ glog .V (100 ).Infof ("Getting MachineConfigPool object %s" , builder .Definition .Name )
124
+
125
+ machineConfigPool := & mcv1.MachineConfigPool {}
126
+ err := builder .apiClient .Get (context .TODO (), runtimeclient.ObjectKey {Name : builder .Definition .Name }, machineConfigPool )
127
+
128
+ if err != nil {
129
+ glog .V (100 ).Infof ("MachineConfigPool object %s does not exist" , builder .Definition .Name )
130
+
131
+ return nil , err
132
+ }
133
+
134
+ return machineConfigPool , nil
135
+ }
136
+
92
137
// Create makes a MachineConfigPool in cluster and stores the created object in struct.
93
138
func (builder * MCPBuilder ) Create () (* MCPBuilder , error ) {
94
139
if valid , err := builder .validate (); ! valid {
@@ -100,8 +145,10 @@ func (builder *MCPBuilder) Create() (*MCPBuilder, error) {
100
145
101
146
var err error
102
147
if ! builder .Exists () {
103
- builder .Object , err = builder .apiClient .MachineConfigPools ().Create (
104
- context .TODO (), builder .Definition , metav1.CreateOptions {})
148
+ err = builder .apiClient .Create (context .TODO (), builder .Definition )
149
+ if err == nil {
150
+ builder .Object = builder .Definition
151
+ }
105
152
}
106
153
107
154
return builder , err
@@ -117,16 +164,20 @@ func (builder *MCPBuilder) Delete() error {
117
164
builder .Definition .Name )
118
165
119
166
if ! builder .Exists () {
120
- return fmt .Errorf ("MachineConfigPool cannot be deleted because it does not exist" )
121
- }
167
+ glog .V (100 ).Infof ("MachineConfigPool %s cannot be deleted because it does not exist" , builder .Definition .Name )
122
168
123
- err := builder .apiClient .MachineConfigPools ().Delete (
124
- context .TODO (), builder .Object .Name , metav1.DeleteOptions {})
169
+ builder .Object = nil
125
170
171
+ return nil
172
+ }
173
+
174
+ err := builder .apiClient .Delete (context .TODO (), builder .Object )
126
175
if err != nil {
127
- return fmt .Errorf ("cannot delete MachineConfigPool : %w" , err )
176
+ return fmt .Errorf ("cannot delete machineconfigpool : %w" , err )
128
177
}
129
178
179
+ builder .Object = nil
180
+
130
181
return err
131
182
}
132
183
@@ -140,8 +191,7 @@ func (builder *MCPBuilder) Exists() bool {
140
191
builder .Definition .Name )
141
192
142
193
var err error
143
- builder .Object , err = builder .apiClient .MachineConfigPools ().Get (
144
- context .TODO (), builder .Definition .Name , metav1.GetOptions {})
194
+ builder .Object , err = builder .Get ()
145
195
146
196
return err == nil || ! k8serrors .IsNotFound (err )
147
197
}
@@ -156,13 +206,15 @@ func (builder *MCPBuilder) WithMcSelector(mcSelector map[string]string) *MCPBuil
156
206
"machineConfigSelector label: %v" , mcSelector )
157
207
158
208
if len (mcSelector ) == 0 {
159
- builder .errorMsg = "'machineConfigSelector MatchLabels' field cannot be empty"
160
- }
209
+ builder .errorMsg = "machineConfigSelector 'MatchLabels' field cannot be empty"
161
210
162
- if builder .errorMsg != "" {
163
211
return builder
164
212
}
165
213
214
+ if builder .Definition .Spec .MachineConfigSelector == nil {
215
+ builder .Definition .Spec .MachineConfigSelector = & metav1.LabelSelector {}
216
+ }
217
+
166
218
builder .Definition .Spec .MachineConfigSelector .MatchLabels = mcSelector
167
219
168
220
return builder
@@ -171,7 +223,7 @@ func (builder *MCPBuilder) WithMcSelector(mcSelector map[string]string) *MCPBuil
171
223
// WaitToBeInCondition waits for a specific time duration until the MachineConfigPool will have a
172
224
// specified condition type with the expected status.
173
225
func (builder * MCPBuilder ) WaitToBeInCondition (
174
- conditionType mcov1 .MachineConfigPoolConditionType ,
226
+ conditionType mcv1 .MachineConfigPoolConditionType ,
175
227
conditionStatus corev1.ConditionStatus ,
176
228
timeout time.Duration ,
177
229
) error {
@@ -184,9 +236,7 @@ func (builder *MCPBuilder) WaitToBeInCondition(
184
236
185
237
return wait .PollUntilContextTimeout (
186
238
context .TODO (), fiveScds , timeout , true , func (ctx context.Context ) (bool , error ) {
187
- mcp , err := builder .apiClient .MachineConfigPools ().Get (context .TODO (),
188
- builder .Object .Name , metav1.GetOptions {})
189
-
239
+ mcp , err := builder .Get ()
190
240
if err != nil {
191
241
return false , nil
192
242
}
@@ -210,26 +260,22 @@ func (builder *MCPBuilder) WaitForUpdate(timeout time.Duration) error {
210
260
glog .V (100 ).Infof ("WaitForUpdate waits up to specified time %v until updating" +
211
261
" machineConfigPool object is updated" , timeout )
212
262
213
- mcpUpdating , err := builder .apiClient .MachineConfigPools ().Get (context .TODO (),
214
- builder .Object .Name , metav1.GetOptions {})
215
-
263
+ mcpUpdating , err := builder .Get ()
216
264
if err != nil {
217
265
return err
218
266
}
219
267
220
268
for _ , condition := range mcpUpdating .Status .Conditions {
221
- if condition .Type == "Updating" && condition .Status == isTrue {
269
+ if condition .Type == "Updating" && condition .Status == corev1 . ConditionTrue {
222
270
err := wait .PollUntilContextTimeout (
223
271
context .TODO (), fiveScds , timeout , true , func (ctx context.Context ) (bool , error ) {
224
- mcpUpdated , err := builder .apiClient .MachineConfigPools ().Get (context .TODO (),
225
- builder .Object .Name , metav1.GetOptions {})
226
-
272
+ mcpUpdated , err := builder .Get ()
227
273
if err != nil {
228
274
return false , nil
229
275
}
230
276
231
277
for _ , condition := range mcpUpdated .Status .Conditions {
232
- if condition .Type == "Updated" && condition .Status == isTrue {
278
+ if condition .Type == "Updated" && condition .Status == corev1 . ConditionTrue {
233
279
return true , nil
234
280
}
235
281
}
@@ -339,7 +385,7 @@ func (builder *MCPBuilder) WithOptions(options ...MCPAdditionalOptions) *MCPBuil
339
385
340
386
// IsInCondition parses MachineConfigPool conditions.
341
387
// Returns true if given MachineConfigPool is in given condition, otherwise false.
342
- func (builder * MCPBuilder ) IsInCondition (mcpConditionType mcov1 .MachineConfigPoolConditionType ) bool {
388
+ func (builder * MCPBuilder ) IsInCondition (mcpConditionType mcv1 .MachineConfigPoolConditionType ) bool {
343
389
if valid , _ := builder .validate (); ! valid {
344
390
return false
345
391
}
@@ -349,7 +395,7 @@ func (builder *MCPBuilder) IsInCondition(mcpConditionType mcov1.MachineConfigPoo
349
395
350
396
if builder .Exists () {
351
397
for _ , condition := range builder .Object .Status .Conditions {
352
- if condition .Type == mcpConditionType && condition .Status == isTrue {
398
+ if condition .Type == mcpConditionType && condition .Status == corev1 . ConditionTrue {
353
399
return true
354
400
}
355
401
}
0 commit comments