Skip to content

Commit

Permalink
💎 Refactor public IP service to get Spec from scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecile Robert-Michon committed Jun 26, 2020
1 parent ac7fd15 commit a89eb3c
Show file tree
Hide file tree
Showing 63 changed files with 697 additions and 691 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ KUSTOMIZE_VER := v3.5.4
KUSTOMIZE_BIN := kustomize
KUSTOMIZE := $(TOOLS_BIN_DIR)/$(KUSTOMIZE_BIN)-$(KUSTOMIZE_VER)

# Using unreleased version until https://github.com/golang/mock/pull/405 is part of a release.
MOCKGEN_VER := 8a3d5958550701de9e6650b84b75a118771e7b49
MOCKGEN_BIN := mockgen
MOCKGEN := $(TOOLS_BIN_DIR)/$(MOCKGEN_BIN)-$(MOCKGEN_VER)
Expand Down
26 changes: 26 additions & 0 deletions cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ package azure

import (
"context"
"github.com/Azure/go-autorest/autorest"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
)

// Service is a generic interface used by components offering a type of service.
// Example: virtualnetworks service would offer Reconcile/Delete methods.
type Service interface {
Reconcile(ctx context.Context) error
Delete(ctx context.Context) error
}

// OldService is a generic interface for services that have not yet been refactored.
// Once all services have been converted to use Service, this should be removed.
// Example: virtualnetworks service would offer Reconcile/Delete methods.
type OldService interface {
Reconcile(ctx context.Context, spec interface{}) error
Delete(ctx context.Context, spec interface{}) error
}
Expand All @@ -40,3 +50,19 @@ type CredentialGetter interface {
Service
GetCredentials(ctx context.Context, group string, cluster string) ([]byte, error)
}

// Authorizer is an interface which can get the subscription ID, base URI, and authorizer for an Azure service.
type Authorizer interface {
SubscriptionID() string
BaseURI() string
Authorizer() autorest.Authorizer
}

// ClusterDescriber is an interface which can get common Azure Cluster information
type ClusterDescriber interface {
Authorizer
ResourceGroup() string
ClusterName() string
Location() string
AdditionalTags() infrav1.Tags
}
37 changes: 36 additions & 1 deletion cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package scope
import (
"context"
"fmt"

"github.com/Azure/go-autorest/autorest"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"k8s.io/klog/klogr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -83,11 +84,39 @@ type ClusterScope struct {
AzureCluster *infrav1.AzureCluster
}

// SubscriptionID returns the Azure client Subscription ID.
func (s *ClusterScope) SubscriptionID() string {
return s.AzureClients.SubscriptionID
}

// BaseURI returns the Azure ResourceManagerEndpoint.
func (s *ClusterScope) BaseURI() string {
return s.ResourceManagerEndpoint
}

// Authorizer returns the Azure client Authorizer.
func (s *ClusterScope) Authorizer() autorest.Authorizer {
return s.AzureClients.Authorizer
}

// Network returns the cluster network object.
func (s *ClusterScope) Network() *infrav1.Network {
return &s.AzureCluster.Status.Network
}

// PublicIPSpec returns the public IP specs.
func (s *ClusterScope) PublicIPSpecs() []azure.PublicIPSpec {
return []azure.PublicIPSpec{
{
Name: azure.GenerateNodeOutboundIPName(s.ClusterName()),
},
{
Name: s.Network().APIServerIP.Name,
DNSName: s.Network().APIServerIP.DNSName,
},
}
}

// Vnet returns the cluster Vnet.
func (s *ClusterScope) Vnet() *infrav1.VnetSpec {
return &s.AzureCluster.Spec.NetworkSpec.Vnet
Expand All @@ -113,7 +142,13 @@ func (s *ClusterScope) ResourceGroup() string {
return s.AzureCluster.Spec.ResourceGroup
}

// ClusterName returns the cluster name.
func (s *ClusterScope) ClusterName() string {
return s.Cluster.Name
}

// Name returns the cluster name.
// DEPRECATED: use ClusterName() instead
func (s *ClusterScope) Name() string {
return s.Cluster.Name
}
Expand Down
42 changes: 0 additions & 42 deletions cloud/scope/getters.go

This file was deleted.

62 changes: 45 additions & 17 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package scope
import (
"context"
"encoding/base64"

"github.com/Azure/go-autorest/autorest"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/klogr"
"k8s.io/utils/pointer"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/controllers/noderefutil"
capierrors "sigs.k8s.io/cluster-api/errors"
Expand All @@ -40,9 +41,8 @@ type MachineScopeParams struct {
AzureClients
Client client.Client
Logger logr.Logger
Cluster *clusterv1.Cluster
ClusterScope *ClusterScope
Machine *clusterv1.Machine
AzureCluster *infrav1.AzureCluster
AzureMachine *infrav1.AzureMachine
}

Expand All @@ -55,16 +55,9 @@ func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
if params.Machine == nil {
return nil, errors.New("machine is required when creating a MachineScope")
}
if params.Cluster == nil {
return nil, errors.New("cluster is required when creating a MachineScope")
}
if params.AzureCluster == nil {
return nil, errors.New("azure cluster is required when creating a MachineScope")
}
if params.AzureMachine == nil {
return nil, errors.New("azure machine is required when creating a MachineScope")
}

if params.Logger == nil {
params.Logger = klogr.New()
}
Expand All @@ -75,12 +68,11 @@ func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
}
return &MachineScope{
client: params.Client,
Cluster: params.Cluster,
Machine: params.Machine,
AzureCluster: params.AzureCluster,
AzureMachine: params.AzureMachine,
Logger: params.Logger,
patchHelper: helper,
ClusterScope: params.ClusterScope,
}, nil
}

Expand All @@ -90,15 +82,51 @@ type MachineScope struct {
client client.Client
patchHelper *patch.Helper

Cluster *clusterv1.Cluster
ClusterScope azure.ClusterDescriber
Machine *clusterv1.Machine
AzureCluster *infrav1.AzureCluster
AzureMachine *infrav1.AzureMachine
}

// Location returns the AzureMachine location.
// PublicIPSpec returns the public IP specs.
func (m *MachineScope) PublicIPSpecs() []azure.PublicIPSpec {
var spec []azure.PublicIPSpec
if m.AzureMachine.Spec.AllocatePublicIP == true {
nicName := azure.GenerateNICName(m.Name())
spec = append(spec, azure.PublicIPSpec{
Name: azure.GenerateNodePublicIPName(nicName),
})
}
return spec
}

// Location returns the AzureCluster location.
func (m *MachineScope) Location() string {
return m.AzureCluster.Spec.Location
return m.ClusterScope.Location()
}

// ResourceGroup returns the AzureCluster resource group.
func (m *MachineScope) ResourceGroup() string {
return m.ClusterScope.ResourceGroup()
}

// ClusterName returns the AzureCluster name.
func (m *MachineScope) ClusterName() string {
return m.ClusterScope.ClusterName()
}

// SubscriptionID returns the Azure client Subscription ID.
func (m *MachineScope) SubscriptionID() string {
return m.ClusterScope.SubscriptionID()
}

// BaseURI returns the Azure ResourceManagerEndpoint.
func (m *MachineScope) BaseURI() string {
return m.ClusterScope.BaseURI()
}

// Authorizer returns the Azure client Authorizer.
func (m *MachineScope) Authorizer() autorest.Authorizer {
return m.ClusterScope.Authorizer()
}

// AvailabilityZone returns the AzureMachine Availability Zone.
Expand Down Expand Up @@ -225,7 +253,7 @@ func (m *MachineScope) AdditionalTags() infrav1.Tags {
tags := make(infrav1.Tags)

// Start with the cluster-wide tags...
tags.Merge(m.AzureCluster.Spec.AdditionalTags)
tags.Merge(m.ClusterScope.AdditionalTags())
// ... and merge in the Machine's
tags.Merge(m.AzureMachine.Spec.AdditionalTags)

Expand Down
Loading

0 comments on commit a89eb3c

Please sign in to comment.