getHostIP: fix failing on DfD and init new host.k3d.internal injection

- do not close connection of exec process before having read everything
  - as a backwards-compatible fix, we now read all logs into a new
  bufio.Reader so we can savely close the connection and original reader
- start tools node with --add-host=host.k3d.internal:host-gateway
(docker feature) and use it for getHostIP on DfD (TODO: we can use this
on all platforms)
pull/872/head
iwilltry42 3 years ago
parent 9b326c2c54
commit ebea3387da
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
  1. 11
      pkg/client/host.go
  2. 4
      pkg/client/tools.go
  3. 18
      pkg/runtimes/docker/node.go
  4. 2
      pkg/types/types.go

@ -79,9 +79,16 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust
return nil, fmt.Errorf("failed to ensure that k3d-tools node is running to get host IP :%w", err)
}
ip, err := resolveHostnameFromInside(ctx, runtime, toolsNode, "host.docker.internal", ResolveHostCmdGetEnt)
k3dInternalIP, err := resolveHostnameFromInside(ctx, runtime, toolsNode, k3d.DefaultK3dInternalHostRecord, ResolveHostCmdGetEnt)
if err == nil {
return ip, nil
return k3dInternalIP, nil
}
l.Log().Debugf("[GetHostIP on Docker Desktop] failed to resolve '%s' from inside the k3d-tools node: %v", k3d.DefaultK3dInternalHostRecord, err)
dockerInternalIP, err := resolveHostnameFromInside(ctx, runtime, toolsNode, "host.docker.internal", ResolveHostCmdGetEnt)
if err == nil {
return dockerInternalIP, nil
}
l.Log().Debugf("[GetHostIP on Docker Desktop] failed to resolve 'host.docker.internal' from inside the k3d-tools node: %v", err)

@ -25,7 +25,6 @@ package client
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
"io"
"os"
"path"
@ -33,6 +32,8 @@ import (
"sync"
"time"
"golang.org/x/sync/errgroup"
l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/rancher/k3d/v5/pkg/runtimes"
k3d "github.com/rancher/k3d/v5/pkg/types"
@ -385,6 +386,7 @@ func runToolsNode(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cl
Cmd: []string{},
Args: []string{"noop"},
RuntimeLabels: labels,
ExtraHosts: []string{fmt.Sprintf("%s:host-gateway", k3d.DefaultK3dInternalHostRecord)},
}
node.RuntimeLabels[k3d.LabelClusterName] = cluster.Name
if err := NodeRun(ctx, runtime, node, k3d.NodeCreateOpts{}); err != nil {

@ -24,6 +24,7 @@ package docker
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
@ -313,13 +314,20 @@ func (d Docker) ExecInNodeGetLogs(ctx context.Context, node *k3d.Node, cmd []str
if resp != nil {
defer resp.Close()
}
if err != nil {
if resp != nil && resp.Reader != nil { // sometimes the exec process returns with a non-zero exit code, but we still have the logs we
return resp.Reader, err
var logreader *bufio.Reader // backwards compatibility: TODO:(breaking) remove in future major version and return simple byte array or reader
if resp != nil && resp.Reader != nil {
logs, err := io.ReadAll(resp.Reader)
if err != nil {
return nil, fmt.Errorf("error reading logs from exec process in node %s: %w", node.Name, err)
}
return nil, err
lr := bytes.NewReader(logs)
logreader = bufio.NewReader(lr)
resp.Close()
}
return resp.Reader, nil
return logreader, err
}
// GetImageStream creates a tar stream for the given images, to be read (and closed) by the caller

@ -200,7 +200,7 @@ var ImportModes = map[string]ImportMode{
type ImageImportOpts struct {
KeepTar bool
KeepToolsNode bool
Mode ImportMode
Mode ImportMode
}
type IPAM struct {

Loading…
Cancel
Save