Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix version issue for auto upgraded clusters #97

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 24 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,24 @@ 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
}
higherVersion, err := versions.GetHigherK8sVersion(v, av)
if err != nil {
return nil
}
return ptr.To(strings.TrimPrefix(higherVersion, "v"))
}
130 changes: 127 additions & 3 deletions azure/scope/managedmachinepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -66,7 +67,7 @@ func TestManagedMachinePoolScope_Autoscaling(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{

OSType: ptr.To("Linux"),
Name: "pool0",
SKU: "Standard_D2s_v3",
Replicas: 1,
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestManagedMachinePoolScope_Autoscaling(t *testing.T) {
MinCount: to.Int32Ptr(2),
MaxCount: to.Int32Ptr(10),
VnetSubnetID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups//providers/Microsoft.Network/virtualNetworks//subnets/",
OSType: ptr.To("Linux"),
},
},
}
Expand Down Expand Up @@ -160,7 +162,7 @@ func TestManagedMachinePoolScope_NodeLabels(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{

OSType: ptr.To("Linux"),
Name: "pool0",
SKU: "Standard_D2s_v3",
Replicas: 1,
Expand Down Expand Up @@ -195,6 +197,7 @@ func TestManagedMachinePoolScope_NodeLabels(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{
OSType: ptr.To("Linux"),
Name: "pool1",
SKU: "Standard_D2s_v3",
Mode: "System",
Expand Down Expand Up @@ -256,6 +259,7 @@ func TestManagedMachinePoolScope_MaxPods(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{
OSType: ptr.To("Linux"),
Name: "pool0",
SKU: "Standard_D2s_v3",
Replicas: 1,
Expand Down Expand Up @@ -288,6 +292,7 @@ func TestManagedMachinePoolScope_MaxPods(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{
OSType: ptr.To("Linux"),
Name: "pool1",
SKU: "Standard_D2s_v3",
Mode: "System",
Expand Down Expand Up @@ -347,7 +352,7 @@ func TestManagedMachinePoolScope_Taints(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{

OSType: ptr.To("Linux"),
Name: "pool0",
SKU: "Standard_D2s_v3",
Replicas: 1,
Expand Down Expand Up @@ -386,6 +391,7 @@ func TestManagedMachinePoolScope_Taints(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{
OSType: ptr.To("Linux"),
Name: "pool1",
SKU: "Standard_D2s_v3",
Mode: "User",
Expand Down Expand Up @@ -445,6 +451,7 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{
OSType: ptr.To("Linux"),
Name: "pool0",
SKU: "Standard_D2s_v3",
Replicas: 1,
Expand Down Expand Up @@ -477,6 +484,7 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) {
},
},
Expected: azure.AgentPoolSpec{
OSType: ptr.To("Linux"),
Name: "pool1",
SKU: "Standard_D2s_v3",
Mode: "User",
Expand All @@ -502,6 +510,122 @@ func TestManagedMachinePoolScope_OSDiskType(t *testing.T) {
}
}

func Test_getManagedMachinePoolVersion(t *testing.T) {
cases := []struct {
Name string
managedControlPlane *infrav1.AzureManagedControlPlane
machinePool *capiv1exp.MachinePool
Expected *string
}{
{
Name: "Empty configs",
managedControlPlane: nil,
machinePool: nil,
Expected: nil,
},
{
Name: "Empty mp",
managedControlPlane: &infrav1.AzureManagedControlPlane{},
machinePool: nil,
Expected: nil,
},
{
Name: "Only machine pool is available",
managedControlPlane: nil,
machinePool: &capiv1exp.MachinePool{
Spec: capiv1exp.MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Version: ptr.To("v1.15.0"),
},
},
},
},
Expected: ptr.To("1.15.0"),
},
{
Name: "Only machine pool is available and cp is nil",
managedControlPlane: nil,
machinePool: &capiv1exp.MachinePool{
Spec: capiv1exp.MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Version: ptr.To("v1.15.0"),
},
},
},
},
Expected: ptr.To("1.15.0"),
},
{
Name: "mcp.status.autoUpgradeVersion > mp.spec.template.spec.version",
managedControlPlane: &infrav1.AzureManagedControlPlane{
Status: infrav1.AzureManagedControlPlaneStatus{
AutoUpgradeVersion: "1.20.3",
},
},
machinePool: &capiv1exp.MachinePool{
Spec: capiv1exp.MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Version: ptr.To("v1.15.0"),
},
},
},
},
Expected: ptr.To("1.20.3"),
},
{
Name: "suffix + mcp.status.autoUpgradeVersion > mp.spec.template.spec.version",
managedControlPlane: &infrav1.AzureManagedControlPlane{
Status: infrav1.AzureManagedControlPlaneStatus{
AutoUpgradeVersion: "v1.20.3",
},
},
machinePool: &capiv1exp.MachinePool{
Spec: capiv1exp.MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Version: ptr.To("v1.15.0"),
},
},
},
},
Expected: ptr.To("1.20.3"),
},
{
Name: "mcp.status.autoUpgradeVersion < mp.spec.template.spec.version",
managedControlPlane: &infrav1.AzureManagedControlPlane{
Status: infrav1.AzureManagedControlPlaneStatus{
AutoUpgradeVersion: "v1.20.3",
},
},
machinePool: &capiv1exp.MachinePool{
Spec: capiv1exp.MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
Spec: clusterv1.MachineSpec{
Version: ptr.To("v1.21.0"),
},
},
},
},
Expected: ptr.To("1.21.0"),
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
g := NewWithT(t)
v := getManagedMachinePoolVersion(c.managedControlPlane, c.machinePool)
if c.Expected != nil {
g.Expect(*v).To(Equal(*c.Expected))
} else {
g.Expect(v).To(Equal(c.Expected))
}
})
}
}

func getAzureMachinePool(name string, mode infrav1.NodePoolMode) *infrav1.AzureManagedMachinePool {
return &infrav1.AzureManagedMachinePool{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading
Loading