Add new "internal" ARO client wrapper over external ARO clients

This commit is contained in:
Tanmay Satam 2024-06-06 17:03:19 -04:00
Родитель 4b35728661
Коммит 510b3ee469
2 изменённых файлов: 122 добавлений и 21 удалений

Просмотреть файл

@ -0,0 +1,116 @@
package cluster
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"encoding/json"
"github.com/Azure/go-autorest/autorest"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
v20231122 "github.com/Azure/ARO-RP/pkg/api/v20231122"
v20240812preview "github.com/Azure/ARO-RP/pkg/api/v20240812preview"
mgmtredhatopenshift20231122 "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2023-11-22/redhatopenshift"
mgmtredhatopenshift20240812preview "github.com/Azure/ARO-RP/pkg/client/services/redhatopenshift/mgmt/2024-08-12-preview/redhatopenshift"
"github.com/Azure/ARO-RP/pkg/env"
redhatopenshift20231122 "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2023-11-22/redhatopenshift"
redhatopenshift20240812preview "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2024-08-12-preview/redhatopenshift"
)
type InternalClient interface {
Get(ctx context.Context, resourceGroupName string, resourceName string) (*api.OpenShiftCluster, error)
CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, resourceName string, parameters *api.OpenShiftCluster) error
DeleteAndWait(ctx context.Context, resourceGroupName string, resourceName string) error
}
type clientCluster interface {
mgmtredhatopenshift20231122.OpenShiftCluster | mgmtredhatopenshift20240812preview.OpenShiftCluster
}
type apiCluster interface {
v20231122.OpenShiftCluster | v20240812preview.OpenShiftCluster
}
type externalClient[ClientCluster clientCluster] interface {
Get(ctx context.Context, resourceGroupName string, resourceName string) (ClientCluster, error)
CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, resourceName string, parameters ClientCluster) error
DeleteAndWait(ctx context.Context, resourceGroupName string, resourceName string) error
}
type internalClient[ClientCluster clientCluster, ApiCluster apiCluster] struct {
externalClient externalClient[ClientCluster]
converter api.OpenShiftClusterConverter
}
func NewInternalClient(log *logrus.Entry, environment env.Core, authorizer autorest.Authorizer) InternalClient {
if env.IsLocalDevelopmentMode() {
log.Infof("Using ARO API version [%s]", v20240812preview.APIVersion)
return &internalClient[mgmtredhatopenshift20240812preview.OpenShiftCluster, v20240812preview.OpenShiftCluster]{
externalClient: redhatopenshift20240812preview.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer),
converter: api.APIs[v20240812preview.APIVersion].OpenShiftClusterConverter,
}
}
log.Infof("Using ARO API version [%s]", v20231122.APIVersion)
return &internalClient[mgmtredhatopenshift20231122.OpenShiftCluster, v20231122.OpenShiftCluster]{
externalClient: redhatopenshift20231122.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer),
converter: api.APIs[v20231122.APIVersion].OpenShiftClusterConverter,
}
}
func (c *internalClient[ClientCluster, ApiCluster]) Get(ctx context.Context, resourceGroupName string, resourceName string) (*api.OpenShiftCluster, error) {
ocExt, err := c.externalClient.Get(ctx, resourceGroupName, resourceName)
if err != nil {
return nil, err
}
return c.toInternal(&ocExt)
}
func (c *internalClient[ClientCluster, ApiCluster]) CreateOrUpdateAndWait(ctx context.Context, resourceGroupName string, resourceName string, parameters *api.OpenShiftCluster) error {
ocExt, err := c.toExternal(parameters)
if err != nil {
return err
}
return c.externalClient.CreateOrUpdateAndWait(ctx, resourceGroupName, resourceName, *ocExt)
}
func (c *internalClient[ClientCluster, ApiCluster]) DeleteAndWait(ctx context.Context, resourceGroupName string, resourceName string) error {
return c.externalClient.DeleteAndWait(ctx, resourceGroupName, resourceName)
}
// We use JSON marshaling/unmarshaling to convert between our "external/versioned" cluster struct in pkg/api,
// and the struct in the generated clients
func (c *internalClient[ClientCluster, ApiCluster]) toExternal(oc *api.OpenShiftCluster) (*ClientCluster, error) {
apiExt := c.converter.ToExternal(oc)
ocExt := new(ClientCluster)
data, err := json.Marshal(apiExt)
if err != nil {
return ocExt, err
}
err = json.Unmarshal(data, &ocExt)
return ocExt, err
}
func (c *internalClient[ClientCluster, ApiCluster]) toInternal(ocExt *ClientCluster) (*api.OpenShiftCluster, error) {
oc := &api.OpenShiftCluster{}
apiExt := new(ApiCluster)
data, err := json.Marshal(ocExt)
if err != nil {
return oc, err
}
err = json.Unmarshal(data, apiExt)
if err != nil {
return oc, err
}
c.converter.ToInternal(apiExt, oc)
return oc, nil
}

Просмотреть файл

@ -31,8 +31,6 @@ 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"
"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 +39,6 @@ import (
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/authorization"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/network"
redhatopenshift20231122 "github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/redhatopenshift/2023-11-22/redhatopenshift"
"github.com/Azure/ARO-RP/pkg/util/azureerrors"
utilgraph "github.com/Azure/ARO-RP/pkg/util/graph"
"github.com/Azure/ARO-RP/pkg/util/rbac"
@ -59,7 +56,7 @@ type Cluster struct {
spGraphClient *utilgraph.GraphServiceClient
deployments features.DeploymentsClient
groups features.ResourceGroupsClient
openshiftclusters redhatopenshift20231122.OpenShiftClustersClient
openshiftclusters InternalClient
securitygroups network.SecurityGroupsClient
subnets network.SubnetsClient
routetables network.RouteTablesClient
@ -110,7 +107,7 @@ func New(log *logrus.Entry, environment env.Core, ci bool) (*Cluster, error) {
spGraphClient: spGraphClient,
deployments: features.NewDeploymentsClient(environment.Environment(), environment.SubscriptionID(), authorizer),
groups: features.NewResourceGroupsClient(environment.Environment(), environment.SubscriptionID(), authorizer),
openshiftclusters: redhatopenshift20231122.NewOpenShiftClustersClient(environment.Environment(), environment.SubscriptionID(), authorizer),
openshiftclusters: NewInternalClient(log, environment, 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),
@ -159,7 +156,7 @@ func (c *Cluster) createApp(ctx context.Context, clusterName string) (applicatio
func (c *Cluster) Create(ctx context.Context, vnetResourceGroup, clusterName string, osClusterVersion string) error {
clusterGet, err := c.openshiftclusters.Get(ctx, vnetResourceGroup, clusterName)
if err == nil {
if clusterGet.ProvisioningState == mgmtredhatopenshift20231122.Failed {
if clusterGet.Properties.ProvisioningState == api.ProvisioningStateFailed {
return fmt.Errorf("cluster exists and is in failed provisioning state, please delete and retry")
}
c.log.Print("cluster already exists, skipping create")
@ -378,7 +375,7 @@ func (c *Cluster) Delete(ctx context.Context, vnetResourceGroup, clusterName str
errs = append(errs, err)
}
errs = append(errs,
c.deleteApplication(ctx, *oc.OpenShiftClusterProperties.ServicePrincipalProfile.ClientID),
c.deleteApplication(ctx, oc.Properties.ServicePrincipalProfile.ClientID),
c.deleteCluster(ctx, vnetResourceGroup, clusterName),
c.ensureResourceGroupDeleted(ctx, clusterResourceGroup),
c.deleteResourceGroup(ctx, vnetResourceGroup),
@ -477,19 +474,7 @@ func (c *Cluster) createCluster(ctx context.Context, vnetResourceGroup, clusterN
oc.Properties.WorkerProfiles[0].VMSize = api.VMSizeStandardD2sV3
}
ext := api.APIs[v20231122.APIVersion].OpenShiftClusterConverter.ToExternal(&oc)
data, err := json.Marshal(ext)
if err != nil {
return err
}
ocExt := mgmtredhatopenshift20231122.OpenShiftCluster{}
err = json.Unmarshal(data, &ocExt)
if err != nil {
return err
}
return c.openshiftclusters.CreateOrUpdateAndWait(ctx, vnetResourceGroup, clusterName, ocExt)
return c.openshiftclusters.CreateOrUpdateAndWait(ctx, vnetResourceGroup, clusterName, &oc)
}
var insecureLocalClient *http.Client = &http.Client{
@ -678,7 +663,7 @@ func (c *Cluster) deleteRoleAssignments(ctx context.Context, vnetResourceGroup,
if err != nil {
return fmt.Errorf("error getting cluster document: %w", err)
}
spObjID, err := utilgraph.GetServicePrincipalIDByAppID(ctx, c.spGraphClient, *oc.OpenShiftClusterProperties.ServicePrincipalProfile.ClientID)
spObjID, err := utilgraph.GetServicePrincipalIDByAppID(ctx, c.spGraphClient, oc.Properties.ServicePrincipalProfile.ClientID)
if err != nil {
return fmt.Errorf("error getting service principal for cluster: %w", err)
}