Skip to content

Commit c6aef0b

Browse files
authored
Merge pull request #1976 from zmalik/zm/unify-agentpool-spec
build aks agentpool spec through one method
2 parents a8555bb + 9260653 commit c6aef0b

File tree

5 files changed

+56
-66
lines changed

5 files changed

+56
-66
lines changed

azure/scope/managedcontrolplane.go

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@ func (s *ManagedControlPlaneScope) ManagedClusterSpec() (azure.ManagedClusterSpe
471471
return managedClusterSpec, nil
472472
}
473473

474-
// GetAgentPoolSpecs gets a slice of azure.AgentPoolSpec for the list of agent pools.
475-
func (s *ManagedControlPlaneScope) GetAgentPoolSpecs(ctx context.Context) ([]azure.AgentPoolSpec, error) {
476-
ctx, log, done := tele.StartSpanWithLogger(ctx, "scope.ManagedControlPlaneScope.GetAgentPoolSpecs")
474+
// GetAllAgentPoolSpecs gets a slice of azure.AgentPoolSpec for the list of agent pools.
475+
func (s *ManagedControlPlaneScope) GetAllAgentPoolSpecs(ctx context.Context) ([]azure.AgentPoolSpec, error) {
476+
ctx, log, done := tele.StartSpanWithLogger(ctx, "scope.ManagedControlPlaneScope.GetAllAgentPoolSpecs")
477477
defer done()
478478

479479
if len(s.AllNodePools) == 0 {
@@ -495,7 +495,7 @@ func (s *ManagedControlPlaneScope) GetAgentPoolSpecs(ctx context.Context) ([]azu
495495
ammps = make([]azure.AgentPoolSpec, 0, len(s.AllNodePools))
496496
foundSystemPool = false
497497
)
498-
for _, pool := range s.AllNodePools {
498+
for i, pool := range s.AllNodePools {
499499
// Fetch the owning MachinePool.
500500

501501
ownerPool, err := capiexputil.GetOwnerMachinePool(ctx, s.Client, pool.ObjectMeta)
@@ -512,34 +512,14 @@ func (s *ManagedControlPlaneScope) GetAgentPoolSpecs(ctx context.Context) ([]azu
512512
foundSystemPool = true
513513
}
514514

515-
ammp := azure.AgentPoolSpec{
516-
Name: to.String(pool.Spec.Name),
517-
SKU: pool.Spec.SKU,
518-
Replicas: 1,
519-
OSDiskSizeGB: 0,
520-
Mode: pool.Spec.Mode,
521-
MaxPods: pool.Spec.MaxPods,
522-
AvailabilityZones: pool.Spec.AvailabilityZones,
523-
OsDiskType: pool.Spec.OsDiskType,
524-
}
525-
526-
// Set optional values
527-
if pool.Spec.OSDiskSizeGB != nil {
528-
ammp.OSDiskSizeGB = *pool.Spec.OSDiskSizeGB
529-
}
530-
531-
if ownerPool.Spec.Replicas != nil {
532-
ammp.Replicas = *ownerPool.Spec.Replicas
533-
}
534-
535515
if ownerPool.Spec.Template.Spec.Version != nil {
536516
version := *ownerPool.Spec.Template.Spec.Version
537517
if semver.Compare(version, s.ControlPlane.Spec.Version) > 0 {
538518
return nil, errors.New("MachinePool version cannot be greater than the AzureManagedControlPlane version")
539519
}
540-
ammp.Version = to.StringPtr(strings.TrimPrefix(version, "v"))
541520
}
542521

522+
ammp := buildAgentPoolSpec(s.ControlPlane, ownerPool, &s.AllNodePools[i])
543523
ammps = append(ammps, ammp)
544524
}
545525

@@ -552,44 +532,50 @@ func (s *ManagedControlPlaneScope) GetAgentPoolSpecs(ctx context.Context) ([]azu
552532

553533
// AgentPoolSpec returns an azure.AgentPoolSpec for currently reconciled AzureManagedMachinePool.
554534
func (s *ManagedControlPlaneScope) AgentPoolSpec() azure.AgentPoolSpec {
535+
return buildAgentPoolSpec(s.ControlPlane, s.MachinePool, s.InfraMachinePool)
536+
}
537+
538+
func buildAgentPoolSpec(managedControlPlane *infrav1exp.AzureManagedControlPlane,
539+
machinePool *expv1.MachinePool,
540+
managedMachinePool *infrav1exp.AzureManagedMachinePool) azure.AgentPoolSpec {
555541
var normalizedVersion *string
556-
if s.MachinePool.Spec.Template.Spec.Version != nil {
557-
v := strings.TrimPrefix(*s.MachinePool.Spec.Template.Spec.Version, "v")
542+
if machinePool.Spec.Template.Spec.Version != nil {
543+
v := strings.TrimPrefix(*machinePool.Spec.Template.Spec.Version, "v")
558544
normalizedVersion = &v
559545
}
560546

561547
replicas := int32(1)
562-
if s.MachinePool.Spec.Replicas != nil {
563-
replicas = *s.MachinePool.Spec.Replicas
548+
if machinePool.Spec.Replicas != nil {
549+
replicas = *machinePool.Spec.Replicas
564550
}
565551

566552
agentPoolSpec := azure.AgentPoolSpec{
567-
Name: to.String(s.InfraMachinePool.Spec.Name),
568-
ResourceGroup: s.ControlPlane.Spec.ResourceGroupName,
569-
Cluster: s.ControlPlane.Name,
570-
SKU: s.InfraMachinePool.Spec.SKU,
553+
Name: to.String(managedMachinePool.Spec.Name),
554+
ResourceGroup: managedControlPlane.Spec.ResourceGroupName,
555+
Cluster: managedControlPlane.Name,
556+
SKU: managedMachinePool.Spec.SKU,
571557
Replicas: replicas,
572558
Version: normalizedVersion,
573559
VnetSubnetID: azure.SubnetID(
574-
s.ControlPlane.Spec.SubscriptionID,
575-
s.ControlPlane.Spec.ResourceGroupName,
576-
s.ControlPlane.Spec.VirtualNetwork.Name,
577-
s.ControlPlane.Spec.VirtualNetwork.Subnet.Name,
560+
managedControlPlane.Spec.SubscriptionID,
561+
managedControlPlane.Spec.ResourceGroupName,
562+
managedControlPlane.Spec.VirtualNetwork.Name,
563+
managedControlPlane.Spec.VirtualNetwork.Subnet.Name,
578564
),
579-
Mode: s.InfraMachinePool.Spec.Mode,
580-
MaxPods: s.InfraMachinePool.Spec.MaxPods,
581-
AvailabilityZones: s.InfraMachinePool.Spec.AvailabilityZones,
582-
OsDiskType: s.InfraMachinePool.Spec.OsDiskType,
565+
Mode: managedMachinePool.Spec.Mode,
566+
MaxPods: managedMachinePool.Spec.MaxPods,
567+
AvailabilityZones: managedMachinePool.Spec.AvailabilityZones,
568+
OsDiskType: managedMachinePool.Spec.OsDiskType,
583569
}
584570

585-
if s.InfraMachinePool.Spec.OSDiskSizeGB != nil {
586-
agentPoolSpec.OSDiskSizeGB = *s.InfraMachinePool.Spec.OSDiskSizeGB
571+
if managedMachinePool.Spec.OSDiskSizeGB != nil {
572+
agentPoolSpec.OSDiskSizeGB = *managedMachinePool.Spec.OSDiskSizeGB
587573
}
588574

589-
if s.InfraMachinePool.Spec.Scaling != nil {
575+
if managedMachinePool.Spec.Scaling != nil {
590576
agentPoolSpec.EnableAutoScaling = to.BoolPtr(true)
591-
agentPoolSpec.MaxCount = s.InfraMachinePool.Spec.Scaling.MaxSize
592-
agentPoolSpec.MinCount = s.InfraMachinePool.Spec.Scaling.MinSize
577+
agentPoolSpec.MaxCount = managedMachinePool.Spec.Scaling.MaxSize
578+
agentPoolSpec.MinCount = managedMachinePool.Spec.Scaling.MinSize
593579
}
594580

595581
return agentPoolSpec

azure/scope/managedcontrolplane_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,12 @@ func TestManagedControlPlaneScope_PoolVersion(t *testing.T) {
169169
},
170170
Expected: []azure.AgentPoolSpec{
171171
{
172-
Name: "pool0",
173-
SKU: "Standard_D2s_v3",
174-
Replicas: 1,
175-
Mode: "System",
172+
Name: "pool0",
173+
SKU: "Standard_D2s_v3",
174+
Replicas: 1,
175+
Mode: "System",
176+
Cluster: "cluster1",
177+
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
176178
},
177179
},
178180
},
@@ -204,11 +206,13 @@ func TestManagedControlPlaneScope_PoolVersion(t *testing.T) {
204206
},
205207
Expected: []azure.AgentPoolSpec{
206208
{
207-
Name: "pool0",
208-
SKU: "Standard_D2s_v3",
209-
Mode: "System",
210-
Replicas: 1,
211-
Version: to.StringPtr("1.21.1"),
209+
Name: "pool0",
210+
SKU: "Standard_D2s_v3",
211+
Mode: "System",
212+
Replicas: 1,
213+
Version: to.StringPtr("1.21.1"),
214+
Cluster: "cluster1",
215+
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
212216
},
213217
},
214218
},
@@ -250,7 +254,7 @@ func TestManagedControlPlaneScope_PoolVersion(t *testing.T) {
250254
c.Input.Client = fakeClient
251255
s, err := NewManagedControlPlaneScope(context.TODO(), c.Input)
252256
g.Expect(err).To(Succeed())
253-
agentPools, err := s.GetAgentPoolSpecs(context.TODO())
257+
agentPools, err := s.GetAllAgentPoolSpecs(context.TODO())
254258
if err != nil {
255259
g.Expect(err.Error()).To(Equal(c.Err))
256260
} else {
@@ -351,7 +355,7 @@ func TestManagedControlPlaneScope_MaxPods(t *testing.T) {
351355
g.Expect(err).To(Succeed())
352356
agentPool := s.AgentPoolSpec()
353357
g.Expect(agentPool).To(Equal(c.Expected))
354-
agentPools, err := s.GetAgentPoolSpecs(context.TODO())
358+
agentPools, err := s.GetAllAgentPoolSpecs(context.TODO())
355359
g.Expect(err).To(Succeed())
356360
g.Expect(agentPools[0].MaxPods).To(Equal(c.Expected.MaxPods))
357361
})

azure/services/managedclusters/managedclusters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var (
4444
type ManagedClusterScope interface {
4545
azure.ClusterDescriber
4646
ManagedClusterSpec() (azure.ManagedClusterSpec, error)
47-
GetAgentPoolSpecs(ctx context.Context) ([]azure.AgentPoolSpec, error)
47+
GetAllAgentPoolSpecs(ctx context.Context) ([]azure.AgentPoolSpec, error)
4848
SetControlPlaneEndpoint(clusterv1.APIEndpoint)
4949
MakeEmptyKubeConfigSecret() corev1.Secret
5050
GetKubeConfigData() []byte
@@ -168,7 +168,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
168168
if azure.ResourceNotFound(err) {
169169
isCreate = true
170170
// Add system agent pool to cluster spec that will be submitted to the API
171-
managedClusterSpec.AgentPools, err = s.Scope.GetAgentPoolSpecs(ctx)
171+
managedClusterSpec.AgentPools, err = s.Scope.GetAllAgentPoolSpecs(ctx)
172172
if err != nil {
173173
return errors.Wrapf(err, "failed to get system agent pool specs for managed cluster %s", s.Scope.ClusterName())
174174
}

azure/services/managedclusters/managedclusters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestReconcile(t *testing.T) {
130130
Name: "my-managedcluster",
131131
ResourceGroupName: "my-rg",
132132
}, nil)
133-
s.GetAgentPoolSpecs(gomockinternal.AContext()).AnyTimes().Return([]azure.AgentPoolSpec{
133+
s.GetAllAgentPoolSpecs(gomockinternal.AContext()).AnyTimes().Return([]azure.AgentPoolSpec{
134134
{
135135
Name: "my-agentpool",
136136
SKU: "Standard_D4s_v3",

azure/services/managedclusters/mock_managedclusters/managedclusters_mock.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)