Skip to content

Commit e47861b

Browse files
committed
fix compute domain controller deletion
1 parent 3669f34 commit e47861b

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

internal/compute-domain-controller/computedomain_controller.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ const (
3838
DefaultComputeDomainAllocationMode = "Single"
3939
)
4040

41+
// isOwnedBy checks if the ResourceClaimTemplate is owned by the given ComputeDomain
42+
func isOwnedBy(template *resourceapi.ResourceClaimTemplate, domain *computedomainv1beta1.ComputeDomain) bool {
43+
for _, owner := range template.OwnerReferences {
44+
if owner.Name == domain.Name && owner.Kind == "ComputeDomain" {
45+
return true
46+
}
47+
}
48+
return false
49+
}
50+
4151
// ComputeDomainReconciler watches ComputeDomain resources and keeps the
4252
// associated ResourceClaimTemplates in sync.
4353
type ComputeDomainReconciler struct {
@@ -135,7 +145,7 @@ func (r *ComputeDomainReconciler) ensureTemplate(
135145
"resource.nvidia.com/computeDomainTarget": templateType,
136146
},
137147
Finalizers: []string{
138-
"resource.nvidia.com/computeDomain",
148+
consts.ComputeDomainFinalizer,
139149
},
140150
},
141151
Spec: resourceapi.ResourceClaimTemplateSpec{
@@ -186,12 +196,28 @@ func (r *ComputeDomainReconciler) ensureTemplate(
186196
}
187197

188198
func (r *ComputeDomainReconciler) deleteResourceClaimTemplates(ctx context.Context, domain *computedomainv1beta1.ComputeDomain) error {
189-
template := &resourceapi.ResourceClaimTemplate{
190-
ObjectMeta: metav1.ObjectMeta{
191-
Name: domain.Name,
192-
Namespace: domain.Namespace,
193-
},
199+
template := &resourceapi.ResourceClaimTemplate{}
200+
key := client.ObjectKey{Namespace: domain.Namespace, Name: domain.Name}
201+
if err := r.Get(ctx, key, template); err != nil {
202+
if apierrors.IsNotFound(err) {
203+
return nil
204+
}
205+
return err
206+
}
207+
208+
if !isOwnedBy(template, domain) {
209+
return nil
194210
}
211+
212+
// Remove our finalizer
213+
if controllerutil.ContainsFinalizer(template, consts.ComputeDomainFinalizer) {
214+
controllerutil.RemoveFinalizer(template, consts.ComputeDomainFinalizer)
215+
if err := r.Update(ctx, template); err != nil {
216+
return err
217+
}
218+
}
219+
220+
// Now delete the template
195221
if err := r.Delete(ctx, template); err != nil && !apierrors.IsNotFound(err) {
196222
return err
197223
}

internal/compute-domain-controller/computedomain_controller_test.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package computedomaincontroller_test
1919
import (
2020
"context"
2121
"testing"
22+
"time"
2223

2324
"github.com/stretchr/testify/assert"
2425
"github.com/stretchr/testify/require"
@@ -44,6 +45,7 @@ func TestComputeDomainReconciler_Reconcile(t *testing.T) {
4445

4546
tests := map[string]struct {
4647
computeDomain *computedomainv1beta1.ComputeDomain
48+
existingObjects []client.Object
4749
expectedWorkloadTemplate bool
4850
expectedFinalizer bool
4951
}{
@@ -58,12 +60,46 @@ func TestComputeDomainReconciler_Reconcile(t *testing.T) {
5860
expectedWorkloadTemplate: true,
5961
expectedFinalizer: true,
6062
},
63+
"deleted ComputeDomain removes templates": {
64+
computeDomain: &computedomainv1beta1.ComputeDomain{
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: "test-domain",
67+
Namespace: "default",
68+
UID: "test-uid",
69+
DeletionTimestamp: &metav1.Time{Time: time.Now()},
70+
Finalizers: []string{consts.ComputeDomainFinalizer},
71+
},
72+
},
73+
existingObjects: []client.Object{
74+
&resourceapi.ResourceClaimTemplate{
75+
ObjectMeta: metav1.ObjectMeta{
76+
Name: "test-domain",
77+
Namespace: "default",
78+
Labels: map[string]string{
79+
"resource.nvidia.com/computeDomain": "test-domain",
80+
},
81+
Finalizers: []string{consts.ComputeDomainFinalizer},
82+
OwnerReferences: []metav1.OwnerReference{
83+
{
84+
Kind: "ComputeDomain",
85+
Name: "test-domain",
86+
},
87+
},
88+
},
89+
},
90+
},
91+
expectedWorkloadTemplate: false,
92+
expectedFinalizer: false,
93+
},
6194
}
6295

6396
for name, test := range tests {
6497
t.Run(name, func(t *testing.T) {
6598
// Setup
6699
objs := []client.Object{test.computeDomain}
100+
if len(test.existingObjects) > 0 {
101+
objs = append(objs, test.existingObjects...)
102+
}
67103

68104
fakeClient := fake.NewClientBuilder().
69105
WithScheme(scheme).
@@ -89,13 +125,13 @@ func TestComputeDomainReconciler_Reconcile(t *testing.T) {
89125
require.NoError(t, err)
90126
assert.Equal(t, ctrl.Result{}, result)
91127

128+
workloadTemplate := &resourceapi.ResourceClaimTemplate{}
129+
err = fakeClient.Get(context.Background(), types.NamespacedName{
130+
Name: "test-domain",
131+
Namespace: "default",
132+
}, workloadTemplate)
92133
// Check ResourceClaimTemplates
93134
if test.expectedWorkloadTemplate {
94-
workloadTemplate := &resourceapi.ResourceClaimTemplate{}
95-
err := fakeClient.Get(context.Background(), types.NamespacedName{
96-
Name: "test-domain",
97-
Namespace: "default",
98-
}, workloadTemplate)
99135
assert.NoError(t, err)
100136
// Check config section
101137
assert.Len(t, workloadTemplate.Spec.Spec.Devices.Config, 1)
@@ -112,7 +148,14 @@ func TestComputeDomainReconciler_Reconcile(t *testing.T) {
112148
// Check labels copied into generated claims
113149
assert.Equal(t, test.computeDomain.GetName(), workloadTemplate.Spec.Labels["nvidia.com/computeDomain"])
114150
// Check finalizers
115-
assert.Contains(t, workloadTemplate.Finalizers, "resource.nvidia.com/computeDomain")
151+
assert.Contains(t, workloadTemplate.Finalizers, consts.ComputeDomainFinalizer)
152+
} else {
153+
assert.Error(t, err)
154+
assert.NoError(t, client.IgnoreNotFound(err))
155+
}
156+
157+
if !test.computeDomain.DeletionTimestamp.IsZero() {
158+
return
116159
}
117160

118161
// Check finalizer

0 commit comments

Comments
 (0)