Skip to content

Commit 341df1a

Browse files
authored
Merge pull request kubernetes#130451 from danwinship/e2e-np-bad-cidrs
Fix bad CIDRs in a NetworkPolicy test
2 parents 8cca6d9 + f2e8fe9 commit 341df1a

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

test/e2e/network/netpol/network_policy.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package netpol
1919
import (
2020
"context"
2121
"fmt"
22+
"net"
2223
"time"
2324

2425
"k8s.io/apimachinery/pkg/util/intstr"
@@ -1022,16 +1023,15 @@ var _ = common.SIGDescribe("Netpol", func() {
10221023
ports := []int32{80}
10231024
k8s = initializeResources(ctx, f, protocols, ports)
10241025
nsX, _, _ := getK8sNamespaces(k8s)
1025-
podList, err := f.ClientSet.CoreV1().Pods(nsX).List(ctx, metav1.ListOptions{LabelSelector: "pod=a"})
1026-
framework.ExpectNoError(err, "Failing to find pod x/a")
1027-
podA := podList.Items[0]
10281026

1029-
podServerAllowCIDR := fmt.Sprintf("%s/4", podA.Status.PodIP)
1030-
1031-
podList, err = f.ClientSet.CoreV1().Pods(nsX).List(ctx, metav1.ListOptions{LabelSelector: "pod=b"})
1027+
podList, err := f.ClientSet.CoreV1().Pods(nsX).List(ctx, metav1.ListOptions{LabelSelector: "pod=b"})
10321028
framework.ExpectNoError(err, "Failing to find pod x/b")
10331029
podB := podList.Items[0]
10341030

1031+
// Create a rule that allows egress to a large set of IPs around
1032+
// podB, but not podB itself.
1033+
1034+
podServerAllowCIDR := makeLargeCIDRForIP(podB.Status.PodIP)
10351035
hostMask := 32
10361036
if utilnet.IsIPv6String(podB.Status.PodIP) {
10371037
hostMask = 128
@@ -1056,21 +1056,19 @@ var _ = common.SIGDescribe("Netpol", func() {
10561056
ports := []int32{80}
10571057
k8s = initializeResources(ctx, f, protocols, ports)
10581058
nsX, _, _ := getK8sNamespaces(k8s)
1059-
podList, err := f.ClientSet.CoreV1().Pods(nsX).List(ctx, metav1.ListOptions{LabelSelector: "pod=a"})
1060-
framework.ExpectNoError(err, "Failing to find pod x/a")
1061-
podA := podList.Items[0]
10621059

1063-
podList, err = f.ClientSet.CoreV1().Pods(nsX).List(ctx, metav1.ListOptions{LabelSelector: "pod=b"})
1060+
podList, err := f.ClientSet.CoreV1().Pods(nsX).List(ctx, metav1.ListOptions{LabelSelector: "pod=b"})
10641061
framework.ExpectNoError(err, "Failing to find pod x/b")
10651062
podB := podList.Items[0]
10661063

1067-
// Exclude podServer's IP with an Except clause
1064+
// Create a rule that allows egress to a large set of IPs around
1065+
// podB, but not podB itself.
1066+
1067+
podServerAllowCIDR := makeLargeCIDRForIP(podB.Status.PodIP)
10681068
hostMask := 32
10691069
if utilnet.IsIPv6String(podB.Status.PodIP) {
10701070
hostMask = 128
10711071
}
1072-
1073-
podServerAllowCIDR := fmt.Sprintf("%s/4", podA.Status.PodIP)
10741072
podServerExceptList := []string{fmt.Sprintf("%s/%d", podB.Status.PodIP, hostMask)}
10751073
egressRule1 := networkingv1.NetworkPolicyEgressRule{}
10761074
egressRule1.To = append(egressRule1.To, networkingv1.NetworkPolicyPeer{IPBlock: &networkingv1.IPBlock{CIDR: podServerAllowCIDR, Except: podServerExceptList}})
@@ -1083,8 +1081,8 @@ var _ = common.SIGDescribe("Netpol", func() {
10831081

10841082
ValidateOrFail(k8s, &TestCase{ToPort: 80, Protocol: v1.ProtocolTCP, Reachability: reachability})
10851083

1084+
// Create a second NetworkPolicy which allows access to podB
10861085
podBIP := fmt.Sprintf("%s/%d", podB.Status.PodIP, hostMask)
1087-
//// Create NetworkPolicy which allows access to the podServer using podServer's IP in allow CIDR.
10881086
egressRule3 := networkingv1.NetworkPolicyEgressRule{}
10891087
egressRule3.To = append(egressRule3.To, networkingv1.NetworkPolicyPeer{IPBlock: &networkingv1.IPBlock{CIDR: podBIP}})
10901088
allowPolicy := GenNetworkPolicyWithNameAndPodMatchLabel("allow-client-a-via-cidr-egress-rule",
@@ -1465,3 +1463,14 @@ func initializeResources(ctx context.Context, f *framework.Framework, protocols
14651463
framework.ExpectNoError(err, "unable to initialize resources")
14661464
return k8s
14671465
}
1466+
1467+
// makeLargeCIDRForIP returns a CIDR that matches the given IP and many many many other
1468+
// IPs. (Specifically, it returns the /4 that contains the IP.)
1469+
func makeLargeCIDRForIP(ip string) string {
1470+
podIP := utilnet.ParseIPSloppy(ip)
1471+
if ip4 := podIP.To4(); ip4 != nil {
1472+
podIP = ip4
1473+
}
1474+
cidrBase := podIP.Mask(net.CIDRMask(4, 8*len(podIP)))
1475+
return fmt.Sprintf("%s/4", cidrBase.String())
1476+
}

0 commit comments

Comments
 (0)