mirror of https://github.com/k3d-io/k3d
[Enhancement] Network Magic (#721)
- before starting the cluster, gather environment info via tools node - use hostIP/gatewayIP for DNS (iptables + resolv.conf updated in entrypoint script) - revamp of custom entrypoint scriptspull/724/head
parent
7c635c29ab
commit
b4158a1dc1
@ -0,0 +1,44 @@ |
||||
/* |
||||
Copyright © 2020-2021 The k3d Author(s) |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in |
||||
all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||||
THE SOFTWARE. |
||||
*/ |
||||
package client |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/rancher/k3d/v4/pkg/runtimes" |
||||
|
||||
k3d "github.com/rancher/k3d/v4/pkg/types" |
||||
) |
||||
|
||||
func GatherEnvironmentInfo(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster) (*k3d.EnvironmentInfo, error) { |
||||
envInfo := &k3d.EnvironmentInfo{} |
||||
|
||||
hostIP, err := GetHostIP(ctx, runtime, cluster) |
||||
if err != nil { |
||||
return envInfo, err |
||||
} |
||||
|
||||
envInfo.HostGateway = hostIP |
||||
|
||||
return envInfo, nil |
||||
|
||||
} |
@ -0,0 +1,34 @@ |
||||
#!/bin/sh |
||||
|
||||
# DISCLAIMER |
||||
# Heavily inspired by / copied from https://github.com/kubernetes-sigs/kind/pull/1414/files#diff-3c55751d83af635109cece495ee2ff38206764a8b95f4cb8f11fc08a5c0ea8dc |
||||
# Apache 2.0 License (Kubernetes Authors): https://github.com/kubernetes-sigs/kind/blob/9222508298c50ce8c5ba1f364f37307e81ba915e/LICENSE |
||||
|
||||
set -o errexit |
||||
set -o nounset |
||||
|
||||
docker_dns="127.0.0.11" |
||||
|
||||
gateway="GATEWAY_IP" # replaced within k3d Go code |
||||
|
||||
echo "[$(date -Iseconds)] [DNS Fix] Use the detected Gateway IP $gateway instead of Docker's embedded DNS ($docker_dns)" |
||||
|
||||
# Change iptables rules added by Docker to route traffic to out Gateway IP instead of Docker's embedded DNS |
||||
echo "[$(date -Iseconds)] [DNS Fix] > Changing iptables rules ..." |
||||
iptables-save \ |
||||
| sed \ |
||||
-e "s/-d ${docker_dns}/-d ${gateway}/g" \ |
||||
-e 's/-A OUTPUT \(.*\) -j DOCKER_OUTPUT/\0\n-A PREROUTING \1 -j DOCKER_OUTPUT/' \ |
||||
-e "s/--to-source :53/--to-source ${gateway}:53/g"\ |
||||
| iptables-restore |
||||
|
||||
# Update resolv.conf to use the Gateway IP if needed: this will also make CoreDNS use it via k3s' default `forward . /etc/resolv.conf` rule in the CoreDNS config |
||||
grep -q "${docker_dns}" /etc/resolv.conf |
||||
grepstatus=$? |
||||
if test $grepstatus -eq 0; then |
||||
echo "[$(date -Iseconds)] [DNS Fix] > Replacing IP in /etc/resolv.conf ..." |
||||
cp /etc/resolv.conf /etc/resolv.conf.original |
||||
sed -e "s/${docker_dns}/${gateway}/g" /etc/resolv.conf.original >/etc/resolv.conf |
||||
fi |
||||
|
||||
echo "[$(date -Iseconds)] [DNS Fix] Done" |
@ -0,0 +1,19 @@ |
||||
#!/bin/sh |
||||
|
||||
set -o errexit |
||||
set -o nounset |
||||
|
||||
LOGFILE="/var/log/k3d-entrypoints_$(date "+%y%m%d%H%M%S").log" |
||||
|
||||
touch "$LOGFILE" |
||||
|
||||
echo "[$(date -Iseconds)] Running k3d entrypoints..." >> "$LOGFILE" |
||||
|
||||
for entrypoint in /bin/k3d-entrypoint-*.sh ; do |
||||
echo "[$(date -Iseconds)] Running $entrypoint" >> "$LOGFILE" |
||||
"$entrypoint" >> "$LOGFILE" 2>&1 || exit 1 |
||||
done |
||||
|
||||
echo "[$(date -Iseconds)] Finished k3d entrypoint scripts!" >> "$LOGFILE" |
||||
|
||||
exec /bin/k3s "$@" |
Loading…
Reference in new issue