Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cli/command/network/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package network

import (
"context"
"fmt"

"errors"


"net"
"net/netip"
"strings"
Expand All @@ -21,6 +25,7 @@ type connectOptions struct {
container string
ipaddress net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
ipv6address net.IP // TODO(thaJeztah): we need a flag-type to handle netip.Addr directly
macAddress net.HardwareAddr
links opts.ListOpts
aliases []string
linklocalips []net.IP // TODO(thaJeztah): we need a flag-type to handle []netip.Addr directly
Expand All @@ -32,6 +37,7 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
options := connectOptions{
links: opts.NewListOpts(opts.ValidateLink),
}
var macStr string

cmd := &cobra.Command{
Use: "connect [OPTIONS] NETWORK CONTAINER",
Expand All @@ -40,6 +46,15 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
options.network = args[0]
options.container = args[1]
// Validate MAC address if provided

if macStr != "" {
mac, err := net.ParseMAC(macStr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the type; we could probably use the network.HardwareAddr's UnmarshalText method for this; that way we wouldn't have to cast it to the right type 🤔

if err != nil {
return fmt.Errorf("invalid MAC address: %q", macStr)
}
options.macAddress = mac
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lacking a special mac-address flag-type (I'm looking into creating some generics for these), we can probably add macAddress as string for now, and handle the parsing in runConnect

}
return runConnect(cmd.Context(), dockerCLI.Client(), options)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand All @@ -60,26 +75,34 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
flags.IPSliceVar(&options.linklocalips, "link-local-ip", nil, "Add a link-local address for the container")
flags.StringSliceVar(&options.driverOpts, "driver-opt", []string{}, "driver options for the network")
flags.IntVar(&options.gwPriority, "gw-priority", 0, "Highest gw-priority provides the default gateway. Accepts positive and negative values.")
flags.StringVar(&macStr, "mac-address", "", "MAC address for the container on this network")
return cmd
}

func runConnect(ctx context.Context, apiClient client.NetworkAPIClient, options connectOptions) error {
driverOpts, err := convertDriverOpt(options.driverOpts)

if err != nil {
return err
}




_, err = apiClient.NetworkConnect(ctx, options.network, client.NetworkConnectOptions{
Container: options.container,
EndpointConfig: &network.EndpointSettings{
IPAMConfig: &network.EndpointIPAMConfig{
IPv4Address: toNetipAddr(options.ipaddress),
IPv6Address: toNetipAddr(options.ipv6address),
LinkLocalIPs: toNetipAddrSlice(options.linklocalips),

},
Links: options.links.GetSlice(),
Aliases: options.aliases,
DriverOpts: driverOpts,
GwPriority: options.gwPriority,
MacAddress : network.HardwareAddr(options.macAddress),
},
})
return err
Expand Down
Loading