@@ -3,6 +3,7 @@ package e2e
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
"time"
7
8
8
9
. "github.com/onsi/gomega"
@@ -85,6 +86,9 @@ func SkipUnlessMachineAPIOperator(dc dynamic.Interface, c coreclient.NamespaceIn
85
86
Expect (err ).NotTo (HaveOccurred ())
86
87
}
87
88
89
+ // LoadInfra retrieves the cluster infrastructure configuration from the API server.
90
+ // It returns the Infrastructure object containing cluster-specific information such as
91
+ // the infrastructure name and platform type.
88
92
func LoadInfra (cfg * rest.Config ) * configv1.Infrastructure {
89
93
configClient , err := configclient .NewForConfig (cfg )
90
94
Expect (err ).NotTo (HaveOccurred ())
@@ -95,6 +99,8 @@ func LoadInfra(cfg *rest.Config) *configv1.Infrastructure {
95
99
return infra
96
100
}
97
101
102
+ // GetMachineSets retrieves all MachineSets from the machine API namespace.
103
+ // It returns a list of MachineSets and any error encountered during the operation.
98
104
func GetMachineSets (cfg * rest.Config ) (* v1beta1.MachineSetList , error ) {
99
105
ctx := context .Background ()
100
106
client , err := machinesetclient .NewForConfig (cfg )
@@ -107,7 +113,15 @@ func GetMachineSets(cfg *rest.Config) (*v1beta1.MachineSetList, error) {
107
113
}
108
114
109
115
// ScaleMachineSet scales a machineSet with a given name to the given number of replicas.
110
- // This was borrowed from origin. Ideally we should make this a sharable method if possible.
116
+ // It uses the scale client to update the replica count and retries the operation if needed.
117
+ // This was borrowed from origin. Ideally we should make this a sharable method if possible.
118
+ //
119
+ // Parameters:
120
+ // - cfg: REST configuration for the Kubernetes client
121
+ // - name: Name of the MachineSet to scale
122
+ // - replicas: Target number of replicas
123
+ //
124
+ // Returns an error if the scaling operation fails.
111
125
func ScaleMachineSet (cfg * rest.Config , name string , replicas int ) error {
112
126
scaleClient , err := GetScaleClient (cfg )
113
127
if err != nil {
@@ -133,6 +147,11 @@ func ScaleMachineSet(cfg *rest.Config, name string, replicas int) error {
133
147
return nil
134
148
}
135
149
150
+ // GetScaleClient creates and returns a scale client for managing resource scaling operations.
151
+ // It sets up the necessary discovery client, REST mapper, and scale kind resolver to enable
152
+ // scaling operations on Kubernetes resources.
153
+ //
154
+ // Returns a ScalesGetter interface and any error encountered during client creation.
136
155
func GetScaleClient (cfg * rest.Config ) (scale.ScalesGetter , error ) {
137
156
discoveryClient , err := discovery .NewDiscoveryClientForConfig (cfg )
138
157
if err != nil {
@@ -153,10 +172,28 @@ func GetScaleClient(cfg *rest.Config) (scale.ScalesGetter, error) {
153
172
return scaleClient , nil
154
173
}
155
174
175
+ // CreateMachine creates a new Machine resource in the machine API namespace.
176
+ // It automatically prefixes the machine name with the cluster infrastructure name if not already present
177
+ // and sets up the machine with appropriate labels, taints, and provider-specific configuration.
178
+ //
179
+ // Parameters:
180
+ // - ctx: Context for the operation
181
+ // - cfg: REST configuration for the Kubernetes client
182
+ // - mc: Machine client for API operations
183
+ // - machineName: Name for the new machine (will be prefixed with cluster name if needed)
184
+ // - role: Role label for the machine (e.g., "worker", "master")
185
+ // - provider: Provider-specific configuration as RawExtension
186
+ //
187
+ // Returns the created Machine object and any error encountered.
156
188
func CreateMachine (ctx context.Context , cfg * rest.Config , mc * machinesetclient.MachineV1beta1Client , machineName , role string , provider * runtime.RawExtension ) (* v1beta1.Machine , error ) {
157
189
// Get infra for configs
158
190
infra := LoadInfra (cfg )
159
191
192
+ // Added cluster name as prefix if missing
193
+ if ! strings .HasPrefix (machineName , infra .Status .InfrastructureName ) {
194
+ machineName = infra .Status .InfrastructureName + machineName
195
+ }
196
+
160
197
machine := & v1beta1.Machine {
161
198
TypeMeta : metav1.TypeMeta {
162
199
Kind : "MachineSet" ,
@@ -190,12 +227,32 @@ func CreateMachine(ctx context.Context, cfg *rest.Config, mc *machinesetclient.M
190
227
return mc .Machines (MachineAPINamespace ).Create (ctx , machine , metav1.CreateOptions {})
191
228
}
192
229
230
+ // CreateMachineSet creates a new MachineSet resource in the machine API namespace.
231
+ // It automatically prefixes the MachineSet name with the cluster infrastructure name if not already present
232
+ // and creates the MachineSet with 0 replicas initially. The MachineSet includes appropriate labels,
233
+ // selectors, and a machine template with the provided configuration.
234
+ //
235
+ // Parameters:
236
+ // - ctx: Context for the operation
237
+ // - cfg: REST configuration for the Kubernetes client
238
+ // - mc: Machine client for API operations
239
+ // - name: Name for the new MachineSet (will be prefixed with cluster name if needed)
240
+ // - role: Role label for machines created by this set (e.g., "worker", "master")
241
+ // - provider: Provider-specific configuration as RawExtension for the machine template
242
+ //
243
+ // Returns the created MachineSet object and any error encountered.
193
244
func CreateMachineSet (ctx context.Context , cfg * rest.Config , mc * machinesetclient.MachineV1beta1Client , name , role string , provider * runtime.RawExtension ) (* v1beta1.MachineSet , error ) {
194
245
replicas := int32 (0 )
246
+ testName := name
195
247
196
248
// Get infra for configs
197
249
infra := LoadInfra (cfg )
198
250
251
+ // Added cluster name as prefix if missing
252
+ if ! strings .HasPrefix (name , infra .Status .InfrastructureName ) {
253
+ name = infra .Status .InfrastructureName + name
254
+ }
255
+
199
256
machineset := & v1beta1.MachineSet {
200
257
TypeMeta : metav1.TypeMeta {
201
258
Kind : "MachineSet" ,
@@ -205,7 +262,7 @@ func CreateMachineSet(ctx context.Context, cfg *rest.Config, mc *machinesetclien
205
262
Name : name ,
206
263
Namespace : MachineAPINamespace ,
207
264
Labels : map [string ]string {
208
- "machine.openshift.io/test" : name ,
265
+ "machine.openshift.io/test" : testName ,
209
266
},
210
267
},
211
268
Spec : v1beta1.MachineSetSpec {
0 commit comments