Skip to content

Commit 9f2bbca

Browse files
committed
feat: rm.IsSynced returns requeueError when false
This changes introduce a new field in generator.yaml SyncedConfig, which lets us define custom reuqueue duration. If it is not defined, the duration will default to the DefaultSyncPeriod
1 parent ed3e8af commit 9f2bbca

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

pkg/config/resource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ type SyncedCondition struct {
152152
Path *string `json:"path"`
153153
// In contains a list of possible values `Path` should be equal to.
154154
In []string `json:"in"`
155+
// RequeueError custom SyncPeriod in seconds
156+
RequeueTime *int `json:"requeueTime,omitempty`
155157
}
156158

157159
// HooksConfig instructs the code generator how to inject custom callback hooks

pkg/generate/code/synced.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ func ResourceIsSynced(
7575
msg := fmt.Sprintf("cannot find top level field of path '%s': %v", *condCfg.Path, err)
7676
panic(msg)
7777
}
78+
notSyncedErr := ""
79+
if condCfg.RequeueTime != nil {
80+
notSyncedErr = fmt.Sprintf("ackrequeue.NeededAfter(fmt.Errorf(\"requeing until %s is %v\"), time.Duration(%d)*time.Second)", *condCfg.Path, condCfg.In, *condCfg.RequeueTime)
81+
} else {
82+
notSyncedErr = fmt.Sprintf("ackrequeue.Needed(fmt.Errorf(\"requeing until %s is %v\"))", *condCfg.Path, condCfg.In)
83+
}
7884
candidatesVarName := fmt.Sprintf("%sCandidates", field.Names.CamelLower)
7985
if fp.Size() == 2 {
80-
out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condCfg)
86+
out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condCfg, notSyncedErr)
8187
} else {
82-
out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condCfg)
88+
out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condCfg, notSyncedErr)
8389
}
8490
}
8591

@@ -118,6 +124,7 @@ func scalarFieldEqual(
118124
candidatesVarName string,
119125
goType string,
120126
condCfg ackgenconfig.SyncedCondition,
127+
notSyncedErr string,
121128
) string {
122129
out := ""
123130
fieldPath := fmt.Sprintf("%s.%s", resVarName, *condCfg.Path)
@@ -156,7 +163,7 @@ func scalarFieldEqual(
156163
)
157164

158165
// return false, nil
159-
out += "\t\treturn false, nil\n"
166+
out += fmt.Sprintf("\t\treturn false, %s\n", notSyncedErr)
160167
// }
161168
out += "\t}\n"
162169
return out
@@ -168,6 +175,7 @@ func fieldPathSafeEqual(
168175
candidatesVarName string,
169176
field *model.Field,
170177
condCfg ackgenconfig.SyncedCondition,
178+
notSyncedErr string,
171179
) string {
172180
out := ""
173181
rootPath := fmt.Sprintf("%s.%s", resVarName, strings.Split(*condCfg.Path, ".")[0])
@@ -191,7 +199,7 @@ func fieldPathSafeEqual(
191199
// }
192200
out += "\t}\n"
193201
}
194-
out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condCfg)
202+
out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condCfg, notSyncedErr)
195203
return out
196204
}
197205

pkg/generate/code/synced_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ func TestSyncedLambdaFunction(t *testing.T) {
3838
}
3939
stateCandidates := []string{"AVAILABLE", "ACTIVE"}
4040
if !ackutil.InStrings(*r.ko.Status.State, stateCandidates) {
41-
return false, nil
41+
return false, ackrequeue.Needed(fmt.Errorf("requeing until Status.State is [AVAILABLE ACTIVE]"))
4242
}
4343
if r.ko.Status.LastUpdateStatus == nil {
4444
return false, nil
4545
}
4646
lastUpdateStatusCandidates := []string{"AVAILABLE", "ACTIVE"}
4747
if !ackutil.InStrings(*r.ko.Status.LastUpdateStatus, lastUpdateStatusCandidates) {
48-
return false, nil
48+
return false, ackrequeue.NeededAfter(fmt.Errorf("requeing until Status.LastUpdateStatus is [AVAILABLE ACTIVE]"), time.Duration(0)*time.Second)
4949
}
5050
if r.ko.Status.CodeSize == nil {
5151
return false, nil
5252
}
5353
codeSizeCandidates := []int{1, 2}
5454
if !ackutil.InStrings(*r.ko.Status.CodeSize, codeSizeCandidates) {
55-
return false, nil
55+
return false, ackrequeue.NeededAfter(fmt.Errorf("requeing until Status.CodeSize is [1 2]"), time.Duration(100)*time.Second)
5656
}
5757
`
5858
assert.Equal(
@@ -78,7 +78,7 @@ func TestSyncedDynamodbTable(t *testing.T) {
7878
}
7979
tableStatusCandidates := []string{"AVAILABLE", "ACTIVE"}
8080
if !ackutil.InStrings(*r.ko.Status.TableStatus, tableStatusCandidates) {
81-
return false, nil
81+
return false, ackrequeue.Needed(fmt.Errorf("requeing until Status.TableStatus is [AVAILABLE ACTIVE]"))
8282
}
8383
if r.ko.Spec.ProvisionedThroughput == nil {
8484
return false, nil
@@ -88,14 +88,14 @@ func TestSyncedDynamodbTable(t *testing.T) {
8888
}
8989
provisionedThroughputCandidates := []int{0, 10}
9090
if !ackutil.InStrings(*r.ko.Spec.ProvisionedThroughput.ReadCapacityUnits, provisionedThroughputCandidates) {
91-
return false, nil
91+
return false, ackrequeue.Needed(fmt.Errorf("requeing until Spec.ProvisionedThroughput.ReadCapacityUnits is [0 10]"))
9292
}
9393
if r.ko.Status.ItemCount == nil {
9494
return false, nil
9595
}
9696
itemCountCandidates := []int{0}
9797
if !ackutil.InStrings(*r.ko.Status.ItemCount, itemCountCandidates) {
98-
return false, nil
98+
return false, ackrequeue.Needed(fmt.Errorf("requeing until Status.ItemCount is [0]"))
9999
}
100100
`
101101
assert.Equal(

pkg/testdata/models/apis/lambda/0000-00-00/generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ resources:
2121
in:
2222
- AVAILABLE
2323
- ACTIVE
24+
requeueTime: 0
2425
- path: Status.CodeSize
2526
in:
2627
- 1
2728
- 2
29+
requeueTime: 100
2830
CodeSigningConfig:
2931
fields:
3032
Tags:

0 commit comments

Comments
 (0)