Skip to content

Commit dc66295

Browse files
committed
Initial Commit: Optional infinite requeue
1 parent 85e4aa7 commit dc66295

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

pkg/generate/config/resource.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type ResourceConfig struct {
5656
// very little consistency to the APIs that we can use to instruct the code
5757
// generator :(
5858
UpdateOperation *UpdateOperationConfig `json:"update_operation,omitempty"`
59+
// ReconcileConfig describes options for controlling the reconciliation
60+
// logic for a particular resource.
61+
Reconcile *ReconcileConfig `json:"reconcile,omitempty"`
5962
// UpdateConditionsCustomMethodName provides the name of the custom method on the
6063
// `resourceManager` struct that will set Conditions on a `resource` struct
6164
// depending on the status of the resource.
@@ -273,6 +276,19 @@ type PrintConfig struct {
273276
AddAgeColumn bool `json:"add_age_column"`
274277
// OrderBy is the field used to sort the list of PrinterColumn options.
275278
OrderBy string `json:"order_by"`
279+
// ReconcileConfig describes options for controlling the reconciliation
280+
// logic for a particular resource.
281+
type ReconcileConfig struct {
282+
// RequeueOnSuccessSeconds indicates the number of seconds after which to requeue a
283+
// resource that has been successfully reconciled (i.e. ConditionTypeResourceSynced=true)
284+
// This is useful for resources that are long-lived and may have observable status fields
285+
// change over time that would be useful to refresh those field values for users.
286+
// This field is optional and the default behaviour of the ACK runtime is to not requeue
287+
// resources that have been successfully reconciled. Note that all ACK controllers will
288+
// *flush and resync their watch caches* every 10 hours by default, which will end up
289+
// causing ACK controllers to refresh the status views of all watched resources, but this
290+
// behaviour is expensive and may be turned off in future ACK runtime options.
291+
RequeueOnSuccessSeconds int `json:"requeue_on_success_seconds,omitempty"`
276292
}
277293

278294
// ResourceConfig returns the ResourceConfig for a given named resource

pkg/model/crd.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,24 @@ func (r *CRD) GetResourcePrintOrderByName() string {
554554
// kubebuilder:printcolumn comment marker
555555
func (r *CRD) PrintAgeColumn() bool {
556556
return r.cfg.GetResourcePrintAddAgeColumn(r.Names.Camel)
557+
558+
// ReconcileRequeuOnSuccessSeconds returns the duration after which to requeue
559+
// the custom resource as int, if specified in generator config.
560+
func (r *CRD) ReconcileRequeuOnSuccessSeconds() int {
561+
if r.cfg == nil {
562+
return 0
563+
}
564+
resGenConfig, found := r.cfg.Resources[r.Names.Original]
565+
if !found {
566+
return 0
567+
}
568+
reconcile := resGenConfig.Reconcile
569+
// handles the default case
570+
if reconcile == nil {
571+
return 0
572+
} else {
573+
return reconcile.RequeueOnSuccessSeconds
574+
}
557575
}
558576

559577
// CustomUpdateMethodName returns the name of the custom resourceManager method

templates/pkg/resource/manager.go.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (rm *resourceManager) onError(
184184
r *resource,
185185
err error,
186186
) (acktypes.AWSResource, error) {
187-
r1, updated := rm.updateConditions(r, err)
187+
r1, updated := rm.updateConditions(r, false, err)
188188
if !updated {
189189
return r, err
190190
}
@@ -204,7 +204,7 @@ func (rm *resourceManager) onError(
204204
func (rm *resourceManager) onSuccess(
205205
r *resource,
206206
) (acktypes.AWSResource, error) {
207-
r1, updated := rm.updateConditions(r, nil)
207+
r1, updated := rm.updateConditions(r, true, nil)
208208
if !updated {
209209
return r, nil
210210
}

templates/pkg/resource/manager_factory.go.tpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ func (f *resourceManagerFactory) IsAdoptable() bool {
6666
return {{ .CRD.IsAdoptable }}
6767
}
6868

69+
// GetRequeueOnSuccessSeconds returns true if the resource should be requeued after specified seconds
70+
// Default is false which means resource will not be requeued after success.
71+
func (f *resourceManagerFactory) GetRequeueOnSuccessSeconds() (int, bool) {
72+
{{- if $reconcileRequeuOnSuccessSeconds := .CRD.ReconcileRequeuOnSuccessSeconds }}
73+
return {{ $reconcileRequeuOnSuccessSeconds }}, true
74+
{{- else }}
75+
return 0, false
76+
{{- end }}
77+
}
78+
6979
func newResourceManagerFactory() *resourceManagerFactory {
7080
return &resourceManagerFactory{
7181
rmCache: map[string]*resourceManager{},

templates/pkg/resource/sdk.go.tpl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (rm *resourceManager) setStatusDefaults (
189189
// else it returns nil, false
190190
func (rm *resourceManager) updateConditions (
191191
r *resource,
192+
onSuccess bool,
192193
err error,
193194
) (*resource, bool) {
194195
ko := r.ko.DeepCopy()
@@ -197,13 +198,17 @@ func (rm *resourceManager) updateConditions (
197198
// Terminal condition
198199
var terminalCondition *ackv1alpha1.Condition = nil
199200
var recoverableCondition *ackv1alpha1.Condition = nil
201+
var syncCondition *ackv1alpha1.Condition = nil
200202
for _, condition := range ko.Status.Conditions {
201203
if condition.Type == ackv1alpha1.ConditionTypeTerminal {
202204
terminalCondition = condition
203205
}
204206
if condition.Type == ackv1alpha1.ConditionTypeRecoverable {
205207
recoverableCondition = condition
206208
}
209+
if condition.Type == ackv1alpha1.ConditionTypeResourceSynced {
210+
syncCondition = condition
211+
}
207212
}
208213

209214
if rm.terminalAWSError(err) {
@@ -245,15 +250,27 @@ func (rm *resourceManager) updateConditions (
245250
}
246251
}
247252

253+
{{- if $reconcileRequeuOnSuccessSeconds := .CRD.ReconcileRequeuOnSuccessSeconds }}
254+
if syncCondition == nil && onSuccess {
255+
syncCondition = &ackv1alpha1.Condition{
256+
Type: ackv1alpha1.ConditionTypeResourceSynced,
257+
}
258+
syncCondition.Status = corev1.ConditionTrue
259+
ko.Status.Conditions = append(ko.Status.Conditions, syncCondition)
260+
}
261+
{{- else }}
262+
// Required to avoid the "declared but not used" error in the default case
263+
syncCondition = nil
264+
{{- end }}
248265

249266
{{- if $updateConditionsCustomMethodName := .CRD.UpdateConditionsCustomMethodName }}
250267
// custom update conditions
251268
customUpdate := rm.{{ $updateConditionsCustomMethodName }}(ko, r, err)
252-
if terminalCondition != nil || recoverableCondition != nil || customUpdate {
269+
if terminalCondition != nil || recoverableCondition != nil || syncCondition != nil || customUpdate {
253270
return &resource{ko}, true // updated
254271
}
255272
{{- else }}
256-
if terminalCondition != nil || recoverableCondition != nil {
273+
if terminalCondition != nil || recoverableCondition != nil || syncCondition != nil {
257274
return &resource{ko}, true // updated
258275
}
259276
{{- end }}

0 commit comments

Comments
 (0)