Просмотр исходного кода

Merge pull request #212 from stv0g/k3s-kubeconfig

k3s: Dynamically generate kubeconfig
leonnicolas 4 лет назад
Родитель
Сommit
daecc2a0bc

+ 59 - 6
manifests/kilo-k3s-flannel.yaml

@@ -44,6 +44,35 @@ subjects:
     name: kilo
     name: kilo
     namespace: kube-system
     namespace: kube-system
 ---
 ---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: kilo-scripts
+  namespace: kube-system
+data:
+  init.sh: |
+    #!/bin/sh
+    cat > /etc/kubernetes/kubeconfig <<EOF
+        apiVersion: v1
+        kind: Config
+        name: kilo
+        clusters:
+        - cluster:
+            server: $(sed -n 's/.*server: \(.*\)/\1/p' /var/lib/rancher/k3s/agent/kubelet.kubeconfig)
+            certificate-authority: /var/lib/rancher/k3s/agent/server-ca.crt
+        users:
+        - name: kilo
+          user:
+            token: $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
+        contexts:
+        - name: kilo
+          context:
+            cluster: kilo
+            namespace: ${NAMESPACE}
+            user: kilo
+        current-context: kilo
+    EOF
+---
 apiVersion: apps/v1
 apiVersion: apps/v1
 kind: DaemonSet
 kind: DaemonSet
 metadata:
 metadata:
@@ -88,7 +117,7 @@ spec:
         - name: kilo-dir
         - name: kilo-dir
           mountPath: /var/lib/kilo
           mountPath: /var/lib/kilo
         - name: kubeconfig
         - name: kubeconfig
-          mountPath: /etc/kubernetes/kubeconfig
+          mountPath: /etc/kubernetes
           readOnly: true
           readOnly: true
         - name: lib-modules
         - name: lib-modules
           mountPath: /lib/modules
           mountPath: /lib/modules
@@ -96,6 +125,28 @@ spec:
         - name: xtables-lock
         - name: xtables-lock
           mountPath: /run/xtables.lock
           mountPath: /run/xtables.lock
           readOnly: false
           readOnly: false
+      initContainers:
+      - name: generate-kubeconfig
+        image: squat/kilo
+        command:
+        - /bin/sh
+        args:
+        - /scripts/init.sh
+        imagePullPolicy: Always
+        volumeMounts:
+        - name: kubeconfig
+          mountPath: /etc/kubernetes
+        - name: scripts
+          mountPath: /scripts/
+          readOnly: true
+        - name: k3s-agent
+          mountPath: /var/lib/rancher/k3s/agent/
+          readOnly: true
+        env:
+        - name: NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
       tolerations:
       tolerations:
       - effect: NoSchedule
       - effect: NoSchedule
         operator: Exists
         operator: Exists
@@ -106,11 +157,13 @@ spec:
         hostPath:
         hostPath:
           path: /var/lib/kilo
           path: /var/lib/kilo
       - name: kubeconfig
       - name: kubeconfig
-        hostPath: 
-          # Since kilo runs as a daemonset, it is recommended that you copy the
-          # k3s.yaml kubeconfig file from the master node to all worker nodes
-          # with the same path structure.
-          path: /etc/rancher/k3s/k3s.yaml
+        emptyDir: {}
+      - name: scripts
+        configMap:
+          name: kilo-scripts
+      - name: k3s-agent
+        hostPath:
+          path: /var/lib/rancher/k3s/agent
       - name: lib-modules
       - name: lib-modules
         hostPath:
         hostPath:
           path: /lib/modules
           path: /lib/modules

+ 118 - 19
manifests/kilo-k3s-userspace-heterogeneous.yaml

@@ -79,6 +79,35 @@ subjects:
     name: kilo
     name: kilo
     namespace: kube-system
     namespace: kube-system
 ---
 ---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: kilo-scripts
+  namespace: kube-system
+data:
+  init.sh: |
+    #!/bin/sh
+    cat > /etc/kubernetes/kubeconfig <<EOF
+        apiVersion: v1
+        kind: Config
+        name: kilo
+        clusters:
+        - cluster:
+            server: $(sed -n 's/.*server: \(.*\)/\1/p' /var/lib/rancher/k3s/agent/kubelet.kubeconfig)
+            certificate-authority: /var/lib/rancher/k3s/agent/server-ca.crt
+        users:
+        - name: kilo
+          user:
+            token: $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
+        contexts:
+        - name: kilo
+          context:
+            cluster: kilo
+            namespace: ${NAMESPACE}
+            user: kilo
+        current-context: kilo
+    EOF
+---
 apiVersion: apps/v1
 apiVersion: apps/v1
 kind: DaemonSet
 kind: DaemonSet
 metadata:
 metadata:
@@ -125,7 +154,7 @@ spec:
         - name: kilo-dir
         - name: kilo-dir
           mountPath: /var/lib/kilo
           mountPath: /var/lib/kilo
         - name: kubeconfig
         - name: kubeconfig
-          mountPath: /etc/kubernetes/kubeconfig
+          mountPath: /etc/kubernetes
           readOnly: true
           readOnly: true
         - name: lib-modules
         - name: lib-modules
           mountPath: /lib/modules
           mountPath: /lib/modules
@@ -134,6 +163,27 @@ spec:
           mountPath: /run/xtables.lock
           mountPath: /run/xtables.lock
           readOnly: false
           readOnly: false
       initContainers:
       initContainers:
+      - name: generate-kubeconfig
+        image: squat/kilo
+        command:
+        - /bin/sh
+        args:
+        - /scripts/init.sh
+        imagePullPolicy: Always
+        volumeMounts:
+        - name: kubeconfig
+          mountPath: /etc/kubernetes
+        - name: scripts
+          mountPath: /scripts/
+          readOnly: true
+        - name: k3s-agent
+          mountPath: /var/lib/rancher/k3s/agent/
+          readOnly: true
+        env:
+        - name: NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
       - name: install-cni
       - name: install-cni
         image: squat/kilo
         image: squat/kilo
         command:
         command:
@@ -174,11 +224,13 @@ spec:
         hostPath:
         hostPath:
           path: /var/lib/kilo
           path: /var/lib/kilo
       - name: kubeconfig
       - name: kubeconfig
-        hostPath: 
-          # Since kilo runs as a daemonset, it is recommended that you copy the
-          # k3s.yaml kubeconfig file from the master node to all worker nodes
-          # with the same path structure.
-          path: /etc/rancher/k3s/k3s.yaml
+        emptyDir: {}
+      - name: scripts
+        configMap:
+          name: kilo-scripts
+      - name: k3s-agent
+        hostPath:
+          path: /var/lib/rancher/k3s/agent
       - name: lib-modules
       - name: lib-modules
         hostPath:
         hostPath:
           path: /lib/modules
           path: /lib/modules
@@ -234,7 +286,7 @@ spec:
         - name: kilo-dir
         - name: kilo-dir
           mountPath: /var/lib/kilo
           mountPath: /var/lib/kilo
         - name: kubeconfig
         - name: kubeconfig
-          mountPath: /etc/kubernetes/kubeconfig
+          mountPath: /etc/kubernetes
           readOnly: true
           readOnly: true
         - name: lib-modules
         - name: lib-modules
           mountPath: /lib/modules
           mountPath: /lib/modules
@@ -258,6 +310,27 @@ spec:
           mountPath: /var/run/wireguard
           mountPath: /var/run/wireguard
           readOnly: false
           readOnly: false
       initContainers:
       initContainers:
+      - name: generate-kubeconfig
+        image: squat/kilo
+        command:
+        - /bin/sh
+        args:
+        - /scripts/init.sh
+        imagePullPolicy: Always
+        volumeMounts:
+        - name: kubeconfig
+          mountPath: /etc/kubernetes
+        - name: scripts
+          mountPath: /scripts/
+          readOnly: true
+        - name: k3s-agent
+          mountPath: /var/lib/rancher/k3s/agent/
+          readOnly: true
+        env:
+        - name: NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
       - name: install-cni
       - name: install-cni
         image: squat/kilo
         image: squat/kilo
         command:
         command:
@@ -298,11 +371,13 @@ spec:
         hostPath:
         hostPath:
           path: /var/lib/kilo
           path: /var/lib/kilo
       - name: kubeconfig
       - name: kubeconfig
-        hostPath: 
-          # Since kilo runs as a daemonset, it is recommended that you copy the
-          # k3s.yaml kubeconfig file from the master node to all worker nodes
-          # with the same path structure.
-          path: /etc/rancher/k3s/k3s.yaml
+        emptyDir: {}
+      - name: scripts
+        configMap:
+          name: kilo-scripts
+      - name: k3s-agent
+        hostPath:
+          path: /var/lib/rancher/k3s/agent
       - name: lib-modules
       - name: lib-modules
         hostPath:
         hostPath:
           path: /lib/modules
           path: /lib/modules
@@ -331,6 +406,7 @@ spec:
         app.kubernetes.io/name: nkml
         app.kubernetes.io/name: nkml
     spec:
     spec:
       hostNetwork: true
       hostNetwork: true
+      serviceAccountName: kilo
       containers:
       containers:
       - name: nkml
       - name: nkml
         image: leonnicolas/nkml
         image: leonnicolas/nkml
@@ -348,13 +424,36 @@ spec:
           containerPort: 8080
           containerPort: 8080
         volumeMounts:
         volumeMounts:
         - name: kubeconfig
         - name: kubeconfig
-          mountPath: /etc/kubernetes/kubeconfig
+          mountPath: /etc/kubernetes
+          readOnly: true
+      initContainers:
+      - name: generate-kubeconfig
+        image: squat/kilo
+        command:
+        - /bin/sh
+        args:
+        - /scripts/init.sh
+        imagePullPolicy: Always
+        volumeMounts:
+        - name: kubeconfig
+          mountPath: /etc/kubernetes
+        - name: scripts
+          mountPath: /scripts/
           readOnly: true
           readOnly: true
+        - name: k3s-agent
+          mountPath: /var/lib/rancher/k3s/agent/
+          readOnly: true
+        env:
+        - name: NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
       volumes:
       volumes:
       - name: kubeconfig
       - name: kubeconfig
-        hostPath: 
-          # since the above DaemonSets are dependant on the labels
-          # and nkml would need a cni to start
-          # it needs run on the hostnetwork and use the kubeconfig
-          # to label the nodes
-          path: /etc/rancher/k3s/k3s.yaml
+        emptyDir: {}
+      - name: scripts
+        configMap:
+          name: kilo-scripts
+      - name: k3s-agent
+        hostPath:
+          path: /var/lib/rancher/k3s/agent

+ 59 - 6
manifests/kilo-k3s-userspace.yaml

@@ -78,6 +78,36 @@ subjects:
     name: kilo
     name: kilo
     namespace: kube-system
     namespace: kube-system
 ---
 ---
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: kilo-scripts
+  namespace: kube-system
+data:
+  init.sh: |
+    #!/bin/sh
+    cat > /etc/kubernetes/kubeconfig <<EOF
+        apiVersion: v1
+        kind: Config
+        name: kilo
+        clusters:
+        - cluster:
+            server: $(sed -n 's/.*server: \(.*\)/\1/p' /var/lib/rancher/k3s/agent/kubelet.kubeconfig)
+            certificate-authority: /var/lib/rancher/k3s/agent/server-ca.crt
+        users:
+        - name: kilo
+          user:
+            token: $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
+        contexts:
+        - name: kilo
+          context:
+            cluster: kilo
+            namespace: ${NAMESPACE}
+            user: kilo
+        current-context: kilo
+    EOF
+---
 apiVersion: apps/v1
 apiVersion: apps/v1
 kind: DaemonSet
 kind: DaemonSet
 metadata:
 metadata:
@@ -123,7 +153,7 @@ spec:
         - name: kilo-dir
         - name: kilo-dir
           mountPath: /var/lib/kilo
           mountPath: /var/lib/kilo
         - name: kubeconfig
         - name: kubeconfig
-          mountPath: /etc/kubernetes/kubeconfig
+          mountPath: /etc/kubernetes
           readOnly: true
           readOnly: true
         - name: lib-modules
         - name: lib-modules
           mountPath: /lib/modules
           mountPath: /lib/modules
@@ -147,6 +177,27 @@ spec:
           mountPath: /var/run/wireguard
           mountPath: /var/run/wireguard
           readOnly: false
           readOnly: false
       initContainers:
       initContainers:
+      - name: generate-kubeconfig
+        image: squat/kilo
+        command:
+        - /bin/sh
+        args:
+        - /scripts/init.sh
+        imagePullPolicy: Always
+        volumeMounts:
+        - name: kubeconfig
+          mountPath: /etc/kubernetes
+        - name: scripts
+          mountPath: /scripts/
+          readOnly: true
+        - name: k3s-agent
+          mountPath: /var/lib/rancher/k3s/agent/
+          readOnly: true
+        env:
+        - name: NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
       - name: install-cni
       - name: install-cni
         image: squat/kilo
         image: squat/kilo
         command:
         command:
@@ -187,11 +238,13 @@ spec:
         hostPath:
         hostPath:
           path: /var/lib/kilo
           path: /var/lib/kilo
       - name: kubeconfig
       - name: kubeconfig
-        hostPath: 
-          # Since kilo runs as a daemonset, it is recommended that you copy the
-          # k3s.yaml kubeconfig file from the master node to all worker nodes
-          # with the same path structure.
-          path: /etc/rancher/k3s/k3s.yaml
+        emptyDir: {}
+      - name: scripts
+        configMap:
+          name: kilo-scripts
+      - name: k3s-agent
+        hostPath:
+          path: /var/lib/rancher/k3s/agent
       - name: lib-modules
       - name: lib-modules
         hostPath:
         hostPath:
           path: /lib/modules
           path: /lib/modules

+ 58 - 6
manifests/kilo-k3s.yaml

@@ -78,6 +78,35 @@ subjects:
     name: kilo
     name: kilo
     namespace: kube-system
     namespace: kube-system
 ---
 ---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: kilo-scripts
+  namespace: kube-system
+data:
+  init.sh: |
+    #!/bin/sh
+    cat > /etc/kubernetes/kubeconfig <<EOF
+        apiVersion: v1
+        kind: Config
+        name: kilo
+        clusters:
+        - cluster:
+            server: $(sed -n 's/.*server: \(.*\)/\1/p' /var/lib/rancher/k3s/agent/kubelet.kubeconfig)
+            certificate-authority: /var/lib/rancher/k3s/agent/server-ca.crt
+        users:
+        - name: kilo
+          user:
+            token: $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
+        contexts:
+        - name: kilo
+          context:
+            cluster: kilo
+            namespace: ${NAMESPACE}
+            user: kilo
+        current-context: kilo
+    EOF
+---
 apiVersion: apps/v1
 apiVersion: apps/v1
 kind: DaemonSet
 kind: DaemonSet
 metadata:
 metadata:
@@ -121,7 +150,7 @@ spec:
         - name: kilo-dir
         - name: kilo-dir
           mountPath: /var/lib/kilo
           mountPath: /var/lib/kilo
         - name: kubeconfig
         - name: kubeconfig
-          mountPath: /etc/kubernetes/kubeconfig
+          mountPath: /etc/kubernetes
           readOnly: true
           readOnly: true
         - name: lib-modules
         - name: lib-modules
           mountPath: /lib/modules
           mountPath: /lib/modules
@@ -130,6 +159,27 @@ spec:
           mountPath: /run/xtables.lock
           mountPath: /run/xtables.lock
           readOnly: false
           readOnly: false
       initContainers:
       initContainers:
+      - name: generate-kubeconfig
+        image: squat/kilo
+        command:
+        - /bin/sh
+        args:
+        - /scripts/init.sh
+        imagePullPolicy: Always
+        volumeMounts:
+        - name: kubeconfig
+          mountPath: /etc/kubernetes
+        - name: scripts
+          mountPath: /scripts/
+          readOnly: true
+        - name: k3s-agent
+          mountPath: /var/lib/rancher/k3s/agent/
+          readOnly: true
+        env:
+        - name: NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
       - name: install-cni
       - name: install-cni
         image: squat/kilo
         image: squat/kilo
         command:
         command:
@@ -170,11 +220,13 @@ spec:
         hostPath:
         hostPath:
           path: /var/lib/kilo
           path: /var/lib/kilo
       - name: kubeconfig
       - name: kubeconfig
-        hostPath: 
-          # Since kilo runs as a daemonset, it is recommended that you copy the
-          # k3s.yaml kubeconfig file from the master node to all worker nodes
-          # with the same path structure.
-          path: /etc/rancher/k3s/k3s.yaml
+        emptyDir: {}
+      - name: scripts
+        configMap:
+          name: kilo-scripts
+      - name: k3s-agent
+        hostPath:
+          path: /var/lib/rancher/k3s/agent
       - name: lib-modules
       - name: lib-modules
         hostPath:
         hostPath:
           path: /lib/modules
           path: /lib/modules