-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix composition deletion when a resource is missing namespace (#180)
If a resource returned by a synthesizer doesn't have a namespace, getting its current state will fail, which prevents it from being marked as deleted, which blocks composition deletion. Co-authored-by: Jordan Olshevski <[email protected]>
- Loading branch information
Showing
2 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package reconciliation | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/util/retry" | ||
"k8s.io/utils/ptr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
apiv1 "github.com/Azure/eno/api/v1" | ||
"github.com/Azure/eno/internal/testutil" | ||
krmv1 "github.com/Azure/eno/pkg/krm/functions/api/v1" | ||
) | ||
|
||
// TestMissingNamespace proves that resynthesis is not blocked by resources that lack a namespace. | ||
func TestMissingNamespace(t *testing.T) { | ||
ctx := testutil.NewContext(t) | ||
mgr := testutil.NewManager(t) | ||
upstream := mgr.GetClient() | ||
|
||
registerControllers(t, mgr) | ||
namespace := atomic.Pointer[string]{} | ||
testutil.WithFakeExecutor(t, mgr, func(ctx context.Context, s *apiv1.Synthesizer, input *krmv1.ResourceList) (*krmv1.ResourceList, error) { | ||
output := &krmv1.ResourceList{} | ||
output.Items = []*unstructured.Unstructured{{ | ||
Object: map[string]any{ | ||
"apiVersion": "v1", | ||
"kind": "ConfigMap", | ||
"metadata": map[string]any{ | ||
"name": "test-obj", | ||
"namespace": namespace.Load(), // bad news | ||
}, | ||
}, | ||
}} | ||
return output, nil | ||
}) | ||
|
||
// Test subject | ||
setupTestSubject(t, mgr) | ||
mgr.Start(t) | ||
syn, comp := writeGenericComposition(t, upstream) | ||
|
||
// Initial synthesis | ||
testutil.Eventually(t, func() bool { | ||
err := upstream.Get(ctx, client.ObjectKeyFromObject(comp), comp) | ||
return err == nil && comp.Status.CurrentSynthesis != nil && comp.Status.CurrentSynthesis.Synthesized != nil && comp.Status.CurrentSynthesis.ObservedSynthesizerGeneration == syn.Generation | ||
}) | ||
|
||
// Fixing the namespace should be possible | ||
namespace.Store(ptr.To("default")) | ||
err := retry.RetryOnConflict(testutil.Backoff, func() error { | ||
upstream.Get(ctx, client.ObjectKeyFromObject(syn), syn) | ||
syn.Spec.Image = "updated" | ||
return upstream.Update(ctx, syn) | ||
}) | ||
require.NoError(t, err) | ||
|
||
testutil.Eventually(t, func() bool { | ||
err := upstream.Get(ctx, client.ObjectKeyFromObject(comp), comp) | ||
return err == nil && comp.Status.CurrentSynthesis != nil && comp.Status.CurrentSynthesis.Reconciled != nil && comp.Status.CurrentSynthesis.ObservedSynthesizerGeneration == syn.Generation | ||
}) | ||
} | ||
|
||
// TestMissingNamespaceDeletion proves that composition deletion is not blocked by resources that lack a namespace. | ||
func TestMissingNamespaceDeletion(t *testing.T) { | ||
ctx := testutil.NewContext(t) | ||
mgr := testutil.NewManager(t) | ||
upstream := mgr.GetClient() | ||
|
||
registerControllers(t, mgr) | ||
testutil.WithFakeExecutor(t, mgr, func(ctx context.Context, s *apiv1.Synthesizer, input *krmv1.ResourceList) (*krmv1.ResourceList, error) { | ||
output := &krmv1.ResourceList{} | ||
output.Items = []*unstructured.Unstructured{{ | ||
Object: map[string]any{ | ||
"apiVersion": "v1", | ||
"kind": "ConfigMap", | ||
"metadata": map[string]any{ | ||
"name": "test-obj", | ||
"namespace": "", // bad news | ||
}, | ||
}, | ||
}} | ||
return output, nil | ||
}) | ||
|
||
// Test subject | ||
setupTestSubject(t, mgr) | ||
mgr.Start(t) | ||
syn, comp := writeGenericComposition(t, upstream) | ||
|
||
// Initial synthesis | ||
testutil.Eventually(t, func() bool { | ||
err := upstream.Get(ctx, client.ObjectKeyFromObject(comp), comp) | ||
return err == nil && comp.Status.CurrentSynthesis != nil && comp.Status.CurrentSynthesis.Synthesized != nil && comp.Status.CurrentSynthesis.ObservedSynthesizerGeneration == syn.Generation | ||
}) | ||
|
||
// Deleting the composition should succeed eventually | ||
require.NoError(t, upstream.Delete(ctx, comp)) | ||
testutil.Eventually(t, func() bool { | ||
return errors.IsNotFound(upstream.Get(ctx, client.ObjectKeyFromObject(comp), comp)) | ||
}) | ||
} |