Merge pull request #71 from andyz-dev/docker-machine

[Feature] Docker machine
This commit is contained in:
Andy Zhou 2019-06-01 11:03:42 -07:00 committed by GitHub
commit bd58e9b874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import (
"os" "os"
"path" "path"
"strconv" "strconv"
"strings"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
@ -139,8 +140,23 @@ func createKubeConfigFile(cluster string) error {
} }
defer kubeconfigfile.Close() defer kubeconfigfile.Close()
// write to file, skipping the first 512 bytes which contain file metadata and trimming any NULL characters // write to file, skipping the first 512 bytes which contain file metadata
_, err = kubeconfigfile.Write(bytes.Trim(readBytes[512:], "\x00")) // and trimming any NULL characters
trimBytes := bytes.Trim(readBytes[512:], "\x00")
// If running on a docker machine, replace localhost with
// docker machine's IP
dockerMachineIp, err := getDockerMachineIp()
if err != nil {
return err
}
if dockerMachineIp != "" {
s := string(trimBytes)
s = strings.Replace(s, "localhost", dockerMachineIp, 1)
trimBytes = []byte(s)
}
_, err = kubeconfigfile.Write(trimBytes)
if err != nil { if err != nil {
return fmt.Errorf("ERROR: couldn't write to kubeconfig.yaml\n%+v", err) return fmt.Errorf("ERROR: couldn't write to kubeconfig.yaml\n%+v", err)
} }

View File

@ -106,6 +106,13 @@ func CreateCluster(c *cli.Context) error {
log.Println("INFO: As of v2.0.0 --port will be used for arbitrary port mapping. Please use --api-port/-a instead for configuring the Api Port") log.Println("INFO: As of v2.0.0 --port will be used for arbitrary port mapping. Please use --api-port/-a instead for configuring the Api Port")
} }
k3sServerArgs := []string{"--https-listen-port", c.String("api-port")} k3sServerArgs := []string{"--https-listen-port", c.String("api-port")}
if ip, err := getDockerMachineIp(); ip != "" || err != nil {
if err != nil {
return err
}
log.Printf("Add TLS SAN for %s", ip)
k3sServerArgs = append(k3sServerArgs, "--tls-san", ip)
}
if c.IsSet("server-arg") || c.IsSet("x") { if c.IsSet("server-arg") || c.IsSet("x") {
k3sServerArgs = append(k3sServerArgs, c.StringSlice("server-arg")...) k3sServerArgs = append(k3sServerArgs, c.StringSlice("server-arg")...)
} }

26
cli/docker-machine.go Normal file
View File

@ -0,0 +1,26 @@
package run
import (
"os"
"os/exec"
"strings"
)
func getDockerMachineIp() (string, error) {
machine := os.ExpandEnv("$DOCKER_MACHINE_NAME")
if machine == "" {
return "", nil
}
dockerMachinePath, err := exec.LookPath("docker-machine")
if err != nil {
return "", err
}
out, err := exec.Command(dockerMachinePath, "ip", machine).Output()
ipStr := strings.TrimSuffix(string(out), "\n")
ipStr = strings.TrimSuffix(ipStr, "\r")
return ipStr, err
}