Skip to content

Commit

Permalink
persist AKS kubelet user-assigned identity in spec
Browse files Browse the repository at this point in the history
  • Loading branch information
nojnhuh committed Jul 6, 2023
1 parent 9f211f0 commit 9098367
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
7 changes: 7 additions & 0 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,13 @@ func (s *ManagedControlPlaneScope) SetKubeConfigData(kubeConfigData []byte) {
s.kubeConfigData = kubeConfigData
}

// SetKubeletIdentity sets the ID of the user-assigned identity for kubelet if not already set.
func (s *ManagedControlPlaneScope) SetKubeletIdentity(id string) {
if s.ControlPlane.Spec.KubeletUserAssignedIdentity == "" {
s.ControlPlane.Spec.KubeletUserAssignedIdentity = id
}

Check warning on line 632 in azure/scope/managedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

azure/scope/managedcontrolplane.go#L629-L632

Added lines #L629 - L632 were not covered by tests
}

// SetLongRunningOperationState will set the future on the AzureManagedControlPlane status to allow the resource to continue
// in the next reconciliation.
func (s *ManagedControlPlaneScope) SetLongRunningOperationState(future *infrav1.Future) {
Expand Down
9 changes: 9 additions & 0 deletions azure/services/managedclusters/managedclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ import (

const serviceName = "managedcluster"

const kubeletIdentityKey = "kubeletidentity"

// ManagedClusterScope defines the scope interface for a managed cluster.
type ManagedClusterScope interface {
azure.Authorizer
azure.AsyncStatusUpdater
ManagedClusterSpec() azure.ResourceSpecGetter
SetControlPlaneEndpoint(clusterv1.APIEndpoint)
SetKubeletIdentity(string)
MakeEmptyKubeConfigSecret() corev1.Secret
GetKubeConfigData() []byte
SetKubeConfigData([]byte)
Expand Down Expand Up @@ -99,6 +102,12 @@ func (s *Service) Reconcile(ctx context.Context) error {
return errors.Wrap(err, "failed to get credentials for managed cluster")
}
s.Scope.SetKubeConfigData(kubeConfigData)

// This field gets populated by AKS when not set by the user. Persist AKS's value so for future diffs,
// the "before" reflects the correct value.
if id := managedCluster.ManagedClusterProperties.IdentityProfile[kubeletIdentityKey]; id != nil && id.ResourceID != nil {
s.Scope.SetKubeletIdentity(*id.ResourceID)
}
}
s.Scope.UpdatePutStatus(infrav1.ManagedClusterRunningCondition, serviceName, resultErr)
return resultErr
Expand Down
6 changes: 6 additions & 0 deletions azure/services/managedclusters/managedclusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestReconcile(t *testing.T) {
ManagedClusterProperties: &containerservice.ManagedClusterProperties{
Fqdn: pointer.String("my-managedcluster-fqdn"),
ProvisioningState: pointer.String("Succeeded"),
IdentityProfile: map[string]*containerservice.UserAssignedIdentity{
kubeletIdentityKey: {
ResourceID: pointer.String("kubelet-id"),
},
},
},
}, nil)
s.SetControlPlaneEndpoint(clusterv1.APIEndpoint{
Expand All @@ -73,6 +78,7 @@ func TestReconcile(t *testing.T) {
})
m.GetCredentials(gomockinternal.AContext(), "my-rg", "my-managedcluster").Return([]byte("credentials"), nil)
s.SetKubeConfigData([]byte("credentials"))
s.SetKubeletIdentity("kubelet-id")
s.UpdatePutStatus(infrav1.ManagedClusterRunningCondition, serviceName, nil)
},
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions azure/services/managedclusters/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing interface{

if s.KubeletUserAssignedIdentity != "" {
managedCluster.ManagedClusterProperties.IdentityProfile = map[string]*containerservice.UserAssignedIdentity{
"kubeletidentity": {
kubeletIdentityKey: {
ResourceID: pointer.String(s.KubeletUserAssignedIdentity),
},
}
Expand Down Expand Up @@ -590,16 +590,16 @@ func computeDiffOfNormalizedClusters(managedCluster containerservice.ManagedClus

if managedCluster.IdentityProfile != nil {
propertiesNormalized.IdentityProfile = map[string]*containerservice.UserAssignedIdentity{
"kubeletidentity": {
ResourceID: managedCluster.IdentityProfile["kubeletidentity"].ResourceID,
kubeletIdentityKey: {
ResourceID: managedCluster.IdentityProfile[kubeletIdentityKey].ResourceID,
},
}
}

if existingMC.IdentityProfile != nil {
existingMCPropertiesNormalized.IdentityProfile = map[string]*containerservice.UserAssignedIdentity{
"kubeletidentity": {
ResourceID: existingMC.IdentityProfile["kubeletidentity"].ResourceID,
kubeletIdentityKey: {
ResourceID: existingMC.IdentityProfile[kubeletIdentityKey].ResourceID,

Check warning on line 602 in azure/services/managedclusters/spec.go

View check run for this annotation

Codecov / codecov/patch

azure/services/managedclusters/spec.go#L601-L602

Added lines #L601 - L602 were not covered by tests
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion azure/services/managedclusters/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestParameters(t *testing.T) {
g.Expect(result.(containerservice.ManagedCluster).KubernetesVersion).To(Equal(pointer.String("v1.22.99")))
g.Expect(result.(containerservice.ManagedCluster).Identity.Type).To(Equal(containerservice.ResourceIdentityType("UserAssigned")))
g.Expect(result.(containerservice.ManagedCluster).Identity.UserAssignedIdentities).To(Equal(map[string]*containerservice.ManagedClusterIdentityUserAssignedIdentitiesValue{"/resource/ID": {}}))
g.Expect(result.(containerservice.ManagedCluster).IdentityProfile).To(Equal(map[string]*containerservice.UserAssignedIdentity{"kubeletidentity": {ResourceID: pointer.String("/resource/ID")}}))
g.Expect(result.(containerservice.ManagedCluster).IdentityProfile).To(Equal(map[string]*containerservice.UserAssignedIdentity{kubeletIdentityKey: {ResourceID: pointer.String("/resource/ID")}}))
},
},
{
Expand Down

0 comments on commit 9098367

Please sign in to comment.