-
Notifications
You must be signed in to change notification settings - Fork 20
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
Setup global initializer for missing central config file #723
Setup global initializer for missing central config file #723
Conversation
b711f56
to
76c083c
Compare
76c083c
to
d324648
Compare
451364b
to
d9e21bc
Compare
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.
Looks great. Just a minor question but otherwise looks good to me.
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.
Good stuff!
Some minor comments on the framework part
kerrors "k8s.io/apimachinery/pkg/util/errors" | ||
) | ||
|
||
type initializer struct { |
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.
a suggestion: how about support providing a name for each initializer registered? It can be useful for a few things:
- Used in aggregated error list to provide some consistency in the output
- In the future, should it happen that a particular initializer consistently fails for a CLI user (intentionally caused by the customer or otherwise), it is much easier to provide some means to turn off certain initializers if we can refer to them by name
// RegisterInitializer registers a new initializer with the global list of initializers. | ||
// The trigger function is used to determine if the initialization function should be run. | ||
// The initialization function is the function that will be run if the trigger function returns true. | ||
// The set of initializer triggers is checked whenever the CLI is run. |
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.
weak suggestion:
while no initializer should be written in a way that depends on other initializers, consider mentioning that the initializers will be run in the order that they are registered.
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.
Good point. In fact, while working on a second initializer I realized that the order may matter.
For example, the current initializer will make sure the central_config.yaml
file is in the cache; another initializer may want to read the the central config, so having the cache refresh run first would be useful.
Controlling the order needs a bit of thought. Currently I use an init()
function, but those are hard to order. We could use an extra order
or rank
argument to the RegisterInitializer()
function of value 1 to 100 and we could then register a position in the order.
Let's discuss.
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.
We will keep this point for later
unexpectedOut: []string{"1", "3"}, | ||
}, | ||
{ | ||
test: "init throws error", |
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.
nit: I think one interesting case to include in multiple initFuncs, returning a mix of success and error (e.g. erro just on the second of 3). Demonstrates that error does not interrupt other initializers.
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.
A couple more nits and suggestions.
Expect(errStream).ToNot(ContainSubstring(initializationStr)) | ||
Expect(errStream).ToNot(ContainSubstring(initErrorStr)) | ||
}) | ||
It("initialization when config file is missing a second time", func() { |
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.
nit: test title and comments aside, this test looks identical to the previous one!
To prove the point that an initialized CLI seeing a deleted central config afterwards can still re-initialize correctly (which I gather is the purpose of this test), you might be better off just running the previous test in a loop of 2 iterations and combining the comments.
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.
Good eye 👍 , will do
The concept of global initializers is added to allow the CLI to prepare its data on certain conditions (triggers). The commit also uses one such initializer to fix a plugin cache that is missing the central_config.yaml file. Signed-off-by: Marc Khouzam <marc.khouzam@broadcom.com>
Signed-off-by: Marc Khouzam <marc.khouzam@broadcom.com>
Signed-off-by: Marc Khouzam <marc.khouzam@broadcom.com>
Signed-off-by: Marc Khouzam <marc.khouzam@broadcom.com>
d9e21bc
to
3e358c7
Compare
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.
Thanks for making prompt updates. The remaining issues (e.g. regarding ordering of the initializers) are not blockers at the moment, and can be addressed in a followup after some discussion
…u#723) The concept of global initializers is added to allow the CLI to prepare its data on certain conditions (triggers). The commit also uses one such initializer to fix a plugin cache that is missing the central_config.yaml file.
The concept of global initializers is added to allow the CLI to prepare its data on certain conditions (triggers). The commit also uses one such initializer to fix a plugin cache that is missing the central_config.yaml file.
What this PR does / why we need it
This PR is provided in two commits to make the review easier.
With the introduction of the Central Configuration in #707, there is now a
central_config.yaml
file part of the plugin inventory OCI image; however, CLIs < 1.3.0 do not copy this file into the cache, so this PR forces a cache invalidation and a re-download of the OCI image as soon as a CLI >= 1.3.0 notices the missing file. This guarantees that thecentral_config.yaml
file will be properly installed. See #722 for more details.To achieve this, the first commit of the PR implements a small framework allowing a "global initialization" of the CLI based on pre-defined triggers. Different features can register their own trigger function and a corresponding initialization function which allow for any require global initialization. The CLI will verify the triggers on any command execution and if the trigger function returns true, the corresponding initialization will be executed.
The first commit implements the global initialization framework.
The second commit uses the framework to initialize the plugin inventory cache if the
central_config.yaml
file is missing.Which issue(s) this PR fixes
Fixes #722
Describe testing done for PR
What the new feature looks like
Run the command again to see there is no initialization anymore as it is only run once:
Now remove the
central_config.yaml
file to see that the CLI notices:Main scenario motivating this PR
Prepare the test :
Use an old CLI to first setup the cache. This is similar to what a user of CLI v1.2 would have done.
Notice the
central_config.yaml
file is not copied to the cacheNow test the actual scenario driving this change.
Normally, when running v1.3.0-alpha.2 with the test repo we should see a notification saying v1.3.3 is available for upgrade. However, notice this does not happen because the
central_config.yaml
file is missing from the cache.Release note
Additional information
Special notes for your reviewer