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

add validation for cluster api and remove validating webhook #1152

Merged
merged 1 commit into from
Jan 14, 2022

Conversation

carlory
Copy link
Member

@carlory carlory commented Dec 23, 2021

What type of PR is this?

/kind feature

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

add validation for cluster API

@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Dec 23, 2021
@karmada-bot
Copy link
Collaborator

Welcome @carlory! It looks like this is your first PR to karmada-io/karmada 🎉

@karmada-bot karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 23, 2021
@XiShanYongYe-Chang
Copy link
Member

Hi @carlory , thanks for your contribution. :)

@carlory carlory force-pushed the add-validation-for-cluster branch 2 times, most recently from 3b13344 to c14bd6e Compare December 24, 2021 16:17
@carlory
Copy link
Member Author

carlory commented Dec 25, 2021

/cc @kevin-wangzefeng @RainbowMango

@RainbowMango
Copy link
Member

Thanks @carlory I'll review it ASAP.

@carlory carlory force-pushed the add-validation-for-cluster branch 2 times, most recently from d589329 to 9a93d43 Compare December 25, 2021 09:04
@carlory
Copy link
Member Author

carlory commented Dec 28, 2021

/cc @kevin-wangzefeng @RainbowMango

@RainbowMango
Copy link
Member

Got it @carlory.

@carlory
Copy link
Member Author

carlory commented Dec 29, 2021

add validation for impersonatorSecretRef
/cc @RainbowMango @XiShanYongYe-Chang

@carlory carlory changed the title add validation for cluster api add validation for cluster api and remove validating webhook Dec 30, 2021
@carlory
Copy link
Member Author

carlory commented Dec 30, 2021

/cc @RainbowMango @XiShanYongYe-Chang

remove validating webhook

@XiShanYongYe-Chang
Copy link
Member

Hi @carlory, I'll review it now.

@XiShanYongYe-Chang
Copy link
Member

Thanks, @carlory. It looks good to me.

@RainbowMango
Copy link
Member

Not an objection. Just a question.

The validation functionality in webhook still works as expected, right?
If it is the case, I'd suggest doing this after the coming new release.

@RainbowMango
Copy link
Member

We should remove the configuration from karmada init:

return fmt.Sprintf(`apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-config
labels:
app: validating-config
webhooks:
- name: cluster.karmada.io
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["cluster.karmada.io"]
apiVersions: ["*"]
resources: ["clusters"]
scope: "Cluster"
clientConfig:
url: https://karmada-webhook.%s.svc:443/validate-cluster

cc @lonelyCZ @prodanlabs

@lonelyCZ
Copy link
Member

We should remove the configuration from karmada init

Ok, I will do it.

@lonelyCZ
Copy link
Member

lonelyCZ commented Dec 31, 2021

This can be removed in this pr. Thanks @carlory

@carlory
Copy link
Member Author

carlory commented Jan 4, 2022

@XiShanYongYe-Chang
Copy link
Member

thanks!
/lgtm

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 5, 2022
@RainbowMango
Copy link
Member

/assign

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

Generally looks good to me.

  1. I suggest just putting validation.go to pkg/apis/cluster directory as it is only for internel types.
  2. One main effect for moving the validation from webhook to AA is if there is a bug, it'll block people registering cluster(They can't disable the validation anymore). So, I suggest more unit test for the validations.

allErrors = append(allErrors, validateTaintEffect(&currTaint.Effect, false, idxPath.Child("effect"))...)

// validate if taint is unique by <key, effect>
if len(uniqueTaints[currTaint.Effect]) > 0 && uniqueTaints[currTaint.Effect].Has(currTaint.Key) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if len(uniqueTaints[currTaint.Effect]) > 0 && uniqueTaints[currTaint.Effect].Has(currTaint.Key) {
if uniqueTaints[currTaint.Effect].Has(currTaint.Key) {

This will be enough, right?
Don't know if len(sets.String) will work.

Copy link
Member Author

@carlory carlory Jan 12, 2022

Choose a reason for hiding this comment

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

@RainbowMango len(uniqueTaints[currTaint.Effect]) > 0 is necessary to avoid panic

Copy link
Member

Choose a reason for hiding this comment

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

Really? Do you mean if we invoke Has() method on an empty sets.String{} will panic?

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. I suggest just putting validation.go to pkg/apis/cluster directory as it is only for internel types.

@RainbowMango

In kubernetes/kubernetes and openshift/origin repo, the validation files are placed in a separate directory.

I recommend keeping the same code style as them, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I recommend keeping the same code style as them, what do you think?

OK for me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Really? Do you mean if we invoke Has() method on an empty sets.String{} will panic?

It may not have been initialized when we invoke Has() method.

// ValidateClusterTaints tests if given taints have valid data.
func ValidateClusterTaints(taints []corev1.Taint, fldPath *field.Path) field.ErrorList {
	allErrors := field.ErrorList{}

	uniqueTaints := map[corev1.TaintEffect]sets.String{}
	for i, currTaint := range taints {
             ...
		// validate if taint is unique by <key, effect>
		if len(uniqueTaints[currTaint.Effect]) > 0 && uniqueTaints[currTaint.Effect].Has(currTaint.Key) {
                      ....
		}

		// add taint to existingTaints for uniqueness check
		if len(uniqueTaints[currTaint.Effect]) == 0 {
			uniqueTaints[currTaint.Effect] = sets.String{}
		}
		uniqueTaints[currTaint.Effect].Insert(currTaint.Key)
	}
	return allErrors
}

Copy link
Member Author

@carlory carlory Jan 12, 2022

Choose a reason for hiding this comment

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

ValidateClusterTaints is lifted from kubernetes validateNodeTaints

Copy link
Member

Choose a reason for hiding this comment

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

For code lifted from k/k, I suggest moving it to a separated file(like kubevalidation.go) and keeping the file header.
If there is a bug we should help fix it in k/k then port it back to Karmada. We can ignore code redundant issues for porting code.

Copy link
Member

Choose a reason for hiding this comment

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

But, I'm still interest if the len(uniqueTaints[currTaint.Effect]) > 0 could be removed, though.
Because, the sets has been initilized by uniqueTaints := map[corev1.TaintEffect]sets.String{}.

// Prefix indicates this name will be used as part of generation, in which case
// trailing dashes are allowed.
func ValidateClusterName(name string, prefix bool) []string {
return utilvalidation.ValidateClusterName(name)
Copy link
Member

Choose a reason for hiding this comment

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

Can we move utilvalidation.ValidateClusterName to here?

@karmada-bot karmada-bot removed the lgtm Indicates that a PR is ready to be merged. label Jan 12, 2022
@carlory carlory force-pushed the add-validation-for-cluster branch 2 times, most recently from ec856dd to 7b66545 Compare January 12, 2022 08:03
@RainbowMango
Copy link
Member

@carlory Please cc me again if it is ready for review. Thanks for doing this, appreciate it!!

@carlory carlory force-pushed the add-validation-for-cluster branch 2 times, most recently from 44637c9 to e42b79e Compare January 13, 2022 02:16
@karmada-bot karmada-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 13, 2022
@carlory
Copy link
Member Author

carlory commented Jan 13, 2022

/cc @RainbowMango @XiShanYongYe-Chang

It's ready for review.

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

LGTM, can you squash some commits.

Signed-off-by: carlory <baofa.fan@daocloud.io>
@carlory
Copy link
Member Author

carlory commented Jan 14, 2022

@XiShanYongYe-Chang done.

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/approve
Leave LGTM to @XiShanYongYe-Chang

@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

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

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 14, 2022
@XiShanYongYe-Chang
Copy link
Member

/lgtm

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 14, 2022
@karmada-bot karmada-bot merged commit 01e4bc9 into karmada-io:master Jan 14, 2022
@carlory carlory deleted the add-validation-for-cluster branch July 13, 2022 14:10
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. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants