Skip to content

Commit 4c24a89

Browse files
committed
don't rollout VMSS model updates when tags change
1 parent 7e8650d commit 4c24a89

File tree

6 files changed

+56
-13
lines changed

6 files changed

+56
-13
lines changed

azure/const.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const (
2323
// for annotation formatting rules.
2424
VMTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-vm"
2525

26+
// VMSSTagsLastAppliedAnnotation is the key for the machine object annotation
27+
// which tracks the AdditionalTags in the MachinePool Provider Config.
28+
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
29+
// for annotation formatting rules.
30+
VMSSTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-vmss"
31+
2632
// RGTagsLastAppliedAnnotation is the key for the Azure Cluster object annotation
2733
// which tracks the AdditionalTags for Resource Group which is part in the Azure Cluster.
2834
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/

azure/defaults.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ func VMID(subscriptionID, resourceGroup, vmName string) string {
217217
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s", subscriptionID, resourceGroup, vmName)
218218
}
219219

220+
// VMSSID returns the azure resource ID for a given VMSS.
221+
func VMSSID(subscriptionID, resourceGroup, vmssName string) string {
222+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s", subscriptionID, resourceGroup, vmssName)
223+
}
224+
220225
// VNetID returns the azure resource ID for a given VNet.
221226
func VNetID(subscriptionID, resourceGroup, vnetName string) string {
222227
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s", subscriptionID, resourceGroup, vnetName)

azure/scope/machinepool.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"crypto/sha256"
2222
"encoding/base64"
23+
"encoding/json"
2324
"fmt"
2425
"io"
2526
"strings"
@@ -946,3 +947,41 @@ func (m *MachinePoolScope) ReconcileReplicas(ctx context.Context, vmss *azure.VM
946947

947948
return nil
948949
}
950+
951+
// AnnotationJSON returns a map[string]interface from a JSON annotation.
952+
func (m *MachinePoolScope) AnnotationJSON(annotation string) (map[string]interface{}, error) {
953+
out := map[string]interface{}{}
954+
jsonAnnotation := m.AzureMachinePool.GetAnnotations()[annotation]
955+
if jsonAnnotation == "" {
956+
return out, nil
957+
}
958+
err := json.Unmarshal([]byte(jsonAnnotation), &out)
959+
if err != nil {
960+
return out, err
961+
}
962+
return out, nil
963+
}
964+
965+
// UpdateAnnotationJSON updates the `annotation` with
966+
// `content`. `content` in this case should be a `map[string]interface{}`
967+
// suitable for turning into JSON. This `content` map will be marshalled into a
968+
// JSON string before being set as the given `annotation`.
969+
func (m *MachinePoolScope) UpdateAnnotationJSON(annotation string, content map[string]interface{}) error {
970+
b, err := json.Marshal(content)
971+
if err != nil {
972+
return err
973+
}
974+
m.SetAnnotation(annotation, string(b))
975+
return nil
976+
}
977+
978+
// TagsSpecs returns the tags for the AzureMachinePool.
979+
func (m *MachinePoolScope) TagsSpecs() []azure.TagsSpec {
980+
return []azure.TagsSpec{
981+
{
982+
Scope: azure.VMSSID(m.SubscriptionID(), m.NodeResourceGroup(), m.Name()),
983+
Tags: m.AdditionalTags(),
984+
Annotation: azure.VMSSTagsLastAppliedAnnotation,
985+
},
986+
}
987+
}

azure/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ func (vmss VMSS) HasModelChanges(other VMSS) bool {
126126
equal := cmp.Equal(vmss.Image, other.Image) &&
127127
cmp.Equal(vmss.Identity, other.Identity) &&
128128
cmp.Equal(vmss.Zones, other.Zones) &&
129-
cmp.Equal(vmss.Tags, other.Tags) &&
130129
cmp.Equal(vmss.Sku, other.Sku)
131130
return !equal
132131
}

azure/types_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,6 @@ func TestVMSS_HasModelChanges(t *testing.T) {
124124
},
125125
HasModelChanges: true,
126126
},
127-
{
128-
Name: "with different Tags",
129-
Factory: func() (VMSS, VMSS) {
130-
l := getDefaultVMSSForModelTesting()
131-
l.Tags = infrav1.Tags{
132-
"bin": "baz",
133-
}
134-
r := getDefaultVMSSForModelTesting()
135-
return r, l
136-
},
137-
HasModelChanges: true,
138-
},
139127
}
140128

141129
for _, c := range cases {

exp/controllers/azuremachinepool_reconciler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
2626
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
2727
"sigs.k8s.io/cluster-api-provider-azure/azure/services/scalesets"
28+
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
2829
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
2930
)
3031

@@ -49,12 +50,17 @@ func newAzureMachinePoolService(machinePoolScope *scope.MachinePoolScope) (*azur
4950
if err != nil {
5051
return nil, errors.Wrap(err, "failed to create a scalesets service")
5152
}
53+
tagsSvc, err := tags.New(machinePoolScope)
54+
if err != nil {
55+
return nil, errors.Wrap(err, "failed creating tags service")
56+
}
5257

5358
return &azureMachinePoolService{
5459
scope: machinePoolScope,
5560
services: []azure.ServiceReconciler{
5661
scaleSetsSvc,
5762
roleAssignmentsSvc,
63+
tagsSvc,
5864
},
5965
skuCache: cache,
6066
}, nil

0 commit comments

Comments
 (0)