summaryrefslogtreecommitdiff
path: root/roles/kubernetes/kubeadm/base/templates
diff options
context:
space:
mode:
Diffstat (limited to 'roles/kubernetes/kubeadm/base/templates')
-rw-r--r--roles/kubernetes/kubeadm/base/templates/net_kubeguard/cni.json.j212
-rw-r--r--roles/kubernetes/kubeadm/base/templates/net_kubeguard/ifupdown.sh.j255
-rw-r--r--roles/kubernetes/kubeadm/base/templates/net_kubeguard/interface.service.j212
-rw-r--r--roles/kubernetes/kubeadm/base/templates/net_kubeguard/peer.service.j237
4 files changed, 116 insertions, 0 deletions
diff --git a/roles/kubernetes/kubeadm/base/templates/net_kubeguard/cni.json.j2 b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/cni.json.j2
new file mode 100644
index 00000000..eb9e3d61
--- /dev/null
+++ b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/cni.json.j2
@@ -0,0 +1,12 @@
+{
+ "cniVersion": "0.3.1",
+ "name": "kubeguard",
+ "type": "bridge",
+ "bridge": "kubeguard-br0",
+ "isDefaultGateway": true,
+ "hairpinMode": true,
+ "ipam": {
+ "type": "host-local",
+ "subnet": "{{ kubernetes.pod_ip_range | ipsubnet(kubernetes.pod_ip_range_size, kubeguard.node_index[inventory_hostname]) }}"
+ }
+}
diff --git a/roles/kubernetes/kubeadm/base/templates/net_kubeguard/ifupdown.sh.j2 b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/ifupdown.sh.j2
new file mode 100644
index 00000000..f940d413
--- /dev/null
+++ b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/ifupdown.sh.j2
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+set -e
+
+CONF_D="/var/lib/kubeguard/"
+
+INET_IF="{{ ansible_default_ipv4.interface }}"
+
+POD_NET_CIDR="{{ kubernetes.pod_ip_range }}"
+
+{% set br_net = kubernetes.pod_ip_range | ipsubnet(kubernetes.pod_ip_range_size, kubeguard.node_index[inventory_hostname]) -%}
+BR_IF="kubeguard-br0"
+BR_IP="{{ br_net | ipaddr(1) | ipaddr('address') }}"
+BR_IP_CIDR="{{ br_net | ipaddr(1) }}"
+BR_NET_CIDR="{{ br_net }}"
+
+TUN_IF="kubeguard-wg0"
+TUN_IP_CIDR="{{ kubernetes.pod_ip_range | ipsubnet(kubernetes.pod_ip_range_size, 0) | ipaddr(kubeguard.node_index[inventory_hostname]) }}"
+
+
+case "$1" in
+ up)
+ # bring up bridge for local pods
+ ip link add dev "$BR_IF" type bridge
+ ip addr add dev "$BR_IF" "$BR_IP_CIDR"
+ ip link set up dev "$BR_IF"
+ iptables -t nat -A POSTROUTING -s "$BR_NET_CIDR" -o "$INET_IF" -j MASQUERADE
+ modprobe br_netfilter
+
+ # bring up wireguard tunnel to other nodes
+ ip link add dev "$TUN_IF" type wireguard
+ ip addr add dev "$TUN_IF" "$TUN_IP_CIDR"
+ wg set "$TUN_IF" listen-port {{ kubeguard_wireguard_port | default(51820) }} private-key "$CONF_D/$TUN_IF.privatekey"
+ ip link set up dev "$TUN_IF"
+
+ # make pods and service IPs reachable
+ # !!! use IP of bridge as source so we don't produce martians if direct-zones are involved!!!
+ ip route add "$POD_NET_CIDR" dev "$TUN_IF" src "$BR_IP"
+ ;;
+ down)
+ # bring down wireguard tunnel to other nodes
+ ip route del "$POD_NET_CIDR" dev "$TUN_IF"
+ ip link del dev "$TUN_IF"
+
+ # bring down bridge for local pods
+ iptables -t nat -D POSTROUTING -s "$BR_NET_CIDR" -o "$INET_IF" -j MASQUERADE
+ ip link del dev "$BR_IF"
+ ;;
+ *)
+ echo "usage: $0 (up|down)"
+ exit 1
+ ;;
+esac
+
+exit 0
diff --git a/roles/kubernetes/kubeadm/base/templates/net_kubeguard/interface.service.j2 b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/interface.service.j2
new file mode 100644
index 00000000..35fc8f90
--- /dev/null
+++ b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/interface.service.j2
@@ -0,0 +1,12 @@
+[Unit]
+Description=Kubeguard Network Setup
+After=network.target
+
+[Service]
+Type=oneshot
+ExecStart=/var/lib/kubeguard/ifupdown.sh up
+ExecStop=/var/lib/kubeguard/ifupdown.sh down
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
diff --git a/roles/kubernetes/kubeadm/base/templates/net_kubeguard/peer.service.j2 b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/peer.service.j2
new file mode 100644
index 00000000..c9d96a5a
--- /dev/null
+++ b/roles/kubernetes/kubeadm/base/templates/net_kubeguard/peer.service.j2
@@ -0,0 +1,37 @@
+[Unit]
+Description=Kubernetes Network Peer {{ peer }}
+After=network.target
+Requires=kubeguard-interface.service
+After=kubeguard-interface.service
+
+{% set pod_ip_self = kubernetes.pod_ip_range | ipsubnet(kubernetes.pod_ip_range_size, kubeguard.node_index[inventory_hostname]) | ipaddr(1) | ipaddr('address') -%}
+{% set pod_net_peer = kubernetes.pod_ip_range | ipsubnet(kubernetes.pod_ip_range_size, kubeguard.node_index[peer]) -%}
+{% set direct_zone = kubeguard.direct_net_zones | default({}) | kubeguard_direct_net_zone(inventory_hostname, peer) -%}
+{% if direct_zone %}
+{% set direct_ip = kubeguard.direct_net_zones[direct_zone].transfer_net | ipaddr(kubeguard.node_index[inventory_hostname]) %}
+{% set direct_interface = kubeguard.direct_net_zones[direct_zone].node_interface[inventory_hostname] %}
+{% set direct_ip_peer = kubeguard.direct_net_zones[direct_zone].transfer_net | ipaddr(kubeguard.node_index[peer]) %}
+{% else %}
+{% set tun_ip = kubernetes.pod_ip_range | ipsubnet(kubernetes.pod_ip_range_size, 0) | ipaddr(kubeguard.node_index[peer]) -%}
+{% set wg_pubkey = hostvars[peer].kubeguard_wireguard_pubkey.stdout -%}
+{% set wg_host = hostvars[peer].external_ip_cooked | default(hostvars[peer].ansible_default_ipv4.address) -%}
+{% set wg_port = hostvars[peer].kubeguard_wireguard_port | default(51820) -%}
+{% set wg_allowedips = (tun_ip | ipaddr('address')) + "/32," + pod_net_peer %}
+{% endif %}
+[Service]
+Type=oneshot
+{% if direct_zone %}
+ExecStart=/sbin/ip addr add {{ direct_ip }} dev {{ direct_interface }}
+ExecStart=/sbin/ip link set up dev {{ direct_interface }}
+ExecStart=/sbin/ip route add {{ pod_net_peer }} via {{ direct_ip_peer | ipaddr('address') }} src {{ pod_ip_self }}
+ExecStop=/sbin/ip route del {{ pod_net_peer }}
+ExecStop=/sbin/ip link set down dev {{ direct_interface }}
+ExecStop=/sbin/ip addr del {{ direct_ip }} dev {{ direct_interface }}
+{% else %}
+ExecStart=/usr/bin/wg set kubeguard-wg0 peer {{ wg_pubkey }} allowed-ips {{ wg_allowedips }} endpoint {{ wg_host }}:{{ wg_port }} persistent-keepalive 10
+ExecStop=/usr/bin/wg set kubeguard-wg0 peer {{ wg_pubkey }} remove
+{% endif %}
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target