Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use track2 SDK in updateAPIIPEarly #3638

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func New(ctx context.Context, log *logrus.Entry, _env env.Interface, db database
return nil, err
}

fpCredential, err := _env.FPNewClientCertificateCredential(_env.TenantID())
fpCredClusterTenant, err := _env.FPNewClientCertificateCredential(subscriptionDoc.Subscription.Properties.TenantID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -170,17 +170,17 @@ func New(ctx context.Context, log *logrus.Entry, _env env.Interface, db database
},
}

armLoadBalancersClient, err := armnetwork.NewLoadBalancersClient(r.SubscriptionID, fpCredential, &clientOptions)
armLoadBalancersClient, err := armnetwork.NewLoadBalancersClient(r.SubscriptionID, fpCredClusterTenant, &clientOptions)
if err != nil {
return nil, err
}

armInterfacesClient, err := armnetwork.NewInterfacesClient(r.SubscriptionID, fpCredential, &clientOptions)
armInterfacesClient, err := armnetwork.NewInterfacesClient(r.SubscriptionID, fpCredClusterTenant, &clientOptions)
if err != nil {
return nil, err
}

armPublicIPAddressesClient, err := armnetwork.NewPublicIPAddressesClient(r.SubscriptionID, fpCredential, &clientOptions)
armPublicIPAddressesClient, err := armnetwork.NewPublicIPAddressesClient(r.SubscriptionID, fpCredClusterTenant, &clientOptions)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/cluster/ipaddresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,27 @@ func (m *manager) populateDatabaseIntIP(ctx context.Context) error {
return err
}

// this function can only be called on create - not on update - because it
// updateAPIIPEarly updates the `doc` with the public and private IP of the API server,
// and updates the DNS record of the API server according to the API server visibility.
// This function can only be called on create - not on update - because it
// refers to -pip-v4, which doesn't exist on pre-DNS change clusters.
func (m *manager) updateAPIIPEarly(ctx context.Context) error {
infraID := m.doc.OpenShiftCluster.Properties.InfraID
resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/')

lb, err := m.loadBalancers.Get(ctx, resourceGroup, infraID+"-internal", "")
lb, err := m.armLoadBalancers.Get(ctx, resourceGroup, infraID+"-internal", nil)
if err != nil {
return err
}
intIPAddress := *((*lb.FrontendIPConfigurations)[0].PrivateIPAddress)
intIPAddress := *lb.Properties.FrontendIPConfigurations[0].Properties.PrivateIPAddress

ipAddress := intIPAddress
if m.doc.OpenShiftCluster.Properties.APIServerProfile.Visibility == api.VisibilityPublic {
ip, err := m.publicIPAddresses.Get(ctx, resourceGroup, infraID+"-pip-v4", "")
ip, err := m.armPublicIPAddresses.Get(ctx, resourceGroup, infraID+"-pip-v4", nil)
if err != nil {
return err
}
ipAddress = *ip.IPAddress
ipAddress = *ip.Properties.IPAddress
}

err = m.dns.Update(ctx, m.doc.OpenShiftCluster, ipAddress)
Expand Down
115 changes: 64 additions & 51 deletions pkg/cluster/ipaddresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"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"
"github.com/golang/mock/gomock"
Expand All @@ -18,6 +19,7 @@ import (

"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
mock_armnetwork "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/azuresdk/armnetwork"
mock_network "github.com/Azure/ARO-RP/pkg/util/mocks/azureclient/mgmt/network"
mock_dns "github.com/Azure/ARO-RP/pkg/util/mocks/dns"
mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env"
Expand All @@ -26,6 +28,11 @@ import (
utilerror "github.com/Azure/ARO-RP/test/util/error"
)

const (
privateIP = "10.0.0.1"
publicIP = "1.2.3.4"
)

func TestCreateOrUpdateRouterIPFromCluster(t *testing.T) {
ctx := context.Background()

Expand Down Expand Up @@ -61,7 +68,7 @@ func TestCreateOrUpdateRouterIPFromCluster(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = "1.2.3.4"
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = publicIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(dns *mock_dns.MockManager) {
Expand All @@ -77,7 +84,7 @@ func TestCreateOrUpdateRouterIPFromCluster(t *testing.T) {
Status: corev1.ServiceStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{{
IP: "1.2.3.4",
IP: publicIP,
}},
},
},
Expand Down Expand Up @@ -217,15 +224,15 @@ func TestCreateOrUpdateRouterIPEarly(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = "1.2.3.4"
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = publicIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(publicIPAddresses *mock_network.MockPublicIPAddressesClient, dns *mock_dns.MockManager, subnet *mock_subnet.MockManager) {
publicIPAddresses.EXPECT().
Get(gomock.Any(), "clusterResourceGroup", "infra-default-v4", "").
Return(mgmtnetwork.PublicIPAddress{
PublicIPAddressPropertiesFormat: &mgmtnetwork.PublicIPAddressPropertiesFormat{
IPAddress: to.StringPtr("1.2.3.4"),
IPAddress: to.StringPtr(publicIP),
},
}, nil)
dns.EXPECT().
Expand Down Expand Up @@ -262,13 +269,13 @@ func TestCreateOrUpdateRouterIPEarly(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = "1.2.3.4"
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = publicIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(publicIPAddresses *mock_network.MockPublicIPAddressesClient, dns *mock_dns.MockManager, subnet *mock_subnet.MockManager) {
subnet.EXPECT().
GetHighestFreeIP(gomock.Any(), "subnetid").
Return("1.2.3.4", nil)
Return(publicIP, nil)
dns.EXPECT().
CreateOrUpdateRouter(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)
Expand Down Expand Up @@ -308,13 +315,13 @@ func TestCreateOrUpdateRouterIPEarly(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = "1.2.3.4"
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = publicIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(publicIPAddresses *mock_network.MockPublicIPAddressesClient, dns *mock_dns.MockManager, subnet *mock_subnet.MockManager) {
subnet.EXPECT().
GetHighestFreeIP(gomock.Any(), "enricheWPsubnetid").
Return("1.2.3.4", nil)
Return(publicIP, nil)
dns.EXPECT().
CreateOrUpdateRouter(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil)
Expand Down Expand Up @@ -402,7 +409,7 @@ func TestPopulateDatabaseIntIP(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = "10.0.0.1"
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = privateIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(loadBalancers *mock_network.MockLoadBalancersClient) {
Expand All @@ -413,7 +420,7 @@ func TestPopulateDatabaseIntIP(t *testing.T) {
FrontendIPConfigurations: &[]mgmtnetwork.FrontendIPConfiguration{
{
FrontendIPConfigurationPropertiesFormat: &mgmtnetwork.FrontendIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr("10.0.0.1"),
PrivateIPAddress: to.StringPtr(privateIP),
},
},
},
Expand Down Expand Up @@ -441,7 +448,7 @@ func TestPopulateDatabaseIntIP(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = "10.0.0.1"
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = privateIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(loadBalancers *mock_network.MockLoadBalancersClient) {
Expand All @@ -452,7 +459,7 @@ func TestPopulateDatabaseIntIP(t *testing.T) {
FrontendIPConfigurations: &[]mgmtnetwork.FrontendIPConfiguration{
{
FrontendIPConfigurationPropertiesFormat: &mgmtnetwork.FrontendIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr("10.0.0.1"),
PrivateIPAddress: to.StringPtr(privateIP),
},
},
},
Expand All @@ -472,7 +479,7 @@ func TestPopulateDatabaseIntIP(t *testing.T) {
ResourceGroupID: resourceGroupID,
},
APIServerProfile: api.APIServerProfile{
IntIP: "10.0.0.1",
IntIP: privateIP,
},
ProvisioningState: api.ProvisioningStateCreating,
InfraID: "infra",
Expand Down Expand Up @@ -540,7 +547,7 @@ func TestUpdateAPIIPEarly(t *testing.T) {
for _, tt := range []struct {
name string
fixtureChecker func(*testdatabase.Fixture, *testdatabase.Checker, *cosmosdb.FakeOpenShiftClusterDocumentClient)
mocks func(*mock_network.MockLoadBalancersClient, *mock_network.MockPublicIPAddressesClient, *mock_dns.MockManager)
mocks func(*mock_armnetwork.MockLoadBalancersClient, *mock_armnetwork.MockPublicIPAddressesClient, *mock_dns.MockManager)
wantErr string
}{
{
Expand All @@ -565,33 +572,37 @@ func TestUpdateAPIIPEarly(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.APIServerProfile.IP = "1.2.3.4"
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = "10.0.0.1"
doc.OpenShiftCluster.Properties.APIServerProfile.IP = publicIP
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = privateIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(loadBalancers *mock_network.MockLoadBalancersClient, publicIPAddresses *mock_network.MockPublicIPAddressesClient, dns *mock_dns.MockManager) {
mocks: func(loadBalancers *mock_armnetwork.MockLoadBalancersClient, publicIPAddresses *mock_armnetwork.MockPublicIPAddressesClient, dns *mock_dns.MockManager) {
loadBalancers.EXPECT().
Get(gomock.Any(), "clusterResourceGroup", "infra-internal", "").
Return(mgmtnetwork.LoadBalancer{
LoadBalancerPropertiesFormat: &mgmtnetwork.LoadBalancerPropertiesFormat{
FrontendIPConfigurations: &[]mgmtnetwork.FrontendIPConfiguration{
{
FrontendIPConfigurationPropertiesFormat: &mgmtnetwork.FrontendIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr("10.0.0.1"),
Get(gomock.Any(), "clusterResourceGroup", "infra-internal", nil).
Return(armnetwork.LoadBalancersClientGetResponse{
LoadBalancer: armnetwork.LoadBalancer{
Properties: &armnetwork.LoadBalancerPropertiesFormat{
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{
{
Properties: &armnetwork.FrontendIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr(privateIP),
},
},
},
},
},
}, nil)
publicIPAddresses.EXPECT().
Get(gomock.Any(), "clusterResourceGroup", "infra-pip-v4", "").
Return(mgmtnetwork.PublicIPAddress{
PublicIPAddressPropertiesFormat: &mgmtnetwork.PublicIPAddressPropertiesFormat{
IPAddress: to.StringPtr("1.2.3.4"),
Get(gomock.Any(), "clusterResourceGroup", "infra-pip-v4", nil).
Return(armnetwork.PublicIPAddressesClientGetResponse{
PublicIPAddress: armnetwork.PublicIPAddress{
Properties: &armnetwork.PublicIPAddressPropertiesFormat{
IPAddress: to.StringPtr(publicIP),
},
},
}, nil)
dns.EXPECT().
Update(gomock.Any(), gomock.Any(), gomock.Any()).
Update(gomock.Any(), gomock.Any(), publicIP).
Return(nil)
},
},
Expand All @@ -617,26 +628,28 @@ func TestUpdateAPIIPEarly(t *testing.T) {
fixture.AddOpenShiftClusterDocuments(doc)

doc.Dequeues = 1
doc.OpenShiftCluster.Properties.APIServerProfile.IP = "10.0.0.1"
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = "10.0.0.1"
doc.OpenShiftCluster.Properties.APIServerProfile.IP = privateIP
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = privateIP
checker.AddOpenShiftClusterDocuments(doc)
},
mocks: func(loadBalancers *mock_network.MockLoadBalancersClient, publicIPAddresses *mock_network.MockPublicIPAddressesClient, dns *mock_dns.MockManager) {
mocks: func(loadBalancers *mock_armnetwork.MockLoadBalancersClient, publicIPAddresses *mock_armnetwork.MockPublicIPAddressesClient, dns *mock_dns.MockManager) {
loadBalancers.EXPECT().
Get(gomock.Any(), "clusterResourceGroup", "infra-internal", "").
Return(mgmtnetwork.LoadBalancer{
LoadBalancerPropertiesFormat: &mgmtnetwork.LoadBalancerPropertiesFormat{
FrontendIPConfigurations: &[]mgmtnetwork.FrontendIPConfiguration{
{
FrontendIPConfigurationPropertiesFormat: &mgmtnetwork.FrontendIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr("10.0.0.1"),
Get(gomock.Any(), "clusterResourceGroup", "infra-internal", nil).
Return(armnetwork.LoadBalancersClientGetResponse{
LoadBalancer: armnetwork.LoadBalancer{
Properties: &armnetwork.LoadBalancerPropertiesFormat{
FrontendIPConfigurations: []*armnetwork.FrontendIPConfiguration{
{
Properties: &armnetwork.FrontendIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr(privateIP),
},
},
},
},
},
}, nil)
dns.EXPECT().
Update(gomock.Any(), gomock.Any(), gomock.Any()).
Update(gomock.Any(), gomock.Any(), privateIP).
Return(nil)
},
},
Expand All @@ -645,8 +658,8 @@ func TestUpdateAPIIPEarly(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

loadBalancers := mock_network.NewMockLoadBalancersClient(controller)
publicIPAddresses := mock_network.NewMockPublicIPAddressesClient(controller)
loadBalancers := mock_armnetwork.NewMockLoadBalancersClient(controller)
publicIPAddresses := mock_armnetwork.NewMockPublicIPAddressesClient(controller)
dns := mock_dns.NewMockManager(controller)
if tt.mocks != nil {
tt.mocks(loadBalancers, publicIPAddresses, dns)
Expand All @@ -671,11 +684,11 @@ func TestUpdateAPIIPEarly(t *testing.T) {
}

m := &manager{
doc: doc,
db: dbOpenShiftClusters,
publicIPAddresses: publicIPAddresses,
loadBalancers: loadBalancers,
dns: dns,
doc: doc,
db: dbOpenShiftClusters,
armPublicIPAddresses: publicIPAddresses,
armLoadBalancers: loadBalancers,
dns: dns,
}

err = m.updateAPIIPEarly(ctx)
Expand Down Expand Up @@ -706,7 +719,7 @@ func TestEnsureGatewayCreate(t *testing.T) {
},
{
name: "noop: IP set",
gatewayPrivateEndpointIP: "1.2.3.4",
gatewayPrivateEndpointIP: privateIP,
},
{
name: "error: private endpoint connection not found",
Expand All @@ -720,7 +733,7 @@ func TestEnsureGatewayCreate(t *testing.T) {
IPConfigurations: &[]mgmtnetwork.InterfaceIPConfiguration{
{
InterfaceIPConfigurationPropertiesFormat: &mgmtnetwork.InterfaceIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr("1.2.3.4"),
PrivateIPAddress: to.StringPtr(privateIP),
},
},
},
Expand Down Expand Up @@ -751,7 +764,7 @@ func TestEnsureGatewayCreate(t *testing.T) {
IPConfigurations: &[]mgmtnetwork.InterfaceIPConfiguration{
{
InterfaceIPConfigurationPropertiesFormat: &mgmtnetwork.InterfaceIPConfigurationPropertiesFormat{
PrivateIPAddress: to.StringPtr("1.2.3.4"),
PrivateIPAddress: to.StringPtr(privateIP),
},
},
},
Expand Down Expand Up @@ -815,7 +828,7 @@ func TestEnsureGatewayCreate(t *testing.T) {
ID: resourceID,
Properties: api.OpenShiftClusterProperties{
NetworkProfile: api.NetworkProfile{
GatewayPrivateEndpointIP: "1.2.3.4",
GatewayPrivateEndpointIP: privateIP,
GatewayPrivateLinkID: "1234",
},
},
Expand Down
Loading