MCPcopy Index your code
hub / github.com/docker/cli / parseNetworkOpts

Function parseNetworkOpts

cli/command/container/opts.go:750–808  ·  view source on GitHub ↗

parseNetworkOpts converts --network advanced options to endpoint-specs, and combines them with the old --network-alias and --links. If returns an error if conflicting options are found. this function may return _multiple_ endpoints, which is not currently supported by the daemon, but may be in futu

(copts *containerOptions)

Source from the content-addressed store, hash-verified

748// by the daemon, but may be in future; it's up to the daemon to produce an error
749// in case that is not supported.
750func parseNetworkOpts(copts *containerOptions) (map[string]*network.EndpointSettings, error) {
751 var (
752 endpoints = make(map[string]*network.EndpointSettings, len(copts.netMode.Value()))
753 hasUserDefined, hasNonUserDefined bool
754 )
755
756 if len(copts.netMode.Value()) == 0 {
757 n := opts.NetworkAttachmentOpts{
758 Target: "default",
759 }
760 if err := applyContainerOptions(&n, copts); err != nil {
761 return nil, err
762 }
763 ep, err := parseNetworkAttachmentOpt(n)
764 if err != nil {
765 return nil, err
766 }
767 endpoints["default"] = ep
768 }
769
770 for i, n := range copts.netMode.Value() {
771 if container.NetworkMode(n.Target).IsUserDefined() {
772 hasUserDefined = true
773 } else {
774 hasNonUserDefined = true
775 }
776 if i == 0 {
777 // The first network corresponds with what was previously the "only"
778 // network, and what would be used when using the non-advanced syntax
779 // `--network-alias`, `--link`, `--ip`, `--ip6`, and `--link-local-ip`
780 // are set on this network, to preserve backward compatibility with
781 // the non-advanced notation
782 if err := applyContainerOptions(&n, copts); err != nil {
783 return nil, err
784 }
785 }
786 ep, err := parseNetworkAttachmentOpt(n)
787 if err != nil {
788 return nil, err
789 }
790 if _, ok := endpoints[n.Target]; ok {
791 return nil, invalidParameter(fmt.Errorf("network %q is specified multiple times", n.Target))
792 }
793
794 // For backward compatibility: if no custom options are provided for the network,
795 // and only a single network is specified, omit the endpoint-configuration
796 // on the client (the daemon will still create it when creating the container)
797 if i == 0 && len(copts.netMode.Value()) == 1 {
798 if ep == nil || reflect.ValueOf(*ep).IsZero() {
799 continue
800 }
801 }
802 endpoints[n.Target] = ep
803 }
804 if hasUserDefined && hasNonUserDefined {
805 return nil, invalidParameter(errors.New("conflicting options: cannot attach both user-defined and non-user-defined network-modes"))
806 }
807 return endpoints, nil

Callers 1

parseFunction · 0.85

Calls 5

applyContainerOptionsFunction · 0.85
NetworkModeMethod · 0.80
invalidParameterFunction · 0.70
ValueMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…