Skip to content

Commit 7c2a700

Browse files
Merge pull request #1398 from vr4manta/SPLAT-2381
SPLAT-2381: Fixed e2e test to generate machines with cluster name prefix
2 parents f6c3239 + 64cdb6c commit 7c2a700

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

test/e2e/util.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package e2e
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

89
. "github.com/onsi/gomega"
@@ -85,6 +86,9 @@ func SkipUnlessMachineAPIOperator(dc dynamic.Interface, c coreclient.NamespaceIn
8586
Expect(err).NotTo(HaveOccurred())
8687
}
8788

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.
8892
func LoadInfra(cfg *rest.Config) *configv1.Infrastructure {
8993
configClient, err := configclient.NewForConfig(cfg)
9094
Expect(err).NotTo(HaveOccurred())
@@ -95,6 +99,8 @@ func LoadInfra(cfg *rest.Config) *configv1.Infrastructure {
9599
return infra
96100
}
97101

102+
// GetMachineSets retrieves all MachineSets from the machine API namespace.
103+
// It returns a list of MachineSets and any error encountered during the operation.
98104
func GetMachineSets(cfg *rest.Config) (*v1beta1.MachineSetList, error) {
99105
ctx := context.Background()
100106
client, err := machinesetclient.NewForConfig(cfg)
@@ -107,7 +113,15 @@ func GetMachineSets(cfg *rest.Config) (*v1beta1.MachineSetList, error) {
107113
}
108114

109115
// 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.
111125
func ScaleMachineSet(cfg *rest.Config, name string, replicas int) error {
112126
scaleClient, err := GetScaleClient(cfg)
113127
if err != nil {
@@ -133,6 +147,11 @@ func ScaleMachineSet(cfg *rest.Config, name string, replicas int) error {
133147
return nil
134148
}
135149

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.
136155
func GetScaleClient(cfg *rest.Config) (scale.ScalesGetter, error) {
137156
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg)
138157
if err != nil {
@@ -153,10 +172,28 @@ func GetScaleClient(cfg *rest.Config) (scale.ScalesGetter, error) {
153172
return scaleClient, nil
154173
}
155174

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.
156188
func CreateMachine(ctx context.Context, cfg *rest.Config, mc *machinesetclient.MachineV1beta1Client, machineName, role string, provider *runtime.RawExtension) (*v1beta1.Machine, error) {
157189
// Get infra for configs
158190
infra := LoadInfra(cfg)
159191

192+
// Added cluster name as prefix if missing
193+
if !strings.HasPrefix(machineName, infra.Status.InfrastructureName) {
194+
machineName = infra.Status.InfrastructureName + machineName
195+
}
196+
160197
machine := &v1beta1.Machine{
161198
TypeMeta: metav1.TypeMeta{
162199
Kind: "MachineSet",
@@ -190,12 +227,32 @@ func CreateMachine(ctx context.Context, cfg *rest.Config, mc *machinesetclient.M
190227
return mc.Machines(MachineAPINamespace).Create(ctx, machine, metav1.CreateOptions{})
191228
}
192229

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.
193244
func CreateMachineSet(ctx context.Context, cfg *rest.Config, mc *machinesetclient.MachineV1beta1Client, name, role string, provider *runtime.RawExtension) (*v1beta1.MachineSet, error) {
194245
replicas := int32(0)
246+
testName := name
195247

196248
// Get infra for configs
197249
infra := LoadInfra(cfg)
198250

251+
// Added cluster name as prefix if missing
252+
if !strings.HasPrefix(name, infra.Status.InfrastructureName) {
253+
name = infra.Status.InfrastructureName + name
254+
}
255+
199256
machineset := &v1beta1.MachineSet{
200257
TypeMeta: metav1.TypeMeta{
201258
Kind: "MachineSet",
@@ -205,7 +262,7 @@ func CreateMachineSet(ctx context.Context, cfg *rest.Config, mc *machinesetclien
205262
Name: name,
206263
Namespace: MachineAPINamespace,
207264
Labels: map[string]string{
208-
"machine.openshift.io/test": name,
265+
"machine.openshift.io/test": testName,
209266
},
210267
},
211268
Spec: v1beta1.MachineSetSpec{

0 commit comments

Comments
 (0)