Skip to content

Commit

Permalink
OCPBUGS-974: Sync upstream (#30)
Browse files Browse the repository at this point in the history
* Add pod-iptables option to store pod iptables

This change introduces pod-iptables option to store iptables-rules
in pod's network namespace. This helps administrator/engineer to
troubleshooting.

* Fix owners file

* Update CI pipeline

* Add label to Dockerfile

* Update github action to simplify

* Use GITHUB_TOKEN for push packages

* Update slack URL in README

* fix workflows

* Fix some timing issue and change memory limit

* Add namespace check between pod and multi-networkpolicy

* Use TCP as default for Port.Protocol

Add ginkgo test to the suite with only default values.
Add `renderProtocol` function with fallback logic.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Fix to work namespacveSelector policy, without labelSelector

* Support for `NamespaceSelector` (#16)

* Add test case for namespace selector

The case is about having two namespaces with pods
and net-attach-def and a multi networkpolicy that
goes through namespace borders.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Add test case with net-attach-def in other ns

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Improve logging in server.go (#19)

* Add object information to update events

This should make it clearer what k8s object the
daemon is working on.

Increase verbosity threshlod for invoke handlers logs.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Improve error logging

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Add IPv6 support in TODO list

* Set specific version for `revive` tool (#20)

"go getting" github.com/mgechev/revive can lead to unreproducible
builds, as it download the latest "dev" version. Stick to the latest
(v1.2.1) version.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Log filter rules (#23)

* Log filter rules

Logging iptables rules before applying them
can be useful to debug complex scenarios.
Setting verbosity level to 6 as they can be
quite cumbersome.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Clean up logging code

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Refine policy generation routine to support multiple policies

This change refines policy rule generation to introduce conntrack
and support multiple policies in a pod. Fix #17 and #18

* Fix capabilities (#25)

fix #24

* Update github action to fit to latest golang

* Remove docker from support runtime due to obsolated

* Bump github.com/containernetworking/cni from 0.7.1 to 0.8.1 (#31)

Bumps [github.com/containernetworking/cni](https://github.com/containernetworking/cni) from 0.7.1 to 0.8.1.
- [Release notes](https://github.com/containernetworking/cni/releases)
- [Commits](containernetworking/cni@v0.7.1...v0.8.1)

---
updated-dependencies:
- dependency-name: github.com/containernetworking/cni
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump vendor packages.

* Graceful shutdown for daemonset (#32)

* Remove unused errCh

`server.Run()` is not a blocking function and returns always `nil`.
There is no need for a struct field channel.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Allow stopping the server

Add signal handler for SIGTERM and SIGINT to main.go.
Add Stop() method to Options to forward os signals.
Add a channel to stop `syncRunner` and clean iptables afterward.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Add sync-period option for fast sync

* Remove deprecated parameters in deploy.yml

* Add e2e test

* e2e-test: Add script to update server image (#35)

Add a script to redeploy the server in the kind cluster. It is
useful to quickly test new changes without tearing down the
cluster and bringing it up again.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Fix yaml syntax error in GH workflow (#36)

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Add CodeQL workflow for GitHub code scanning (#38)

Co-authored-by: LGTM Migrator <lgtm-migrator@users.noreply.github.com>

* Add NOTICE file for Apache license 2.0 (#39)

This change adds NOTICE file in repository as [1].

[1]: https://infra.apache.org/apply-license.html#new

* IPv6 support in multi-networkpolicy-iptables (#40)

* Support IPv6 networks (#27)

Make Server generates rules for both IP family.
Make iptableBuffer aware of the IP family it is managing, in
order to skip wrong addresses.

Add unit and e2e tests for IPv6 and dual stack networks.

Remove IPv6 item from TODO

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* fix merge-conflict to rebase

* Add e2e ipv6 ingress tests

* IPv6 fix  for NDP and DHCPv6 (#37)

* Add Requirements section to README

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Allow ipv6 Neighbor Discovery Protocol

NDP leverages icmpv6 packets to discover hosts
IPv6 addresses. This kind of packet must be allowed
between hosts, otherwise some policy-allowed traffic
may get blocked.

Adjust unit tests expected output strings.

See https://www.rfc-editor.org/rfc/rfc2373

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Allow DHCPv6 traffic

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Refine icmp/dhcpv6 code

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>
Co-authored-by: Tomofumi Hayashi <tohayash@redhat.com>

* Use string instead of byte in unit-test cases

In real code, use bytes for performance, however, we don't care
about performance for unit-test, hence change bytes to string
for ease of troubleshooting.

* Make INGRESS/EGRESS-COMMON configurable by command line option

This change makes MULTI-{INGRESS,EGRESS}-COMMON chain configurable
to provide a way to support various v4/v6 network.

* Fix CodeQL warnings

* Update docs/configurations.md

Co-authored-by: Nikhil Simha <simha.nikhil@gmail.com>

* Update docs/configurations.md

Co-authored-by: Nikhil Simha <simha.nikhil@gmail.com>

* Wait for sync between policy/iptables in e2e tests

---------

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>
Co-authored-by: Andrea Panattoni <apanatto@redhat.com>
Co-authored-by: Nikhil Simha <simha.nikhil@gmail.com>

* Fix github action

* Avoid using cri-api `v1alpha2` (#43)

As of v1.26.0 kubernetes removed support for api cri-api
v1alpha2
kubernetes/kubernetes#110618

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Fix typo in container registry domain (#44)

* Update go mod to security vulnerability

Update golang.org/x/text to v0.3.8 for vulnerability.

* Fix github action

* Update vendors to fix dependabot alerts

* Add ipblock bat tests in e2e (#48)

This change introduces ipblock tests in e2e and enables v6
ingress tests in e2e as well.

* Fix iptables rules in multiple items in ingress/egress (#49)

This change fixes iptables rules for multiple items
in ingress/egress. It also adds e2e tests for that.
fix #45

* Update golang to 1.20

* Fix end2end tests (#53)

* e2e: Save kind logs as artifacts

Saving `kind export logs` output when
end-to-end job fails helps debugging flakes
and test failures.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* build: set CGO_ENABLED=0

Setting CGO_ENABLED=0 for go builds
produces GLIBC independant binaries.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

---------

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Infer PolicyTypes if missing (#50)

* Infer PolicyTypes if missing

In cases where Spec.PolicyTypes is not specified, it should
default to the existence of Ingress or Egress rules.

Updating end2end tests to cover also this scenario.

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* e2e: Wait for policy sync during setup

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

---------

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>

* Bump google.golang.org/grpc from 1.38.0 to 1.53.0 (#52)

Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.38.0 to 1.53.0.
- [Release notes](https://github.com/grpc/grpc-go/releases)
- [Commits](grpc/grpc-go@v1.38.0...v1.53.0)

---
updated-dependencies:
- dependency-name: google.golang.org/grpc
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Update e2e environments (#54)

* Fix linter warning (#55)

---------

Signed-off-by: Andrea Panattoni <apanatto@redhat.com>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Doug Smith <dosmith@redhat.com>
Co-authored-by: Andrea Panattoni <apanatto@redhat.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: lgtm-com[bot] <43144390+lgtm-com[bot]@users.noreply.github.com>
Co-authored-by: LGTM Migrator <lgtm-migrator@users.noreply.github.com>
Co-authored-by: Nikhil Simha <simha.nikhil@gmail.com>
Co-authored-by: Peter Stöckli <p-@github.com>
  • Loading branch information
8 people committed Jul 14, 2023
1 parent cf5e9cf commit 0d76ba7
Show file tree
Hide file tree
Showing 576 changed files with 59,834 additions and 19,636 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-images.yml
Expand Up @@ -16,4 +16,4 @@ jobs:
uses: docker/build-push-action@v2
with:
push: false
tags: gchr.io/${{ github.repository }}:latest-amd64
tags: ghcr.io/${{ github.repository }}:latest-amd64
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.17.x, 1.18.x]
go-version: [1.19.x, 1.20.x]
goarch: [amd64, arm64]
runs-on: ubuntu-latest
if: >
Expand Down
26 changes: 23 additions & 3 deletions .github/workflows/kind-e2e.yml
Expand Up @@ -11,7 +11,7 @@ jobs:
run: sudo apt install bats

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup registry
run: docker run -d --restart=always -p "5000:5000" --name "kind-registry" registry:2
Expand All @@ -28,8 +28,28 @@ jobs:
working-directory: ./e2e
run: |
export TERM=dumb
# enable ip6_tables
sudo modprobe ip6_tables
bats ./tests/simple-v4-ingress.bats
bats ./tests/simple-v4-egress.bats
bats ./tests/simple-v6-ingress.bats
bats ./tests/ipblock.bats
# stacked case
bats ./tests/stacked.bats
# this should be validated once the v6 issue is fixed.
#bats ./v6work/simple-v6-ingress.bats
bats ./tests/ipblock-stacked.bats
# multiple items in egress/ingress
bats ./tests/ipblock-list.bats
bats ./tests/simple-v4-ingress-list.bats
bats ./tests/simple-v4-egress-list.bats
- name: Export kind logs
if: ${{ failure() }}
run:
./e2e/bin/kind export logs /tmp/kind-logs

- name: Upload logs
uses: actions/upload-artifact@v2
if: ${{ failure() }}
with:
name: kind-logs-e2e
path: /tmp/kind-logs/
2 changes: 1 addition & 1 deletion .github/workflows/push-master.yml
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: docker/build-push-action@v3
with:
push: true
platform: linux/arm64
platform: linux/amd64
tags: |
ghcr.io/${{ github.repository }}:latest-amd64
ghcr.io/${{ github.repository }}:snapshot-amd64
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.18.x]
go-version: [1.20.x]
os: [ubuntu-latest] #, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
9 changes: 5 additions & 4 deletions Dockerfile
@@ -1,15 +1,16 @@
# This Dockerfile is used to build the image available on DockerHub
FROM golang:1.18 as build
FROM golang:1.20 as build

# Add everything
ADD . /usr/src/multi-networkpolicy-iptables

RUN cd /usr/src/multi-networkpolicy-iptables && \
go build ./cmd/multi-networkpolicy-iptables/
CGO_ENABLED=0 go build ./cmd/multi-networkpolicy-iptables/

FROM centos:centos7
FROM fedora:38
LABEL org.opencontainers.image.source https://github.com/k8snetworkplumbingwg/multi-networkpolicy-iptables
RUN yum install -y iptables-utils
RUN dnf install -y iptables-utils iptables-legacy iptables-nft
RUN alternatives --set iptables /usr/sbin/iptables-nft
COPY --from=build /usr/src/multi-networkpolicy-iptables/multi-networkpolicy-iptables /usr/bin
WORKDIR /usr/bin

Expand Down
5 changes: 3 additions & 2 deletions e2e/Dockerfile
@@ -1,5 +1,6 @@
FROM docker.io/fedora:36
FROM docker.io/fedora:38

LABEL org.opencontainers.image.source https://github.com/k8snetworkplumbingwg/multi-networkpolicy-iptables
LABEL org.opencontainers.image.base.name ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
RUN dnf install -y nginx iptables net-tools iputils iproute tcpdump wireshark-cli nmap-ncat
RUN dnf install -y nginx iptables-utils iptables-legacy iptables-nft net-tools iputils iproute tcpdump wireshark-cli nmap-ncat
RUN alternatives --set iptables /usr/sbin/iptables-nft
2 changes: 1 addition & 1 deletion e2e/get_tools.sh
Expand Up @@ -5,7 +5,7 @@ if [ ! -d bin ]; then
mkdir bin
fi

curl -Lo ./bin/kind "https://github.com/kubernetes-sigs/kind/releases/download/v0.15.0/kind-$(uname)-amd64"
curl -Lo ./bin/kind "https://github.com/kubernetes-sigs/kind/releases/download/v0.20.0/kind-$(uname)-amd64"
chmod +x ./bin/kind
curl -Lo ./bin/kubectl https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./bin/kubectl
Expand Down
42 changes: 42 additions & 0 deletions e2e/tests/ipblock-list.bats
@@ -0,0 +1,42 @@
#!/usr/bin/env bats

# Note:
# These test cases, stacked, will create stacked policy rules in one multi-networkpolicy and test the
# traffic policying by ncat (nc) command.

setup() {
cd $BATS_TEST_DIRNAME
load "common"

server_net1=$(get_net1_ip "test-ipblock-list" "pod-server")
client_a_net1=$(get_net1_ip "test-ipblock-list" "pod-client-a")
client_b_net1=$(get_net1_ip "test-ipblock-list" "pod-client-b")
client_c_net1=$(get_net1_ip "test-ipblock-list" "pod-client-c")
}

@test "setup ipblock-list test environments" {
kubectl create -f ipblock-list.yml
run kubectl -n test-ipblock-list wait --for=condition=ready -l app=test-ipblock-list pod --timeout=${kubewait_timeout}
[ "$status" -eq "0" ]
}

@test "test-ipblock-list check client-a" {
run kubectl -n test-ipblock-list exec pod-client-a -- sh -c "echo x | nc -w 1 ${server_net1} 5555"
[ "$status" -eq "0" ]
}

@test "test-ipblock-list check client-b" {
run kubectl -n test-ipblock-list exec pod-client-b -- sh -c "echo x | nc -w 1 ${server_net1} 5555"
[ "$status" -eq "0" ]
}

@test "test-ipblock-list check client-c" {
run kubectl -n test-ipblock-list exec pod-client-c -- sh -c "echo x | nc -w 1 ${server_net1} 5555"
[ "$status" -eq "1" ]
}

@test "cleanup environments" {
kubectl delete -f ipblock-list.yml
run kubectl -n test-ipblock-list wait --for=delete -l app=test-ipblock-list pod --timeout=${kubewait_timeout}
[ "$status" -eq "0" ]
}
140 changes: 140 additions & 0 deletions e2e/tests/ipblock-list.yml
@@ -0,0 +1,140 @@
---
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
namespace: default
name: macvlan1-ipblock
spec:
config: '{
"cniVersion": "0.3.1",
"name": "macvlan1-ipblock",
"plugins": [
{
"type": "macvlan",
"mode": "bridge",
"capabilities": {"ips": true },
"ipam":{
"type":"static"
}
}]
}'
---
apiVersion: v1
kind: Namespace
metadata:
name: test-ipblock-list
---
# Pods
apiVersion: v1
kind: Pod
metadata:
name: pod-server
namespace: test-ipblock-list
annotations:
k8s.v1.cni.cncf.io/networks: '[{
"name": "macvlan1-ipblock",
"namespace": "default",
"ips": ["2.2.5.1/24"]
}]'
labels:
app: test-ipblock-list
name: pod-server
spec:
containers:
- name: macvlan-worker1
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
command: ["nc", "-kl", "0.0.0.0", "5555"]
securityContext:
privileged: true
---
apiVersion: v1
kind: Pod
metadata:
name: pod-client-a
namespace: test-ipblock-list
annotations:
k8s.v1.cni.cncf.io/networks: '[{
"name": "macvlan1-ipblock",
"namespace": "default",
"ips": ["2.2.5.11/24"]
}]'
labels:
app: test-ipblock-list
name: pod-client-a
spec:
containers:
- name: macvlan-worker1
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
command: ["nc", "-kl", "0.0.0.0", "5555"]
securityContext:
privileged: true
---
apiVersion: v1
kind: Pod
metadata:
name: pod-client-b
namespace: test-ipblock-list
annotations:
k8s.v1.cni.cncf.io/networks: '[{
"name": "macvlan1-ipblock",
"namespace": "default",
"ips": ["2.2.5.12/24"]
}]'
labels:
app: test-ipblock-list
name: pod-client-b
spec:
containers:
- name: macvlan-worker1
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
command: ["nc", "-kl", "0.0.0.0", "5555"]
securityContext:
privileged: true
---
apiVersion: v1
kind: Pod
metadata:
name: pod-client-c
namespace: test-ipblock-list
annotations:
k8s.v1.cni.cncf.io/networks: '[{
"name": "macvlan1-ipblock",
"namespace": "default",
"ips": ["2.2.5.13/24"]
}]'
labels:
app: test-ipblock-list
name: pod-client-c
spec:
containers:
- name: macvlan-worker1
image: ghcr.io/k8snetworkplumbingwg/multi-networkpolicy-iptables:e2e-test
command: ["nc", "-kl", "0.0.0.0", "5555"]
securityContext:
privileged: true
---
# MultiNetworkPolicies
# this policy accepts ingress trafic from pod-client-a to pod-server
# and ingress trafic from pod-client-b to pod-server
# as a result, these policies accepts ingress traffic from pod-client-a
# or from pod-client-b, to pod-server.
apiVersion: k8s.cni.cncf.io/v1beta1
kind: MultiNetworkPolicy
metadata:
name: testnetwork-policy-ipblock-1
namespace: test-ipblock-list
annotations:
k8s.v1.cni.cncf.io/policy-for: default/macvlan1-ipblock
spec:
podSelector:
matchLabels:
name: pod-server
policyTypes:
- Ingress
ingress:
- from:
- ipBlock:
cidr: 2.2.5.11/32
- from:
- ipBlock:
cidr: 2.2.5.12/32
55 changes: 55 additions & 0 deletions e2e/tests/ipblock-stacked.bats
@@ -0,0 +1,55 @@
#!/usr/bin/env bats

# Note:
# These test cases, stacked, will create stacked policy rules in one multi-networkpolicy and test the
# traffic policying by ncat (nc) command.

setup() {
cd $BATS_TEST_DIRNAME
load "common"

server_net1=$(get_net1_ip "test-ipblock-stacked" "pod-server")
client_a_net1=$(get_net1_ip "test-ipblock-stacked" "pod-client-a")
client_b_net1=$(get_net1_ip "test-ipblock-stacked" "pod-client-b")
client_c_net1=$(get_net1_ip "test-ipblock-stacked" "pod-client-c")
}

@test "setup ipblock-stacked test environments" {
kubectl create -f ipblock-stacked.yml
run kubectl -n test-ipblock-stacked wait --for=condition=ready -l app=test-ipblock-stacked pod --timeout=${kubewait_timeout}
[ "$status" -eq "0" ]
}

@test "check generated iptables rules" {
# wait for sync
sleep 3
run kubectl -n test-ipblock-stacked exec pod-server -it -- sh -c "iptables-save | grep MULTI-0-INGRESS"
[ "$status" -eq "0" ]
run kubectl -n test-ipblock-stacked exec pod-client-a -it -- sh -c "iptables-save | grep MULTI-0-INGRESS"
[ "$status" -eq "1" ]
run kubectl -n test-ipblock-stacked exec pod-client-b -it -- sh -c "iptables-save | grep MULTI-0-INGRESS"
[ "$status" -eq "1" ]
run kubectl -n test-ipblock-stacked exec pod-client-c -it -- sh -c "iptables-save | grep MULTI-0-INGRESS"
[ "$status" -eq "1" ]
}

@test "test-ipblock-stacked check client-a" {
run kubectl -n test-ipblock-stacked exec pod-client-a -- sh -c "echo x | nc -w 1 ${server_net1} 5555"
[ "$status" -eq "0" ]
}

@test "test-ipblock-stacked check client-b" {
run kubectl -n test-ipblock-stacked exec pod-client-b -- sh -c "echo x | nc -w 1 ${server_net1} 5555"
[ "$status" -eq "0" ]
}

@test "test-ipblock-stacked check client-c" {
run kubectl -n test-ipblock-stacked exec pod-client-c -- sh -c "echo x | nc -w 1 ${server_net1} 5555"
[ "$status" -eq "1" ]
}

@test "cleanup environments" {
kubectl delete -f ipblock-stacked.yml
run kubectl -n test-ipblock-stacked wait --for=delete -l app=test-ipblock-stacked pod --timeout=${kubewait_timeout}
[ "$status" -eq "0" ]
}

0 comments on commit 0d76ba7

Please sign in to comment.