Skip to content

Commit

Permalink
feat: resolve special ip host-gateway
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <ningziwe@amazon.com>
  • Loading branch information
ningziwen committed Feb 10, 2023
1 parent 875560e commit 31e6a78
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/nerdctl/container_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ import (
"github.com/containerd/nerdctl/pkg/referenceutil"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/containerd/nerdctl/pkg/taskutil"
dopts "github.com/docker/cli/opts"
dockercliopts "github.com/docker/cli/opts"
dockerops "github.com/docker/docker/opts"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -707,10 +708,25 @@ func createContainer(ctx context.Context, cmd *cobra.Command, client *containerd
return nil, nil, err
}
extraHosts = strutil.DedupeStrSlice(extraHosts)
for _, host := range extraHosts {
if _, err := dopts.ValidateExtraHost(host); err != nil {
for i, host := range extraHosts {
if _, err := dockercliopts.ValidateExtraHost(host); err != nil {
return nil, nil, err
}
parts := strings.SplitN(host, ":", 2)
// If the IP Address is a string called "host-gateway", replace this value with the IP address stored
// in the daemon level HostGateway IP config variable.
if parts[1] == dockerops.HostGatewayName {
gateway, err := cmd.Flags().GetString("host-gateway-ip")
logrus.Info("gateway: " + gateway) // TODO: remove in final PR
if err != nil {
return nil, nil, err
}
if gateway == "" {
return nil, nil, fmt.Errorf("unable to derive the IP value for host-gateway")
}
parts[1] = gateway
extraHosts[i] = fmt.Sprintf(`%s:%s`, parts[0], parts[1])
}
}
internalLabels.extraHosts = extraHosts

Expand Down Expand Up @@ -995,7 +1011,7 @@ func readKVStringsMapfFromLabel(cmd *cobra.Command) (map[string]string, error) {
return nil, err
}
labelsFilePath = strutil.DedupeStrSlice(labelsFilePath)
labels, err := dopts.ReadKVStrings(labelsFilePath, labelsMap)
labels, err := dockercliopts.ReadKVStrings(labelsFilePath, labelsMap)
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/nerdctl/flagutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func processRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error)
if err != nil {
return types.GlobalCommandOptions{}, err
}
hostGatewayIP, err := cmd.Flags().GetString("host-gateway-ip")
if err != nil {
return types.GlobalCommandOptions{}, err
}
return types.GlobalCommandOptions{
Debug: debug,
DebugFull: debugFull,
Expand All @@ -83,5 +87,6 @@ func processRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error)
InsecureRegistry: insecureRegistry,
HostsDir: hostsDir,
Experimental: experimental,
HostGatewayIP: hostGatewayIP,
}, nil
}
1 change: 1 addition & 0 deletions cmd/nerdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet,
rootCmd.PersistentFlags().StringSlice("hosts-dir", cfg.HostsDir, "A directory that contains <HOST:PORT>/hosts.toml (containerd style) or <HOST:PORT>/{ca.cert, cert.pem, key.pem} (docker style)")
// Experimental enable experimental feature, see in https://github.com/containerd/nerdctl/blob/main/docs/experimental.md
AddPersistentBoolFlag(rootCmd, "experimental", nil, nil, cfg.Experimental, "NERDCTL_EXPERIMENTAL", "Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md")
rootCmd.PersistentFlags().String("host-gateway-ip", cfg.HostGatewayIP, "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge")
return aliasToBeInherited, nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
InsecureRegistry bool `toml:"insecure_registry"`
HostsDir []string `toml:"hosts_dir"`
Experimental bool `toml:"experimental"`
HostGatewayIP string `toml:"host_gateway_ip"`
}

// New creates a default Config object statically,
Expand All @@ -56,5 +57,6 @@ func New() *Config {
InsecureRegistry: false,
HostsDir: ncdefaults.HostsDirs(),
Experimental: true,
HostGatewayIP: "",
}
}

0 comments on commit 31e6a78

Please sign in to comment.