Skip to content

Commit

Permalink
Refactor managed machine pool
Browse files Browse the repository at this point in the history
  • Loading branch information
nprokopic committed Aug 11, 2021
1 parent e1652c3 commit 37fabd4
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 225 deletions.
8 changes: 0 additions & 8 deletions azure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ type Reconciler interface {
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
}

// CredentialGetter is a Service which knows how to retrieve credentials for an Azure
// resource in a resource group.
type CredentialGetter interface {
Expand Down
51 changes: 0 additions & 51 deletions azure/mocks/service_mock.go

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

59 changes: 59 additions & 0 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ func (s *ManagedControlPlaneScope) ResourceGroup() string {
return s.ControlPlane.Spec.ResourceGroupName
}

// NodeResourceGroup returns the managed control plane's node resource group.
func (s *ManagedControlPlaneScope) NodeResourceGroup() string {
if s.ControlPlane == nil {
return ""
}
return s.ControlPlane.Spec.NodeResourceGroupName
}

// ClusterName returns the managed control plane's name.
func (s *ManagedControlPlaneScope) ClusterName() string {
return s.Cluster.Name
Expand Down Expand Up @@ -450,6 +458,57 @@ func (s *ManagedControlPlaneScope) GetSystemAgentPoolSpecs(ctx context.Context)
return ammps, nil
}

// AgentPoolSpec returns an azure.AgentPoolSpec for currently reconciled AzureManagedMachinePool.
func (s *ManagedControlPlaneScope) AgentPoolSpec() azure.AgentPoolSpec {
var normalizedVersion *string
if s.MachinePool.Spec.Template.Spec.Version != nil {
v := strings.TrimPrefix(*s.MachinePool.Spec.Template.Spec.Version, "v")
normalizedVersion = &v
}

replicas := int32(1)
if s.MachinePool.Spec.Replicas != nil {
replicas = *s.MachinePool.Spec.Replicas
}

agentPoolSpec := azure.AgentPoolSpec{
Name: s.InfraMachinePool.Name,
ResourceGroup: s.ControlPlane.Spec.ResourceGroupName,
Cluster: s.ControlPlane.Name,
SKU: s.InfraMachinePool.Spec.SKU,
Replicas: replicas,
Version: normalizedVersion,
VnetSubnetID: azure.SubnetID(
s.ControlPlane.Spec.SubscriptionID,
s.ControlPlane.Spec.ResourceGroupName,
s.ControlPlane.Spec.VirtualNetwork.Name,
s.ControlPlane.Spec.VirtualNetwork.Subnet.Name,
),
Mode: s.InfraMachinePool.Spec.Mode,
}

if s.InfraMachinePool.Spec.OSDiskSizeGB != nil {
agentPoolSpec.OSDiskSizeGB = *s.InfraMachinePool.Spec.OSDiskSizeGB
}

return agentPoolSpec
}

// SetAgentPoolProviderIDList sets a list of agent pool's Azure VM IDs.
func (s *ManagedControlPlaneScope) SetAgentPoolProviderIDList(providerIDs []string) {
s.InfraMachinePool.Spec.ProviderIDList = providerIDs
}

// SetAgentPoolReplicas sets the number of agent pool replicas.
func (s *ManagedControlPlaneScope) SetAgentPoolReplicas(replicas int32) {
s.InfraMachinePool.Status.Replicas = replicas
}

// SetAgentPoolReady sets the flag that indicates if the agent pool is ready or not.
func (s *ManagedControlPlaneScope) SetAgentPoolReady(ready bool) {
s.InfraMachinePool.Status.Ready = ready
}

// SetControlPlaneEndpoint sets a control plane endpoint.
func (s *ManagedControlPlaneScope) SetControlPlaneEndpoint(endpoint clusterv1.APIEndpoint) {
s.ControlPlane.Spec.ControlPlaneEndpoint = endpoint
Expand Down
50 changes: 29 additions & 21 deletions azure/services/agentpools/agentpools.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-05-01/containerservice"
"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"k8s.io/klog/v2"
Expand All @@ -30,28 +31,38 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

// Spec contains properties to create a agent pool.
type Spec struct {
Name string
ResourceGroup string
Cluster string
Version *string
SKU string
Replicas int32
OSDiskSizeGB int32
VnetSubnetID string
Mode string
// ManagedMachinePoolScope defines the scope interface for a managed machine pool.
type ManagedMachinePoolScope interface {
logr.Logger
azure.ClusterDescriber

NodeResourceGroup() string
AgentPoolSpec() azure.AgentPoolSpec
SetAgentPoolProviderIDList([]string)
SetAgentPoolReplicas(int32)
SetAgentPoolReady(bool)
}

// Service provides operations on Azure resources.
type Service struct {
scope ManagedMachinePoolScope
Client
}

// New creates a new service.
func New(scope ManagedMachinePoolScope) *Service {
return &Service{
scope: scope,
Client: NewClient(scope),
}
}

// Reconcile idempotently creates or updates a agent pool, if possible.
func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
func (s *Service) Reconcile(ctx context.Context) error {
ctx, span := tele.Tracer().Start(ctx, "agentpools.Service.Reconcile")
defer span.End()

agentPoolSpec, ok := spec.(*Spec)
if !ok {
return errors.New("invalid agent pool specification")
}
agentPoolSpec := s.scope.AgentPoolSpec()

profile := containerservice.AgentPool{
ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{
Expand Down Expand Up @@ -123,14 +134,11 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
}

// Delete deletes the virtual network with the provided name.
func (s *Service) Delete(ctx context.Context, spec interface{}) error {
func (s *Service) Delete(ctx context.Context) error {
ctx, span := tele.Tracer().Start(ctx, "agentpools.Service.Delete")
defer span.End()

agentPoolSpec, ok := spec.(*Spec)
if !ok {
return errors.New("invalid agent pool specification")
}
agentPoolSpec := s.scope.AgentPoolSpec()

klog.V(2).Infof("deleting agent pool %s ", agentPoolSpec.Name)
err := s.Client.Delete(ctx, agentPoolSpec.ResourceGroup, agentPoolSpec.Cluster, agentPoolSpec.Name)
Expand Down
Loading

0 comments on commit 37fabd4

Please sign in to comment.