Skip to content

Commit fbeaccf

Browse files
committed
Possible to define multiple sequencing rules
Signed-off-by: Hasan Turken <[email protected]>
1 parent acaa9b5 commit fbeaccf

File tree

5 files changed

+77
-35
lines changed

5 files changed

+77
-35
lines changed

example/composition.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ spec:
7070
input:
7171
apiVersion: template.fn.crossplane.io/v1beta1
7272
kind: Input
73-
sequence:
74-
- first-resource
75-
- second-resource
76-
- third-resource
73+
rules:
74+
- sequence:
75+
- first-resource
76+
- second-resource
77+
- sequence:
78+
- first-resource
79+
- third-resource

fn.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/crossplane/function-sdk-go/resource"
67

78
"github.com/crossplane/crossplane-runtime/pkg/errors"
@@ -31,9 +32,6 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
3132
return rsp, nil
3233
}
3334

34-
response.Normalf(rsp, "I was run with input %q!", in.Sequence)
35-
f.log.Info("I was run!", "input", in.Sequence)
36-
3735
// Get the desired composed resources from the request.
3836
desiredComposed, err := request.GetDesiredComposedResources(req)
3937
if err != nil {
@@ -47,22 +45,32 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
4745
return rsp, nil
4846
}
4947

50-
for i, r := range in.Sequence {
51-
if i == 0 {
52-
// We don't need to do anything for the first resource in the sequence.
53-
continue
54-
}
55-
if _, created := observedComposed[r]; created {
56-
// We've already created this resource, so we don't need to do anything.
57-
// We only sequence creation of resources that don't exist yet.
58-
continue
59-
}
60-
for _, before := range in.Sequence[:i] {
61-
if b, ok := desiredComposed[before]; !ok || b.Ready != resource.ReadyTrue {
62-
// A resource that should exist before this one is not in the desired list, or it is not ready yet.
63-
// So, we should not create the resource waiting for it yet.
64-
delete(desiredComposed, r)
65-
break
48+
sequences := make([][]resource.Name, 0, len(in.Rules))
49+
for _, rule := range in.Rules {
50+
sequences = append(sequences, rule.Sequence)
51+
}
52+
53+
for _, sequence := range sequences {
54+
for i, r := range sequence {
55+
if i == 0 {
56+
// We don't need to do anything for the first resource in the sequence.
57+
continue
58+
}
59+
if _, created := observedComposed[r]; created {
60+
// We've already created this resource, so we don't need to do anything.
61+
// We only sequence creation of resources that don't exist yet.
62+
continue
63+
}
64+
for _, before := range sequence[:i] {
65+
if b, ok := desiredComposed[before]; !ok || b.Ready != resource.ReadyTrue {
66+
// A resource that should exist before this one is not in the desired list, or it is not ready yet.
67+
// So, we should not create the resource waiting for it yet.
68+
msg := fmt.Sprintf("Delaying creation of resource %q because %q is not ready or does not exist yet", r, before)
69+
response.Normal(rsp, msg)
70+
f.log.Info(msg)
71+
delete(desiredComposed, r)
72+
break
73+
}
6674
}
6775
}
6876
}

input/v1beta1/input.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ import (
1212
// This isn't a custom resource, in the sense that we never install its CRD.
1313
// It is a KRM-like object, so we generate a CRD to describe its schema.
1414

15-
// TODO: Add your input type here! It doesn't need to be called 'Input', you can
16-
// rename it to anything you like.
15+
type SequencingRule struct {
16+
// TODO: InferFromUsages
17+
// InferFromUsages bool `json:"inferFromUsages,omitempty"`
18+
Sequence []resource.Name `json:"sequence,omitempty"`
19+
}
1720

1821
// Input can be used to provide input to this Function.
1922
// +kubebuilder:object:root=true
@@ -23,5 +26,5 @@ type Input struct {
2326
metav1.TypeMeta `json:",inline"`
2427
metav1.ObjectMeta `json:"metadata,omitempty"`
2528

26-
Sequence []resource.Name `json:"sequence"`
29+
Rules []SequencingRule `json:"rules"`
2730
}

input/v1beta1/zz_generated.deepcopy.go

Lines changed: 26 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/input/template.fn.crossplane.io_inputs.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,20 @@ spec:
3333
type: string
3434
metadata:
3535
type: object
36-
sequence:
36+
rules:
3737
items:
38-
description: A Name uniquely identifies a composed resource within a
39-
Composition Function pipeline. It's not the resource's metadata.name.
40-
type: string
38+
properties:
39+
sequence:
40+
description: 'TODO: InferFromUsages InferFromUsages bool `json:"inferFromUsages,omitempty"`'
41+
items:
42+
description: A Name uniquely identifies a composed resource within
43+
a Composition Function pipeline. It's not the resource's metadata.name.
44+
type: string
45+
type: array
46+
type: object
4147
type: array
4248
required:
43-
- sequence
49+
- rules
4450
type: object
4551
served: true
4652
storage: true

0 commit comments

Comments
 (0)