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

kubeadm: add the "config validate" subcommand #118013

Merged
merged 1 commit into from
May 25, 2023

Conversation

neolit123
Copy link
Member

@neolit123 neolit123 commented May 15, 2023

What type of PR is this?

/kind feature

What this PR does / why we need it:

kubeadm: add the "config validate" subcommand

The command can be used to validate an input --config and throw
warnings and errors.

Add a new argument strctErrors to the functions
documentMapTo{Init|Join}Configuration(). This allows
to return errors from the calls to VerifyUnmarshalStrict().

Add a new function verifyKnownGVKs() in config/common.go
that is used to verify if a list of GVKs in a config file is
known. This function is used by the "validate" and "migrate"
commands.

Both commands now throw errors for unknown APIs or fields.

Which issue(s) this PR fixes:

Fixes kubernetes/kubeadm#2871

Special notes for your reviewer:

NONE

Does this PR introduce a user-facing change?

kubeadm: add a new "kubeadm config validate" command that can be used to validate any input config file. Use the --config flag to pass a config file to it. See the command --help screen for more information. As a result of adding this new command, enhance the validation capabilities of the existing "kubeadm config migrate" command. For both commands unknown APIs or fields will throw errors.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/kubeadm sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 15, 2023
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 15, 2023
Comment on lines 284 to 291
This command lets you validate a kubeadm configuration API file and report any warnings and errors.
If there are no errors the exit status will be zero, otherwise it will be non-zero.
Any unmarshaling problems such as unknown API fields will trigger warnings. Unknown API versions and
fields with invalid values will trigger fatal errors. Any other errors or warnings may be reported
depending on contents of the input file.

In this version of kubeadm, the following API versions are supported:
- %s
Copy link
Member Author

@neolit123 neolit123 May 15, 2023

Choose a reason for hiding this comment

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

this behavior is exactly the same as any kubeadm command that accepts a --config flag. same for "kubeadm config migrate". my thinking was that we don't want to make e.g. warnings into errors when validating but instead keep the validation behavior consistent across commands. warnings are shown with klog on stderr, so it is possible for the user to check if stderr is empty on bash from the command line.

@howardjohn @jordanbreen28

for feedback

Copy link
Member Author

Choose a reason for hiding this comment

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

also please check these comments about printing to stdout
#118013 (comment)

// MigrateOldConfig performs conversion to internal type, strict unmarshaling,
// defaulting and validation. It can be used for the validation purpose with output bytes
// ignored.
if _, err = configutil.MigrateOldConfig(cfgBytes); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

this is essentially reusing the migrate backend and ignoring the output.
LMK, if you think we should refactor it for more clarity.

Copy link
Member

Choose a reason for hiding this comment

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

+1 to define another func for this command even if it just calls MigrateOldConfig instead.

Copy link
Member

Choose a reason for hiding this comment

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

I tried this command, one thing might worth to discuss here is that if only unknown obj defined in the cfg, there is no warning at all.

e.g. the config is defined as this,

kind: unknownKind
apiVersion: kubeadm.k8s.io/v1beta3
unknowkey: unknownvalue
kubeadm config validate --config /home/davche02/backup/config/kubeadm/bootstrapfake.cfg

validation passed in this case.
We'd better to thrown a warning here.

Copy link
Member Author

Choose a reason for hiding this comment

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

we should throw an error for unknown GVK.
IIRC, the code paths for init, join do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

+1 to define another func for this command even if it just calls MigrateOldConfig instead.

there could be migrate and validate funcs that share a common backend.

the validate func will not have the marshaling / output.

Copy link
Member Author

Choose a reason for hiding this comment

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

+1 to define another func for this command even if it just calls MigrateOldConfig instead.

added a new ValidateConfig util function now.

Copy link
Member

@pacoxu pacoxu May 22, 2023

Choose a reason for hiding this comment

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

---
apiVersion: foo/v1beta1
kind: Bar

Should we add this to the unit test case?
BTW, an empty file would be a test case as well.

Copy link
Member

Choose a reason for hiding this comment

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

With the latest code 0d85d67, there is no warning if the configuration is not match Join/Init/Cluster according to https://github.com/kubernetes/kubernetes/pull/118013/files#diff-5569e71dfa98866b583171d92e018cf11680c6e1ab16e55ecd0c1e2a0f1f4f4eR250-R264.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we add this to the unit test case?

it's just a warning on stderr (klog) so there is no straightforward way to test it. we could switch it to error and it would be easier to unit test.

With the latest code 0d85d67, there is no warning

i think the problem is that i did not test just unknown GVK. it was combined with known GVK. the logic needs update..

Copy link
Member Author

@neolit123 neolit123 May 22, 2023

Choose a reason for hiding this comment

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

updated, see new PR description and release note amend.

  • unknown APIs (GVK) and unknown fields now throw errors in migrate/validate commands
  • other commands like init/join are not affected
  • added better unit tests for both new commands

@neolit123
Copy link
Member Author

/hold
please keep the hold until discussion is resolved

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 15, 2023
@neolit123
Copy link
Member Author

/retest

untelated test failures

@chendave
Copy link
Member

/assign

// MigrateOldConfig performs conversion to internal type, strict unmarshaling,
// defaulting and validation. It can be used for the validation purpose with output bytes
// ignored.
if _, err = configutil.MigrateOldConfig(cfgBytes); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

+1 to define another func for this command even if it just calls MigrateOldConfig instead.

// ignored.
if _, err = configutil.MigrateOldConfig(cfgBytes); err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to print something if the validation is okay, nothing output to stdout now if everything is good.

Copy link
Member Author

Choose a reason for hiding this comment

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

perhaps, we should. i'd like to get more opinions on this. the exit code 0 / 1 may be sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like there are no comments on this. my preference is to not print on stdout on "OK", similarly to some go tools.

Copy link
Member

Choose a reason for hiding this comment

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

If making it like the dry-run, we can print a result for each configuration in the file

[root@daocloud ~]# ./kubeadm-dev  config  validate --config kubeadmcfg.yaml
InitConfiguration validated
ClusterConfiguration validated
Bar unknown

This would be much clear if there are multi items in the configuration files.

Or, we can just print OK/Validated/Done when there is no error/warning. This is also good.

Copy link
Member Author

@neolit123 neolit123 May 23, 2023

Choose a reason for hiding this comment

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

actually, there is no clean way to detect if a warning was printed. it just goes to stderr from klog. non warnings go there too.

printing per validated config is possible but it will complicate the plumbing of functions a bit. we don't want to print the same messages during normal init/join...but the backend functions are the same.

i will check what options work well.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated to just print "ok" if validation has no errors.

// MigrateOldConfig performs conversion to internal type, strict unmarshaling,
// defaulting and validation. It can be used for the validation purpose with output bytes
// ignored.
if _, err = configutil.MigrateOldConfig(cfgBytes); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I tried this command, one thing might worth to discuss here is that if only unknown obj defined in the cfg, there is no warning at all.

e.g. the config is defined as this,

kind: unknownKind
apiVersion: kubeadm.k8s.io/v1beta3
unknowkey: unknownvalue
kubeadm config validate --config /home/davche02/backup/config/kubeadm/bootstrapfake.cfg

validation passed in this case.
We'd better to thrown a warning here.

@pacoxu
Copy link
Member

pacoxu commented May 21, 2023

/cc
/priority important-soon
/triage accepted

@k8s-ci-robot k8s-ci-robot requested a review from pacoxu May 21, 2023 12:49
@k8s-ci-robot k8s-ci-robot added priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels May 21, 2023
@@ -190,8 +191,37 @@ func ChooseAPIServerBindAddress(bindAddress net.IP) (net.IP, error) {
return ip, nil
}

// verifyKnownGVKs takes a list of GVKs and verifies if they are known in kubeadm or component config schemes
func verifyKnownGVKs(gvks []schema.GroupVersionKind) error {
Copy link
Member Author

@neolit123 neolit123 May 22, 2023

Choose a reason for hiding this comment

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

documentMapTo{Init|Join}Configuration() calls VerifyUnmarshalStrict() which contains similar logic to test for known GVKs. having verifyKnownGVKs() and calling it early in MigrateOldConfig() and ValidateConfig() has the benefits of exiting early for unknown GVKs. logic is a bit duplicated, yet for both migrate/validate commands we want a common method to check if an API is known early on and include component config in there as well. during VerifyUnmarshalStrict() for documentMapToJoinConfiguration() component config is not considered.

please LMK, if someone has strong opinions for refactors here.

gvks = append(gvks, gvk)
}

if err := verifyKnownGVKs(gvks); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

When a user is using the v1beta2, it will just be shown as unknown.

For kubeadm config migrate, it will show the message that you can migrate v1beta2 to v1beta3 using kubeadm v1.22. I think the message helps when validating an old version of the kubeadm configuration.

https://github.com/kubernetes/kubernetes/blob/ed1e45b1092a6d736895b6fd0764b2b01b258e04/cmd/kubeadm/app/util/config/common.go#L57-L58

Using validateSupportedVersion can show that message.

Copy link
Member Author

Choose a reason for hiding this comment

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

i also think the message is valuable.

this is a bit problematic because the validateSupportedVersion function only validates a known GroupVersion. it does not know what Kinds existed in the version.

i need to think what is best here.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated to keep legacy versions in mind.

@pacoxu
Copy link
Member

pacoxu commented May 24, 2023

/lgtm

The current behavior and warning messages are LGTM.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 24, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 81efb3435b92bb18176b93d022747b35927a02b1

Copy link
Member

@chendave chendave left a comment

Choose a reason for hiding this comment

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

/lgtm

Looks great!

return err
}

// Migrate InitConfiguration and ClusterConfiguration if there are any in the config
Copy link
Member

Choose a reason for hiding this comment

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

Looks like these comments is from MigrateOldConfig, it is a little confusion here to say Migrate?

Copy link
Member Author

Choose a reason for hiding this comment

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

changed comments to // Validate ...

}
}

// Migrate JoinConfiguration if there is any
Copy link
Member

Choose a reason for hiding this comment

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

ditto.

The command can be used to validate an input --config and throw
warnings and errors.

Add a new argument strctErrors to the functions
documentMapTo{Init|Join}Configuration(). This allows
to return errors from the calls to VerifyUnmarshalStrict().

Add a new function verifyKnownGVKs() in config/common.go
that is used to verify if a list of GVKs in a config file is
known. This function is used by the "validate" and "migrate"
commands.

Both commands now throw errors for unknown APIs or fields.
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 24, 2023
Copy link
Member

@chendave chendave left a comment

Choose a reason for hiding this comment

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

/lgtm
thanks!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 25, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 5e2a878812ce3011bb60b479e3fec3853ef3775a

@pacoxu
Copy link
Member

pacoxu commented May 25, 2023

/unhold
as all discussions are addressed.
/approve

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 25, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chendave, neolit123, pacoxu

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:

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

@k8s-ci-robot k8s-ci-robot merged commit 90ed8ba into kubernetes:master May 25, 2023
12 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.28 milestone May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubeadm cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to validate configuration yaml
4 participants