hostconfig_unix.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build !windows,!solaris
  2. package runconfig
  3. import (
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "github.com/docker/engine-api/types/container"
  8. )
  9. // DefaultDaemonNetworkMode returns the default network stack the daemon should
  10. // use.
  11. func DefaultDaemonNetworkMode() container.NetworkMode {
  12. return container.NetworkMode("bridge")
  13. }
  14. // IsPreDefinedNetwork indicates if a network is predefined by the daemon
  15. func IsPreDefinedNetwork(network string) bool {
  16. n := container.NetworkMode(network)
  17. return n.IsBridge() || n.IsHost() || n.IsNone() || n.IsDefault()
  18. }
  19. // ValidateNetMode ensures that the various combinations of requested
  20. // network settings are valid.
  21. func ValidateNetMode(c *container.Config, hc *container.HostConfig) error {
  22. // We may not be passed a host config, such as in the case of docker commit
  23. if hc == nil {
  24. return nil
  25. }
  26. parts := strings.Split(string(hc.NetworkMode), ":")
  27. if parts[0] == "container" {
  28. if len(parts) < 2 || parts[1] == "" {
  29. return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
  30. }
  31. }
  32. if hc.NetworkMode.IsContainer() && c.Hostname != "" {
  33. return ErrConflictNetworkHostname
  34. }
  35. if hc.UTSMode.IsHost() && c.Hostname != "" {
  36. return ErrConflictUTSHostname
  37. }
  38. if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
  39. return ErrConflictHostNetworkAndLinks
  40. }
  41. if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
  42. return ErrConflictContainerNetworkAndLinks
  43. }
  44. if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
  45. return ErrConflictNetworkAndDNS
  46. }
  47. if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
  48. return ErrConflictNetworkHosts
  49. }
  50. if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
  51. return ErrConflictContainerNetworkAndMac
  52. }
  53. if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
  54. return ErrConflictNetworkPublishPorts
  55. }
  56. if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
  57. return ErrConflictNetworkExposePorts
  58. }
  59. return nil
  60. }
  61. // ValidateIsolation performs platform specific validation of
  62. // isolation in the hostconfig structure. Linux only supports "default"
  63. // which is LXC container isolation
  64. func ValidateIsolation(hc *container.HostConfig) error {
  65. // We may not be passed a host config, such as in the case of docker commit
  66. if hc == nil {
  67. return nil
  68. }
  69. if !hc.Isolation.IsValid() {
  70. return fmt.Errorf("invalid --isolation: %q - %s only supports 'default'", hc.Isolation, runtime.GOOS)
  71. }
  72. return nil
  73. }
  74. // ValidateQoS performs platform specific validation of the QoS settings
  75. // a disk can be limited by either Bps or IOps, but not both.
  76. func ValidateQoS(hc *container.HostConfig) error {
  77. // We may not be passed a host config, such as in the case of docker commit
  78. if hc == nil {
  79. return nil
  80. }
  81. if hc.IOMaximumBandwidth != 0 {
  82. return fmt.Errorf("invalid QoS settings: %s does not support --maximum-bandwidth", runtime.GOOS)
  83. }
  84. if hc.IOMaximumIOps != 0 {
  85. return fmt.Errorf("invalid QoS settings: %s does not support --maximum-iops", runtime.GOOS)
  86. }
  87. return nil
  88. }