Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions examples/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,20 @@ eval $(minikube docker-env)
#### Create HelloWorldService Instances

```sh
kubectl create -f hello-world-service-composition.yaml --kubeconfig=provider.conf
kubectl create -f hs1.yaml --kubeconfig=provider.conf
kubectl create -f hs2.yaml --kubeconfig=provider.conf
kubectl create -f examples/multitenancy/hello-world/hello-world-service-composition.yaml --kubeconfig=kubeplus-saas-provider.json
```

Wait for the HelloWorldService CRD to be registered:

```sh
until kubectl get crds --kubeconfig=kubeplus-saas-provider.json | grep helloworldservices.platformapi.kubeplus ; do echo "Waiting for HelloWorldService CRD to be registered.."; sleep 1; done
```

Then create the HelloWorldService instances:

```sh
kubectl create -f examples/multitenancy/hello-world/hs1.yaml --kubeconfig=kubeplus-saas-provider.json
kubectl create -f examples/multitenancy/hello-world/hs2.yaml --kubeconfig=kubeplus-saas-provider.json
```

#### Test Network Isolation
Expand All @@ -142,17 +153,17 @@ kubectl create -f hs2.yaml --kubeconfig=provider.conf

```sh
# Get the Pod name for hs1
HELLOWORLD_POD_HS1=$(kubectl get pods -n hs1 --kubeconfig=provider.conf -o jsonpath='{.items[0].metadata.name}')
HELLOWORLD_POD_HS1=$(kubectl get pods -n hs1 --kubeconfig=kubeplus-saas-provider.json -o jsonpath='{.items[0].metadata.name}')

# Get the Pod IP for hs2
HS2_POD_IP=$(kubectl get pods -n hs2 --kubeconfig=provider.conf -o jsonpath='{.items[0].status.podIP}')
HS2_POD_IP=$(kubectl get pods -n hs2 --kubeconfig=kubeplus-saas-provider.json -o jsonpath='{.items[0].status.podIP}')

# Update and install curl on hs1 pod
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- apt update
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- apt install curl -y
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=kubeplus-saas-provider.json -- apt update
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=kubeplus-saas-provider.json -- apt install curl -y

# Test connectivity from hs1 to hs2 using the IP
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP:5000
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=kubeplus-saas-provider.json -- curl $HS2_POD_IP:5000
```

The connection should be denied.
Expand All @@ -161,17 +172,17 @@ kubectl create -f hs2.yaml --kubeconfig=provider.conf

```sh
# Get the Pod name for hs2
HELLOWORLD_POD_HS2=$(kubectl get pods -n hs2 --kubeconfig=provider.conf -o jsonpath='{.items[0].metadata.name}')
HELLOWORLD_POD_HS2=$(kubectl get pods -n hs2 --kubeconfig=kubeplus-saas-provider.json -o jsonpath='{.items[0].metadata.name}')

# Get the Pod IP for hs1
HS1_POD_IP=$(kubectl get pods -n hs1 --kubeconfig=provider.conf -o jsonpath='{.items[0].status.podIP}')
HS1_POD_IP=$(kubectl get pods -n hs1 --kubeconfig=kubeplus-saas-provider.json -o jsonpath='{.items[0].status.podIP}')

# Update and install curl on hs2 pod
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- apt update
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- apt install curl -y
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=kubeplus-saas-provider.json -- apt update
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=kubeplus-saas-provider.json -- apt install curl -y

# Test connectivity from hs2 to hs1 using the IP
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP:5000
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=kubeplus-saas-provider.json -- curl $HS1_POD_IP:5000
```

The connection should be denied.
Expand All @@ -181,15 +192,15 @@ kubectl create -f hs2.yaml --kubeconfig=provider.conf
In some scenarios, you might want to enable controlled communication between instances running in different namespaces. KubePlus provides a custom kubectl plugin for this purpose. To allow bi-directional traffic between the two HelloWorldService instances (deployed in namespaces `hs1` and `hs2`), run:

```sh
kubectl allow network traffic hs1 hs2 -k provider.conf
kubectl allow network traffic hs1 hs2 -k kubeplus-saas-provider.json
```

```sh
# Test connectivity from hs1 to hs2 using the IP
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP:5000
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=kubeplus-saas-provider.json -- curl $HS2_POD_IP:5000

# Test connectivity from hs2 to hs1 using the IP
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP:5000
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=kubeplus-saas-provider.json -- curl $HS1_POD_IP:5000

kubectl get networkpolicy -o yaml restrict-cross-ns-traffic -n hs1
kubectl get networkpolicy -o yaml restrict-cross-ns-traffic -n hs2
Expand All @@ -205,25 +216,25 @@ The connection should be allowed
To deny the traffic between namespace

```sh
kubectl deny network traffic hs1 hs2 -k provider.conf
kubectl deny network traffic hs1 hs2 -k kubeplus-saas-provider.json
```

```sh
# Test connectivity from hs1 to hs2 using the IP
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP:5000
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=kubeplus-saas-provider.json -- curl $HS2_POD_IP:5000

# Test connectivity from hs2 to hs1 using the IP
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP:5000
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=kubeplus-saas-provider.json -- curl $HS1_POD_IP:5000
```



## Clean Up

```sh
kubectl delete -f hs1-no-replicas.yaml --kubeconfig=provider.conf
kubectl delete -f hs2-no-replicas.yaml --kubeconfig=provider.conf
kubectl delete -f hello-world-service-composition.yaml --kubeconfig=provider.conf
kubectl delete -f examples/multitenancy/hello-world/hs1-no-replicas.yaml --kubeconfig=kubeplus-saas-provider.json
kubectl delete -f examples/multitenancy/hello-world/hs2-no-replicas.yaml --kubeconfig=kubeplus-saas-provider.json
kubectl delete -f examples/multitenancy/hello-world/hello-world-service-composition.yaml --kubeconfig=kubeplus-saas-provider.json
```

Ensure the `helloworldservices.platformapi.kubeplus` CRD is removed.
Expand Down
19 changes: 17 additions & 2 deletions plugins/kubectl-allow-network-traffic
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@

source utils.sh # if you have common utility functions; else remove this line

# This wrapper passes all arguments to our Python script.
python3 "$KUBEPLUS_HOME/plugins/network_traffic.py" allow "$@"
# Usage (as advertised in kubectl-kubeplus-commands):
# kubectl allow network traffic <ns1> <ns2> [-k <kubeconfig>]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will this change work if we don't provide the kubeconfig parameter (i.e., if -k is missing)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it works without -k.
Without -k, the plugin uses the default kubeconfig. For me, that is the same cluster with enough access, so it works
Screenshot 2026-02-03 at 9 08 56 AM

#
# kubectl will translate that to the plugin name:
# kubectl-allow-network-traffic <ns1> <ns2> [-k <kubeconfig>]
#
# Here we:
# - Take the first two positional args as namespaces
# - Pass any remaining args (e.g. -k <kubeconfig>) as global options
# - Reorder so global options come before the subcommand for argparse

ns1="$1"
ns2="$2"
shift 2

# "$@" now contains any remaining flags (e.g. -k kubeplus-saas-provider.json)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!!

python3 "$KUBEPLUS_HOME/plugins/network_traffic.py" "$@" allow "$ns1" "$ns2"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

By shifting out ns1 and ns2, there is no issue of duplicates in $@.

19 changes: 17 additions & 2 deletions plugins/kubectl-deny-network-traffic
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@

source utils.sh # if you have common utility functions; else remove this line

# This wrapper passes all arguments to our Python script.
python3 "$KUBEPLUS_HOME/plugins/network_traffic.py" deny "$@"
# Usage (as advertised in kubectl-kubeplus-commands):
# kubectl deny network traffic <ns1> <ns2> [-k <kubeconfig>]
#
# kubectl will translate that to the plugin name:
# kubectl-deny-network-traffic <ns1> <ns2> [-k <kubeconfig>]
#
# Here we:
# - Take the first two positional args as namespaces
# - Pass any remaining args (e.g. -k <kubeconfig>) as global options
# - Reorder so global options come before the subcommand for argparse

ns1="$1"
ns2="$2"
shift 2

# "$@" now contains any remaining flags (e.g. -k kubeplus-saas-provider.json)
python3 "$KUBEPLUS_HOME/plugins/network_traffic.py" "$@" deny "$ns1" "$ns2"