Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update fetchPreviousSlices func #300

Merged
merged 3 commits into from
Mar 13, 2025
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
8 changes: 6 additions & 2 deletions internal/execution/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,18 @@ func (e *Executor) writeSlices(ctx context.Context, comp *apiv1.Composition, rl
return sliceRefs, nil
}

// fetchPreviousSlices retrieves the previous slices from the composition's current synthesis status.
// This function runs before the updateComposition function, which will later swap the current synthesis
// to become the previous synthesis. Therefore, the resourceslice retrieved from the current synthesis is
// actually the "previous" resource slices after the update is complete.
func (e *Executor) fetchPreviousSlices(ctx context.Context, comp *apiv1.Composition) ([]*apiv1.ResourceSlice, error) {
if comp.Status.PreviousSynthesis == nil {
if comp.Status.CurrentSynthesis == nil {
return nil, nil // nothing to fetch
}
logger := logr.FromContextOrDiscard(ctx)

slices := []*apiv1.ResourceSlice{}
for _, ref := range comp.Status.PreviousSynthesis.ResourceSlices {
for _, ref := range comp.Status.CurrentSynthesis.ResourceSlices {
slice := &apiv1.ResourceSlice{}
slice.Name = ref.Name
slice.Namespace = comp.Namespace
Expand Down
140 changes: 130 additions & 10 deletions internal/execution/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package execution

import (
"context"
"slices"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -53,11 +55,11 @@ func TestBasics(t *testing.T) {
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]string{
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]string{"foo": "bar"},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{
Expand Down Expand Up @@ -164,11 +166,11 @@ func TestWithInputs(t *testing.T) {
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]string{
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]string{"foo": "bar"},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{Items: []*unstructured.Unstructured{out}}, nil
Expand Down Expand Up @@ -241,11 +243,11 @@ func TestWithVersionedInput(t *testing.T) {
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]string{
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]string{"foo": "bar"},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{Items: []*unstructured.Unstructured{out}}, nil
Expand Down Expand Up @@ -300,11 +302,11 @@ func TestUUIDMismatch(t *testing.T) {
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]string{
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]string{"foo": "bar"},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{
Expand Down Expand Up @@ -381,11 +383,11 @@ func TestCompletionMismatchDuringSynthesis(t *testing.T) {
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]string{
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]string{"foo": "bar"},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{
Expand All @@ -402,3 +404,121 @@ func TestCompletionMismatchDuringSynthesis(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, originalSynthTime, *comp.Status.InFlightSynthesis.Synthesized)
}

// TestDeleteResource verifies any resources that were previously created
// but are no longer included in the executor's output will be deleted.
func TestDeleteResource(t *testing.T) {
ctx := context.Background()
scheme := runtime.NewScheme()
require.NoError(t, apiv1.SchemeBuilder.AddToScheme(scheme))

cli := fake.NewClientBuilder().
WithScheme(scheme).
WithStatusSubresource(&apiv1.ResourceSlice{}, &apiv1.Composition{}).
Build()

syn := &apiv1.Synthesizer{}
syn.Name = "test-synth"
err := cli.Create(ctx, syn)
require.NoError(t, err)

comp := &apiv1.Composition{}
comp.Name = "test-comp"
comp.Namespace = "default"
comp.Spec.Synthesizer.Name = syn.Name
err = cli.Create(ctx, comp)
require.NoError(t, err)

comp.Status.InFlightSynthesis = &apiv1.Synthesis{UUID: "test-uuid"}
err = cli.Status().Update(ctx, comp)
require.NoError(t, err)

env := &Env{
CompositionName: comp.Name,
CompositionNamespace: comp.Namespace,
SynthesisUUID: comp.Status.InFlightSynthesis.UUID,
}
e := &Executor{
Reader: cli,
Writer: cli,
Handler: func(ctx context.Context, s *apiv1.Synthesizer, rl *krmv1.ResourceList) (*krmv1.ResourceList, error) {
out := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]any{"foo": "bar"},
},
}
out2 := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]any{
"name": "test2",
"namespace": "default",
},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{
Items: []*unstructured.Unstructured{out, out2},
Results: []*krmv1.Result{{Message: "foo", Severity: "error"}},
}, nil
},
}

err = e.Synthesize(ctx, env)
require.NoError(t, err)

// remove test2 configmap
e.Handler = func(ctx context.Context, s *apiv1.Synthesizer, rl *krmv1.ResourceList) (*krmv1.ResourceList, error) {
out := &unstructured.Unstructured{
Object: map[string]any{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]any{
"name": "test",
"namespace": "default",
},
"data": map[string]any{"foo": "bar"},
},
}
return &krmv1.ResourceList{
Items: []*unstructured.Unstructured{out},
Results: []*krmv1.Result{{Message: "foo", Severity: "error"}},
}, nil
}

// Set InFlightSynthesis for the second synthesis
err = cli.Get(ctx, client.ObjectKeyFromObject(comp), comp)
require.NoError(t, err)
comp.Status.InFlightSynthesis = &apiv1.Synthesis{UUID: "test-uuid-2"}
err = cli.Status().Update(ctx, comp)
require.NoError(t, err)

// Resynthesize with only one configmap in output.
env.SynthesisUUID = comp.Status.InFlightSynthesis.UUID
err = e.Synthesize(ctx, env)
require.NoError(t, err)

// Get current synthesis's resource slice
err = cli.Get(ctx, client.ObjectKeyFromObject(comp), comp)
assert.NoError(t, err)
rs := &apiv1.ResourceSlice{}
rs.Name = comp.Status.CurrentSynthesis.ResourceSlices[0].Name
rs.Namespace = comp.Namespace
err = cli.Get(ctx, client.ObjectKeyFromObject(rs), rs)
assert.NoError(t, err)

// Check resource slice and should find resource removed from output is marked as deleted
deletedIdx := slices.IndexFunc(rs.Spec.Resources, func(resource apiv1.Manifest) bool {
return strings.Contains(resource.Manifest, "test2")
})

assert.NotEqual(t, -1, deletedIdx)
assert.Equal(t, rs.Spec.Resources[deletedIdx].Deleted, true)
}
Loading