Skip to content

Commit

Permalink
✨ integrate AzureMachine with AzureManagedControlPlane
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeldeib committed Jul 23, 2020
1 parent 06649e1 commit c11856b
Show file tree
Hide file tree
Showing 74 changed files with 5,632 additions and 692 deletions.
1 change: 1 addition & 0 deletions api/v1alpha2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions api/v1alpha3/azurecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha3

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
)

Expand Down Expand Up @@ -112,6 +113,101 @@ func (c *AzureCluster) SetConditions(conditions clusterv1.Conditions) {
c.Status.Conditions = conditions
}

// ClusterDescriber implementation

// SubscriptionID returns the cluster resource group.
func (c *AzureCluster) SubscriptionID() string {
return c.Spec.SubscriptionID
}

// ResourceGroup returns the cluster resource group.
func (c *AzureCluster) ResourceGroup() string {
return c.Spec.ResourceGroup
}

// ClusterName returns the cluster name.
func (c *AzureCluster) ClusterName() string {
for _, ref := range c.OwnerReferences {
if ref.Kind != "Cluster" {
continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return ""
}
if gv.Group == clusterv1.GroupVersion.Group {
return ref.Name
}
}
return ""
}

// Location returns the cluster location.
func (c *AzureCluster) Location() string {
return c.Spec.Location
}

// SetFailureDomain will set the spec for a for a given key
func (c *AzureCluster) SetFailureDomain(id string, spec clusterv1.FailureDomainSpec) {
if c.Status.FailureDomains == nil {
c.Status.FailureDomains = make(clusterv1.FailureDomains, 0)
}
c.Status.FailureDomains[id] = spec
}

// AdditionalTags returns AdditionalTags from the scope's AzureCluster.
func (c *AzureCluster) AdditionalTags() Tags {
tags := make(Tags)
if c.Spec.AdditionalTags != nil {
tags = c.Spec.AdditionalTags.DeepCopy()
}
return tags
}

// END

// NetworkDescriber implementation

// LoadBalancerName returns the node load balancer name.
func (c *AzureCluster) LoadBalancerName() string {
return c.ClusterName()
}

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

// Vnet returns the cluster Vnet.
func (c *AzureCluster) Vnet() *VnetSpec {
return &c.Spec.NetworkSpec.Vnet
}

// IsVnetManaged returns true if the vnet is managed.
func (c *AzureCluster) IsVnetManaged() bool {
return c.Spec.NetworkSpec.Vnet.ID == "" || c.Spec.NetworkSpec.Vnet.Tags.HasOwned(c.ClusterName())
}

// Subnets returns the cluster subnets.
func (c *AzureCluster) Subnets() Subnets {
return c.Spec.NetworkSpec.Subnets
}

// ControlPlaneSubnet returns the cluster control plane subnet.
func (c *AzureCluster) ControlPlaneSubnet() *SubnetSpec {
return c.Spec.NetworkSpec.GetControlPlaneSubnet()
}

// NodeSubnet returns the cluster node subnet.
func (c *AzureCluster) NodeSubnet() *SubnetSpec {
return c.Spec.NetworkSpec.GetNodeSubnet()
}

// RouteTable returns the cluster node routetable.
func (c *AzureCluster) RouteTable() *RouteTable {
return &c.Spec.NetworkSpec.GetNodeSubnet().RouteTable
}

func init() {
SchemeBuilder.Register(&AzureCluster{}, &AzureClusterList{})
}
2 changes: 2 additions & 0 deletions api/v1alpha3/azuremachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ type AzureMachineSpec struct {
// SpotVMOptions allows the ability to specify the Machine should use a Spot VM
// +optional
SpotVMOptions *SpotVMOptions `json:"spotVMOptions,omitempty"`

Subnet *string `json:"subnet,omitempty"`
}

// SpotVMOptions defines the options relevant to running the Machine on Spot VMs
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (

"github.com/Azure/go-autorest/autorest"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
expv1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha3"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// Service is a generic interface used by components offering a type of service.
Expand Down Expand Up @@ -54,21 +57,37 @@ type CredentialGetter interface {

// 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
// ClusterDescriber describe a Kubernetes resource with full
// serializability, object meta and type meta, plus can describe enough
// data to build an Azure cluster
type ClusterDescriber interface {
Authorizer
controllerutil.Object

NetworkDescriber
SubscriptionID() string
ResourceGroup() string
ClusterName() string
Location() string
SetFailureDomain(id string, spec clusterv1.FailureDomainSpec)
AdditionalTags() infrav1.Tags
}

// NetworkDescriber describes the vnet and subnet configuration for a cluster
// abstracted because it is implemented by managed and unmanaged cluster.
type NetworkDescriber interface {
LoadBalancerName() string
Network() *infrav1.Network
Vnet() *infrav1.VnetSpec
IsVnetManaged() bool
Subnets() infrav1.Subnets
NodeSubnet() *infrav1.SubnetSpec
ControlPlaneSubnet() *infrav1.SubnetSpec
RouteTable() *infrav1.RouteTable
}

var _ ClusterDescriber = new(infrav1.AzureCluster)
var _ ClusterDescriber = new(expv1.AzureManagedControlPlane)
91 changes: 50 additions & 41 deletions cloud/scope/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ limitations under the License.
package scope

import (
"os"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/pkg/errors"
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
)

const (
Expand All @@ -35,56 +33,67 @@ const (
USGovernmentCloud = "AzureUSGovernmentCloud"
)

var _ azure.Authorizer = new(AzureClients)

// AzureClients contains all the Azure clients used by the scopes.
type AzureClients struct {
SubscriptionID string
auth.EnvironmentSettings
subscriptionID string
ResourceManagerEndpoint string
ResourceManagerVMDNSSuffix string
Authorizer autorest.Authorizer
authorizer autorest.Authorizer
}

func (c *AzureClients) setCredentials(subscriptionID string) error {
subID, err := getSubscriptionID(subscriptionID)
if err != nil {
return err
}
c.SubscriptionID = subID
// NewAzureClients discovers and initializes
func NewAzureClients(subscriptionID string) (*AzureClients, error) {
c := new(AzureClients)
settings, err := auth.GetSettingsFromEnvironment()
if err != nil {
return err
return nil, err
}
c.ResourceManagerEndpoint = settings.Environment.ResourceManagerEndpoint
c.ResourceManagerVMDNSSuffix = GetAzureDNSZoneForEnvironment(settings.Environment.Name)
settings.Values[auth.SubscriptionID] = subscriptionID
c.Authorizer, err = settings.GetAuthorizer()
return err
}

func getSubscriptionID(subscriptionID string) (string, error) {
if subscriptionID != "" {
return subscriptionID, nil
settings.Values[auth.SubscriptionID] = subscriptionID
}
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
if subscriptionID == "" {
return "", errors.New("error creating azure services. Environment variable AZURE_SUBSCRIPTION_ID is not set")

c.subscriptionID = settings.Values[auth.SubscriptionID]
c.ResourceManagerEndpoint = settings.Environment.ResourceManagerEndpoint
c.ResourceManagerVMDNSSuffix = settings.Environment.ResourceManagerVMDNSSuffix
c.EnvironmentSettings = settings
c.authorizer, err = settings.GetAuthorizer()
if err != nil {
return nil, err
}
return subscriptionID, nil
return c, err
}

// GetAzureDNSZoneForEnvironment returnes the DNSZone to be used with the
// cloud environment, the default is the public cloud
func GetAzureDNSZoneForEnvironment(environmentName string) string {
// default is public cloud
switch environmentName {
case ChinaCloud:
return "cloudapp.chinacloudapi.cn"
case GermanCloud:
return "cloudapp.microsoftazure.de"
case PublicCloud:
return "cloudapp.azure.com"
case USGovernmentCloud:
return "cloudapp.usgovcloudapi.net"
default:
return "cloudapp.azure.com"
}
// // SubscriptionID returns the Azure client Subscription ID.
// func (c *AzureClients) SubscriptionID() string {
// return c.subscriptionID
// }

// BaseURI returns the Azure ResourceManagerEndpoint.
func (c *AzureClients) BaseURI() string {
return c.Environment.ResourceManagerEndpoint
}

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

// func (c *AzureClients) setCredentials(subscriptionID string) error {
// settings, err := auth.GetSettingsFromEnvironment()
// if err != nil {
// return err
// }

// if subscriptionID != "" {
// settings.Values[auth.SubscriptionID] = subscriptionID
// }

// c.SubscriptionID = settings.Values[auth.SubscriptionID]
// c.ResourceManagerEndpoint = settings.Environment.ResourceManagerEndpoint
// c.ResourceManagerVMDNSSuffix = settings.Environment.ResourceManagerVMDNSSuffix
// c.Authorizer, err = settings.GetAuthorizer()
// return err
// }
Loading

0 comments on commit c11856b

Please sign in to comment.