-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to override options for specific controllers
Later some resources may be monitored using EventBridge events and these controller will have longer poll intervals. Resources without EventBridge events will have short poll intervals. Options are parsed from env variables with `PROVIDER_AWS_` prefix, for example: `PROVIDER_AWS_ec2.instance.pollInterval=10m`. Signed-off-by: Max Melentyev <[email protected]>
- Loading branch information
1 parent
637d383
commit 13bbdd1
Showing
7 changed files
with
327 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/controller" | ||
) | ||
|
||
// Options allows to override controller.Options for specific controllers. | ||
type Options struct { | ||
defaultOptions controller.Options | ||
specific map[string]OptionsOverride | ||
} | ||
|
||
// OptionsOverride allows to override specific controller.Options properties. | ||
type OptionsOverride struct { | ||
PollInterval *time.Duration | ||
MaxConcurrentReconciles *int | ||
} | ||
|
||
func (override OptionsOverride) applyTo(options *controller.Options) { | ||
if override.PollInterval != nil { | ||
options.PollInterval = *override.PollInterval | ||
} | ||
|
||
if override.MaxConcurrentReconciles != nil { | ||
options.MaxConcurrentReconciles = *override.MaxConcurrentReconciles | ||
} | ||
} | ||
|
||
func NewOptions(defaultOptions controller.Options) Options { | ||
return Options{ | ||
defaultOptions: defaultOptions, | ||
specific: map[string]OptionsOverride{}, | ||
} | ||
} | ||
|
||
// AddOverrides adds overrides for specific controllers from the provided map | ||
// which is similar to ConfigMap data. | ||
// Key format is "<scope>.<property>". Properties without scope or with "default" scope | ||
// owerride default values. | ||
func (options *Options) AddOverrides(values map[string]string) error { | ||
for key, value := range values { | ||
if err := options.addOverride(key, value); err != nil { | ||
return fmt.Errorf("failed to add override for %s: %w", key, err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (options *Options) addOverride(key, value string) error { | ||
propSeparatorIdx := strings.LastIndex(key, ".") | ||
propName := key | ||
scope := "default" | ||
if propSeparatorIdx != -1 { | ||
propName = key[propSeparatorIdx+1:] | ||
scope = key[:propSeparatorIdx] | ||
} | ||
overrides := options.specific[scope] | ||
|
||
switch propName { | ||
case "pollInterval": | ||
if duration, err := time.ParseDuration(value); err != nil { | ||
return fmt.Errorf("failed to parse pollInterval value %s: %w", value, err) | ||
} else { | ||
overrides.PollInterval = &duration | ||
} | ||
case "maxConcurrentReconciles": | ||
if maxConcurrentReconciles, err := strconv.Atoi(value); err != nil { | ||
return fmt.Errorf("failed to parse maxConcurrentReconciles value %s: %w", value, err) | ||
} else { | ||
overrides.MaxConcurrentReconciles = &maxConcurrentReconciles | ||
} | ||
default: | ||
return fmt.Errorf("unknown override property %s", propName) | ||
} | ||
|
||
if scope == "default" { | ||
overrides.applyTo(&options.defaultOptions) | ||
} else { | ||
options.specific[scope] = overrides | ||
} | ||
return nil | ||
} | ||
|
||
// Default returns default controller.Options. | ||
func (options Options) Default() controller.Options { | ||
return options.defaultOptions | ||
} | ||
|
||
// Get returns controller.Options for the specific controller. | ||
func (options Options) Get(name string) controller.Options { | ||
result := options.defaultOptions | ||
if override, ok := options.specific[name]; ok { | ||
override.applyTo(&result) | ||
} | ||
return result | ||
} |
Oops, something went wrong.