Skip to content
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

Validate resource names for drift remediation flags #117

Conversation

a-hilaly
Copy link
Member

@a-hilaly a-hilaly commented Mar 3, 2023

[fixes https://github.com/aws-controllers-k8s/community/issues/1647]

As discussed in #106

this patch brings resource name validation to the arguments passed to
--reconcile-resource-resync-seconds. It also slightly changes the
previously implemented ParseReconcileResourceResyncSeconds to avoid
uncessary validation ops.

Signed-off-by: Amine Hilaly hilalyamine@gmail.com

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ack-prow ack-prow bot added the approved label Mar 3, 2023
@a-hilaly a-hilaly force-pushed the drift-remediation/resource-validation branch from 6013708 to ffbf989 Compare March 3, 2023 03:43
a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Mar 3, 2023
Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

I'm good with this. In the future, it may be good to adopt an "option strategy" for the Config validation (or initialization). This "option strategy" would allow for introducing changes to the runtime that would not break binary/compile-time compatibility in the code generator (like has happened on the related PR there that introduces the resource manager factories getter).

The "option strategy" would look something like this:

// pkg/config/option.go
type Option struct {
    kinds []string
    // other fields that can be used in validation/initialization...
}

// WithKinds instructs the configuration to validate against a set of
// supplied resource kinds
func WithKinds(kinds []string) Option {
    return Option{kinds: kinds}
}

// mergeOptions merges all Option structs into a single Option
// and sets any defaults to missing values
func mergeOptions(opts []Option) Option {
    merged := Option{}
    for _, opt := range opts {
        if opt.kinds != nil {
            merged.kinds = opt.kinds
        }
        // repeat for each field in Option ...
    }
    // set any defaults on merged if still nil...
    return merged
}
// pkg/config/config.go
...
// Note this could also be a New() function instead of Config.Validate()...
func (cfg *Config) Validate(
    opts ...Option,
) error {
    merged := mergeOptions(opts)
    if merged.kinds != nil {
        // Validate the kinds...
    }
}

This way as we add additional validations or configuration options, we don't need to change the function signature for Config.Validate() -- or pkg/config.New().

Your code-generator template code would call like so:

if !cfg.Validate(WithKinds(resourceKinds)) {
     // blah blah..
}

@@ -32,6 +32,7 @@ import (

ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
ackrtutil "github.com/aws-controllers-k8s/runtime/pkg/util"
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can just name this alias ackutil since we're already in the runtime package.

@a-hilaly
Copy link
Member Author

a-hilaly commented Mar 3, 2023

I'm good with this. In the future, it may be good to adopt an "option strategy" for the Config validation (or initialization). This "option strategy" would allow for introducing changes to the runtime that would not break binary/compile-time compatibility in the code generator (like has happened on the related PR there that introduces the resource manager factories getter).

I love this idea. Let me implement it in this PR...

a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Mar 6, 2023
Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
@a-hilaly a-hilaly force-pushed the drift-remediation/resource-validation branch 2 times, most recently from e7422f5 to dd79a82 Compare March 6, 2023 15:52
Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

muy bueno, thanks @a-hilaly!

pkg/config/config.go Outdated Show resolved Hide resolved
pkg/config/config.go Outdated Show resolved Hide resolved
Comment on lines 215 to 217
func (cfg *Config) Validate(options ...Option) error {
merged := mergeOptions(options)
if len(merged.kinds) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't really understand the value in the options struct? I think it's an early optimisation that isn't adding much - especially since we aren't providing defaults.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a know paradigm in Go - we could definitely do a follow up to bring default values this way. I agree that it's not bringing that much, but it allows us to add good stuff if needed :)

Copy link
Member Author

Choose a reason for hiding this comment

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

@a-hilaly a-hilaly force-pushed the drift-remediation/resource-validation branch from dd79a82 to 84557c0 Compare March 6, 2023 19:52
pkg/config/option.go Outdated Show resolved Hide resolved
@a-hilaly a-hilaly force-pushed the drift-remediation/resource-validation branch 3 times, most recently from 704ec6b to d25db5b Compare March 6, 2023 20:09
a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Mar 6, 2023
Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
@a-hilaly a-hilaly force-pushed the drift-remediation/resource-validation branch from d25db5b to 3a3c931 Compare April 7, 2023 14:56
a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Apr 7, 2023
Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
As discussed in https://github.com/aws-controllers-k8s/runtime/pull/10t6
this patch brings resource name validation to the arguments passed to
`--reconcile-resource-resync-seconds`. It also slightly changes the
previously implemented `ParseReconcileResourceResyncSeconds` to avoid
uncessary validation ops.

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
@a-hilaly a-hilaly force-pushed the drift-remediation/resource-validation branch from 3a3c931 to 0aacc2e Compare April 7, 2023 15:53
Copy link
Contributor

@RedbackThomson RedbackThomson left a comment

Choose a reason for hiding this comment

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

/lgtm

@ack-prow ack-prow bot added the lgtm Indicates that a PR is ready to be merged. label Apr 13, 2023
@ack-prow
Copy link

ack-prow bot commented Apr 13, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: A-Hilaly, jaypipes, RedbackThomson

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [A-Hilaly,RedbackThomson,jaypipes]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-prow ack-prow bot merged commit 9655d6d into aws-controllers-k8s:main Apr 13, 2023
a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Apr 13, 2023
Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
a-hilaly added a commit to a-hilaly/ack-code-generator that referenced this pull request Apr 19, 2023
Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>
ack-prow bot pushed a commit to aws-controllers-k8s/code-generator that referenced this pull request May 1, 2023
[fixes aws-controllers-k8s/community#1647]

Completes aws-controllers-k8s/runtime#117

This patch adds a tiny modification to the controllers `main.go` that
passes the resource names to `ackCfg.Validate` method.

Needs a new runtime release.
/hold

Signed-off-by: Amine Hilaly <hilalyamine@gmail.com>

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged.
Projects
None yet
3 participants