github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/api/types/container/hostconfig_unix.go (about)

     1  // +build !windows
     2  
     3  package container
     4  
     5  import "strings"
     6  
     7  // IsValid indicates if an isolation technology is valid
     8  func (i Isolation) IsValid() bool {
     9  	return i.IsDefault()
    10  }
    11  
    12  // IsPrivate indicates whether container uses its private network stack.
    13  func (n NetworkMode) IsPrivate() bool {
    14  	return !(n.IsHost() || n.IsContainer())
    15  }
    16  
    17  // IsDefault indicates whether container uses the default network stack.
    18  func (n NetworkMode) IsDefault() bool {
    19  	return n == "default"
    20  }
    21  
    22  // NetworkName returns the name of the network stack.
    23  func (n NetworkMode) NetworkName() string {
    24  	if n.IsBridge() {
    25  		return "bridge"
    26  	} else if n.IsHost() {
    27  		return "host"
    28  	} else if n.IsContainer() {
    29  		return "container"
    30  	} else if n.IsNone() {
    31  		return "none"
    32  	} else if n.IsDefault() {
    33  		return "default"
    34  	} else if n.IsUserDefined() {
    35  		return n.UserDefined()
    36  	}
    37  	return ""
    38  }
    39  
    40  // IsBridge indicates whether container uses the bridge network stack
    41  func (n NetworkMode) IsBridge() bool {
    42  	return n == "bridge"
    43  }
    44  
    45  // IsHost indicates whether container uses the host network stack.
    46  func (n NetworkMode) IsHost() bool {
    47  	return n == "host"
    48  }
    49  
    50  // IsContainer indicates whether container uses a container network stack.
    51  func (n NetworkMode) IsContainer() bool {
    52  	parts := strings.SplitN(string(n), ":", 2)
    53  	return len(parts) > 1 && parts[0] == "container"
    54  }
    55  
    56  // IsNone indicates whether container isn't using a network stack.
    57  func (n NetworkMode) IsNone() bool {
    58  	return n == "none"
    59  }
    60  
    61  // ConnectedContainer is the id of the container which network this container is connected to.
    62  func (n NetworkMode) ConnectedContainer() string {
    63  	parts := strings.SplitN(string(n), ":", 2)
    64  	if len(parts) > 1 {
    65  		return parts[1]
    66  	}
    67  	return ""
    68  }
    69  
    70  // IsUserDefined indicates user-created network
    71  func (n NetworkMode) IsUserDefined() bool {
    72  	return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer()
    73  }
    74  
    75  //UserDefined indicates user-created network
    76  func (n NetworkMode) UserDefined() string {
    77  	if n.IsUserDefined() {
    78  		return string(n)
    79  	}
    80  	return ""
    81  }