Skip to content

Commit a70c116

Browse files
authored
Merge pull request #1 from netinsight/support-macvlan
Support macvlan and ipvlan.
2 parents b2b7266 + 63e6904 commit a70c116

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

pkg/cluster/internal/providers/docker/provider.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,36 @@ func (p *provider) GetAPIServerEndpoint(cluster string) (string, error) {
188188
}
189189

190190
// else, retrieve the specific port mapping via NetworkSettings.Ports
191+
// Use a template that handles nil Ports gracefully
191192
cmd = exec.Command(
192193
"docker", "inspect",
193194
"--format", fmt.Sprintf(
194-
"{{ with (index (index .NetworkSettings.Ports \"%d/tcp\") 0) }}{{ printf \"%%s\t%%s\" .HostIp .HostPort }}{{ end }}", common.APIServerInternalPort,
195+
"{{ if .NetworkSettings.Ports }}{{ with (index (index .NetworkSettings.Ports \"%d/tcp\") 0) }}{{ printf \"%%s\t%%s\" .HostIp .HostPort }}{{ end }}{{ end }}", common.APIServerInternalPort,
195196
),
196197
n.String(),
197198
)
198199
lines, err = exec.OutputLines(cmd)
199-
if err != nil {
200-
return "", errors.Wrap(err, "failed to get api server port")
200+
// if the command failed or returned empty (nil Ports or no port mapping),
201+
// fall back to using the container's IP address directly
202+
// This handles macvlan/ipvlan networks where port mappings don't exist
203+
if err != nil || len(lines) != 1 || lines[0] == "" {
204+
// query the container's IP address from NetworkSettings
205+
cmd = exec.Command(
206+
"docker", "inspect",
207+
"--format", "{{ range .NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}",
208+
n.String(),
209+
)
210+
lines, err = exec.OutputLines(cmd)
211+
if err != nil {
212+
return "", errors.Wrap(err, "failed to get container IP address")
213+
}
214+
if len(lines) != 1 || lines[0] == "" {
215+
return "", errors.Errorf("failed to get container IP address, got %d lines", len(lines))
216+
}
217+
// use the container's IP address with the internal API server port
218+
return net.JoinHostPort(lines[0], fmt.Sprintf("%d", common.APIServerInternalPort)), nil
201219
}
220+
202221
if len(lines) != 1 {
203222
return "", errors.Errorf("network details should only be one line, got %d lines", len(lines))
204223
}

0 commit comments

Comments
 (0)