|
| 1 | +package k3d |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/url" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/docker/docker/api/types" |
| 13 | + "github.com/docker/docker/api/types/container" |
| 14 | + "github.com/docker/docker/api/types/filters" |
| 15 | + "github.com/k3d-io/k3d/v5/pkg/runtimes" |
| 16 | + "github.com/k3d-io/k3d/v5/pkg/runtimes/docker" |
| 17 | + "github.com/sirupsen/logrus" |
| 18 | + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + dockerHost string |
| 23 | + once sync.Once |
| 24 | +) |
| 25 | + |
| 26 | +func getDockerHost() string { |
| 27 | + once.Do(func() { |
| 28 | + var err error |
| 29 | + if runtimes.SelectedRuntime.ID() != "docker" { |
| 30 | + logrus.Debugf("runtime not docker") |
| 31 | + return |
| 32 | + } |
| 33 | + // TODO find a better way to get container id via docker.sock |
| 34 | + // Hostname as container id is not 100% reliable. |
| 35 | + hostname := os.Getenv("HOSTNAME_OVERRIDE") |
| 36 | + if hostname == "" { |
| 37 | + hostname, err = os.Hostname() |
| 38 | + if err != nil { |
| 39 | + logrus.Debugf("failed to get hostname, %v", err) |
| 40 | + return |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + runtime := runtimes.Docker |
| 45 | + |
| 46 | + if runtime.GetHost() != "" { |
| 47 | + logrus.Debugf("runtime host is %s", runtime.GetHost()) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + if _, err = os.Stat(runtime.GetRuntimePath()); err != nil { |
| 52 | + logrus.Debugf("runtime path %s doesn't exist", runtime.GetRuntimePath()) |
| 53 | + return |
| 54 | + } |
| 55 | + nodes, err := getContainersByLabel(context.Background(), map[string]string{ |
| 56 | + "org.opencontainers.image.title": "autok3s", |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + logrus.Debugf("failed to get container from runtime %s, %v", runtime.ID(), err) |
| 60 | + return |
| 61 | + } |
| 62 | + if len(nodes) == 0 { |
| 63 | + logrus.Debug("autok3s docker container not found. Skip finding docker host IP.") |
| 64 | + return |
| 65 | + } |
| 66 | + var currentContainer *types.Container |
| 67 | + for i := range nodes { |
| 68 | + node := nodes[i] |
| 69 | + if strings.HasPrefix(node.ID, hostname) { |
| 70 | + currentContainer = &node |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + if currentContainer == nil { |
| 75 | + logrus.Debugf("no container found for hostname %s", hostname) |
| 76 | + return |
| 77 | + } |
| 78 | + if currentContainer.HostConfig.NetworkMode == "host" { |
| 79 | + logrus.Debug("do nothing when running host network") |
| 80 | + return |
| 81 | + } |
| 82 | + logrus.Debugf("found container %s", currentContainer.ID) |
| 83 | + gw, err := runtime.GetHostIP(context.Background(), currentContainer.HostConfig.NetworkMode) |
| 84 | + if err != nil { |
| 85 | + logrus.Debugf("failed to get gateway ip for network %s, %v", currentContainer.HostConfig.NetworkMode, err) |
| 86 | + return |
| 87 | + } |
| 88 | + dockerHost = gw.String() |
| 89 | + logrus.Infof("found docker host IP %s", dockerHost) |
| 90 | + }) |
| 91 | + return dockerHost |
| 92 | +} |
| 93 | + |
| 94 | +func getContainersByLabel(ctx context.Context, labels map[string]string) ([]types.Container, error) { |
| 95 | + // (0) create docker client |
| 96 | + docker, err := docker.GetDockerClient() |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("Failed to create docker client. %+v", err) |
| 99 | + } |
| 100 | + defer docker.Close() |
| 101 | + |
| 102 | + filters := filters.NewArgs() |
| 103 | + for k, v := range labels { |
| 104 | + filters.Add("label", fmt.Sprintf("%s=%s", k, v)) |
| 105 | + } |
| 106 | + |
| 107 | + containers, err := docker.ContainerList(ctx, container.ListOptions{ |
| 108 | + Filters: filters, |
| 109 | + All: true, |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("failed to list containers: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + return containers, nil |
| 116 | +} |
| 117 | + |
| 118 | +func OverrideK3dKubeConfigServer(from, to string, config *clientcmdapi.Config) { |
| 119 | + if to == "" { |
| 120 | + return |
| 121 | + } |
| 122 | + if from == "" && getDockerHost() != "" { |
| 123 | + from = getDockerHost() |
| 124 | + } |
| 125 | + for key := range config.Clusters { |
| 126 | + cluster := config.Clusters[key] |
| 127 | + serverURL, _ := url.Parse(cluster.Server) |
| 128 | + if serverURL.Hostname() == from { |
| 129 | + _, port, _ := net.SplitHostPort(serverURL.Host) |
| 130 | + serverURL.Host = fmt.Sprintf("%s:%s", to, port) |
| 131 | + } |
| 132 | + cluster.Server = serverURL.String() |
| 133 | + return |
| 134 | + } |
| 135 | +} |
0 commit comments