From babb4198a66d4fe5361d641dec8199a3578e4778 Mon Sep 17 00:00:00 2001 From: Ayato Tokubi Date: Tue, 28 May 2024 16:14:50 +0100 Subject: [PATCH] use track2 sdk in ensureGatewayCreate --- pkg/cluster/ipaddresses.go | 22 ++++--- pkg/cluster/ipaddresses_test.go | 112 +++++++++++++++++--------------- 2 files changed, 72 insertions(+), 62 deletions(-) diff --git a/pkg/cluster/ipaddresses.go b/pkg/cluster/ipaddresses.go index 95f05559e33..57f4c768eff 100644 --- a/pkg/cluster/ipaddresses.go +++ b/pkg/cluster/ipaddresses.go @@ -10,9 +10,11 @@ import ( "net/http" "strings" + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v2" mgmtnetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-08-01/network" "github.com/Azure/go-autorest/autorest/to" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" "github.com/Azure/ARO-RP/pkg/api" "github.com/Azure/ARO-RP/pkg/database/cosmosdb" @@ -212,12 +214,12 @@ func (m *manager) ensureGatewayCreate(ctx context.Context) error { resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/') - pe, err := m.privateEndpoints.Get(ctx, resourceGroup, infraID+"-pe", "networkInterfaces") + pe, err := m.armPrivateEndpoints.Get(ctx, resourceGroup, infraID+"-pe", &armnetwork.PrivateEndpointsClientGetOptions{Expand: ptr.To("networkInterfaces")}) if err != nil { return err } - pls, err := m.rpPrivateLinkServices.Get(ctx, m.env.GatewayResourceGroup(), "gateway-pls-001", "") + pls, err := m.armRPPrivateLinkServices.Get(ctx, m.env.GatewayResourceGroup(), "gateway-pls-001", nil) if err != nil { return err } @@ -227,18 +229,18 @@ func (m *manager) ensureGatewayCreate(ctx context.Context) error { // call to the resource graph service, but it's not worth the effort to do // that here. var linkIdentifier string - for _, conn := range *pls.PrivateEndpointConnections { - if !strings.EqualFold(*conn.PrivateEndpoint.ID, *pe.ID) { + for _, conn := range pls.Properties.PrivateEndpointConnections { + if !strings.EqualFold(*conn.Properties.PrivateEndpoint.ID, *pe.ID) { continue } - linkIdentifier = *conn.LinkIdentifier + linkIdentifier = *conn.Properties.LinkIdentifier - if !strings.EqualFold(*conn.PrivateLinkServiceConnectionState.Status, "Approved") { - conn.PrivateLinkServiceConnectionState.Status = to.StringPtr("Approved") - conn.PrivateLinkServiceConnectionState.Description = to.StringPtr("Approved") + if !strings.EqualFold(*conn.Properties.PrivateLinkServiceConnectionState.Status, "Approved") { + conn.Properties.PrivateLinkServiceConnectionState.Status = to.StringPtr("Approved") + conn.Properties.PrivateLinkServiceConnectionState.Description = to.StringPtr("Approved") - _, err = m.rpPrivateLinkServices.UpdatePrivateEndpointConnection(ctx, m.env.GatewayResourceGroup(), "gateway-pls-001", *conn.Name, conn) + _, err = m.armRPPrivateLinkServices.UpdatePrivateEndpointConnection(ctx, m.env.GatewayResourceGroup(), "gateway-pls-001", *conn.Name, *conn, nil) if err != nil { return err } @@ -280,7 +282,7 @@ func (m *manager) ensureGatewayCreate(ctx context.Context) error { } m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error { - doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateEndpointIP = *(*(*pe.PrivateEndpointProperties.NetworkInterfaces)[0].IPConfigurations)[0].PrivateIPAddress + doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateEndpointIP = *pe.Properties.NetworkInterfaces[0].Properties.IPConfigurations[0].Properties.PrivateIPAddress doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateLinkID = linkIdentifier return nil }) diff --git a/pkg/cluster/ipaddresses_test.go b/pkg/cluster/ipaddresses_test.go index eac3d534e13..4a481ba9fc1 100644 --- a/pkg/cluster/ipaddresses_test.go +++ b/pkg/cluster/ipaddresses_test.go @@ -707,7 +707,7 @@ func TestEnsureGatewayCreate(t *testing.T) { for _, tt := range []struct { name string - mocks func(*mock_env.MockInterface, *mock_network.MockPrivateEndpointsClient, *mock_network.MockPrivateLinkServicesClient) + mocks func(*mock_env.MockInterface, *mock_armnetwork.MockPrivateEndpointsClient, *mock_armnetwork.MockPrivateLinkServicesClient) fixture func(*testdatabase.Fixture) checker func(*testdatabase.Checker) gatewayEnabled bool @@ -723,29 +723,33 @@ func TestEnsureGatewayCreate(t *testing.T) { }, { name: "error: private endpoint connection not found", - mocks: func(env *mock_env.MockInterface, privateEndpoints *mock_network.MockPrivateEndpointsClient, rpPrivateLinkServices *mock_network.MockPrivateLinkServicesClient) { + mocks: func(env *mock_env.MockInterface, privateEndpoints *mock_armnetwork.MockPrivateEndpointsClient, rpPrivateLinkServices *mock_armnetwork.MockPrivateLinkServicesClient) { env.EXPECT().GatewayResourceGroup().AnyTimes().Return("gatewayResourceGroup") - privateEndpoints.EXPECT().Get(ctx, "clusterResourceGroup", "infra-pe", "networkInterfaces").Return(mgmtnetwork.PrivateEndpoint{ - PrivateEndpointProperties: &mgmtnetwork.PrivateEndpointProperties{ - NetworkInterfaces: &[]mgmtnetwork.Interface{ - { - InterfacePropertiesFormat: &mgmtnetwork.InterfacePropertiesFormat{ - IPConfigurations: &[]mgmtnetwork.InterfaceIPConfiguration{ - { - InterfaceIPConfigurationPropertiesFormat: &mgmtnetwork.InterfaceIPConfigurationPropertiesFormat{ - PrivateIPAddress: to.StringPtr(privateIP), + privateEndpoints.EXPECT().Get(ctx, "clusterResourceGroup", "infra-pe", &armnetwork.PrivateEndpointsClientGetOptions{Expand: to.StringPtr("networkInterfaces")}).Return(armnetwork.PrivateEndpointsClientGetResponse{ + PrivateEndpoint: armnetwork.PrivateEndpoint{ + Properties: &armnetwork.PrivateEndpointProperties{ + NetworkInterfaces: []*armnetwork.Interface{ + { + Properties: &armnetwork.InterfacePropertiesFormat{ + IPConfigurations: []*armnetwork.InterfaceIPConfiguration{ + { + Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{ + PrivateIPAddress: to.StringPtr(privateIP), + }, }, }, }, }, }, }, + ID: to.StringPtr("peID"), }, - ID: to.StringPtr("peID"), }, nil) - rpPrivateLinkServices.EXPECT().Get(ctx, "gatewayResourceGroup", "gateway-pls-001", "").Return(mgmtnetwork.PrivateLinkService{ - PrivateLinkServiceProperties: &mgmtnetwork.PrivateLinkServiceProperties{ - PrivateEndpointConnections: &[]mgmtnetwork.PrivateEndpointConnection{}, + rpPrivateLinkServices.EXPECT().Get(ctx, "gatewayResourceGroup", "gateway-pls-001", nil).Return(armnetwork.PrivateLinkServicesClientGetResponse{ + PrivateLinkService: armnetwork.PrivateLinkService{ + Properties: &armnetwork.PrivateLinkServiceProperties{ + PrivateEndpointConnections: []*armnetwork.PrivateEndpointConnection{}, + }, }, }, nil) }, @@ -754,64 +758,68 @@ func TestEnsureGatewayCreate(t *testing.T) { }, { name: "ok", - mocks: func(env *mock_env.MockInterface, privateEndpoints *mock_network.MockPrivateEndpointsClient, rpPrivateLinkServices *mock_network.MockPrivateLinkServicesClient) { + mocks: func(env *mock_env.MockInterface, privateEndpoints *mock_armnetwork.MockPrivateEndpointsClient, rpPrivateLinkServices *mock_armnetwork.MockPrivateLinkServicesClient) { env.EXPECT().GatewayResourceGroup().AnyTimes().Return("gatewayResourceGroup") - privateEndpoints.EXPECT().Get(ctx, "clusterResourceGroup", "infra-pe", "networkInterfaces").Return(mgmtnetwork.PrivateEndpoint{ - PrivateEndpointProperties: &mgmtnetwork.PrivateEndpointProperties{ - NetworkInterfaces: &[]mgmtnetwork.Interface{ - { - InterfacePropertiesFormat: &mgmtnetwork.InterfacePropertiesFormat{ - IPConfigurations: &[]mgmtnetwork.InterfaceIPConfiguration{ - { - InterfaceIPConfigurationPropertiesFormat: &mgmtnetwork.InterfaceIPConfigurationPropertiesFormat{ - PrivateIPAddress: to.StringPtr(privateIP), + privateEndpoints.EXPECT().Get(ctx, "clusterResourceGroup", "infra-pe", &armnetwork.PrivateEndpointsClientGetOptions{Expand: to.StringPtr("networkInterfaces")}).Return(armnetwork.PrivateEndpointsClientGetResponse{ + PrivateEndpoint: armnetwork.PrivateEndpoint{ + Properties: &armnetwork.PrivateEndpointProperties{ + NetworkInterfaces: []*armnetwork.Interface{ + { + Properties: &armnetwork.InterfacePropertiesFormat{ + IPConfigurations: []*armnetwork.InterfaceIPConfiguration{ + { + Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{ + PrivateIPAddress: to.StringPtr(privateIP), + }, }, }, }, }, }, }, + ID: to.StringPtr("peID"), }, - ID: to.StringPtr("peID"), }, nil) - rpPrivateLinkServices.EXPECT().Get(ctx, "gatewayResourceGroup", "gateway-pls-001", "").Return(mgmtnetwork.PrivateLinkService{ - PrivateLinkServiceProperties: &mgmtnetwork.PrivateLinkServiceProperties{ - PrivateEndpointConnections: &[]mgmtnetwork.PrivateEndpointConnection{ - { - PrivateEndpointConnectionProperties: &mgmtnetwork.PrivateEndpointConnectionProperties{ - PrivateEndpoint: &mgmtnetwork.PrivateEndpoint{ - ID: to.StringPtr("otherPeID"), + rpPrivateLinkServices.EXPECT().Get(ctx, "gatewayResourceGroup", "gateway-pls-001", nil).Return(armnetwork.PrivateLinkServicesClientGetResponse{ + PrivateLinkService: armnetwork.PrivateLinkService{ + Properties: &armnetwork.PrivateLinkServiceProperties{ + PrivateEndpointConnections: []*armnetwork.PrivateEndpointConnection{ + { + Properties: &armnetwork.PrivateEndpointConnectionProperties{ + PrivateEndpoint: &armnetwork.PrivateEndpoint{ + ID: to.StringPtr("otherPeID"), + }, }, }, - }, - { - PrivateEndpointConnectionProperties: &mgmtnetwork.PrivateEndpointConnectionProperties{ - PrivateEndpoint: &mgmtnetwork.PrivateEndpoint{ - ID: to.StringPtr("peID"), - }, - PrivateLinkServiceConnectionState: &mgmtnetwork.PrivateLinkServiceConnectionState{ - Status: to.StringPtr(""), + { + Properties: &armnetwork.PrivateEndpointConnectionProperties{ + PrivateEndpoint: &armnetwork.PrivateEndpoint{ + ID: to.StringPtr("peID"), + }, + PrivateLinkServiceConnectionState: &armnetwork.PrivateLinkServiceConnectionState{ + Status: to.StringPtr(""), + }, + LinkIdentifier: to.StringPtr("1234"), }, - LinkIdentifier: to.StringPtr("1234"), + Name: to.StringPtr("conn"), }, - Name: to.StringPtr("conn"), }, }, }, }, nil) - rpPrivateLinkServices.EXPECT().UpdatePrivateEndpointConnection(ctx, "gatewayResourceGroup", "gateway-pls-001", "conn", mgmtnetwork.PrivateEndpointConnection{ - PrivateEndpointConnectionProperties: &mgmtnetwork.PrivateEndpointConnectionProperties{ - PrivateEndpoint: &mgmtnetwork.PrivateEndpoint{ + rpPrivateLinkServices.EXPECT().UpdatePrivateEndpointConnection(ctx, "gatewayResourceGroup", "gateway-pls-001", "conn", armnetwork.PrivateEndpointConnection{ + Properties: &armnetwork.PrivateEndpointConnectionProperties{ + PrivateEndpoint: &armnetwork.PrivateEndpoint{ ID: to.StringPtr("peID"), }, - PrivateLinkServiceConnectionState: &mgmtnetwork.PrivateLinkServiceConnectionState{ + PrivateLinkServiceConnectionState: &armnetwork.PrivateLinkServiceConnectionState{ Status: to.StringPtr("Approved"), Description: to.StringPtr("Approved"), }, LinkIdentifier: to.StringPtr("1234"), }, Name: to.StringPtr("conn"), - }).Return(mgmtnetwork.PrivateEndpointConnection{}, nil) + }, nil).Return(armnetwork.PrivateLinkServicesClientUpdatePrivateEndpointConnectionResponse{}, nil) }, fixture: func(f *testdatabase.Fixture) { f.AddOpenShiftClusterDocuments(&api.OpenShiftClusterDocument{ @@ -851,8 +859,8 @@ func TestEnsureGatewayCreate(t *testing.T) { defer controller.Finish() env := mock_env.NewMockInterface(controller) - privateEndpoints := mock_network.NewMockPrivateEndpointsClient(controller) - rpPrivateLinkServices := mock_network.NewMockPrivateLinkServicesClient(controller) + privateEndpoints := mock_armnetwork.NewMockPrivateEndpointsClient(controller) + rpPrivateLinkServices := mock_armnetwork.NewMockPrivateLinkServicesClient(controller) dbOpenShiftClusters, clientOpenShiftClusters := testdatabase.NewFakeOpenShiftClusters() dbGateway, clientGateway := testdatabase.NewFakeGateway() @@ -893,8 +901,8 @@ func TestEnsureGatewayCreate(t *testing.T) { }, }, }, - privateEndpoints: privateEndpoints, - rpPrivateLinkServices: rpPrivateLinkServices, + armPrivateEndpoints: privateEndpoints, + armRPPrivateLinkServices: rpPrivateLinkServices, } err = m.ensureGatewayCreate(ctx)