Skip to content

Commit

Permalink
feat: docker setup container logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnr committed Jan 12, 2022
1 parent 824c209 commit 4d0b4ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func main() {
os.Exit(ExitSetupFailed)
}

fmt.Printf("Setting up interface %s\n", interfaceName)

links, err := netlink.LinkList()
if err != nil {
fmt.Printf("Could not list links: %v\n", err)
Expand All @@ -80,6 +78,8 @@ func main() {
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = interfaceName

fmt.Printf("Creating WireGuard interface %s\n", interfaceName)

wireguard := &netlink.Wireguard{LinkAttrs: linkAttrs}
err = netlink.LinkAdd(wireguard)
if err != nil {
Expand All @@ -95,6 +95,8 @@ func main() {
fmt.Printf("Could not parse host peer IPNet: %v\n", err)
}

fmt.Println("Assigning IP to WireGuard interface")

addr := netlink.Addr{IPNet: vmIpNet, Peer: hostIpNet}
netlink.AddrAdd(wireguard, &addr)

Expand Down Expand Up @@ -146,6 +148,8 @@ func main() {
},
}

fmt.Println("Configuring WireGuard device")

err = c.ConfigureDevice(interfaceName, wgtypes.Config{
PrivateKey: &vmPrivateKey,
Peers: []wgtypes.PeerConfig{peer},
Expand All @@ -167,6 +171,8 @@ func main() {
os.Exit(ExitSetupFailed)
}

fmt.Println("Adding iptables NAT rule for host WireGuard IP")

// Add iptables NAT rule to translate incoming packet's
// source IP to the respective Docker network interface IP.
// Required to route reply packets back through correct
Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/ipc"
Expand Down Expand Up @@ -314,5 +315,27 @@ func setupVm(
return fmt.Errorf("failed to start container: %w", err)
}

func() error {
reader, err := dockerCli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
return fmt.Errorf("failed to get logs for container %s: %w", resp.ID, err)
}

defer reader.Close()

_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, reader)
if err != nil {
return err
}

return nil
}()

fmt.Println("Setup container complete")

return nil
}

0 comments on commit 4d0b4ff

Please sign in to comment.