diff --git a/pkg/config/resource.go b/pkg/config/resource.go index aa050b95..ef21a82b 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -152,6 +152,8 @@ type SyncedCondition struct { Path *string `json:"path"` // In contains a list of possible values `Path` should be equal to. In []string `json:"in"` + // RequeueError custom SyncPeriod in seconds + RequeueTime *int `json:"requeueTime,omitempty` } // HooksConfig instructs the code generator how to inject custom callback hooks diff --git a/pkg/generate/code/synced.go b/pkg/generate/code/synced.go index 9a2f3194..0d346ebf 100644 --- a/pkg/generate/code/synced.go +++ b/pkg/generate/code/synced.go @@ -75,11 +75,17 @@ func ResourceIsSynced( msg := fmt.Sprintf("cannot find top level field of path '%s': %v", *condCfg.Path, err) panic(msg) } + notSyncedErr := "" + if condCfg.RequeueTime != nil { + notSyncedErr = fmt.Sprintf("ackrequeue.NeededAfter(fmt.Errorf(\"requeing until %s is %v\"), time.Duration(%d)*time.Second)", *condCfg.Path, condCfg.In, *condCfg.RequeueTime) + } else { + notSyncedErr = fmt.Sprintf("ackrequeue.Needed(fmt.Errorf(\"requeing until %s is %v\"))", *condCfg.Path, condCfg.In) + } candidatesVarName := fmt.Sprintf("%sCandidates", field.Names.CamelLower) if fp.Size() == 2 { - out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condCfg) + out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condCfg, notSyncedErr) } else { - out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condCfg) + out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condCfg, notSyncedErr) } } @@ -118,6 +124,7 @@ func scalarFieldEqual( candidatesVarName string, goType string, condCfg ackgenconfig.SyncedCondition, + notSyncedErr string, ) string { out := "" fieldPath := fmt.Sprintf("%s.%s", resVarName, *condCfg.Path) @@ -156,7 +163,7 @@ func scalarFieldEqual( ) // return false, nil - out += "\t\treturn false, nil\n" + out += fmt.Sprintf("\t\treturn false, %s\n", notSyncedErr) // } out += "\t}\n" return out @@ -168,6 +175,7 @@ func fieldPathSafeEqual( candidatesVarName string, field *model.Field, condCfg ackgenconfig.SyncedCondition, + notSyncedErr string, ) string { out := "" rootPath := fmt.Sprintf("%s.%s", resVarName, strings.Split(*condCfg.Path, ".")[0]) @@ -191,7 +199,7 @@ func fieldPathSafeEqual( // } out += "\t}\n" } - out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condCfg) + out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condCfg, notSyncedErr) return out } diff --git a/pkg/generate/code/synced_test.go b/pkg/generate/code/synced_test.go index 853658f0..18da32fe 100644 --- a/pkg/generate/code/synced_test.go +++ b/pkg/generate/code/synced_test.go @@ -38,21 +38,21 @@ func TestSyncedLambdaFunction(t *testing.T) { } stateCandidates := []string{"AVAILABLE", "ACTIVE"} if !ackutil.InStrings(*r.ko.Status.State, stateCandidates) { - return false, nil + return false, ackrequeue.Needed(fmt.Errorf("requeing until Status.State is [AVAILABLE ACTIVE]")) } if r.ko.Status.LastUpdateStatus == nil { return false, nil } lastUpdateStatusCandidates := []string{"AVAILABLE", "ACTIVE"} if !ackutil.InStrings(*r.ko.Status.LastUpdateStatus, lastUpdateStatusCandidates) { - return false, nil + return false, ackrequeue.NeededAfter(fmt.Errorf("requeing until Status.LastUpdateStatus is [AVAILABLE ACTIVE]"), time.Duration(0)*time.Second) } if r.ko.Status.CodeSize == nil { return false, nil } codeSizeCandidates := []int{1, 2} if !ackutil.InStrings(*r.ko.Status.CodeSize, codeSizeCandidates) { - return false, nil + return false, ackrequeue.NeededAfter(fmt.Errorf("requeing until Status.CodeSize is [1 2]"), time.Duration(100)*time.Second) } ` assert.Equal( @@ -78,7 +78,7 @@ func TestSyncedDynamodbTable(t *testing.T) { } tableStatusCandidates := []string{"AVAILABLE", "ACTIVE"} if !ackutil.InStrings(*r.ko.Status.TableStatus, tableStatusCandidates) { - return false, nil + return false, ackrequeue.Needed(fmt.Errorf("requeing until Status.TableStatus is [AVAILABLE ACTIVE]")) } if r.ko.Spec.ProvisionedThroughput == nil { return false, nil @@ -88,14 +88,14 @@ func TestSyncedDynamodbTable(t *testing.T) { } provisionedThroughputCandidates := []int{0, 10} if !ackutil.InStrings(*r.ko.Spec.ProvisionedThroughput.ReadCapacityUnits, provisionedThroughputCandidates) { - return false, nil + return false, ackrequeue.Needed(fmt.Errorf("requeing until Spec.ProvisionedThroughput.ReadCapacityUnits is [0 10]")) } if r.ko.Status.ItemCount == nil { return false, nil } itemCountCandidates := []int{0} if !ackutil.InStrings(*r.ko.Status.ItemCount, itemCountCandidates) { - return false, nil + return false, ackrequeue.Needed(fmt.Errorf("requeing until Status.ItemCount is [0]")) } ` assert.Equal( diff --git a/pkg/testdata/models/apis/lambda/0000-00-00/generator.yaml b/pkg/testdata/models/apis/lambda/0000-00-00/generator.yaml index 20b3aaf7..709487eb 100644 --- a/pkg/testdata/models/apis/lambda/0000-00-00/generator.yaml +++ b/pkg/testdata/models/apis/lambda/0000-00-00/generator.yaml @@ -21,10 +21,12 @@ resources: in: - AVAILABLE - ACTIVE + requeueTime: 0 - path: Status.CodeSize in: - 1 - 2 + requeueTime: 100 CodeSigningConfig: fields: Tags: