Skip to content

Commit

Permalink
Remove GlobalCredentials usages from pkg/resourcemanager/vnet
Browse files Browse the repository at this point in the history
Credentials are now passed in when constructing a manager.
  • Loading branch information
babbageclunk committed Nov 10, 2020
1 parent 6716545 commit fb4999a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func setup() error {
err = (&VirtualNetworkReconciler{
Reconciler: &AsyncReconciler{
Client: k8sManager.GetClient(),
AzureClient: resourcemanagervnet.NewAzureVNetManager(),
AzureClient: resourcemanagervnet.NewAzureVNetManager(config.GlobalCredentials()),
Telemetry: telemetry.InitializeTelemetryDefault(
"VirtualNetwork",
ctrl.Log.WithName("controllers").WithName("VirtualNetwork"),
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func main() {
// creds for the specific resource being reconciled.
apimManager := resourceapimanagement.NewManager(config.GlobalCredentials())
apimServiceManager := apimservice.NewAzureAPIMgmtServiceManager(config.GlobalCredentials())
vnetManager := vnet.NewAzureVNetManager()
vnetManager := vnet.NewAzureVNetManager(config.GlobalCredentials())
resourceGroupManager := resourcemanagerresourcegroup.NewAzureResourceGroupManager(config.GlobalCredentials())

redisCacheManager := rediscache.NewAzureRedisCacheManager(
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourcemanager/rediscaches/redis/rediscaches.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r *AzureRedisCacheManager) CreateRedisCache(
if len(props.SubnetID) > 0 {
ip := props.StaticIP
if len(props.StaticIP) == 0 {
vnetManager := vnet.NewAzureVNetManager()
vnetManager := vnet.NewAzureVNetManager(r.Creds)
sid := vnet.ParseSubnetID(props.SubnetID)

ip, err = vnetManager.GetAvailableIP(ctx, sid.ResourceGroup, sid.VNet, sid.Subnet)
Expand Down
18 changes: 10 additions & 8 deletions pkg/resourcemanager/vnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ import (
)

// AzureSubnetManager is the struct that the manager functions hang off
type AzureSubnetManager struct{}
type AzureSubnetManager struct {
creds config.Credentials
}

//NewAzureSubnetManager returns a new client for subnets
func NewAzureSubnetManager() *AzureSubnetManager {
return &AzureSubnetManager{}
func NewAzureSubnetManager(creds config.Credentials) *AzureSubnetManager {
return &AzureSubnetManager{creds: creds}
}

// getSubnetClient returns a new instance of an subnet client
func getSubnetClient() (vnetwork.SubnetsClient, error) {
client := vnetwork.NewSubnetsClientWithBaseURI(config.BaseURI(), config.GlobalCredentials().SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer(config.GlobalCredentials())
func getSubnetClient(creds config.Credentials) (vnetwork.SubnetsClient, error) {
client := vnetwork.NewSubnetsClientWithBaseURI(config.BaseURI(), creds.SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer(creds)
if err != nil {
client = vnetwork.SubnetsClient{}
} else {
Expand All @@ -34,8 +36,8 @@ func getSubnetClient() (vnetwork.SubnetsClient, error) {
}

// Get gets a Subnet from Azure
func (v *AzureSubnetManager) Get(ctx context.Context, resourceGroup, vnet, subnet string) (vnetwork.Subnet, error) {
client, err := getSubnetClient()
func (m *AzureSubnetManager) Get(ctx context.Context, resourceGroup, vnet, subnet string) (vnetwork.Subnet, error) {
client, err := getSubnetClient(m.creds)
if err != nil {
return vnetwork.Subnet{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourcemanager/vnet/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var _ = BeforeSuite(func() {
AddressSpace: "10.0.0.0/8",
SubnetName: "test-subnet-" + helpers.RandomString(5),
SubnetAddressPrefix: "10.1.0.0/16",
VirtualNetworkManager: NewAzureVNetManager(),
VirtualNetworkManager: NewAzureVNetManager(config.GlobalCredentials()),
ResourceGroupManager: resourceGroupManager,
timeout: 20 * time.Minute,
retryInterval: 3 * time.Second,
Expand Down
25 changes: 13 additions & 12 deletions pkg/resourcemanager/vnet/vnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import (

// AzureVNetManager is the struct that the manager functions hang off
type AzureVNetManager struct {
Creds config.Credentials
Telemetry telemetry.Telemetry
}

// getVNetClient returns a new instance of an VirtualNetwork client
func getVNetClient() (vnetwork.VirtualNetworksClient, error) {
client := vnetwork.NewVirtualNetworksClientWithBaseURI(config.BaseURI(), config.GlobalCredentials().SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer(config.GlobalCredentials())
func getVNetClient(creds config.Credentials) (vnetwork.VirtualNetworksClient, error) {
client := vnetwork.NewVirtualNetworksClientWithBaseURI(config.BaseURI(), creds.SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer(creds)
if err != nil {
client = vnetwork.VirtualNetworksClient{}
} else {
Expand All @@ -36,8 +37,8 @@ func getVNetClient() (vnetwork.VirtualNetworksClient, error) {
}

// CreateVNet creates VNets
func (*AzureVNetManager) CreateVNet(ctx context.Context, location string, resourceGroupName string, resourceName string, addressSpace string, subnets []azurev1alpha1.VNetSubnets) (vnetwork.VirtualNetwork, error) {
client, err := getVNetClient()
func (m *AzureVNetManager) CreateVNet(ctx context.Context, location string, resourceGroupName string, resourceName string, addressSpace string, subnets []azurev1alpha1.VNetSubnets) (vnetwork.VirtualNetwork, error) {
client, err := getVNetClient(m.Creds)
if err != nil {
return vnetwork.VirtualNetwork{}, err
}
Expand Down Expand Up @@ -86,8 +87,8 @@ func (*AzureVNetManager) CreateVNet(ctx context.Context, location string, resour
}

// DeleteVNet deletes a VNet
func (*AzureVNetManager) DeleteVNet(ctx context.Context, resourceGroupName string, resourceName string) (autorest.Response, error) {
client, err := getVNetClient()
func (m *AzureVNetManager) DeleteVNet(ctx context.Context, resourceGroupName string, resourceName string) (autorest.Response, error) {
client, err := getVNetClient(m.Creds)
if err != nil {
return autorest.Response{}, err
}
Expand All @@ -105,22 +106,22 @@ func (*AzureVNetManager) DeleteVNet(ctx context.Context, resourceGroupName strin
}

// GetVNet gets a VNet
func (v *AzureVNetManager) GetVNet(ctx context.Context, resourceGroupName string, resourceName string) (vNet vnetwork.VirtualNetwork, err error) {
client, err := getVNetClient()
func (m *AzureVNetManager) GetVNet(ctx context.Context, resourceGroupName string, resourceName string) (vNet vnetwork.VirtualNetwork, err error) {
client, err := getVNetClient(m.Creds)
if err != nil {
return vnetwork.VirtualNetwork{}, err
}

return client.Get(ctx, resourceGroupName, resourceName, "")
}

func (v *AzureVNetManager) GetAvailableIP(ctx context.Context, resourceGroup, vnet, subnet string) (string, error) {
client, err := getVNetClient()
func (m *AzureVNetManager) GetAvailableIP(ctx context.Context, resourceGroup, vnet, subnet string) (string, error) {
client, err := getVNetClient(m.Creds)
if err != nil {
return "", err
}

sclient := NewAzureSubnetManager()
sclient := NewAzureSubnetManager(m.Creds)

sub, err := sclient.Get(ctx, resourceGroup, vnet, subnet)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/resourcemanager/vnet/vnet_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
vnetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/go-autorest/autorest"
)

// NewAzureVNetManager creates a new instance of AzureVNetManager
func NewAzureVNetManager() *AzureVNetManager {
return &AzureVNetManager{}
func NewAzureVNetManager(creds config.Credentials) *AzureVNetManager {
return &AzureVNetManager{Creds: creds}
}

// VNetManager manages VNet service components
Expand Down

0 comments on commit fb4999a

Please sign in to comment.