Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions azure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,7 @@ type ASOResourceSpecGetter interface {
// Parameters returns a modified object if it points to a non-nil resource.
// Otherwise it returns a new value or nil if no updates are needed.
Parameters(ctx context.Context, object genruntime.MetaObject) (genruntime.MetaObject, error)
// WasManaged returns whether or not the given resource was managed by a
// non-ASO-backed CAPZ and should be considered eligible for adoption.
WasManaged(genruntime.MetaObject) bool
}
14 changes: 14 additions & 0 deletions azure/mock_azure/azure_mock.go

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

2 changes: 2 additions & 0 deletions azure/services/aso/aso.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func (s *Service) CreateOrUpdateResource(ctx context.Context, spec azure.ASOReso
// Azure and the ASO resource will be adopted by changing this
// annotation to "manage".
annotations[ReconcilePolicyAnnotation] = ReconcilePolicySkip
} else {
adopt = adopt || spec.WasManaged(existing)
}
if adopt {
annotations[ReconcilePolicyAnnotation] = ReconcilePolicyManage
Expand Down
60 changes: 60 additions & 0 deletions azure/services/aso/aso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func TestCreateOrUpdateResource(t *testing.T) {
group.Spec.Location = pointer.String("location")
return group, nil
})
specMock.EXPECT().WasManaged(gomock.Any()).Return(false)

ctx := context.Background()
g.Expect(c.Create(ctx, &asoresourcesv1.ResourceGroup{
Expand Down Expand Up @@ -426,6 +427,63 @@ func TestCreateOrUpdateResource(t *testing.T) {
}))
})

t.Run("adopt previously managed resource", func(t *testing.T) {
g := NewGomegaWithT(t)

sch := runtime.NewScheme()
g.Expect(asoresourcesv1.AddToScheme(sch)).To(Succeed())
c := fakeclient.NewClientBuilder().
WithScheme(sch).
Build()
clusterName := "cluster"
s := New(c, clusterName)

mockCtrl := gomock.NewController(t)
specMock := mock_azure.NewMockASOResourceSpecGetter(mockCtrl)
specMock.EXPECT().ResourceRef().Return(&asoresourcesv1.ResourceGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
})
specMock.EXPECT().Parameters(gomockinternal.AContext(), gomock.Not(gomock.Nil())).DoAndReturn(func(_ context.Context, object genruntime.MetaObject) (genruntime.MetaObject, error) {
return nil, nil
})
specMock.EXPECT().WasManaged(gomock.Any()).Return(true)

ctx := context.Background()
g.Expect(c.Create(ctx, &asoresourcesv1.ResourceGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
Labels: map[string]string{
infrav1.OwnedByClusterLabelKey: clusterName,
},
Annotations: map[string]string{
ReconcilePolicyAnnotation: ReconcilePolicySkip,
},
},
Status: asoresourcesv1.ResourceGroup_STATUS{
Conditions: []conditions.Condition{
{
Type: conditions.ConditionTypeReady,
Status: metav1.ConditionTrue,
},
},
},
})).To(Succeed())

result, err := s.CreateOrUpdateResource(ctx, specMock, "service")
g.Expect(result).To(BeNil())
g.Expect(err).NotTo(BeNil())

updated := &asoresourcesv1.ResourceGroup{}
g.Expect(c.Get(ctx, types.NamespacedName{Name: "name", Namespace: "namespace"}, updated)).To(Succeed())
g.Expect(updated.Annotations).To(Equal(map[string]string{
ReconcilePolicyAnnotation: ReconcilePolicyManage,
}))
})

t.Run("Parameters error", func(t *testing.T) {
g := NewGomegaWithT(t)

Expand Down Expand Up @@ -532,6 +590,7 @@ func TestCreateOrUpdateResource(t *testing.T) {
specMock.EXPECT().Parameters(gomockinternal.AContext(), gomock.Any()).DoAndReturn(func(_ context.Context, object genruntime.MetaObject) (genruntime.MetaObject, error) {
return nil, nil
})
specMock.EXPECT().WasManaged(gomock.Any()).Return(false)

ctx := context.Background()
g.Expect(c.Create(ctx, &asoresourcesv1.ResourceGroup{
Expand Down Expand Up @@ -590,6 +649,7 @@ func TestCreateOrUpdateResource(t *testing.T) {
group.Spec.Location = pointer.String("location")
return group, nil
})
specMock.EXPECT().WasManaged(gomock.Any()).Return(false)

ctx := context.Background()
g.Expect(c.Create(ctx, &asoresourcesv1.ResourceGroup{
Expand Down
9 changes: 9 additions & 0 deletions azure/services/asogroups/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ func (s *GroupSpec) Parameters(ctx context.Context, object genruntime.MetaObject
},
}, nil
}

// WasManaged implements azure.ASOResourceSpecGetter.
func (s *GroupSpec) WasManaged(object genruntime.MetaObject) bool {
group, ok := object.(*asoresourcesv1.ResourceGroup)
if !ok {
return false
}
return infrav1.Tags(group.Status.Tags).HasOwned(s.ClusterName)
}
59 changes: 59 additions & 0 deletions azure/services/asogroups/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,62 @@ func TestParameters(t *testing.T) {
})
}
}

func TestWasManaged(t *testing.T) {
clusterName := "cluster"

tests := []struct {
name string
object genruntime.MetaObject
expected bool
}{
{
name: "wrong type",
object: struct {
genruntime.MetaObject
}{},
expected: false,
},
{
name: "no owned label",
object: &asoresourcesv1.ResourceGroup{},
expected: false,
},
{
name: "wrong owned label value",
object: &asoresourcesv1.ResourceGroup{
Status: asoresourcesv1.ResourceGroup_STATUS{
Tags: infrav1.Build(infrav1.BuildParams{
ClusterName: clusterName,
Lifecycle: infrav1.ResourceLifecycle("not owned"),
}),
},
},
expected: false,
},
{
name: "with owned label",
object: &asoresourcesv1.ResourceGroup{
Status: asoresourcesv1.ResourceGroup_STATUS{
Tags: infrav1.Build(infrav1.BuildParams{
ClusterName: clusterName,
Lifecycle: infrav1.ResourceLifecycleOwned,
}),
},
},
expected: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)

s := &GroupSpec{
ClusterName: clusterName,
}

g.Expect(s.WasManaged(test.object)).To(Equal(test.expected))
})
}
}