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 policy in cli according to policy schema #1817

Merged

Conversation

NoSkillGirl
Copy link
Contributor

@NoSkillGirl NoSkillGirl commented Apr 18, 2021

Related issue

closes #1700

What type of PR is this

/kind bug

Proposed Changes

  • Changed policy crd format.
  • Using K8s validate function to validate the resource.

Proof Manifests

Use the below policy (addRequest.yaml):

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: add-requests
spec:
  rules:
  - name: "Set memory and/or cpu requests for all pods in namespaces labeled 'myprivatelabel'"
    match:
      resources:
        kinds:
        - Pod
        namespaceSelector:
          matchExpressions:
            - key: namespace-type
              operator: In
              values:
              - myprivatelabel
    mutate:
      overlay:
        spec:
          containers:
          - (name): "*"
            resources:
              requests:
                cpu: "1000m"

Validate the above policy in kyverno CLI using:
kyverno validate addRequest.yaml

As the rule name is greater than 63 characters, kyverno validation fails with the following error:

Policy add-requests is invalid.
Error: invalid policy.
Cause: [spec.rules.name: Invalid value: "Set memory and/or cpu requests for all pods in namespaces labeled 'myprivatelabel'": spec.rules.name in body should be at most 63 chars long]

Checklist

  • I have read the contributing guidelines.
  • [] I have added tests that prove my fix is effective or that my feature works.
  • [] My PR contains new or altered behavior to Kyverno and
    • [] I have added or changed the documentation myself in an existing PR and the link is:
    • [] I have raised an issue in kyverno/website to track the doc update and the link is:
    • [] I have read the PR documentation guide and followed the process including adding proof manifests to this PR.

@NoSkillGirl NoSkillGirl added wip work in progress and removed wip work in progress labels Apr 18, 2021
@realshuting
Copy link
Member

@NoSkillGirl - can you add "proof manifests" to this PR?

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
@NoSkillGirl NoSkillGirl force-pushed the validate_policy_in_cli_according_to_crd branch from 1341272 to fe05f03 Compare April 21, 2021 04:59
@NoSkillGirl
Copy link
Contributor Author

@NoSkillGirl - can you add "proof manifests" to this PR?

Added.

Copy link
Member

@realshuting realshuting left a comment

Choose a reason for hiding this comment

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

@NoSkillGirl - the changes look good, can you also add unit tests to cover this change?

@realshuting realshuting self-assigned this Apr 21, 2021
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
@NoSkillGirl NoSkillGirl added the wip work in progress label Apr 22, 2021
Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
@NoSkillGirl NoSkillGirl removed the wip work in progress label Apr 22, 2021
Copy link
Member

@realshuting realshuting left a comment

Choose a reason for hiding this comment

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

Looks good!

Forgot to mention this last time, can you address this?

pkg/kyverno/validate/command.go#L199

[golangci] reported by reviewdog 🐶
S1002: should omit comparison to bool constant, can be simplified to `invalidPolicyFound` (gosimple)

Raw Output:
pkg/kyverno/validate/command.go:199:5: S1002: should omit comparison to bool constant, can be simplified to `invalidPolicyFound` (gosimple)
	if invalidPolicyFound == true {

@NoSkillGirl
Copy link
Contributor Author

Looks good!

Forgot to mention this last time, can you address this?

pkg/kyverno/validate/command.go#L199

[golangci] reported by reviewdog 🐶
S1002: should omit comparison to bool constant, can be simplified to `invalidPolicyFound` (gosimple)

Raw Output:
pkg/kyverno/validate/command.go:199:5: S1002: should omit comparison to bool constant, can be simplified to `invalidPolicyFound` (gosimple)
	if invalidPolicyFound == true {

will add the change

Signed-off-by: NoSkillGirl <singhpooja240393@gmail.com>
@NoSkillGirl NoSkillGirl merged commit 1da4f74 into kyverno:main Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

policy valid or not ?
2 participants