Skip to content

Commit

Permalink
use tags service for managedcluster tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nojnhuh committed Oct 25, 2023
1 parent 0b1ee54 commit acd37a9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
12 changes: 12 additions & 0 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,18 @@ func (s *ManagedControlPlaneScope) SetAnnotation(key, value string) {
s.ControlPlane.Annotations[key] = value
}

// TagsSpecs returns the tag specs for the ManagedControlPlane.
func (s *ManagedControlPlaneScope) TagsSpecs() []azure.TagsSpec {
specs := []azure.TagsSpec{
{
Scope: azure.ManagedClusterID(s.SubscriptionID(), s.ResourceGroup(), s.ManagedClusterSpec().ResourceRef().Name),

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

View check run for this annotation

Codecov / codecov/patch

azure/scope/managedcontrolplane.go#L771

Added line #L771 was not covered by tests
Tags: s.AdditionalTags(),
Annotation: azure.ManagedClusterTagsLastAppliedAnnotation,
},
}
return specs
}

// AvailabilityStatusResource refers to the AzureManagedControlPlane.
func (s *ManagedControlPlaneScope) AvailabilityStatusResource() conditions.Setter {
return s.ControlPlane
Expand Down
30 changes: 6 additions & 24 deletions azure/services/managedclusters/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/agentpools"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/aso"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
"sigs.k8s.io/cluster-api/util/secret"
)
Expand Down Expand Up @@ -308,7 +307,7 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing *asocontai
ClusterName: s.ClusterName,
Name: ptr.To(s.Name),
Role: ptr.To(infrav1.CommonRole),
// Additional tags managed separately
Additional: s.Tags,
}),
},
}
Expand Down Expand Up @@ -513,6 +512,11 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing *asocontai
}
}

if managedCluster.Status.Tags != nil {
// tags managed separately
spec.Tags = nil
}

return managedCluster, nil
}

Expand Down Expand Up @@ -588,25 +592,3 @@ func (s *ManagedClusterSpec) WasManaged(resource *asocontainerservicev1.ManagedC
// CAPZ has never supported BYO managed clusters.
return true

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

View check run for this annotation

Codecov / codecov/patch

azure/services/managedclusters/spec.go#L591-L593

Added lines #L591 - L593 were not covered by tests
}

var _ aso.TagsGetterSetter[*asocontainerservicev1.ManagedCluster] = (*ManagedClusterSpec)(nil)

// GetAdditionalTags implements aso.TagsGetterSetter.
func (s *ManagedClusterSpec) GetAdditionalTags() infrav1.Tags {
return s.Tags
}

// GetDesiredTags implements aso.TagsGetterSetter.
func (*ManagedClusterSpec) GetDesiredTags(resource *asocontainerservicev1.ManagedCluster) infrav1.Tags {
return resource.Spec.Tags
}

// GetActualTags implements aso.TagsGetterSetter.
func (*ManagedClusterSpec) GetActualTags(resource *asocontainerservicev1.ManagedCluster) infrav1.Tags {
return resource.Status.Tags
}

// SetTags implements aso.TagsGetterSetter.
func (*ManagedClusterSpec) SetTags(resource *asocontainerservicev1.ManagedCluster, tags infrav1.Tags) {
resource.Spec.Tags = tags
}
4 changes: 4 additions & 0 deletions azure/services/managedclusters/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ func getExistingCluster() *asocontainerservicev1.ManagedCluster {
// only nil vs. non-nil matters here
mc.Status.AgentPoolProfiles = []asocontainerservicev1.ManagedClusterAgentPoolProfile_STATUS{}

// tags managed separately
mc.Spec.Tags = nil
mc.Status.Tags = map[string]string{}

return mc
}

Expand Down
6 changes: 6 additions & 0 deletions controllers/azuremanagedcontrolplane_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/privateendpoints"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourcehealth"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/subnets"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualnetworks"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
"sigs.k8s.io/cluster-api/util/secret"
Expand Down Expand Up @@ -56,6 +57,10 @@ func newAzureManagedControlPlaneReconciler(scope *scope.ManagedControlPlaneScope
if err != nil {
return nil, err
}
tagsSvc, err := tags.New(scope)
if err != nil {
return nil, err
}
virtualNetworksSvc, err := virtualnetworks.New(scope)
if err != nil {
return nil, err
Expand All @@ -69,6 +74,7 @@ func newAzureManagedControlPlaneReconciler(scope *scope.ManagedControlPlaneScope
subnetsSvc,
managedclusters.New(scope),
privateEndpointsSvc,
tagsSvc,
resourceHealthSvc,
},
}, nil
Expand Down
23 changes: 13 additions & 10 deletions test/e2e/aks_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ func AKSAdditionalTagsSpec(ctx context.Context, inputGetter func() AKSAdditional
}
}

By("Deleting all tags for control plane")
expectedTags = nil
var initialTags infrav1.Tags
Eventually(func(g Gomega) {
g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(infraControlPlane), infraControlPlane)).To(Succeed())
initialTags = infraControlPlane.Spec.AdditionalTags
infraControlPlane.Spec.AdditionalTags = expectedTags
g.Expect(mgmtClient.Update(ctx, infraControlPlane)).To(Succeed())
}, inputGetter().WaitForUpdate...).Should(Succeed())
Eventually(checkTags, input.WaitForUpdate...).Should(Succeed())

By("Creating tags for control plane")
expectedTags = infrav1.Tags{
Expand All @@ -127,16 +132,14 @@ func AKSAdditionalTagsSpec(ctx context.Context, inputGetter func() AKSAdditional
}, inputGetter().WaitForUpdate...).Should(Succeed())
Eventually(checkTags, input.WaitForUpdate...).Should(Succeed())

if initialTags != nil {
By("Restoring initial tags for control plane")
expectedTags = initialTags
Eventually(func(g Gomega) {
g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(infraControlPlane), infraControlPlane)).To(Succeed())
infraControlPlane.Spec.AdditionalTags = expectedTags
g.Expect(mgmtClient.Update(ctx, infraControlPlane)).To(Succeed())
}, inputGetter().WaitForUpdate...).Should(Succeed())
Eventually(checkTags, input.WaitForUpdate...).Should(Succeed())
}
By("Restoring initial tags for control plane")
expectedTags = initialTags
Eventually(func(g Gomega) {
g.Expect(mgmtClient.Get(ctx, client.ObjectKeyFromObject(infraControlPlane), infraControlPlane)).To(Succeed())
infraControlPlane.Spec.AdditionalTags = expectedTags
g.Expect(mgmtClient.Update(ctx, infraControlPlane)).To(Succeed())
}, inputGetter().WaitForUpdate...).Should(Succeed())
Eventually(checkTags, input.WaitForUpdate...).Should(Succeed())
}()

for _, mp := range input.MachinePools {
Expand Down

0 comments on commit acd37a9

Please sign in to comment.