Skip to content

Commit 6eb879f

Browse files
author
Cecile Robert-Michon
committed
💎 Update tags at scope instead of updating VMs
1 parent 3c0b4df commit 6eb879f

File tree

20 files changed

+1025
-273
lines changed

20 files changed

+1025
-273
lines changed

api/v1alpha3/tags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ const (
118118

119119
// CommonRole describes the value for the common role
120120
CommonRole = "common"
121+
122+
// VMTagsLastAppliedAnnotation is the key for the machine object annotation
123+
// which tracks the AdditionalTags in the Machine Provider Config.
124+
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
125+
// for annotation formatting rules.
126+
VMTagsLastAppliedAnnotation = "sigs.k8s.io/cluster-api-provider-azure-last-applied-tags-vm"
121127
)
122128

123129
// ClusterTagKey generates the key for resources associated with a cluster.

cloud/defaults.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,49 +100,54 @@ func GenerateDataDiskName(machineName, nameSuffix string) string {
100100
return fmt.Sprintf("%s_%s", machineName, nameSuffix)
101101
}
102102

103+
// VMID returns the azure resource ID for a given VM.
104+
func VMID(subscriptionID, resourceGroup, vmName string) string {
105+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s", subscriptionID, resourceGroup, vmName)
106+
}
107+
103108
// SubnetID returns the azure resource ID for a given subnet.
104-
func SubnetID(subscriptionID, resoourceGroup, vnetName, subnetName string) string {
105-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s", subscriptionID, resoourceGroup, vnetName, subnetName)
109+
func SubnetID(subscriptionID, resourceGroup, vnetName, subnetName string) string {
110+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s", subscriptionID, resourceGroup, vnetName, subnetName)
106111
}
107112

108113
// PublicIPID returns the azure resource ID for a given public IP.
109-
func PublicIPID(subscriptionID, resoourceGroup, ipName string) string {
110-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s", subscriptionID, resoourceGroup, ipName)
114+
func PublicIPID(subscriptionID, resourceGroup, ipName string) string {
115+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/publicIPAddresses/%s", subscriptionID, resourceGroup, ipName)
111116
}
112117

113118
// RouteTableID returns the azure resource ID for a given route table.
114-
func RouteTableID(subscriptionID, resoourceGroup, routeTableName string) string {
115-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/routeTables/%s", subscriptionID, resoourceGroup, routeTableName)
119+
func RouteTableID(subscriptionID, resourceGroup, routeTableName string) string {
120+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/routeTables/%s", subscriptionID, resourceGroup, routeTableName)
116121
}
117122

118123
// SecurityGroupID returns the azure resource ID for a given security group.
119-
func SecurityGroupID(subscriptionID, resoourceGroup, routeTableName string) string {
120-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s", subscriptionID, resoourceGroup, routeTableName)
124+
func SecurityGroupID(subscriptionID, resourceGroup, routeTableName string) string {
125+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s", subscriptionID, resourceGroup, routeTableName)
121126
}
122127

123128
// NetworkInterfaceID returns the azure resource ID for a given network interface.
124-
func NetworkInterfaceID(subscriptionID, resoourceGroup, nicName string) string {
125-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkInterfaces/%s", subscriptionID, resoourceGroup, nicName)
129+
func NetworkInterfaceID(subscriptionID, resourceGroup, nicName string) string {
130+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkInterfaces/%s", subscriptionID, resourceGroup, nicName)
126131
}
127132

128133
// FrontendIPConfigID returns the azure resource ID for a given frontend IP config.
129-
func FrontendIPConfigID(subscriptionID, resoourceGroup, loadBalancerName, configName string) string {
130-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/frontendIPConfigurations/%s", subscriptionID, resoourceGroup, loadBalancerName, configName)
134+
func FrontendIPConfigID(subscriptionID, resourceGroup, loadBalancerName, configName string) string {
135+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/frontendIPConfigurations/%s", subscriptionID, resourceGroup, loadBalancerName, configName)
131136
}
132137

133138
// AddressPoolID returns the azure resource ID for a given backend address pool.
134-
func AddressPoolID(subscriptionID, resoourceGroup, loadBalancerName, backendPoolName string) string {
135-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/backendAddressPools/%s", subscriptionID, resoourceGroup, loadBalancerName, backendPoolName)
139+
func AddressPoolID(subscriptionID, resourceGroup, loadBalancerName, backendPoolName string) string {
140+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/backendAddressPools/%s", subscriptionID, resourceGroup, loadBalancerName, backendPoolName)
136141
}
137142

138143
// ProbeID returns the azure resource ID for a given probe.
139-
func ProbeID(subscriptionID, resoourceGroup, loadBalancerName, probeName string) string {
140-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/probes/%s", subscriptionID, resoourceGroup, loadBalancerName, probeName)
144+
func ProbeID(subscriptionID, resourceGroup, loadBalancerName, probeName string) string {
145+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/probes/%s", subscriptionID, resourceGroup, loadBalancerName, probeName)
141146
}
142147

143148
// NATRuleID returns the azure resource ID for a inbound NAT rule.
144-
func NATRuleID(subscriptionID, resoourceGroup, loadBalancerName, natRuleName string) string {
145-
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/inboundNatRules/%s", subscriptionID, resoourceGroup, loadBalancerName, natRuleName)
149+
func NATRuleID(subscriptionID, resourceGroup, loadBalancerName, natRuleName string) string {
150+
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/loadBalancers/%s/inboundNatRules/%s", subscriptionID, resourceGroup, loadBalancerName, natRuleName)
146151
}
147152

148153
// GetDefaultImageSKUID gets the SKU ID of the image to use for the provided version of Kubernetes.

cloud/scope/machine.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package scope
1919
import (
2020
"context"
2121
"encoding/base64"
22+
"encoding/json"
2223

2324
"github.com/Azure/go-autorest/autorest/to"
2425
"github.com/go-logr/logr"
@@ -106,6 +107,17 @@ func (m *MachineScope) VMSpecs() []azure.VMSpec {
106107
}
107108
}
108109

110+
// TagsSpecs returns the tags for the AzureMachine.
111+
func (m *MachineScope) TagsSpecs() []azure.TagsSpec {
112+
return []azure.TagsSpec{
113+
{
114+
Scope: azure.VMID(m.SubscriptionID(), m.ResourceGroup(), m.Name()),
115+
Tags: m.AdditionalTags(),
116+
Annotation: infrav1.VMTagsLastAppliedAnnotation,
117+
},
118+
}
119+
}
120+
109121
// PublicIPSpec returns the public IP specs.
110122
func (m *MachineScope) PublicIPSpecs() []azure.PublicIPSpec {
111123
var spec []azure.PublicIPSpec
@@ -328,6 +340,33 @@ func (m *MachineScope) SetAnnotation(key, value string) {
328340
m.AzureMachine.Annotations[key] = value
329341
}
330342

343+
// AnnotationJSON returns a map[string]interface from a JSON annotation.
344+
func (m *MachineScope) AnnotationJSON(annotation string) (map[string]interface{}, error) {
345+
out := map[string]interface{}{}
346+
jsonAnnotation := m.AzureMachine.GetAnnotations()[annotation]
347+
if len(jsonAnnotation) == 0 {
348+
return out, nil
349+
}
350+
err := json.Unmarshal([]byte(jsonAnnotation), &out)
351+
if err != nil {
352+
return out, err
353+
}
354+
return out, nil
355+
}
356+
357+
// UpdateAnnotationJSON updates the `annotation` with
358+
// `content`. `content` in this case should be a `map[string]interface{}`
359+
// suitable for turning into JSON. This `content` map will be marshalled into a
360+
// JSON string before being set as the given `annotation`.
361+
func (m *MachineScope) UpdateAnnotationJSON(annotation string, content map[string]interface{}) error {
362+
b, err := json.Marshal(content)
363+
if err != nil {
364+
return err
365+
}
366+
m.SetAnnotation(annotation, string(b))
367+
return nil
368+
}
369+
331370
// SetAddresses sets the Azure address status.
332371
func (m *MachineScope) SetAddresses(addrs []corev1.NodeAddress) {
333372
m.AzureMachine.Status.Addresses = addrs

cloud/services/networkinterfaces/networkinterfaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
2626
gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
2727

28-
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
28+
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
2929
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
3030
"github.com/Azure/go-autorest/autorest"
3131
"github.com/Azure/go-autorest/autorest/to"

cloud/services/roleassignments/roleassignments.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
4848
return errors.Wrapf(err, "cannot assign role to VM system assigned identity")
4949
}
5050

51-
s.Scope.V(2).Info("successfully created role assignment for generated Identity for VM %s ", "virtual machine", roleSpec.MachineName)
51+
s.Scope.V(2).Info("successfully created role assignment for generated Identity for VM", "virtual machine", roleSpec.MachineName)
5252
}
5353
return nil
5454
}

cloud/services/roleassignments/roleassignments_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"sigs.k8s.io/cluster-api-provider-azure/cloud/services/virtualmachines/mock_virtualmachines"
3434
)
3535

36-
func TestReconcileVM(t *testing.T) {
36+
func TestReconcileRoleAssignments(t *testing.T) {
3737
testcases := []struct {
3838
name string
3939
expect func(s *mock_roleassignments.MockRoleAssignmentScopeMockRecorder, m *mock_roleassignments.MockClientMockRecorder, v *mock_virtualmachines.MockClientMockRecorder)

cloud/services/subnets/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
2222
)
2323

24-
// SubnetScope defines the scope interface for a network interfaces service.
24+
// SubnetScope defines the scope interface for a subnet service.
2525
type SubnetScope interface {
2626
azure.ClusterDescriber
2727
logr.Logger

cloud/services/tags/client.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tags
18+
19+
import (
20+
"context"
21+
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-10-01/resources"
22+
23+
"github.com/Azure/go-autorest/autorest"
24+
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
25+
)
26+
27+
// Client wraps go-sdk
28+
type Client interface {
29+
GetAtScope(context.Context, string) (resources.TagsResource, error)
30+
CreateOrUpdateAtScope(context.Context, string, resources.TagsResource) (resources.TagsResource, error)
31+
}
32+
33+
// AzureClient contains the Azure go-sdk Client
34+
type AzureClient struct {
35+
tags resources.TagsClient
36+
}
37+
38+
var _ Client = &AzureClient{}
39+
40+
// NewClient creates a new tags client from subscription ID.
41+
func NewClient(auth azure.Authorizer) *AzureClient {
42+
c := newTagsClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
43+
return &AzureClient{c}
44+
}
45+
46+
// newTagsClient creates a new tags client from subscription ID.
47+
func newTagsClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) resources.TagsClient {
48+
tagsClient := resources.NewTagsClientWithBaseURI(baseURI, subscriptionID)
49+
tagsClient.Authorizer = authorizer
50+
tagsClient.AddToUserAgent(azure.UserAgent())
51+
return tagsClient
52+
}
53+
54+
// GetAtScope sends the get at scope request.
55+
func (ac *AzureClient) GetAtScope(ctx context.Context, scope string) (resources.TagsResource, error) {
56+
return ac.tags.GetAtScope(ctx, scope)
57+
}
58+
59+
// CreateOrUpdate allows adding or replacing the entire set of tags on the specified resource or subscription.
60+
func (ac *AzureClient) CreateOrUpdateAtScope(ctx context.Context, scope string, parameters resources.TagsResource) (resources.TagsResource, error) {
61+
return ac.tags.CreateOrUpdateAtScope(ctx, scope, parameters)
62+
}

cloud/services/tags/mock_tags/client_mock.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Run go generate to regenerate this mock.
18+
//go:generate ../../../../hack/tools/bin/mockgen -destination client_mock.go -package mock_tags -source ../client.go Client
19+
//go:generate ../../../../hack/tools/bin/mockgen -destination tags_mock.go -package mock_tags -source ../service.go TagScope
20+
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt client_mock.go > _client_mock.go && mv _client_mock.go client_mock.go"
21+
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt tags_mock.go > _tags_mock.go && mv _tags_mock.go tags_mock.go"
22+
package mock_tags //nolint

0 commit comments

Comments
 (0)