From 12863076fb43648baed393cbe6cfd65ba578e61b Mon Sep 17 00:00:00 2001 From: Tanmay Satam Date: Tue, 20 Feb 2024 14:58:10 -0500 Subject: [PATCH] Revert pkg/util/cluster creation to use 20230904 API (#3415) * Revert pkg/util/cluster creation to use 20230904 API * remove 20231122 client from cluster.go entirely --- .../redhatopenshift/openshiftclusters.go | 54 ++++++++++++++ .../openshiftclusters_addons.go | 72 +++++++++++++++++++ .../2023-09-04/redhatopenshift/operations.go | 48 +++++++++++++ .../redhatopenshift/operations_addons.go | 33 +++++++++ pkg/util/cluster/cluster.go | 22 +++--- 5 files changed, 217 insertions(+), 12 deletions(-) create mode 100644 pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters.go create mode 100644 pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters_addons.go create mode 100644 pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations.go create mode 100644 pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations_addons.go diff --git a/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters.go b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters.go new file mode 100644 index 00000000000..09bc10dbf10 --- /dev/null +++ b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters.go @@ -0,0 +1,54 @@ +package redhatopenshift + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "context" + "crypto/tls" + "net/http" + "time" + + "github.com/Azure/go-autorest/autorest" + + mgmtredhatopenshift20230904 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-09-04/redhatopenshift" + "github.com/Azure/ARO-RP/pkg/env" + "github.com/Azure/ARO-RP/pkg/util/azureclient" +) + +// OpenShiftClustersClient is a minimal interface for azure OpenshiftClustersClient +type OpenShiftClustersClient interface { + ListCredentials(ctx context.Context, resourceGroupName string, resourceName string) (result mgmtredhatopenshift20230904.OpenShiftClusterCredentials, err error) + Get(ctx context.Context, resourceGroupName string, resourceName string) (result mgmtredhatopenshift20230904.OpenShiftCluster, err error) + OpenShiftClustersClientAddons +} + +type openShiftClustersClient struct { + mgmtredhatopenshift20230904.OpenShiftClustersClient +} + +var _ OpenShiftClustersClient = &openShiftClustersClient{} + +// NewOpenShiftClustersClient creates a new OpenShiftClustersClient +func NewOpenShiftClustersClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) OpenShiftClustersClient { + var client mgmtredhatopenshift20230904.OpenShiftClustersClient + if env.IsLocalDevelopmentMode() { + client = mgmtredhatopenshift20230904.NewOpenShiftClustersClientWithBaseURI("https://localhost:8443", subscriptionID) + client.Sender = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, // #nosec G402 + }, + }, + } + } else { + client = mgmtredhatopenshift20230904.NewOpenShiftClustersClientWithBaseURI(environment.ResourceManagerEndpoint, subscriptionID) + client.Authorizer = authorizer + } + client.PollingDelay = 10 * time.Second + client.PollingDuration = 2 * time.Hour + + return &openShiftClustersClient{ + OpenShiftClustersClient: client, + } +} diff --git a/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters_addons.go b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters_addons.go new file mode 100644 index 00000000000..e1dc0603761 --- /dev/null +++ b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/openshiftclusters_addons.go @@ -0,0 +1,72 @@ +package redhatopenshift + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "context" + + mgmtredhatopenshift20230904 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-09-04/redhatopenshift" +) + +// OpenShiftClustersClientAddons contains addons for OpenShiftClustersClient +type OpenShiftClustersClientAddons interface { + CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, resourceName string, parameters mgmtredhatopenshift20230904.OpenShiftCluster) error + DeleteAndWait(ctx context.Context, resourceGroupName string, resourceName string) error + List(ctx context.Context) (clusters []mgmtredhatopenshift20230904.OpenShiftCluster, err error) + ListByResourceGroup(ctx context.Context, resourceGroupName string) (clusters []mgmtredhatopenshift20230904.OpenShiftCluster, err error) +} + +func (c *openShiftClustersClient) CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, resourceName string, parameters mgmtredhatopenshift20230904.OpenShiftCluster) error { + future, err := c.CreateOrUpdate(ctx, resourceGroupName, resourceName, parameters) + if err != nil { + return err + } + + return future.WaitForCompletionRef(ctx, c.Client) +} + +func (c *openShiftClustersClient) DeleteAndWait(ctx context.Context, resourceGroupName string, resourceName string) error { + future, err := c.Delete(ctx, resourceGroupName, resourceName) + if err != nil { + return err + } + + return future.WaitForCompletionRef(ctx, c.Client) +} + +func (c *openShiftClustersClient) List(ctx context.Context) (clusters []mgmtredhatopenshift20230904.OpenShiftCluster, err error) { + page, err := c.OpenShiftClustersClient.List(ctx) + if err != nil { + return nil, err + } + + for page.NotDone() { + clusters = append(clusters, page.Values()...) + + err = page.NextWithContext(ctx) + if err != nil { + return nil, err + } + } + + return clusters, nil +} + +func (c *openShiftClustersClient) ListByResourceGroup(ctx context.Context, resourceGroupName string) (clusters []mgmtredhatopenshift20230904.OpenShiftCluster, err error) { + page, err := c.OpenShiftClustersClient.ListByResourceGroup(ctx, resourceGroupName) + if err != nil { + return nil, err + } + + for page.NotDone() { + clusters = append(clusters, page.Values()...) + + err = page.NextWithContext(ctx) + if err != nil { + return nil, err + } + } + + return clusters, nil +} diff --git a/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations.go b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations.go new file mode 100644 index 00000000000..0b4ee641485 --- /dev/null +++ b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations.go @@ -0,0 +1,48 @@ +package redhatopenshift + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "crypto/tls" + "net/http" + + "github.com/Azure/go-autorest/autorest" + + mgmtredhatopenshift20230904 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-09-04/redhatopenshift" + "github.com/Azure/ARO-RP/pkg/env" + "github.com/Azure/ARO-RP/pkg/util/azureclient" +) + +// OperationsClient is a minimal interface for azure OperationsClient +type OperationsClient interface { + OperationsClientAddons +} + +type operationsClient struct { + mgmtredhatopenshift20230904.OperationsClient +} + +var _ OperationsClient = &operationsClient{} + +// NewOperationsClient creates a new OperationsClient +func NewOperationsClient(environment *azureclient.AROEnvironment, subscriptionID string, authorizer autorest.Authorizer) OperationsClient { + var client mgmtredhatopenshift20230904.OperationsClient + if env.IsLocalDevelopmentMode() { + client = mgmtredhatopenshift20230904.NewOperationsClientWithBaseURI("https://localhost:8443", subscriptionID) + client.Sender = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, // #nosec G402 + }, + }, + } + } else { + client = mgmtredhatopenshift20230904.NewOperationsClientWithBaseURI(environment.ResourceManagerEndpoint, subscriptionID) + client.Authorizer = authorizer + } + + return &operationsClient{ + OperationsClient: client, + } +} diff --git a/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations_addons.go b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations_addons.go new file mode 100644 index 00000000000..d375141cf07 --- /dev/null +++ b/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift/operations_addons.go @@ -0,0 +1,33 @@ +package redhatopenshift + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "context" + + mgmtredhatopenshift20230904 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-09-04/redhatopenshift" +) + +// OperationsClientAddons contains addons for OperationsClient +type OperationsClientAddons interface { + List(ctx context.Context) (operations []mgmtredhatopenshift20230904.Operation, err error) +} + +func (c *operationsClient) List(ctx context.Context) (operations []mgmtredhatopenshift20230904.Operation, err error) { + page, err := c.OperationsClient.List(ctx) + if err != nil { + return nil, err + } + + for page.NotDone() { + operations = append(operations, page.Values()...) + + err = page.NextWithContext(ctx) + if err != nil { + return nil, err + } + } + + return operations, nil +} diff --git a/pkg/util/cluster/cluster.go b/pkg/util/cluster/cluster.go index fe0d5cd8e27..cc80e083c94 100644 --- a/pkg/util/cluster/cluster.go +++ b/pkg/util/cluster/cluster.go @@ -28,8 +28,8 @@ import ( "k8s.io/apimachinery/pkg/util/wait" "github.com/Azure/ARO-RP/pkg/api" - v20231122 "github.com/Azure/ARO-RP/pkg/api/v20231122" - mgmtredhatopenshift20231122 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-11-22/redhatopenshift" + v20230904 "github.com/Azure/ARO-RP/pkg/api/v20230904" + mgmtredhatopenshift20230904 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-09-04/redhatopenshift" "github.com/Azure/ARO-RP/pkg/deploy/assets" "github.com/Azure/ARO-RP/pkg/deploy/generator" "github.com/Azure/ARO-RP/pkg/env" @@ -41,7 +41,7 @@ import ( redhatopenshift20200430 "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2020-04-30/redhatopenshift" redhatopenshift20210901preview "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2021-09-01-preview/redhatopenshift" redhatopenshift20220401 "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2022-04-01/redhatopenshift" - redhatopenshift20231122 "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2023-11-22/redhatopenshift" + redhatopenshift20230904 "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2023-09-04/redhatopenshift" utilgraph "github.com/Azure/ARO-RP/pkg/util/graph" "github.com/Azure/ARO-RP/pkg/util/rbac" "github.com/Azure/ARO-RP/pkg/util/uuid" @@ -60,8 +60,7 @@ type Cluster struct { openshiftclustersv20200430 redhatopenshift20200430.OpenShiftClustersClient openshiftclustersv20210901preview redhatopenshift20210901preview.OpenShiftClustersClient openshiftclustersv20220401 redhatopenshift20220401.OpenShiftClustersClient - openshiftclustersv20220904 redhatopenshift20231122.OpenShiftClustersClient - openshiftclustersv20231122 redhatopenshift20231122.OpenShiftClustersClient + openshiftclustersv20230904 redhatopenshift20230904.OpenShiftClustersClient securitygroups network.SecurityGroupsClient subnets network.SubnetsClient routetables network.RouteTablesClient @@ -116,8 +115,7 @@ func New(log *logrus.Entry, environment env.Core, ci bool) (*Cluster, error) { openshiftclustersv20200430: redhatopenshift20200430.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer), openshiftclustersv20210901preview: redhatopenshift20210901preview.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer), openshiftclustersv20220401: redhatopenshift20220401.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer), - openshiftclustersv20220904: redhatopenshift20231122.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer), - openshiftclustersv20231122: redhatopenshift20231122.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer), + openshiftclustersv20230904: redhatopenshift20230904.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer), securitygroups: network.NewSecurityGroupsClient(environment.Environment(), environment.SubscriptionID(), authorizer), subnets: network.NewSubnetsClient(environment.Environment(), environment.SubscriptionID(), authorizer), routetables: network.NewRouteTablesClient(environment.Environment(), environment.SubscriptionID(), authorizer), @@ -142,9 +140,9 @@ func New(log *logrus.Entry, environment env.Core, ci bool) (*Cluster, error) { } func (c *Cluster) Create(ctx context.Context, vnetResourceGroup, clusterName string, osClusterVersion string) error { - clusterGet, err := c.openshiftclustersv20231122.Get(ctx, vnetResourceGroup, clusterName) + clusterGet, err := c.openshiftclustersv20230904.Get(ctx, vnetResourceGroup, clusterName) if err == nil { - if clusterGet.ProvisioningState == mgmtredhatopenshift20231122.Failed { + if clusterGet.ProvisioningState == mgmtredhatopenshift20230904.Failed { return fmt.Errorf("cluster exists and is in failed provisioning state, please delete and retry") } c.log.Print("cluster already exists, skipping create") @@ -503,19 +501,19 @@ func (c *Cluster) createCluster(ctx context.Context, vnetResourceGroup, clusterN oc.Properties.WorkerProfiles[0].VMSize = api.VMSizeStandardD2sV3 } - ext := api.APIs[v20231122.APIVersion].OpenShiftClusterConverter.ToExternal(&oc) + ext := api.APIs[v20230904.APIVersion].OpenShiftClusterConverter.ToExternal(&oc) data, err := json.Marshal(ext) if err != nil { return err } - ocExt := mgmtredhatopenshift20231122.OpenShiftCluster{} + ocExt := mgmtredhatopenshift20230904.OpenShiftCluster{} err = json.Unmarshal(data, &ocExt) if err != nil { return err } - return c.openshiftclustersv20231122.CreateOrUpdateAndWait(ctx, vnetResourceGroup, clusterName, ocExt) + return c.openshiftclustersv20230904.CreateOrUpdateAndWait(ctx, vnetResourceGroup, clusterName, ocExt) } func (c *Cluster) registerSubscription(ctx context.Context) error {