# Development ## Contribute Before you submit your PRs, please ensure no error with following command: * `make presubmit` * `make test` To do more comprehensive testing, please refer to [Local Testing](#local-testing). ## Local Testing Along with running in a Kubernetes, ztunnel can be run locally for development purposes. This doc covers ztunnel specifically, for general Istio local development see [Local Istio Development](https://github.com/howardjohn/local-istio-development). ### Overrides There are a variety of config options that can be used to replace components with mocked ones: * `FAKE_CA="true"`: this will use self-signed fake certificates, eliminating a dependency on a CA * `XDS_ADDRESS=""`: disables XDS client completely * `LOCAL_XDS_PATH=./examples/localhost.yaml`: read XDS config from a file. * `CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="sudo -E"`: have cargo run as sudo * `PROXY_MODE=dedicated`: Dedicated mode is the single-tenant proxy mode and is strongly recommended for local development, as it works for 95% of cases and doesn't require manually constructing Linux network namespaces to use. The following command (with `--no-default-features` if you have FIPS disabled) can be used to run entirely locally, without a Kubernetes or Istiod dependency. ```shell FAKE_CA="true" \ XDS_ADDRESS="" \ LOCAL_XDS_PATH=./examples/localhost.yaml \ CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="sudo -E" \ PROXY_MODE=dedicated \ PROXY_WORKLOAD_INFO=default/local/default \ cargo run --features testing ``` ### In-pod mode setup Create a netns for your "pod" (in this case, just a network namespace): ```shell ip netns add pod1 ip -n pod1 link set lo up # veth device ip link add pod1 type veth peer name pod1-eth0 # move one end to the pod ip link set pod1-eth0 netns pod1 # configure the veth devices ip link set pod1 up ip -n pod1 link set pod1-eth0 up ip addr add dev pod1 10.0.0.1/24 ip -n pod1 addr add dev pod1-eth0 10.0.0.2/24 ``` run fake server with: ```shell INPOD_UDS=/tmp/ztunnel cargo run --example inpodserver -- pod1 ``` (note: In the above command, you can override PROXY_WORKLOAD_INFO default value if you need to match to different values in your `localhost.yaml` file) run ztunnel (as root) with: ```shell RUST_LOG=debug PROXY_MODE=shared INPOD_UDS=/tmp/ztunnel FAKE_CA="true" XDS_ADDRESS="" LOCAL_XDS_PATH=./examples/localhost.yaml cargo run --features testing ``` (note: to run ztunnel as root, consider using `export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="sudo -E"` so cargo `sudo` the binary) see the ztunnel sockets: ```shell ip netns exec pod1 ss -ntlp | grep ztunnel ``` ```shell # redirect traffic to ztunnel ip netns exec pod1 ./scripts/ztunnel-redirect-inpod.sh ``` To get traffic to work you may need to adjust the IPs in localhost.yaml and start processes in the pod netns. You can also do `make build FEATURES="--features testing` and use `./out/rust/debug/ztunnel` instead of `cargo run ...` ### In-pod mode with istiod on kind setup Run ztunnel on from your terminal. With istiod and workloads running in KinD. This works on Linux only. This approach will have traffic running through your local ztunnel - running outside of k8s as a regular, non-containerized userspace process. This can make certain kinds of debugging and local development flows faster/simpler. In this setup we will replace ztunnel in one of the nodes. In this example we replace the node named `ambient-worker`. Created cluster with and add an extraMount to the worker node. This will allow the ztunnel on your laptop to connect to the cni-socket of the worker node. ```shell kind create cluster --config=- < /tmp/istio-root.pem kubectl create -f - < ./var/run/secrets/tokens/istio-token xargs env <` redirects all traffic from `$ZTUNNEL_REDIRECT_USER` to the given port. * `redirect-to-clean` removes any iptables rules setup by `redirect-to` * `redirect-run ` runs the command as `$ZTUNNEL_REDIRECT_USER`. To setup redirection logic for all requests from the `iptables1` user to 15001: ```shell source ./scripts/local.sh export ZTUNNEL_REDIRECT_USER="iptables1" redirect-user-setup redirect-to 15001 ``` Finally, requests can be sent through the ztunnel: ```shell redirect-run curl localhost:8080 ``` In the example request above, the request will go from `curl -> ztunnel (15001) --HBONE--> ztunnel (15008) -> localhost:8080`. If you wanted the same request to not go over HBONE, you could connect to/from another unknown IP like `127.0.0.2`.