Skip to content

Commit 3294a57

Browse files
committed
Add initial Rosa machine pool integration tests
1 parent 85759ce commit 3294a57

File tree

10 files changed

+850
-42
lines changed

10 files changed

+850
-42
lines changed

controlplane/rosa/controllers/rosacontrolplane_controller.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
stsv2 "github.com/aws/aws-sdk-go-v2/service/sts"
3333
sts "github.com/aws/aws-sdk-go/service/sts"
34+
"github.com/aws/aws-sdk-go/service/sts/stsiface"
3435
"github.com/google/go-cmp/cmp"
3536
idputils "github.com/openshift-online/ocm-common/pkg/idp/utils"
3637
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
@@ -40,6 +41,7 @@ import (
4041
corev1 "k8s.io/api/core/v1"
4142
apierrors "k8s.io/apimachinery/pkg/api/errors"
4243
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
44+
"k8s.io/apimachinery/pkg/runtime"
4345
"k8s.io/apimachinery/pkg/types"
4446
kerrors "k8s.io/apimachinery/pkg/util/errors"
4547
"k8s.io/apiserver/pkg/storage/names"
@@ -58,6 +60,7 @@ import (
5860
rosacontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/rosa/api/v1beta2"
5961
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
6062
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/annotations"
63+
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud"
6164
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
6265
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
6366
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/rosa"
@@ -89,11 +92,15 @@ type ROSAControlPlaneReconciler struct {
8992
WatchFilterValue string
9093
WaitInfraPeriod time.Duration
9194
Endpoints []scope.ServiceEndpoint
95+
NewStsClient func(cloud.ScopeUsage, cloud.Session, logger.Wrapper, runtime.Object) stsiface.STSAPI
96+
NewOCMClient func(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope) (rosa.OCMClient, error)
9297
}
9398

9499
// SetupWithManager is used to setup the controller.
95100
func (r *ROSAControlPlaneReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
96101
log := logger.FromContext(ctx)
102+
r.NewOCMClient = rosa.NewOCMClient
103+
r.NewStsClient = scope.NewSTSClient
97104

98105
rosaControlPlane := &rosacontrolplanev1.ROSAControlPlane{}
99106
c, err := ctrl.NewControllerManagedBy(mgr).
@@ -173,6 +180,7 @@ func (r *ROSAControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Req
173180
ControllerName: strings.ToLower(rosaControlPlaneKind),
174181
Endpoints: r.Endpoints,
175182
Logger: log,
183+
NewStsClient: r.NewStsClient,
176184
})
177185
if err != nil {
178186
return ctrl.Result{}, fmt.Errorf("failed to create scope: %w", err)
@@ -203,8 +211,8 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
203211
}
204212
}
205213

206-
ocmClient, err := rosa.NewOCMClient(ctx, rosaScope)
207-
if err != nil {
214+
ocmClient, err := r.NewOCMClient(ctx, rosaScope)
215+
if err != nil || ocmClient == nil {
208216
// TODO: need to expose in status, as likely the credentials are invalid
209217
return ctrl.Result{}, fmt.Errorf("failed to create OCM client: %w", err)
210218
}
@@ -332,7 +340,7 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
332340
}
333341

334342
ocmClient, err := rosa.NewOCMClient(ctx, rosaScope)
335-
if err != nil {
343+
if err != nil || ocmClient == nil {
336344
// TODO: need to expose in status, as likely the credentials are invalid
337345
return ctrl.Result{}, fmt.Errorf("failed to create OCM client: %w", err)
338346
}
@@ -406,7 +414,7 @@ func (r *ROSAControlPlaneReconciler) deleteMachinePools(ctx context.Context, ros
406414
return len(machinePools) == 0, nil
407415
}
408416

409-
func (r *ROSAControlPlaneReconciler) reconcileClusterVersion(rosaScope *scope.ROSAControlPlaneScope, ocmClient *ocm.Client, cluster *cmv1.Cluster) error {
417+
func (r *ROSAControlPlaneReconciler) reconcileClusterVersion(rosaScope *scope.ROSAControlPlaneScope, ocmClient rosa.OCMClient, cluster *cmv1.Cluster) error {
410418
version := rosaScope.ControlPlane.Spec.Version
411419
if version == rosa.RawVersionID(cluster.Version()) {
412420
conditions.MarkFalse(rosaScope.ControlPlane, rosacontrolplanev1.ROSAControlPlaneUpgradingCondition, "upgraded", clusterv1.ConditionSeverityInfo, "")
@@ -461,7 +469,7 @@ func (r *ROSAControlPlaneReconciler) reconcileClusterVersion(rosaScope *scope.RO
461469
return nil
462470
}
463471

464-
func (r *ROSAControlPlaneReconciler) updateOCMCluster(rosaScope *scope.ROSAControlPlaneScope, ocmClient *ocm.Client, cluster *cmv1.Cluster, creator *rosaaws.Creator) error {
472+
func (r *ROSAControlPlaneReconciler) updateOCMCluster(rosaScope *scope.ROSAControlPlaneScope, ocmClient rosa.OCMClient, cluster *cmv1.Cluster, creator *rosaaws.Creator) error {
465473
ocmClusterSpec, updated := r.updateOCMClusterSpec(rosaScope.ControlPlane, cluster)
466474

467475
if updated {
@@ -758,7 +766,7 @@ func (r *ROSAControlPlaneReconciler) reconcileExternalAuthBootstrapKubeconfig(ct
758766
return nil
759767
}
760768

761-
func (r *ROSAControlPlaneReconciler) reconcileKubeconfig(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope, ocmClient *ocm.Client, cluster *cmv1.Cluster) error {
769+
func (r *ROSAControlPlaneReconciler) reconcileKubeconfig(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope, ocmClient rosa.OCMClient, cluster *cmv1.Cluster) error {
762770
rosaScope.Debug("Reconciling ROSA kubeconfig for cluster", "cluster-name", rosaScope.RosaClusterName())
763771

764772
clusterRef := client.ObjectKeyFromObject(rosaScope.Cluster)
@@ -870,7 +878,7 @@ func (r *ROSAControlPlaneReconciler) reconcileClusterAdminPassword(ctx context.C
870878
return password, nil
871879
}
872880

873-
func validateControlPlaneSpec(ocmClient *ocm.Client, rosaScope *scope.ROSAControlPlaneScope) (string, error) {
881+
func validateControlPlaneSpec(ocmClient rosa.OCMClient, rosaScope *scope.ROSAControlPlaneScope) (string, error) {
874882
version := rosaScope.ControlPlane.Spec.Version
875883
valid, err := ocmClient.ValidateHypershiftVersion(version, ocm.DefaultChannelGroup)
876884
if err != nil {

exp/controllers/rosamachinepool_controller.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/aws/aws-sdk-go/aws"
99
"github.com/aws/aws-sdk-go/service/ec2"
10+
"github.com/aws/aws-sdk-go/service/sts/stsiface"
1011
"github.com/blang/semver"
1112
"github.com/google/go-cmp/cmp"
1213
"github.com/google/go-cmp/cmp/cmpopts"
@@ -16,6 +17,7 @@ import (
1617
corev1 "k8s.io/api/core/v1"
1718
apierrors "k8s.io/apimachinery/pkg/api/errors"
1819
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/apimachinery/pkg/runtime"
1921
"k8s.io/apimachinery/pkg/runtime/schema"
2022
"k8s.io/apimachinery/pkg/util/intstr"
2123
"k8s.io/client-go/tools/record"
@@ -31,6 +33,7 @@ import (
3133

3234
rosacontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/rosa/api/v1beta2"
3335
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
36+
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud"
3437
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
3538
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
3639
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/rosa"
@@ -48,11 +51,15 @@ type ROSAMachinePoolReconciler struct {
4851
Recorder record.EventRecorder
4952
WatchFilterValue string
5053
Endpoints []scope.ServiceEndpoint
54+
NewStsClient func(cloud.ScopeUsage, cloud.Session, logger.Wrapper, runtime.Object) stsiface.STSAPI
55+
NewOCMClient func(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope) (rosa.OCMClient, error)
5156
}
5257

5358
// SetupWithManager is used to setup the controller.
5459
func (r *ROSAMachinePoolReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
5560
log := logger.FromContext(ctx)
61+
r.NewOCMClient = rosa.NewOCMClient
62+
r.NewStsClient = scope.NewSTSClient
5663

5764
gvk, err := apiutil.GVKForObject(new(expinfrav1.ROSAMachinePool), mgr.GetScheme())
5865
if err != nil {
@@ -148,6 +155,7 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
148155
ControlPlane: controlPlane,
149156
ControllerName: "rosaControlPlane",
150157
Endpoints: r.Endpoints,
158+
NewStsClient: r.NewStsClient,
151159
})
152160
if err != nil {
153161
return ctrl.Result{}, errors.Wrap(err, "failed to create rosaControlPlane scope")
@@ -186,8 +194,8 @@ func (r *ROSAMachinePoolReconciler) reconcileNormal(ctx context.Context,
186194
}
187195
}
188196

189-
ocmClient, err := rosa.NewOCMClient(ctx, rosaControlPlaneScope)
190-
if err != nil {
197+
ocmClient, err := r.NewOCMClient(ctx, rosaControlPlaneScope)
198+
if err != nil || ocmClient == nil {
191199
// TODO: need to expose in status, as likely the credentials are invalid
192200
return ctrl.Result{}, fmt.Errorf("failed to create OCM client: %w", err)
193201
}
@@ -298,8 +306,8 @@ func (r *ROSAMachinePoolReconciler) reconcileDelete(
298306
) error {
299307
machinePoolScope.Info("Reconciling deletion of RosaMachinePool")
300308

301-
ocmClient, err := rosa.NewOCMClient(ctx, rosaControlPlaneScope)
302-
if err != nil {
309+
ocmClient, err := r.NewOCMClient(ctx, rosaControlPlaneScope)
310+
if err != nil || ocmClient == nil {
303311
// TODO: need to expose in status, as likely the credentials are invalid
304312
return fmt.Errorf("failed to create OCM client: %w", err)
305313
}
@@ -320,7 +328,7 @@ func (r *ROSAMachinePoolReconciler) reconcileDelete(
320328
return nil
321329
}
322330

323-
func (r *ROSAMachinePoolReconciler) reconcileMachinePoolVersion(machinePoolScope *scope.RosaMachinePoolScope, ocmClient *ocm.Client, nodePool *cmv1.NodePool) error {
331+
func (r *ROSAMachinePoolReconciler) reconcileMachinePoolVersion(machinePoolScope *scope.RosaMachinePoolScope, ocmClient rosa.OCMClient, nodePool *cmv1.NodePool) error {
324332
version := machinePoolScope.RosaMachinePool.Spec.Version
325333
if version == "" || version == rosa.RawVersionID(nodePool.Version()) {
326334
conditions.MarkFalse(machinePoolScope.RosaMachinePool, expinfrav1.RosaMachinePoolUpgradingCondition, "upgraded", clusterv1.ConditionSeverityInfo, "")
@@ -356,7 +364,7 @@ func (r *ROSAMachinePoolReconciler) reconcileMachinePoolVersion(machinePoolScope
356364
return nil
357365
}
358366

359-
func (r *ROSAMachinePoolReconciler) updateNodePool(machinePoolScope *scope.RosaMachinePoolScope, ocmClient *ocm.Client, nodePool *cmv1.NodePool) (*cmv1.NodePool, error) {
367+
func (r *ROSAMachinePoolReconciler) updateNodePool(machinePoolScope *scope.RosaMachinePoolScope, ocmClient rosa.OCMClient, nodePool *cmv1.NodePool) (*cmv1.NodePool, error) {
360368
machinePool := machinePoolScope.RosaMachinePool.DeepCopy()
361369
// default all fields before comparing, so that nil/unset fields don't cause an unnecessary update call.
362370
machinePool.Default()

0 commit comments

Comments
 (0)