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

Allow extra bindings #161

Merged
merged 1 commit into from
Jul 16, 2024
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
13 changes: 12 additions & 1 deletion api/v1/composition.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,19 @@ func (s *Synthesis) Failed() bool {
return false
}

func (c *Composition) InputsExist() bool {
func (c *Composition) InputsExist(syn *Synthesizer) bool {
refs := map[string]struct{}{}
for _, ref := range syn.Spec.Refs {
refs[ref.Key] = struct{}{}
}

for _, binding := range c.Spec.Bindings {
if _, ok := refs[binding.Key]; !ok {
// Ignore missing resources if the synthesizer doesn't require them
// This is important for forwards compatibility- compositions can bind to refs that don't exist, but will in future synths
continue
}

var found bool
for _, rev := range c.Status.InputRevisions {
if binding.Key == rev.Key {
Expand Down
18 changes: 17 additions & 1 deletion api/v1/composition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ func TestCompositionInputsExist(t *testing.T) {
},
Expectation: true,
},
{
Name: "Bindings with matching revisions but missing ref",
Comp: Composition{
Spec: CompositionSpec{Bindings: []Binding{
{Key: "key1"},
{Key: "key5"},
}},
Status: CompositionStatus{InputRevisions: []InputRevisions{
{Key: "key1"},
{Key: "key5"},
}},
},
Expectation: true,
},
{
Name: "Bindings with non-matching revisions",
Comp: Composition{
Expand Down Expand Up @@ -243,7 +257,9 @@ func TestCompositionInputsExist(t *testing.T) {

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
result := tt.Comp.InputsExist()
s := &Synthesizer{}
s.Spec.Refs = []Ref{{Key: "key1"}, {Key: "key2"}, {Key: "key3"}}
result := tt.Comp.InputsExist(s)
assert.Equal(t, tt.Expectation, result)
})
}
Expand Down
19 changes: 11 additions & 8 deletions internal/controllers/aggregation/composition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
apiv1 "github.com/Azure/eno/api/v1"
"github.com/Azure/eno/internal/manager"
krmv1 "github.com/Azure/eno/pkg/krm/functions/api/v1"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -27,16 +27,19 @@ func NewCompositionController(mgr ctrl.Manager) error {
}

func (c *compositionController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := logr.FromContextOrDiscard(ctx)

comp := &apiv1.Composition{}
err := c.client.Get(ctx, req.NamespacedName, comp)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
logger = logger.WithValues("compositionName", comp.Name, "compositionNamespace", comp.Namespace)

next := c.aggregate(comp)
synth := &apiv1.Synthesizer{}
err = c.client.Get(ctx, types.NamespacedName{Name: comp.Spec.Synthesizer.Name}, synth)
if client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, err
}

next := c.aggregate(synth, comp)
if equality.Semantic.DeepEqual(next, comp.Status.Simplified) {
return ctrl.Result{}, nil
}
Expand All @@ -49,7 +52,7 @@ func (c *compositionController) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

func (c *compositionController) aggregate(comp *apiv1.Composition) *apiv1.SimplifiedStatus {
func (c *compositionController) aggregate(synth *apiv1.Synthesizer, comp *apiv1.Composition) *apiv1.SimplifiedStatus {
copy := comp.Status.Simplified.DeepCopy()
if copy == nil {
copy = &apiv1.SimplifiedStatus{}
Expand All @@ -62,7 +65,7 @@ func (c *compositionController) aggregate(comp *apiv1.Composition) *apiv1.Simpli

copy.Status = "PendingSynthesis"
copy.Error = ""
if !comp.InputsExist() {
if !comp.InputsExist(synth) {
copy.Status = "MissingInputs"
}
if comp.Status.CurrentSynthesis == nil {
Expand All @@ -82,7 +85,7 @@ func (c *compositionController) aggregate(comp *apiv1.Composition) *apiv1.Simpli
}

copy.Status = "Synthesizing"
if !comp.InputsExist() {
if !comp.InputsExist(synth) {
copy.Status = "MissingInputs"
}
if comp.Status.CurrentSynthesis.Synthesized != nil {
Expand Down
51 changes: 50 additions & 1 deletion internal/controllers/aggregation/composition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestCompositionSimplification(t *testing.T) {
tests := []struct {
Bindings []apiv1.Binding
Input apiv1.CompositionStatus
Synth apiv1.Synthesizer
Deleting bool
Expected apiv1.SimplifiedStatus
}{
Expand Down Expand Up @@ -58,17 +59,65 @@ func TestCompositionSimplification(t *testing.T) {
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{},
Synth: apiv1.Synthesizer{
Spec: apiv1.SynthesizerSpec{
Refs: []apiv1.Ref{{Key: "foo"}},
},
},
Expected: apiv1.SimplifiedStatus{
Status: "MissingInputs",
},
},
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{CurrentSynthesis: &apiv1.Synthesis{UUID: "uuid"}},
Synth: apiv1.Synthesizer{
Spec: apiv1.SynthesizerSpec{
Refs: []apiv1.Ref{{Key: "foo"}},
},
},
Expected: apiv1.SimplifiedStatus{
Status: "MissingInputs",
},
},
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{},
Synth: apiv1.Synthesizer{
Spec: apiv1.SynthesizerSpec{
Refs: []apiv1.Ref{{Key: "bar"}},
},
},
Expected: apiv1.SimplifiedStatus{
Status: "PendingSynthesis",
},
},
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{CurrentSynthesis: &apiv1.Synthesis{UUID: "uuid"}},
Synth: apiv1.Synthesizer{
Spec: apiv1.SynthesizerSpec{
Refs: []apiv1.Ref{{Key: "bar"}},
},
},
Expected: apiv1.SimplifiedStatus{
Status: "Synthesizing",
},
},
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{},
Expected: apiv1.SimplifiedStatus{
Status: "PendingSynthesis",
},
},
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{CurrentSynthesis: &apiv1.Synthesis{UUID: "uuid"}},
Expected: apiv1.SimplifiedStatus{
Status: "Synthesizing",
},
},
{
Bindings: []apiv1.Binding{{Key: "foo"}},
Input: apiv1.CompositionStatus{
Expand Down Expand Up @@ -120,7 +169,7 @@ func TestCompositionSimplification(t *testing.T) {
if tc.Deleting {
comp.DeletionTimestamp = ptr.To(metav1.Now())
}
output := c.aggregate(comp)
output := c.aggregate(&tc.Synth, comp)
assert.Equal(t, tc.Expected, *output)
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/synthesis/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func shouldSwapStates(synth *apiv1.Synthesizer, comp *apiv1.Composition) bool {
// - synthesis is not already pending
// - all bound input resources exist
syn := comp.Status.CurrentSynthesis
return comp.InputsExist() && (syn == nil || syn.ObservedCompositionGeneration != comp.Generation || (!inputRevisionsEqual(synth, comp.Status.InputRevisions, syn.InputRevisions) && syn.Synthesized != nil))
return comp.InputsExist(synth) && (syn == nil || syn.ObservedCompositionGeneration != comp.Generation || (!inputRevisionsEqual(synth, comp.Status.InputRevisions, syn.InputRevisions) && syn.Synthesized != nil))
}

func shouldBackOffPodCreation(comp *apiv1.Composition) bool {
Expand Down
4 changes: 3 additions & 1 deletion internal/controllers/synthesis/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,9 @@ func TestShouldSwapStates(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
assert.Equal(t, tc.Expectation, shouldSwapStates(&apiv1.Synthesizer{}, &tc.Composition))
syn := &apiv1.Synthesizer{}
syn.Spec.Refs = []apiv1.Ref{{Key: "foo"}}
assert.Equal(t, tc.Expectation, shouldSwapStates(syn, &tc.Composition))
})
}
}
Expand Down
Loading