Skip to content

feat: rm.IsSynced returns requeueError when false #597

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Comment on lines +155 to +156
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when someone provides 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we panic code-gen?

}

// HooksConfig instructs the code generator how to inject custom callback hooks
Expand Down
16 changes: 12 additions & 4 deletions pkg/generate/code/synced.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get unit tests here?

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/generate/code/synced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions pkg/testdata/models/apis/lambda/0000-00-00/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ resources:
in:
- AVAILABLE
- ACTIVE
requeueTime: 0
- path: Status.CodeSize
in:
- 1
- 2
requeueTime: 100
CodeSigningConfig:
fields:
Tags:
Expand Down