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

core: validate crd name size from admission controller #11233

Merged
merged 1 commit into from
Nov 3, 2022

Conversation

parth-gr
Copy link
Member

@parth-gr parth-gr commented Oct 27, 2022

Description of your changes:

Which issue is resolved by this Pull Request:
Resolves ##11212

Checklist:

  • Commit Message Formatting: Commit titles and messages follow guidelines in the developer guide).
  • Skip Tests for Docs: If this is only a documentation change, add the label skip-ci on the PR.
  • Reviewed the developer guide on Submitting a Pull Request
  • Pending release notes updated with breaking and/or notable changes for the next minor release.
  • Documentation has been updated, if necessary.
  • Unit tests have been added, if necessary.
  • Integration tests have been added, if necessary.

@parth-gr parth-gr changed the title Crd length validation core: validate crd name size from admission controller Oct 27, 2022
for _, crd := range o.resources {
var crdNameLength int = len(crd.Name)
if crdNameLength > 38 {
return errors.New("CRD name cannot be bigger than 38 characters")
Copy link
Member

Choose a reason for hiding this comment

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

As discussed, having a single limit for all CRs may break some existing CRs if it's not actually a limit already hit with some K8s error. We may want to just address the original bug for the object store and only add a check for that CR length to start with.

func (o *Operator) startCRDManager(context context.Context, mgrErrorCh chan error) {
logger.Info("check if CRDs name are valid")
Copy link
Member

Choose a reason for hiding this comment

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

We need to check the CR name lengths in the admission controller. For example, in the object store CR this would be in the ValidateObjectSpec() method.

Checking at operator startup time won't prevent the CRs from being created incorrectly.

Copy link
Contributor

Choose a reason for hiding this comment

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

right and actually we need to update this method

func (o *CephObjectStore) ValidateCreate() error {
logger.Infof("validate create cephobjectstore %v", o)
if err := ValidateObjectSpec(o); err != nil {
return err
}
return nil

since ValidateObjectSpec() is called during validate updatealso which will be problem for upgrade.

Copy link
Member Author

Choose a reason for hiding this comment

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

@subhamkrai why it would be a problem during upgrade

Copy link
Member Author

@parth-gr parth-gr Oct 28, 2022

Choose a reason for hiding this comment

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

@travisn >Checking at operator startup time won't prevent the CRs from being created incorrectly.
That right,
I thought this process will reconcile later, but if it is been created at very first then there is no need to add a check,
So updated the validate method
Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

@subhamkrai why it would be a problem during upgrade

see here #11233 (comment)

@@ -76,6 +76,9 @@ func ValidateObjectSpec(gs *CephObjectStore) error {
if gs.Namespace == "" {
return errors.New("missing namespace")
}
if len(gs.Name) > 38 {
return errors.New("object store name cannot be bigger than 38 characters")
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
return errors.New("object store name cannot be bigger than 38 characters")
return errors.New("object store name cannot be longer than 38 characters")

@@ -76,6 +76,9 @@ func ValidateObjectSpec(gs *CephObjectStore) error {
if gs.Namespace == "" {
return errors.New("missing namespace")
}
if len(gs.Name) > 38 {
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment here that explains why 38 is the critical number, and maybe show a formula for how it was decided.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

@@ -76,6 +76,9 @@ func ValidateObjectSpec(gs *CephObjectStore) error {
if gs.Namespace == "" {
return errors.New("missing namespace")
}
if len(gs.Name) > 38 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make 38 a const variable on the top

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the reason in the comment

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 vote for the const as well, still helpful for code readability.

Comment on lines 79 to 86
if len(gs.Name) > 38 {
return errors.New("object store name cannot be bigger than 38 characters")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if len(gs.Name) > 38 {
return errors.New("object store name cannot be bigger than 38 characters")
}
if len(gs.Name) > 38 {
return errors.New("object store name cannot be bigger than 38 characters")
}

Also, as I mentioned let's move this block to L63 inside ValidateCluster as this method will trigger during the update also and will cause errors because earlier it was >38 and now webhook will validate with <38 and it will through an error.

@travisn right?

Copy link
Member

Choose a reason for hiding this comment

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

This method is called by ValidateCreate() on L65, so why do we need to move it? I didn't follow.

Copy link
Member Author

@parth-gr parth-gr Oct 31, 2022

Choose a reason for hiding this comment

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

It will not create a problem for upgrade cluster, because a deployment is not possible if the store name exceeded this length

Copy link
Contributor

Choose a reason for hiding this comment

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

let's discuss this

Copy link
Member

@travisn travisn left a comment

Choose a reason for hiding this comment

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

Actually we should be able to this with a schema length check on the types.go property such as:

+kubebuilder:validation:MaxLength=38

Then we don't need code in the admission controller.

@parth-gr
Copy link
Member Author

parth-gr commented Nov 2, 2022

Actually we should be able to this with a schema length check on the types.go property such as:

+kubebuilder:validation:MaxLength=38

Then we don't need code in the admission controller.

That was very first thing I wanted to do but, objectstore is define as

type CephObjectStore struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata"`

metav1.ObjectMeta json:"metadata"`` contains the name definition of the objectstore and Kubernetes define, which should not be edited, right?

@parth-gr parth-gr requested a review from travisn November 2, 2022 14:37
Copy link
Member

@travisn travisn left a comment

Choose a reason for hiding this comment

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

That was very first thing I wanted to do but, objectstore is define as

type CephObjectStore struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata"`

metav1.ObjectMeta json:"metadata"`` contains the name definition of the objectstore and Kubernetes define, which should not be edited, right?

Oh of course, that won't work!

@@ -76,6 +76,9 @@ func ValidateObjectSpec(gs *CephObjectStore) error {
if gs.Namespace == "" {
return errors.New("missing namespace")
}
if len(gs.Name) > 38 {
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 vote for the const as well, still helpful for code readability.

Copy link
Contributor

@subhamkrai subhamkrai left a comment

Choose a reason for hiding this comment

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

build is failing, can you restart and confirm? @parth-gr

@parth-gr parth-gr force-pushed the crd-length-validation branch 2 times, most recently from 8cc0233 to 468747c Compare November 3, 2022 14:27
Closes: rook#11212

Signed-off-by: parth-gr <paarora@redhat.com>
@parth-gr
Copy link
Member Author

parth-gr commented Nov 3, 2022

build is failing, can you restart and confirm? @parth-gr

The build passed thanks @sp98 for pointing out the problem in the comment using an extra ` symbol

@travisn travisn merged commit 9c7a004 into rook:master Nov 3, 2022
travisn added a commit that referenced this pull request Nov 3, 2022
core: validate crd name size from admission controller  (backport #11233)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants