Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#5020 from nojnhuh/amp-tags
Browse files Browse the repository at this point in the history
don't rollout VMSS model updates when tags change
  • Loading branch information
k8s-ci-robot authored Jul 24, 2024
2 parents 7e8650d + 4c24a89 commit 4c87a70
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
6 changes: 6 additions & 0 deletions azure/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const (
// for annotation formatting rules.
VMTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-vm"

// VMSSTagsLastAppliedAnnotation is the key for the machine object annotation
// which tracks the AdditionalTags in the MachinePool Provider Config.
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
// for annotation formatting rules.
VMSSTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-vmss"

// RGTagsLastAppliedAnnotation is the key for the Azure Cluster object annotation
// which tracks the AdditionalTags for Resource Group which is part in the Azure Cluster.
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
Expand Down
5 changes: 5 additions & 0 deletions azure/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func VMID(subscriptionID, resourceGroup, vmName string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s", subscriptionID, resourceGroup, vmName)
}

// VMSSID returns the azure resource ID for a given VMSS.
func VMSSID(subscriptionID, resourceGroup, vmssName string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s", subscriptionID, resourceGroup, vmssName)
}

// VNetID returns the azure resource ID for a given VNet.
func VNetID(subscriptionID, resourceGroup, vnetName string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s", subscriptionID, resourceGroup, vnetName)
Expand Down
39 changes: 39 additions & 0 deletions azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -946,3 +947,41 @@ func (m *MachinePoolScope) ReconcileReplicas(ctx context.Context, vmss *azure.VM

return nil
}

// AnnotationJSON returns a map[string]interface from a JSON annotation.
func (m *MachinePoolScope) AnnotationJSON(annotation string) (map[string]interface{}, error) {
out := map[string]interface{}{}
jsonAnnotation := m.AzureMachinePool.GetAnnotations()[annotation]
if jsonAnnotation == "" {
return out, nil
}
err := json.Unmarshal([]byte(jsonAnnotation), &out)
if err != nil {
return out, err
}
return out, nil
}

// UpdateAnnotationJSON updates the `annotation` with
// `content`. `content` in this case should be a `map[string]interface{}`
// suitable for turning into JSON. This `content` map will be marshalled into a
// JSON string before being set as the given `annotation`.
func (m *MachinePoolScope) UpdateAnnotationJSON(annotation string, content map[string]interface{}) error {
b, err := json.Marshal(content)
if err != nil {
return err
}
m.SetAnnotation(annotation, string(b))
return nil
}

// TagsSpecs returns the tags for the AzureMachinePool.
func (m *MachinePoolScope) TagsSpecs() []azure.TagsSpec {
return []azure.TagsSpec{
{
Scope: azure.VMSSID(m.SubscriptionID(), m.NodeResourceGroup(), m.Name()),
Tags: m.AdditionalTags(),
Annotation: azure.VMSSTagsLastAppliedAnnotation,
},
}
}
1 change: 0 additions & 1 deletion azure/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func (vmss VMSS) HasModelChanges(other VMSS) bool {
equal := cmp.Equal(vmss.Image, other.Image) &&
cmp.Equal(vmss.Identity, other.Identity) &&
cmp.Equal(vmss.Zones, other.Zones) &&
cmp.Equal(vmss.Tags, other.Tags) &&
cmp.Equal(vmss.Sku, other.Sku)
return !equal
}
Expand Down
12 changes: 0 additions & 12 deletions azure/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,6 @@ func TestVMSS_HasModelChanges(t *testing.T) {
},
HasModelChanges: true,
},
{
Name: "with different Tags",
Factory: func() (VMSS, VMSS) {
l := getDefaultVMSSForModelTesting()
l.Tags = infrav1.Tags{
"bin": "baz",
}
r := getDefaultVMSSForModelTesting()
return r, l
},
HasModelChanges: true,
},
}

for _, c := range cases {
Expand Down
6 changes: 6 additions & 0 deletions exp/controllers/azuremachinepool_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/scalesets"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

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

return &azureMachinePoolService{
scope: machinePoolScope,
services: []azure.ServiceReconciler{
scaleSetsSvc,
roleAssignmentsSvc,
tagsSvc,
},
skuCache: cache,
}, nil
Expand Down

0 comments on commit 4c87a70

Please sign in to comment.