Skip to content

Commit e5b5d2a

Browse files
authored
Merge pull request #4234 from AkihiroSuda/k8s-keep-config
template: k8s: keep KUBECONFIG in the guest unmodified
2 parents e72abc8 + a329cca commit e5b5d2a

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

pkg/guestagent/kubernetesservice/kubernetesservice.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (s *ServiceWatcher) Start(ctx context.Context) {
8282
func tryGetKubeClient() (kubernetes.Interface, error) {
8383
candidateKubeConfigs := []string{
8484
"/etc/rancher/k3s/k3s.yaml",
85+
"/root/.kube/config.localhost", // Created by template:k8s
8586
"/root/.kube/config",
8687
}
8788

templates/experimental/u7s.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ provision:
5454
set -eux -o pipefail
5555
test -e ~/usernetes/kubeconfig && exit 0
5656
cd ~/usernetes
57-
export KUBECONFIG=./kubeconfig
57+
export KUBECONFIG="$(pwd)/kubeconfig"
5858
patch --forward -r - kubeadm-config.yaml <<EOF
5959
@@ -7,6 +7,9 @@
6060
---
@@ -75,9 +75,12 @@ provision:
7575
# Control plane node isolation
7676
make kubeconfig
7777
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
78-
# Replace the server address with localhost, so that it works also from the host
79-
sed -e "/server:/ s|https://.*:\([0-9]*\)$|https://127.0.0.1:\1|" -i $KUBECONFIG
80-
mkdir -p ~/.kube && cp -f $KUBECONFIG ~/.kube/config
78+
# Symlink the kubeconfig file to the default location for kubectl
79+
mkdir -p ~/.kube && ln -sf $KUBECONFIG ~/.kube/config
80+
# Replace the server address with localhost, so that it works from the host.
81+
# The original kubeconfig is kept unmodified, so that `kubeadm token create --print-join-command`
82+
# can still print the reachable address.
83+
sed -e "/server:/ s|https://.*:\([0-9]*\)$|https://127.0.0.1:\1|" $KUBECONFIG >kubeconfig.localhost
8184
probes:
8285
- description: "kubectl to be installed"
8386
script: |
@@ -113,7 +116,7 @@ probes:
113116
set -eux -o pipefail
114117
kubectl wait -n kube-system --timeout=180s --for=condition=available deploy coredns
115118
copyToHost:
116-
- guest: "{{.Home}}/usernetes/kubeconfig"
119+
- guest: "{{.Home}}/usernetes/kubeconfig.localhost"
117120
host: "{{.Dir}}/copied-from-guest/kubeconfig.yaml"
118121
deleteOnStop: true
119122
message: |

templates/k8s.yaml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
# NAME STATUS ROLES AGE VERSION
1111
# lima-k8s Ready control-plane,master 44s v1.22.3
1212

13+
# A multi-node cluster can be created by starting multiple instances of this template
14+
# connected via the `lima:user-v2` network.
15+
#
16+
# $ limactl start --name k8s-0 --network lima:user-v2 template:k8s
17+
# $ limactl shell k8s-0 sudo kubeadm token create --print-join-command
18+
# (The join command printed here)
19+
#
20+
# $ limactl start --name k8s-1 --network lima:user-v2 template:k8s
21+
# $ limactl shell k8s-1 sudo kubeadm reset --force
22+
# $ limactl shell k8s-1 sudo <JOIN_COMMAND_FROM_ABOVE>
23+
1324
minimumLimaVersion: 2.0.0
1425

1526
base: template://_images/ubuntu-lts
@@ -108,9 +119,12 @@ provision:
108119
kubectl apply -f https://github.com/flannel-io/flannel/releases/download/v0.26.7/kube-flannel.yml
109120
# Control plane node isolation
110121
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
111-
# Replace the server address with localhost, so that it works also from the host
112-
sed -e "/server:/ s|https://.*:\([0-9]*\)$|https://127.0.0.1:\1|" -i $KUBECONFIG
113-
mkdir -p ${HOME:-/root}/.kube && cp -f $KUBECONFIG ${HOME:-/root}/.kube/config
122+
# Symlink the kubeconfig file to the default location for kubectl
123+
mkdir -p /root/.kube && ln -sf $KUBECONFIG /root/.kube/config
124+
# Replace the server address with localhost, so that it works from the host.
125+
# The original kubeconfig is kept unmodified, so that `kubeadm token create --print-join-command`
126+
# can still print the reachable address.
127+
sed -e "/server:/ s|https://.*:\([0-9]*\)$|https://127.0.0.1:\1|" $KUBECONFIG >/root/.kube/config.localhost
114128
- mode: system
115129
script: |
116130
#!/bin/bash
@@ -162,7 +176,7 @@ probes:
162176
set -eux -o pipefail
163177
kubectl wait -n kube-system --timeout=180s --for=condition=available deploy coredns
164178
copyToHost:
165-
- guest: "/etc/kubernetes/admin.conf"
179+
- guest: "/root/.kube/config.localhost"
166180
host: "{{.Dir}}/copied-from-guest/kubeconfig.yaml"
167181
deleteOnStop: true
168182
message: |

0 commit comments

Comments
 (0)