Skip to content

Commit e4bcb89

Browse files
authored
Merge pull request #2309 from k8s-infra-cherrypick-robot/cherry-pick-2214-to-release-1.3
[release-1.3] Use MSI ClientID as userAssignedIdentityID in azure.json
2 parents f2715e1 + 5e2bd4e commit e4bcb89

15 files changed

+170
-10
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2022 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 identities
18+
19+
import (
20+
"context"
21+
22+
"github.com/Azure/azure-sdk-for-go/services/msi/mgmt/2018-11-30/msi"
23+
"github.com/Azure/go-autorest/autorest"
24+
azuresdk "github.com/Azure/go-autorest/autorest/azure"
25+
"sigs.k8s.io/cluster-api-provider-azure/azure"
26+
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
27+
)
28+
29+
// AzureClient contains the Azure go-sdk Client.
30+
type AzureClient struct {
31+
userAssignedIdentities msi.UserAssignedIdentitiesClient
32+
}
33+
34+
// NewClient creates a new MSI client from auth info.
35+
func NewClient(auth azure.Authorizer) *AzureClient {
36+
c := newUserAssignedIdentitiesClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
37+
return &AzureClient{c}
38+
}
39+
40+
// newUserAssignedIdentitiesClient creates a new MSI client from subscription ID, base URI, and authorizer.
41+
func newUserAssignedIdentitiesClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) msi.UserAssignedIdentitiesClient {
42+
userAssignedIdentitiesClient := msi.NewUserAssignedIdentitiesClientWithBaseURI(baseURI, subscriptionID)
43+
azure.SetAutoRestClientDefaults(&userAssignedIdentitiesClient.Client, authorizer)
44+
return userAssignedIdentitiesClient
45+
}
46+
47+
// Get returns a managed service identity.
48+
func (ac *AzureClient) Get(ctx context.Context, resourceGroupName, name string) (msi.Identity, error) {
49+
ctx, _, done := tele.StartSpanWithLogger(ctx, "identities.AzureClient.Get")
50+
defer done()
51+
52+
return ac.userAssignedIdentities.Get(ctx, resourceGroupName, name)
53+
}
54+
55+
// GetClientID returns the client ID of a managed service identity, given its full URL identifier.
56+
func (ac *AzureClient) GetClientID(ctx context.Context, providerID string) (string, error) {
57+
ctx, _, done := tele.StartSpanWithLogger(ctx, "identities.GetClientID")
58+
defer done()
59+
60+
parsed, err := azuresdk.ParseResourceID(providerID)
61+
if err != nil {
62+
return "", err
63+
}
64+
ident, err := ac.Get(ctx, parsed.ResourceGroup, parsed.ResourceName)
65+
if err != nil {
66+
return "", err
67+
}
68+
return ident.ClientID.String(), nil
69+
}

controllers/azurejson_machine_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/client-go/tools/record"
3131
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
3232
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
33+
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
3334
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
3435
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3536
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -184,7 +185,13 @@ func (r *AzureJSONMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
184185
// Construct secret for this machine
185186
userAssignedIdentityIfExists := ""
186187
if len(azureMachine.Spec.UserAssignedIdentities) > 0 {
187-
userAssignedIdentityIfExists = azureMachine.Spec.UserAssignedIdentities[0].ProviderID
188+
// TODO: remove this ClientID lookup code when the fixed cloud-provider-azure is default
189+
idsClient := identities.NewClient(clusterScope)
190+
userAssignedIdentityIfExists, err = idsClient.GetClientID(
191+
ctx, azureMachine.Spec.UserAssignedIdentities[0].ProviderID)
192+
if err != nil {
193+
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
194+
}
188195
}
189196

190197
if azureMachine.Spec.Identity == infrav1.VMIdentityNone {

controllers/azurejson_machinepool_controller.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/client-go/tools/record"
3030
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
3131
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
32+
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
3233
expv1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
3334
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
3435
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
@@ -131,12 +132,6 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl
131132
return reconcile.Result{}, err
132133
}
133134

134-
// Construct secret for this machine
135-
userAssignedIdentityIfExists := ""
136-
if len(azureMachinePool.Spec.UserAssignedIdentities) > 0 {
137-
userAssignedIdentityIfExists = azureMachinePool.Spec.UserAssignedIdentities[0].ProviderID
138-
}
139-
140135
// Create the scope.
141136
clusterScope, err := scope.NewClusterScope(ctx, scope.ClusterScopeParams{
142137
Client: r.Client,
@@ -147,6 +142,18 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl
147142
return reconcile.Result{}, errors.Wrap(err, "failed to create scope")
148143
}
149144

145+
// Construct secret for this machine
146+
userAssignedIdentityIfExists := ""
147+
if len(azureMachinePool.Spec.UserAssignedIdentities) > 0 {
148+
// TODO: remove this ClientID lookup code when the fixed cloud-provider-azure is default
149+
idsClient := identities.NewClient(clusterScope)
150+
userAssignedIdentityIfExists, err = idsClient.GetClientID(
151+
ctx, azureMachinePool.Spec.UserAssignedIdentities[0].ProviderID)
152+
if err != nil {
153+
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
154+
}
155+
}
156+
150157
apiVersion, kind := infrav1.GroupVersion.WithKind("AzureMachinePool").ToAPIVersionAndKind()
151158
owner := metav1.OwnerReference{
152159
APIVersion: apiVersion,

controllers/azurejson_machinetemplate_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/client-go/tools/record"
3030
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
3131
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
32+
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
3233
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
3334
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3435
"sigs.k8s.io/cluster-api/util"
@@ -143,7 +144,13 @@ func (r *AzureJSONTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Re
143144
// Construct secret for this machine template
144145
userAssignedIdentityIfExists := ""
145146
if len(azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities) > 0 {
146-
userAssignedIdentityIfExists = azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID
147+
// TODO: remove this ClientID lookup code when the fixed cloud-provider-azure is default
148+
idsClient := identities.NewClient(clusterScope)
149+
userAssignedIdentityIfExists, err = idsClient.GetClientID(
150+
ctx, azureMachineTemplate.Spec.Template.Spec.UserAssignedIdentities[0].ProviderID)
151+
if err != nil {
152+
return reconcile.Result{}, errors.Wrap(err, "failed to get user-assigned identity ClientID")
153+
}
147154
}
148155

149156
if azureMachineTemplate.Spec.Template.Spec.Identity == infrav1.VMIdentityNone {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ require (
9797
github.com/go-openapi/swag v0.19.14 // indirect
9898
github.com/gobuffalo/flect v0.2.4 // indirect
9999
github.com/gobwas/glob v0.2.3 // indirect
100+
github.com/gofrs/uuid v4.2.0+incompatible // indirect
100101
github.com/gogo/protobuf v1.3.2 // indirect
101102
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
102103
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5x
536536
github.com/godror/godror v0.24.2/go.mod h1:wZv/9vPiUib6tkoDl+AZ/QLf5YZgMravZ7jxH2eQWAE=
537537
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
538538
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
539+
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
540+
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
539541
github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU=
540542
github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c=
541543
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=

templates/test/ci/cluster-template-prow-dual-stack.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/test/ci/cluster-template-prow-external-cloud-provider.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/test/ci/prow-dual-stack/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ patchesStrategicMerge:
88
- ../patches/tags.yaml
99
- ../patches/controller-manager.yaml
1010
- ../patches/cluster-cni.yaml
11+
- patches/azure-machine-template-control-plane.yaml
12+
- patches/azure-machine-template.yaml
1113
configMapGenerator:
1214
- name: cni-${CLUSTER_NAME}-calico
1315
files:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
2+
kind: AzureMachineTemplate
3+
metadata:
4+
name: ${CLUSTER_NAME}-control-plane
5+
namespace: default
6+
spec:
7+
template:
8+
spec:
9+
identity: UserAssigned
10+
userAssignedIdentities:
11+
- providerID: /subscriptions/${AZURE_SUBSCRIPTION_ID}/resourceGroups/${CI_RG:=capz-ci}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/${USER_IDENTITY:=cloud-provider-user-identity}

0 commit comments

Comments
 (0)