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 IPBlock to Network Policy #50033

Merged
merged 3 commits into from
Aug 25, 2017

Conversation

cmluciano
Copy link

@cmluciano cmluciano commented Aug 2, 2017

What this PR does / why we need it:
Add ipBlockRule to NetworkPolicyPeer.

Which issue this PR fixes
fixes #49978

Special notes for your reviewer:

  • I added this directly as a field on the existing API per guidance from API-Machinery/lazy SIG-Network consensus.

Todo:

  • Documentation comments to mention this is beta, unless we want to go straight to GA
  • e2e tests

Release note:

Support ipBlock in NetworkPolicy

@cmluciano cmluciano added the sig/network Categorizes an issue or PR as relevant to SIG Network. label Aug 2, 2017
@cmluciano cmluciano added this to the v1.8 milestone Aug 2, 2017
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 2, 2017
@k8s-github-robot k8s-github-robot added kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API size/L Denotes a PR that changes 100-499 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 2, 2017
@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from 7c5094e to 7ad35fb Compare August 9, 2017 17:57
@cmluciano
Copy link
Author

I'm not sure if I generated the code improperly. The build failures are a bit cryptic to me.

@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from 7ad35fb to f3b9bc4 Compare August 11, 2017 17:54
@k8s-github-robot k8s-github-robot 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 Aug 11, 2017
@cmluciano
Copy link
Author

@kubernetes/sig-api-machinery-misc Can anyone help diagnose what I'm failing to run/add here?

@k8s-ci-robot k8s-ci-robot added the sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. label Aug 11, 2017
@thockin
Copy link
Member

thockin commented Aug 11, 2017

What is the use case for the selector semantic here, vs simply "from cidr". Can you show concretely how this is to be used that can't be solved by simpler? This backdoor-introduces "deny" logic, and I worry that this turns into a firewall.

@cmluciano
Copy link
Author

This was based on conversations in #49978 (comment) about how to implement the deny-type of wording. I went with this since it matched current styles for podSelectors.

cc @danwinship @caseydavenport

@caseydavenport
Copy link
Member

@thockin I expressed similar concern. The reasoning is documented in this discussion: #49978 (comment)

The TL;DR is that some users want to allow traffic to/from a set of IPs that doesn't cleanly align with a single CIDR.

e.g. "allow from everywhere in 192.168.0.0/16 except for 192.168.8.0/24"

To reiterate my stance from the other thread:

  • I'd much much prefer a simple CIDR field
  • This is a bit more flexible (also more confusing), and it's easy for us to implement at least so I won't fight it tooth-and-nail.

@thockin
Copy link
Member

thockin commented Aug 11, 2017 via email

@thockin
Copy link
Member

thockin commented Aug 11, 2017 via email

@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from f3b9bc4 to 66d2516 Compare August 15, 2017 18:21
@k8s-github-robot k8s-github-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 15, 2017
@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from 66d2516 to f1fe142 Compare August 15, 2017 19:57
@cmluciano
Copy link
Author

I think my test failures are caused by the v1beta code not converting or dropping the new ipBlock field.

@thockin @sttts Does the conversion code under pkg/apis/extensions/conversion.go persist for one more release?

@thockin
Copy link
Member

thockin commented Aug 17, 2017 via email

@thockin
Copy link
Member

thockin commented Aug 17, 2017 via email

@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from f1fe142 to cbbd2e6 Compare August 18, 2017 02:17
type CIDR string

// Except is a slice of CIDRs to not include within an IpBlockRule
type Except []string
Copy link
Contributor

Choose a reason for hiding this comment

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

You're not using either the CIDR type or the Except type. (IpBlockRule just uses string and []string directly. And I feel like that's correct.)

// IpBlockRule describes a particular CIDR (Ex. "192.168.1.1/24") that is allowed to the pods
// matched by a NetworkPolicySpec's podSelector. The except entry describes CIDRs that should
// not be included within this rule.
type IpBlockRule struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Capital P. IPBlockRule. Acronyms are always either all-caps or all-lowercase. (So IPBlockRule here and ipBlockRule in the JSON.)

// matched by a NetworkPolicySpec's podSelector. The except entry describes CIDRs that should
// not be included within this rule.
type IpBlockRule struct {
// CIDR is a string representing a Classless inter-domain route
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't explain what "CIDR" stands for anywhere else, so I'd just say "is a string representing the IP block". (If you do want to expand the acronym, be consistent with the capitalization.)

// CIDR is a string representing a Classless inter-domain route
// Valid examples are "192.168.1.1/24"
CIDR string
// Except is a slice of CIDRs that should not be included within an IPBlock rule
Copy link
Contributor

Choose a reason for hiding this comment

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

There's inconsistency throughout about "IPBlockRule" vs "IPBlock"

Copy link
Author

Choose a reason for hiding this comment

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

You're right. I have no idea why I came up with rule and it becomes terribly confusing. I switched the the networking.IPBlock to represent the type for a IPBlockSelector.

Should we continue to use something like ipBlock in the json or ipBlockSelector?


// IpBlockrule defines policy on a particular CIDR
// +optional
IpBlockRule *IpBlockRule
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe call it IPBlockSelector or CIDRSelector for consistency with the other fields?

func ValidateIpBlock(br *networking.IpBlockRule, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(br.CIDR) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("cidr"), "ibBlock rules must specify a CIDR"))
Copy link
Contributor

Choose a reason for hiding this comment

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

beyond the continued inconsistency about "ip block" vs "ip block rule", there's also a typo here ("ib")

for i, exceptIP := range br.Except {
exceptPath := fldPath.Child("except").Index(i)
if exceptIP == "" || exceptIP == " " {
allErrs = append(allErrs, field.Invalid(exceptPath.Child("except"), exceptIP, "value cannot be an empty string"))
Copy link
Contributor

Choose a reason for hiding this comment

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

should call IsValidCIDR

// Except is a slice of CIDRs that should not be included within an IPBlock rule
// Valid examples are "192.168.1.1/24"
// +optional
Except []string `json:"except,omitempty" protobuf:"bytes,2,opt,name=except"`
Copy link
Contributor

Choose a reason for hiding this comment

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

rep not opt in the protobuf spec (because it's an array)

@k8s-github-robot k8s-github-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 21, 2017
@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from 800f481 to 107c56a Compare August 22, 2017 01:53
@cmluciano
Copy link
Author

/retest

@cmluciano
Copy link
Author

@thockin Any ideas on why roundtrips fail only sometimes for me? I thought my fuzzer would help with this and I see the errors irregularly. I'm not sure if this is intentional or if my fuzzer/assumptions about compatibility are wrong.

@cmluciano
Copy link
Author

Talked with Stefan a bit about this. Since we still have v1beta1 I need to encode the new field in an annotation. Also we are still using v1beta1 as the default storage backend. #50604 is open for this.

@thockin
Copy link
Member

thockin commented Aug 22, 2017

I'll review now. What is teh story with test fail? Do we understand it? ETA for fix?

@k8s-github-robot k8s-github-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Aug 22, 2017
Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

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

small changes, pls. Otherwise LGTM

// CIDR is a string representing the IP Block
// Valid examples are "192.168.1.1/24"
CIDR string
// Except is a slice of CIDRs that should not be included within an IP Block
Copy link
Member

Choose a reason for hiding this comment

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

Comment (and validate) that the CIDRs in except must be subsets of cidr


// IPBlockSelector defines policy on a particular IPBlock
// +optional
IPBlockSelector *IPBlock
Copy link
Member

Choose a reason for hiding this comment

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

This should be IPBlock. It is not a Selector.

func ValidateIPBlock(br *networking.IPBlock, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(br.CIDR) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("cidr"), "ipBlock must specify a CIDR"))
Copy link
Member

Choose a reason for hiding this comment

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

field.Required(fldPath.Child("cidr"), "") - the last error string is redundant here.

allErrs = append(allErrs, field.Required(fldPath.Child("cidr"), "ipBlock must specify a CIDR"))
}
if !IsValidCIDR(br.CIDR) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("cidr"), br.CIDR, "value is not a valid CIDR"))
Copy link
Member

Choose a reason for hiding this comment

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

"not a valid CIDR"

for i, exceptIP := range br.Except {
exceptPath := fldPath.Child("except").Index(i)
if !IsValidCIDR(exceptIP) {
allErrs = append(allErrs, field.Invalid(exceptPath.Child("except"), exceptIP, "value cannot be an empty string"))
Copy link
Member

Choose a reason for hiding this comment

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

"not a valid CIDR"

Copy link
Member

Choose a reason for hiding this comment

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

validate that it is "in" the main CIDR

}

// IsValidCIDR validates whether a CIDR matches the conventions expected by net.ParseCIDR
func IsValidCIDR(cidr string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

does this need to be exposed or could it be isValidCIDR ?

@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from 107c56a to a9528b6 Compare August 22, 2017 22:08
@thockin
Copy link
Member

thockin commented Aug 24, 2017

ping me on slack when ready. I want to turn around PRs as fast as I can...

@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from a9528b6 to f89fd5a Compare August 24, 2017 19:10
@cmluciano cmluciano changed the title WIP: Add CIDRSelector to Network Policy Add IPBlock to Network Policy Aug 24, 2017
@cmluciano
Copy link
Author

@thockin @caseydavenport @danwinship Ready for review

Christopher M. Luciano added 3 commits August 24, 2017 16:20
Signed-off-by: Christopher M. Luciano <cmluciano@us.ibm.com>
Signed-off-by: Christopher M. Luciano <cmluciano@us.ibm.com>
Signed-off-by: Christopher M. Luciano <cmluciano@us.ibm.com>
@cmluciano cmluciano force-pushed the cml/addnpcidrselector branch from f89fd5a to 02735c3 Compare August 24, 2017 20:20
@thockin
Copy link
Member

thockin commented Aug 24, 2017

/lgtm
/approve
/retest

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cmluciano, thockin

Associated issue: 49978

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 24, 2017
// matched by a NetworkPolicySpec's podSelector. The except entry describes CIDRs that should
// not be included within this rule.
type IPBlock struct {
// CIDR is a string representing the IP Block
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we usually avoid putting the field name into the description? For some doc generation reason I believe.

@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 50033, 49988, 51132, 49674, 51207)

@k8s-github-robot k8s-github-robot merged commit c04e516 into kubernetes:master Aug 25, 2017
@k8s-ci-robot
Copy link
Contributor

@cmluciano: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kubernetes-e2e-gce-etcd3 02735c3 link /test pull-kubernetes-e2e-gce-etcd3
pull-kubernetes-e2e-kops-aws 02735c3 link /test pull-kubernetes-e2e-kops-aws

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/network Categorizes an issue or PR as relevant to SIG Network. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support CIDR on ingress network-policy
7 participants