@@ -46,6 +46,10 @@ inputs:
4646 description : Enable K3s to use the Docker daemon
4747 required : false
4848 default : " false"
49+ ip-mode :
50+ description : ' Switch between IPv4 and/or IPv6: "default", "dual" or "ipv6"'
51+ required : false
52+ default : default
4953 extra-setup-args :
5054 description : Addition arguments to be passed to the K3S setup script
5155 required : false
9094 echo "::endgroup::"
9195 shell : bash
9296
97+ # https://docs.k3s.io/networking/basic-network-options#dual-stack-ipv4--ipv6-networking
98+ - name : Set IP mode variables
99+ run : |
100+ echo "::group::Setting IP variables"
101+ if [[ "${{ inputs.ip-mode }}" != "default" ]]; then
102+ NODE_IP4=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)([^/]+)')
103+ if [ -z "$NODE_IP4" ]; then
104+ echo "Failed to get node IP4"
105+ exit 1
106+ fi
107+ NODE_IP6=$(ip -6 addr show eth0 | grep -oP '(?<=inet6\s)([^/]+)')
108+ if [ -z "$NODE_IP6" ]; then
109+ echo "Failed to get node IP6"
110+ exit 1
111+ fi
112+
113+ if [[ "${{ inputs.ip-mode }}" = "dual" ]]; then
114+ echo K3S_CLUSTER_CIDR="10.42.0.0/16,2001:cafe:42::/56"
115+ echo K3S_SERVICE_CIDR="10.43.0.0/16,2001:cafe:43::/112"
116+ echo K3S_NODE_IP="${NODE_IP4},{NODE_IP6}"
117+ elif [[ "${{ inputs.ip-mode }}" = "ipv6" ]]; then
118+ echo K3S_CLUSTER_CIDR="2001:cafe:42::/56"
119+ echo K3S_SERVICE_CIDR="2001:cafe:43::/112"
120+ echo K3S_NODE_IP="${NODE_IP6}"
121+ else
122+ echo "Invalid ip-mode: ${{ inputs.ip-mode }}"
123+ exit 1
124+ fi
125+ fi
126+ echo "K3S_CLUSTER_CIDR=$K3S_CLUSTER_CIDR" >> $GITHUB_ENV
127+ echo "K3S_SERVICE_CIDR=$K3S_SERVICE_CIDR" >> $GITHUB_ENV
128+ echo "K3S_NODE_IP=$K3S_NODE_IP" >> $GITHUB_ENV
129+ echo "::endgroup::"
130+ shell : bash
131+
93132 # NOTE: We apply a workaround as of version 3.0.1 by passing
94133 # --egress-selector-mode=disabled by default as not doing so following
95134 # modern versions of k3s has led to issues with `kubectl exec` and
@@ -121,12 +160,25 @@ runs:
121160 if [[ "${{ inputs.extra-setup-args }}" != *--egress-selector-mode* ]]; then
122161 default_extra_setup_args=--egress-selector-mode=disabled
123162 fi
163+ if [[ -n "$K3S_CLUSTER_CIDR" ]]; then
164+ k3s_cluster_cidr="--cluster-cidr=$K3S_CLUSTER_CIDR"
165+ fi
166+ if [[ -n "$K3S_SERVICE_CIDR" ]]; then
167+ k3s_service_cidr="--service-cidr=$K3S_SERVICE_CIDR"
168+ fi
169+ if [[ -n "$K3S_NODE_IP" ]]; then
170+ k3s_node_ip="--service-cidr=$K3S_NODE_IP"
171+ fi
172+
124173 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="${{ inputs.k3s-version }}" INSTALL_K3S_CHANNEL="${{ inputs.k3s-channel }}" sh -s - \
125174 ${k3s_disable_metrics} \
126175 ${k3s_disable_traefik} \
127176 --disable-network-policy \
128177 --flannel-backend=none \
129178 ${k3s_docker} \
179+ ${k3s_cluster_cidr} \
180+ ${k3s_service_cidr} \
181+ ${k3s_node_ip} \
130182 ${{ inputs.extra-setup-args }} \
131183 ${default_extra_setup_args}
132184 echo "::endgroup::"
@@ -150,6 +202,8 @@ runs:
150202 #
151203 # ref: https://rancher.com/docs/k3s/latest/en/installation/network-options/
152204 #
205+ # IPv6: https://docs.tigera.io/calico/latest/networking/ipam/ipv6
206+ #
153207 - name : Setup calico
154208 run : |
155209 echo "::group::Setup calico"
@@ -159,12 +213,37 @@ runs:
159213 cd calico
160214 yq -s '"\(.kind)-\(.metadata.name).yaml"' /tmp/calico.yaml
161215
162- # Modify ConfigMap/calico-config: Look for `"type": "calico"` and add
216+ # ConfigMap/calico-config: Look for `"type": "calico"` and add
163217 # `"container_settings": ...` on the next line
164218 sed -i.bak '/"type": "calico"/a\
165219 "container_settings": {"allow_ip_forwarding": true},'\
166220 ConfigMap-calico-config.yaml
167221
222+ # Optionally configure IPv6
223+ # https://docs.tigera.io/calico/latest/networking/ipam/ipv6
224+ # ConfigMap/calico-config: Look for `"type": "calico-ipam"` and add
225+ # additional properties
226+ if [[ "${{ inputs.ip-mode }}" = "dual" ]]; then
227+ sed -i '/"type": "calico-ipam"/a\
228+ , "assign_ipv4": "true", "assign_ipv6": "true"'\
229+ ConfigMap-calico-config.yaml
230+ fi
231+ if [[ "${{ inputs.ip-mode }}" = "ipv6" ]]; then
232+ sed -i '/"type": "calico-ipam"/a\
233+ , "assign_ipv4": "false", "assign_ipv6": "true"'\
234+ ConfigMap-calico-config.yaml
235+ fi
236+
237+ if [[ "${{ inputs.ip-mode }}" = "dual" || "${{ inputs.ip-mode }}" = "ipv6" ]]; then
238+ # DaemonSet/calico-node: Modify and add environment variables
239+ sed -i.bak -re '/- name: FELIX_IPV6SUPPORT/{n; s/"false"/"true"\n\
240+ - name: IP6\n\
241+ value: autodetect\n\
242+ - name: CALICO_IPV6POOL_CIDR\n\
243+ value: "2001:cafe:42::\/56"/}'\
244+ DaemonSet-calico-node.yaml
245+ fi
246+
168247 for f in *.yaml.bak; do
169248 diff -u "$f" "${f%.bak}" || :
170249 done
0 commit comments