Skip to content

Commit

Permalink
fix version issue for auto upgraded clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
LochanRn authored and LochanRn committed Oct 2, 2023
1 parent 5a53d64 commit c59921c
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 106 deletions.
35 changes: 7 additions & 28 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,37 +437,16 @@ func (s *ManagedControlPlaneScope) SetAutoUpgradeVersionStatus(version string) {
s.ControlPlane.Status.AutoUpgradeVersion = version
}

// IsManagedVersionUpgrade checks if auto upgrade profile is set and if the upgradeChannel is of the type patch, stable or rapid.
// IsManagedVersionUpgrade checks if version is auto managed by AKS.
func (s *ManagedControlPlaneScope) IsManagedVersionUpgrade() bool {
return s.IsPatchAutoUpgrade() || s.IsStableAutoUpgrade() || s.IsRapidAutoUpgrade()
return isManagedVersionUpgrade(s.ControlPlane)
}

// IsAutoUpgradeStable checks if auto upgrade channel is stable.
func (s *ManagedControlPlaneScope) IsStableAutoUpgrade() bool {
if s.ControlPlane.Spec.AutoUpgradeProfile != nil {
if upgradeChannel := s.ControlPlane.Spec.AutoUpgradeProfile.UpgradeChannel; upgradeChannel == infrav1exp.UpgradeChannelStable {
return true
}
}
return false
}

// IsAutoUpgradeStable checks if auto upgrade channel is rapid.
func (s *ManagedControlPlaneScope) IsRapidAutoUpgrade() bool {
if s.ControlPlane.Spec.AutoUpgradeProfile != nil {
if upgradeChannel := s.ControlPlane.Spec.AutoUpgradeProfile.UpgradeChannel; upgradeChannel == infrav1exp.UpgradeChannelRapid {
return true
}
}
return false
}

// IsAutoUpgradeStable checks if auto upgrade channel is patch.
func (s *ManagedControlPlaneScope) IsPatchAutoUpgrade() bool {
if s.ControlPlane.Spec.AutoUpgradeProfile != nil {
if upgradeChannel := s.ControlPlane.Spec.AutoUpgradeProfile.UpgradeChannel; upgradeChannel == infrav1exp.UpgradeChannelPatch {
return true
}
func isManagedVersionUpgrade(managedControlPlane *infrav1exp.AzureManagedControlPlane) bool {
if managedControlPlane.Spec.AutoUpgradeProfile != nil &&
(managedControlPlane.Spec.AutoUpgradeProfile.UpgradeChannel != infrav1exp.UpgradeChannelNone &&
managedControlPlane.Spec.AutoUpgradeProfile.UpgradeChannel != infrav1exp.UpgradeChannelNodeImage) {
return true
}
return false
}
Expand Down
17 changes: 9 additions & 8 deletions azure/scope/managedcontrolplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"testing"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
pointer "k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/managedclusters"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
Expand Down Expand Up @@ -81,6 +80,7 @@ func TestManagedControlPlaneScope_PoolVersion(t *testing.T) {
Mode: "System",
Cluster: "cluster1",
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
OSType: pointer.To("Linux"),
},
},
},
Expand Down Expand Up @@ -119,9 +119,10 @@ func TestManagedControlPlaneScope_PoolVersion(t *testing.T) {
SKU: "Standard_D2s_v3",
Mode: "System",
Replicas: 1,
Version: to.StringPtr("1.21.1"),
Version: pointer.To("1.21.1"),
Cluster: "cluster1",
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
OSType: pointer.To("Linux"),
},
},
},
Expand Down Expand Up @@ -515,7 +516,7 @@ func TestManagedControlPlaneScope_DisableLocalAccounts(t *testing.T) {
},
Spec: infrav1.AzureManagedControlPlaneSpec{
SubscriptionID: "00000000-0000-0000-0000-000000000000",
DisableLocalAccounts: pointer.BoolPtr(true),
DisableLocalAccounts: pointer.To(true),
},
},
ManagedMachinePools: []ManagedMachinePool{
Expand Down Expand Up @@ -550,7 +551,7 @@ func TestManagedControlPlaneScope_DisableLocalAccounts(t *testing.T) {
Managed: true,
AdminGroupObjectIDs: []string{"00000000-0000-0000-0000-000000000000"},
},
DisableLocalAccounts: pointer.BoolPtr(true),
DisableLocalAccounts: pointer.To(true),
},
},
ManagedMachinePools: []ManagedMachinePool{
Expand All @@ -560,7 +561,7 @@ func TestManagedControlPlaneScope_DisableLocalAccounts(t *testing.T) {
},
},
},
Expected: pointer.BoolPtr(true),
Expected: pointer.To(true),
},
}
for _, c := range cases {
Expand Down Expand Up @@ -642,7 +643,7 @@ func TestIsAaDEnabled(t *testing.T) {
Managed: true,
AdminGroupObjectIDs: []string{"00000000-0000-0000-0000-000000000000"},
},
DisableLocalAccounts: pointer.BoolPtr(true),
DisableLocalAccounts: pointer.To(true),
},
},
ManagedMachinePools: []ManagedMachinePool{
Expand Down Expand Up @@ -766,7 +767,7 @@ func TestIsLocalAcountsDisabled(t *testing.T) {
Managed: true,
AdminGroupObjectIDs: []string{"00000000-0000-0000-0000-000000000000"},
},
DisableLocalAccounts: pointer.BoolPtr(true),
DisableLocalAccounts: pointer.To(true),
},
},
ManagedMachinePools: []ManagedMachinePool{
Expand Down
30 changes: 25 additions & 5 deletions azure/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (

"github.com/Azure/go-autorest/autorest/to"
"github.com/pkg/errors"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
"sigs.k8s.io/cluster-api-provider-azure/util/versions"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
Expand Down Expand Up @@ -135,11 +137,7 @@ func (s *ManagedMachinePoolScope) AgentPoolSpec() azure.AgentPoolSpec {
func buildAgentPoolSpec(managedControlPlane *infrav1exp.AzureManagedControlPlane,
machinePool *clusterv1exp.MachinePool,
managedMachinePool *infrav1exp.AzureManagedMachinePool) azure.AgentPoolSpec {
var normalizedVersion *string
if machinePool.Spec.Template.Spec.Version != nil {
v := strings.TrimPrefix(*machinePool.Spec.Template.Spec.Version, "v")
normalizedVersion = &v
}
normalizedVersion := getManagedMachinePoolVersion(managedControlPlane, machinePool)

replicas := int32(1)
if machinePool.Spec.Replicas != nil {
Expand Down Expand Up @@ -300,3 +298,25 @@ func (s *ManagedMachinePoolScope) UpdateCAPIMachinePoolAnnotations(ctx context.C
func (s *ManagedMachinePoolScope) GetCAPIMachinePoolAnnotation(ctx context.Context, key string) string {
return s.MachinePool.Annotations[key]
}

// IsManagedVersionUpgrade checks if version is auto managed by AKS.
func (s *ManagedMachinePoolScope) IsManagedAutoUpgrade() bool {
return isManagedVersionUpgrade(s.ControlPlane)
}

func getManagedMachinePoolVersion(managedControlPlane *infrav1exp.AzureManagedControlPlane,
machinePool *clusterv1exp.MachinePool) *string {
var v, av string
if machinePool != nil {
v = ptr.Deref(machinePool.Spec.Template.Spec.Version, "")
}
if managedControlPlane != nil {
av = managedControlPlane.Status.AutoUpgradeVersion
}
version := strings.TrimPrefix(v, "v")
higherVersion, err := versions.GetHigherK8sVersion(version, av)
if err != nil {
return nil
}
return ptr.To(strings.TrimPrefix(higherVersion, "v"))
}
Loading

0 comments on commit c59921c

Please sign in to comment.