github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_run_network.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"net"
    21  
    22  	gocni "github.com/containerd/go-cni"
    23  	"github.com/containerd/nerdctl/pkg/api/types"
    24  	"github.com/containerd/nerdctl/pkg/portutil"
    25  	"github.com/containerd/nerdctl/pkg/strutil"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  func loadNetworkFlags(cmd *cobra.Command) (types.NetworkOptions, error) {
    30  	netOpts := types.NetworkOptions{}
    31  
    32  	// --net/--network=<net name> ...
    33  	var netSlice = []string{}
    34  	var networkSet = false
    35  	if cmd.Flags().Lookup("network").Changed {
    36  		network, err := cmd.Flags().GetStringSlice("network")
    37  		if err != nil {
    38  			return netOpts, err
    39  		}
    40  		netSlice = append(netSlice, network...)
    41  		networkSet = true
    42  	}
    43  	if cmd.Flags().Lookup("net").Changed {
    44  		net, err := cmd.Flags().GetStringSlice("net")
    45  		if err != nil {
    46  			return netOpts, err
    47  		}
    48  		netSlice = append(netSlice, net...)
    49  		networkSet = true
    50  	}
    51  
    52  	if !networkSet {
    53  		network, err := cmd.Flags().GetStringSlice("network")
    54  		if err != nil {
    55  			return netOpts, err
    56  		}
    57  		netSlice = append(netSlice, network...)
    58  	}
    59  	netOpts.NetworkSlice = strutil.DedupeStrSlice(netSlice)
    60  
    61  	// --mac-address=<MAC>
    62  	macAddress, err := cmd.Flags().GetString("mac-address")
    63  	if err != nil {
    64  		return netOpts, err
    65  	}
    66  	if macAddress != "" {
    67  		if _, err := net.ParseMAC(macAddress); err != nil {
    68  			return netOpts, err
    69  		}
    70  	}
    71  	netOpts.MACAddress = macAddress
    72  
    73  	// --ip=<container static IP>
    74  	ipAddress, err := cmd.Flags().GetString("ip")
    75  	if err != nil {
    76  		return netOpts, err
    77  	}
    78  	netOpts.IPAddress = ipAddress
    79  
    80  	// --ip6=<container static IP6>
    81  	ip6Address, err := cmd.Flags().GetString("ip6")
    82  	if err != nil {
    83  		return netOpts, err
    84  	}
    85  	netOpts.IP6Address = ip6Address
    86  
    87  	// -h/--hostname=<container hostname>
    88  	hostName, err := cmd.Flags().GetString("hostname")
    89  	if err != nil {
    90  		return netOpts, err
    91  	}
    92  	netOpts.Hostname = hostName
    93  
    94  	// --dns=<DNS host> ...
    95  	dnsSlice, err := cmd.Flags().GetStringSlice("dns")
    96  	if err != nil {
    97  		return netOpts, err
    98  	}
    99  	netOpts.DNSServers = strutil.DedupeStrSlice(dnsSlice)
   100  
   101  	// --dns-search=<domain name> ...
   102  	dnsSearchSlice, err := cmd.Flags().GetStringSlice("dns-search")
   103  	if err != nil {
   104  		return netOpts, err
   105  	}
   106  	netOpts.DNSSearchDomains = strutil.DedupeStrSlice(dnsSearchSlice)
   107  
   108  	// --dns-opt/--dns-option=<resolv.conf line> ...
   109  	dnsOptions := []string{}
   110  
   111  	dnsOptFlags, err := cmd.Flags().GetStringSlice("dns-opt")
   112  	if err != nil {
   113  		return netOpts, err
   114  	}
   115  	dnsOptions = append(dnsOptions, dnsOptFlags...)
   116  
   117  	dnsOptionFlags, err := cmd.Flags().GetStringSlice("dns-option")
   118  	if err != nil {
   119  		return netOpts, err
   120  	}
   121  	dnsOptions = append(dnsOptions, dnsOptionFlags...)
   122  
   123  	netOpts.DNSResolvConfOptions = strutil.DedupeStrSlice(dnsOptions)
   124  
   125  	// --add-host=<host:IP> ...
   126  	addHostFlags, err := cmd.Flags().GetStringSlice("add-host")
   127  	if err != nil {
   128  		return netOpts, err
   129  	}
   130  	netOpts.AddHost = addHostFlags
   131  
   132  	// --uts=<Unix Time Sharing namespace>
   133  	utsNamespace, err := cmd.Flags().GetString("uts")
   134  	if err != nil {
   135  		return netOpts, err
   136  	}
   137  	netOpts.UTSNamespace = utsNamespace
   138  
   139  	// -p/--publish=127.0.0.1:80:8080/tcp ...
   140  	portSlice, err := cmd.Flags().GetStringSlice("publish")
   141  	if err != nil {
   142  		return netOpts, err
   143  	}
   144  	portSlice = strutil.DedupeStrSlice(portSlice)
   145  	portMappings := []gocni.PortMapping{}
   146  	for _, p := range portSlice {
   147  		pm, err := portutil.ParseFlagP(p)
   148  		if err != nil {
   149  			return netOpts, err
   150  		}
   151  		portMappings = append(portMappings, pm...)
   152  	}
   153  	netOpts.PortMappings = portMappings
   154  
   155  	return netOpts, nil
   156  }