mirror of
https://github.com/kubernetes-sigs/kind.git
synced 2025-12-01 07:26:05 +07:00
break up kubeadm init action into logical actions
This commit is contained in:
@@ -17,13 +17,10 @@ limitations under the License.
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -225,45 +222,6 @@ func (n *Node) Role() (role string, err error) {
|
||||
return role, nil
|
||||
}
|
||||
|
||||
// matches kubeconfig server entry like:
|
||||
// server: https://172.17.0.2:6443
|
||||
// which we rewrite to:
|
||||
// server: https://localhost:$PORT
|
||||
var serverAddressRE = regexp.MustCompile(`^(\s+server:) https://.*:\d+$`)
|
||||
|
||||
// WriteKubeConfig writes a fixed KUBECONFIG to dest
|
||||
// this should only be called on a control plane node
|
||||
// While copyng to the host machine the control plane address
|
||||
// is replaced with local host and the control plane port with
|
||||
// a randomly generated port reserved during node creation.
|
||||
func (n *Node) WriteKubeConfig(dest string, hostPort int32) error {
|
||||
cmd := n.Command("cat", "/etc/kubernetes/admin.conf")
|
||||
lines, err := exec.CombinedOutputLines(cmd)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get kubeconfig from node")
|
||||
}
|
||||
|
||||
// fix the config file, swapping out the server for the forwarded localhost:port
|
||||
var buff bytes.Buffer
|
||||
for _, line := range lines {
|
||||
match := serverAddressRE.FindStringSubmatch(line)
|
||||
if len(match) > 1 {
|
||||
line = fmt.Sprintf("%s https://localhost:%d", match[1], hostPort)
|
||||
}
|
||||
buff.WriteString(line)
|
||||
buff.WriteString("\n")
|
||||
}
|
||||
|
||||
// create the directory to contain the KUBECONFIG file.
|
||||
// 0755 is taken from client-go's config handling logic: https://github.com/kubernetes/client-go/blob/5d107d4ebc00ee0ea606ad7e39fd6ce4b0d9bf9e/tools/clientcmd/loader.go#L412
|
||||
err = os.MkdirAll(filepath.Dir(dest), 0755)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create kubeconfig output directory")
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(dest, buff.Bytes(), 0600)
|
||||
}
|
||||
|
||||
// WriteFile writes content to dest on the node
|
||||
func (n *Node) WriteFile(dest, content string) error {
|
||||
// create destination directory
|
||||
|
||||
Reference in New Issue
Block a user