-
Notifications
You must be signed in to change notification settings - Fork 372
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
Allow to override options for specific controllers #2030
Closed
max-melentyev
wants to merge
1
commit into
crossplane-contrib:master
from
max-melentyev:controller-specific-options
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't kingpin support natively loading flags from environment variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it has binding of particular flag to an env variable. This will add ~320 new flags and it'll still require some code to collect these flags into overrides map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a maintainer of this provider anymore, so I'll defer to @MisterMX and @chlunde.
That said, I'm a bit concerned about:
I think if this is a problem for this provider it's probably a problem for other providers. It would be ideal to agree on the problem, and a standard way to address it. Usually to do that we'd write a one-pager in the crossplane/crossplane repo, that provider owners can adopt.
@ulucinar is the TL of Upbound's extensions team (he owns tools like upjet that generate a lot of the other providers). He might be a good person to review a one-pager if you're interested in writing one.