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

Improve Antrea-native Policy CRD schema verification #2125

Merged
merged 5 commits into from
May 6, 2021

Conversation

wenqiq
Copy link
Contributor

@wenqiq wenqiq commented Apr 25, 2021

Add namespaceSelector/podSelector validations in all CRD schema.

The incorrect input of labelSelector fields that have known schema should be rejected;

In the following example, we want to create ClusterNetworkPolicy resource:

spec.appliedTo.namespaceSelector must be matchLabels or matchExpressions,
actually in the existing version the spelling mistake ‘matchLables’ can not be checked out.
Save the CustomResourceDefinition to resourcedefinition.yaml:

cat acnp_demo.yaml

apiVersion: crd.antrea.io/v1alpha1
kind: ClusterNetworkPolicy
metadata:
  name: blacklist
spec:
    priority: 5
    tier: securityops
    appliedTo:
      - namespaceSelector:
          matchLables:
            env: dummy
    ingress:
      - action: Drop

We created it succeeded without any error.

After fixed the issue, we will get the following error message as expected:

kubectl create -f acnp_demo.yaml
error: error validating "acnp_demo.yaml": error validating data: ValidationError(ClusterNetworkPolicy.spec.appliedTo[0].namespaceSelector): unknown field "matchLables" in io.antrea.crd.v1alpha1.ClusterNetworkPolicy.spec.appliedTo.namespaceSelector; if you choose to ignore these errors, turn validation off with --validate=false

In addition, all of namespaceSelector/podSelector parameters verification in CRD schema will be improved.

Fix #2090

@codecov-commenter
Copy link

codecov-commenter commented Apr 25, 2021

Codecov Report

Merging #2125 (2a50612) into main (d258bbc) will increase coverage by 19.83%.
The diff coverage is n/a.

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2125       +/-   ##
===========================================
+ Coverage   41.41%   61.25%   +19.83%     
===========================================
  Files         131      269      +138     
  Lines       16502    20453     +3951     
===========================================
+ Hits         6834    12528     +5694     
+ Misses       9084     6631     -2453     
- Partials      584     1294      +710     
Flag Coverage Δ
kind-e2e-tests 52.09% <ø> (?)
unit-tests 41.40% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/apiserver/handlers/webhook/mutation_labels.go 24.71% <0.00%> (ø)
...ntroller/crdmirroring/crdhandler/externalentity.go 16.27% <0.00%> (ø)
...lientset/versioned/typed/ops/v1alpha1/traceflow.go 24.09% <0.00%> (ø)
...tset/versioned/typed/core/v1alpha2/clustergroup.go 24.09% <0.00%> (ø)
pkg/agent/apiserver/handlers/errors.go 0.00% <0.00%> (ø)
pkg/ovs/ovsctl/interface.go 0.00% <0.00%> (ø)
pkg/agent/interfacestore/interface_cache.go 81.37% <0.00%> (ø)
pkg/signals/signals.go 100.00% <0.00%> (ø)
...gacyclient/listers/core/v1alpha2/externalentity.go 6.25% <0.00%> (ø)
pkg/ipfix/ipfix_process.go 100.00% <0.00%> (ø)
... and 225 more

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

Could you remove the ending ";" and issue ID from the commit title?

build/yamls/base/crds.yml Outdated Show resolved Hide resolved
@wenqiq wenqiq force-pushed the fix-issue-2090 branch 3 times, most recently from 22559c6 to 4463d19 Compare April 27, 2021 03:58
@wenqiq wenqiq closed this Apr 27, 2021
@wenqiq wenqiq reopened this Apr 27, 2021
@wenqiq wenqiq force-pushed the fix-issue-2090 branch 3 times, most recently from 4907307 to a59b35b Compare April 27, 2021 08:05
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

Is the PR ready for review? I see commented code

@wenqiq
Copy link
Contributor Author

wenqiq commented Apr 27, 2021

Is the PR ready for review? I see commented code

yes, wait for code review

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

LGTM overall, maybe @Dyanngg can take a look too.


t.Run("TestGroupInvalidLabelSelectorInResource", func(t *testing.T) {
t.Run("Case=InvalidACNPPodSelectorNsSelectorMatchExpressions", func(t *testing.T) {
testInvalidACNPPodSelectorNsSelectorMatchExpressions(t)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add it to TestGroupValidateAntreaNativePolicies? it seems not different from cases in that group.
And maybe remove testInvalidCGPPodSelectorNsSelectorMatchExpressions as it uses same verification as policies (normally it's fine to have a separate test for ClusterGroup but we are working on reducing e2e runtime #2014 so let's avoid adding it unless it can cover something new)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in a new commit

tnqn
tnqn previously approved these changes Apr 27, 2021
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

LGTM

@tnqn
Copy link
Member

tnqn commented Apr 27, 2021

/test-all

@tnqn
Copy link
Member

tnqn commented Apr 28, 2021

All e2e tests failed on antrea networkpolicy case, which seems related to this change. @wenqiq could you take a look at the failures?

@wenqiq
Copy link
Contributor Author

wenqiq commented Apr 28, 2021

All e2e tests failed on antrea networkpolicy case, which seems related to this change. @wenqiq could you take a look at the failures?

well, I will check, It sames the e2e tests fails even with the upstream/main branch as I test yesterday.

@tnqn
Copy link
Member

tnqn commented Apr 28, 2021

We run e2e tests on VMs and Kind clusters, all tests related to Antrea policy failed on all platforms, and only they failed. This indicated it's related to the PR.

@wenqiq wenqiq force-pushed the fix-issue-2090 branch 2 times, most recently from 2a50612 to c6183c3 Compare April 28, 2021 11:59
items:
type: string
matchLabels:
type: object
Copy link
Member

Choose a reason for hiding this comment

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

I guess you need to set "x-kubernetes-preserve-unknown-fields: true" for matchLabels, otherwise all keys are pruned so the e2e tests failed. Glad to see it's caught by the tests.
https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#controlling-pruning

Copy link
Contributor Author

@wenqiq wenqiq Apr 29, 2021

Choose a reason for hiding this comment

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

Thanks for advice,obviously,some resource can not created as expected in e2e test cases cause the fails, but I have verified those crds in my local kind cluster; I tried reviewing the e2e test cases to find sth, however I did not find an efficient method to implement the e2e test in my local dev env.

https://github.com/vmware-tanzu/antrea/runs/2463089906?check_suite_focus=true
It sames work, thks so much.

@wenqiq
Copy link
Contributor Author

wenqiq commented Apr 29, 2021

e2e test back to normal, pls review the latest changes, thanks a lot @tnqn
https://github.com/vmware-tanzu/antrea/actions/runs/794762925

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

@tnqn
Copy link
Member

tnqn commented Apr 29, 2021

/test-all

@tnqn
Copy link
Member

tnqn commented Apr 29, 2021

/test-windows-e2e

@@ -14,3 +14,4 @@ bin

.idea/
.vscode/
vendor
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this should be a local ignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

github/gitignore/Go.gitignore doesn't add vendor/ to its .gitignore file.

# Dependency directories (remove the comment below to include it)
# vendor/

gitignore.io/api/go does add vendor/ to its .gitignore.

# Dependency directories (remove the comment below to include it)
# vendor/

### Go Patch ###
/vendor/
/Godeps/

Copy link
Member

Choose a reason for hiding this comment

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

@wenqiq Normally it would be better to add this via a separate PR if it is controversial so this PR can proceed faster.

Anyway Antrea doesn't include vendor directory in the repo so at least it's harmless to add it here for the convenience of vendor mode developers. @Dyanngg Do you have strong opinion to remove it from this PR? If not, I'm going to merge it as is.

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 have a strong opinion for this. Merged.

@Dyanngg
Copy link
Contributor

Dyanngg commented Apr 29, 2021

/test-networkpolicy

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.

Antrea-native Policy CRD schema verification should be improved
5 participants