Skip to content

Commit f57be1b

Browse files
authored
Merge pull request #34 from deveeztech/bugfix/await-correctly-for-regex-sequence
fix: await correctly for regex sequence
2 parents 2625649 + e471b03 commit f57be1b

File tree

3 files changed

+112
-25
lines changed

3 files changed

+112
-25
lines changed

example/observed-regex.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
annotations:
55
crossplane.io/external-name: foo
66
crossplane.io/composition-resource-name: first-subresource-2
7-
name: first
7+
name: first-subresource-2
88
spec:
99
forProvider:
1010
conditionAfter:
@@ -32,7 +32,7 @@ metadata:
3232
annotations:
3333
crossplane.io/external-name: foo
3434
crossplane.io/composition-resource-name: first-subresource-1
35-
name: first
35+
name: first-subresource-1
3636
spec:
3737
forProvider:
3838
conditionAfter:

fn.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,14 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
7171
continue
7272
}
7373
for _, before := range sequence[:i] {
74-
strictPattern := string(before)
75-
if !strings.HasPrefix(strictPattern, START) && !strings.HasSuffix(strictPattern, END) {
76-
// if the user provides a delimited regex, we'll use it as is
77-
// if not, add the regex with ^ & $ to match the entire string
78-
// possibly avoid using regex for matching literal strings
79-
strictPattern = fmt.Sprintf("%s%s%s", START, string(before), END)
80-
}
81-
re, err := regexp.Compile(strictPattern)
74+
beforeRegex, err := getStrictRegex(string(before))
8275
if err != nil {
83-
response.Fatal(rsp, errors.Wrapf(err, "cannot compile regex %s", strictPattern))
76+
response.Fatal(rsp, errors.Wrapf(err, "cannot compile regex %s", before))
8477
return rsp, nil
8578
}
8679
keys := []resource.Name{}
8780
for k := range desiredComposed {
88-
if re.MatchString(string(k)) {
81+
if beforeRegex.MatchString(string(k)) {
8982
keys = append(keys, k)
9083
}
9184
}
@@ -103,11 +96,11 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
10396

10497
if desired == 0 || desired != readyResources {
10598
// no resource created
106-
msg := fmt.Sprintf("Delaying creation of resource %q because %q does not exist yet", r, before)
99+
msg := fmt.Sprintf("Delaying creation of resource(s) matching %q because %q does not exist yet", r, before)
107100
if desired > 0 {
108101
// provide a nicer message if there are resources.
109102
msg = fmt.Sprintf(
110-
"Delaying creation of resource %q because %q is not fully ready (%d of %d)",
103+
"Delaying creation of resource(s) matching %q because %q is not fully ready (%d of %d)",
111104
r,
112105
before,
113106
readyResources,
@@ -116,7 +109,13 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
116109
}
117110
response.Normal(rsp, msg)
118111
f.log.Info(msg)
119-
delete(desiredComposed, r)
112+
// find all objects that match the regex and delete them from the desiredComposed map
113+
currentRegex, _ := getStrictRegex(string(r))
114+
for k := range desiredComposed {
115+
if currentRegex.MatchString(string(k)) {
116+
delete(desiredComposed, k)
117+
}
118+
}
120119
break
121120
}
122121
}
@@ -126,3 +125,13 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
126125
rsp.Desired.Resources = nil
127126
return rsp, response.SetDesiredComposedResources(rsp, desiredComposed)
128127
}
128+
129+
func getStrictRegex(pattern string) (*regexp.Regexp, error) {
130+
if !strings.HasPrefix(pattern, START) && !strings.HasSuffix(pattern, END) {
131+
// if the user provides a delimited regex, we'll use it as is
132+
// if not, add the regex with ^ & $ to match the entire string
133+
// possibly avoid using regex for matching literal strings
134+
pattern = fmt.Sprintf("%s%s%s", START, pattern, END)
135+
}
136+
return regexp.Compile(pattern)
137+
}

fn_test.go

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestRunFunction(t *testing.T) {
8989
Results: []*fnv1beta1.Result{
9090
{
9191
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
92-
Message: "Delaying creation of resource \"third\" because \"first\" is not fully ready (0 of 1)",
92+
Message: "Delaying creation of resource(s) matching \"third\" because \"first\" is not fully ready (0 of 1)",
9393
},
9494
},
9595
Desired: &fnv1beta1.State{
@@ -142,7 +142,7 @@ func TestRunFunction(t *testing.T) {
142142
Results: []*fnv1beta1.Result{
143143
{
144144
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
145-
Message: "Delaying creation of resource \"second\" because \"first\" does not exist yet",
145+
Message: "Delaying creation of resource(s) matching \"second\" because \"first\" does not exist yet",
146146
},
147147
},
148148
Desired: &fnv1beta1.State{
@@ -197,7 +197,7 @@ func TestRunFunction(t *testing.T) {
197197
Results: []*fnv1beta1.Result{
198198
{
199199
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
200-
Message: "Delaying creation of resource \"second\" because \"first\" is not fully ready (0 of 1)",
200+
Message: "Delaying creation of resource(s) matching \"second\" because \"first\" is not fully ready (0 of 1)",
201201
},
202202
},
203203
Desired: &fnv1beta1.State{
@@ -383,11 +383,11 @@ func TestRunFunction(t *testing.T) {
383383
Results: []*fnv1beta1.Result{
384384
{
385385
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
386-
Message: "Delaying creation of resource \"second\" because \"first\" is not fully ready (0 of 1)",
386+
Message: "Delaying creation of resource(s) matching \"second\" because \"first\" is not fully ready (0 of 1)",
387387
},
388388
{
389389
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
390-
Message: "Delaying creation of resource \"fourth\" because \"third\" is not fully ready (0 of 1)",
390+
Message: "Delaying creation of resource(s) matching \"fourth\" because \"third\" is not fully ready (0 of 1)",
391391
},
392392
},
393393
Desired: &fnv1beta1.State{
@@ -528,7 +528,7 @@ func TestRunFunction(t *testing.T) {
528528
Results: []*fnv1beta1.Result{
529529
{
530530
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
531-
Message: "Delaying creation of resource \"second\" because \"first\" is not fully ready (0 of 1)",
531+
Message: "Delaying creation of resource(s) matching \"second\" because \"first\" is not fully ready (0 of 1)",
532532
},
533533
},
534534
Desired: &fnv1beta1.State{
@@ -597,7 +597,7 @@ func TestRunFunction(t *testing.T) {
597597
Results: []*fnv1beta1.Result{
598598
{
599599
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
600-
Message: "Delaying creation of resource \"second\" because \"first-.*\" is not fully ready (2 of 3)",
600+
Message: "Delaying creation of resource(s) matching \"second\" because \"first-.*\" is not fully ready (2 of 3)",
601601
},
602602
},
603603
Desired: &fnv1beta1.State{
@@ -679,7 +679,7 @@ func TestRunFunction(t *testing.T) {
679679
Results: []*fnv1beta1.Result{
680680
{
681681
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
682-
Message: "Delaying creation of resource \"third\" because \"second-.*\" is not fully ready (1 of 2)",
682+
Message: "Delaying creation of resource(s) matching \"third\" because \"second-.*\" is not fully ready (1 of 2)",
683683
},
684684
},
685685
Desired: &fnv1beta1.State{
@@ -761,7 +761,7 @@ func TestRunFunction(t *testing.T) {
761761
Results: []*fnv1beta1.Result{
762762
{
763763
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
764-
Message: "Delaying creation of resource \"third\" because \"second-.*\" is not fully ready (1 of 2)",
764+
Message: "Delaying creation of resource(s) matching \"third\" because \"second-.*\" is not fully ready (1 of 2)",
765765
},
766766
},
767767
Desired: &fnv1beta1.State{
@@ -785,6 +785,84 @@ func TestRunFunction(t *testing.T) {
785785
},
786786
},
787787
},
788+
"SequenceRegexWaitComplex": {
789+
reason: "The function should not modify the sequence regex, since it's already prefixed",
790+
args: args{
791+
req: &fnv1beta1.RunFunctionRequest{
792+
Input: resource.MustStructObject(&v1beta1.Input{
793+
Rules: []v1beta1.SequencingRule{
794+
{
795+
Sequence: []resource.Name{
796+
"first-.*",
797+
"second$",
798+
"third-resource",
799+
},
800+
},
801+
},
802+
}),
803+
Observed: &fnv1beta1.State{
804+
Composite: &fnv1beta1.Resource{
805+
Resource: resource.MustStructJSON(xr),
806+
},
807+
Resources: map[string]*fnv1beta1.Resource{},
808+
},
809+
Desired: &fnv1beta1.State{
810+
Composite: &fnv1beta1.Resource{
811+
Resource: resource.MustStructJSON(xr),
812+
},
813+
Resources: map[string]*fnv1beta1.Resource{
814+
"first-0": {
815+
Resource: resource.MustStructJSON(mr),
816+
Ready: fnv1beta1.Ready_READY_TRUE,
817+
},
818+
"first-1": {
819+
Resource: resource.MustStructJSON(mr),
820+
Ready: fnv1beta1.Ready_READY_FALSE,
821+
},
822+
"0-second": {
823+
Resource: resource.MustStructJSON(mr),
824+
},
825+
"1-second": {
826+
Resource: resource.MustStructJSON(mr),
827+
},
828+
"third-resource": {
829+
Resource: resource.MustStructJSON(mr),
830+
},
831+
},
832+
},
833+
},
834+
},
835+
want: want{
836+
rsp: &fnv1beta1.RunFunctionResponse{
837+
Meta: &fnv1beta1.ResponseMeta{Ttl: durationpb.New(response.DefaultTTL)},
838+
Results: []*fnv1beta1.Result{
839+
{
840+
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
841+
Message: "Delaying creation of resource(s) matching \"second$\" because \"first-.*\" is not fully ready (1 of 2)",
842+
},
843+
{
844+
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
845+
Message: "Delaying creation of resource(s) matching \"third-resource\" because \"first-.*\" is not fully ready (1 of 2)",
846+
},
847+
},
848+
Desired: &fnv1beta1.State{
849+
Composite: &fnv1beta1.Resource{
850+
Resource: resource.MustStructJSON(xr),
851+
},
852+
Resources: map[string]*fnv1beta1.Resource{
853+
"first-0": {
854+
Resource: resource.MustStructJSON(mr),
855+
Ready: fnv1beta1.Ready_READY_TRUE,
856+
},
857+
"first-1": {
858+
Resource: resource.MustStructJSON(mr),
859+
Ready: fnv1beta1.Ready_READY_FALSE,
860+
},
861+
},
862+
},
863+
},
864+
},
865+
},
788866
"SequenceRegexAlreadyPrefixed": {
789867
reason: "The function should not modify the sequence regex, since it's already prefixed",
790868
args: args{
@@ -837,7 +915,7 @@ func TestRunFunction(t *testing.T) {
837915
Results: []*fnv1beta1.Result{
838916
{
839917
Severity: fnv1beta1.Severity_SEVERITY_NORMAL,
840-
Message: "Delaying creation of resource \"fourth\" because \"third-.*$\" is not fully ready (0 of 1)",
918+
Message: "Delaying creation of resource(s) matching \"fourth\" because \"third-.*$\" is not fully ready (0 of 1)",
841919
},
842920
},
843921
Desired: &fnv1beta1.State{

0 commit comments

Comments
 (0)