labels.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package netlabel
  2. import (
  3. "strings"
  4. )
  5. const (
  6. // Prefix constant marks the reserved label space for libnetwork
  7. Prefix = "com.docker.network"
  8. // DriverPrefix constant marks the reserved label space for libnetwork drivers
  9. DriverPrefix = Prefix + ".driver"
  10. // DriverPrivatePrefix constant marks the reserved label space
  11. // for internal libnetwork drivers
  12. DriverPrivatePrefix = DriverPrefix + ".private"
  13. // GenericData constant that helps to identify an option as a Generic constant
  14. GenericData = Prefix + ".generic"
  15. // PortMap constant represents Port Mapping
  16. PortMap = Prefix + ".portmap"
  17. // MacAddress constant represents Mac Address config of a Container
  18. MacAddress = Prefix + ".endpoint.macaddress"
  19. // ExposedPorts constant represents the container's Exposed Ports
  20. ExposedPorts = Prefix + ".endpoint.exposedports"
  21. //EnableIPv6 constant represents enabling IPV6 at network level
  22. EnableIPv6 = Prefix + ".enable_ipv6"
  23. // DriverMTU constant represents the MTU size for the network driver
  24. DriverMTU = DriverPrefix + ".mtu"
  25. // OverlayBindInterface constant represents overlay driver bind interface
  26. OverlayBindInterface = DriverPrefix + ".overlay.bind_interface"
  27. // OverlayNeighborIP constant represents overlay driver neighbor IP
  28. OverlayNeighborIP = DriverPrefix + ".overlay.neighbor_ip"
  29. // Gateway represents the gateway for the network
  30. Gateway = Prefix + ".gateway"
  31. )
  32. var (
  33. // GlobalKVProvider constant represents the KV provider backend
  34. GlobalKVProvider = MakeKVProvider("global")
  35. // GlobalKVProviderURL constant represents the KV provider URL
  36. GlobalKVProviderURL = MakeKVProviderURL("global")
  37. // GlobalKVProviderConfig constant represents the KV provider Config
  38. GlobalKVProviderConfig = MakeKVProviderConfig("global")
  39. // LocalKVProvider constant represents the KV provider backend
  40. LocalKVProvider = MakeKVProvider("local")
  41. // LocalKVProviderURL constant represents the KV provider URL
  42. LocalKVProviderURL = MakeKVProviderURL("local")
  43. // LocalKVProviderConfig constant represents the KV provider Config
  44. LocalKVProviderConfig = MakeKVProviderConfig("local")
  45. )
  46. // MakeKVProvider returns the kvprovider label for the scope
  47. func MakeKVProvider(scope string) string {
  48. return DriverPrivatePrefix + scope + "kv_provider"
  49. }
  50. // MakeKVProviderURL returns the kvprovider url label for the scope
  51. func MakeKVProviderURL(scope string) string {
  52. return DriverPrivatePrefix + scope + "kv_provider_url"
  53. }
  54. // MakeKVProviderConfig returns the kvprovider config label for the scope
  55. func MakeKVProviderConfig(scope string) string {
  56. return DriverPrivatePrefix + scope + "kv_provider_config"
  57. }
  58. // Key extracts the key portion of the label
  59. func Key(label string) (key string) {
  60. if kv := strings.SplitN(label, "=", 2); len(kv) > 0 {
  61. key = kv[0]
  62. }
  63. return
  64. }
  65. // Value extracts the value portion of the label
  66. func Value(label string) (value string) {
  67. if kv := strings.SplitN(label, "=", 2); len(kv) > 1 {
  68. value = kv[1]
  69. }
  70. return
  71. }
  72. // KeyValue decomposes the label in the (key,value) pair
  73. func KeyValue(label string) (key string, value string) {
  74. if kv := strings.SplitN(label, "=", 2); len(kv) > 0 {
  75. key = kv[0]
  76. if len(kv) > 1 {
  77. value = kv[1]
  78. }
  79. }
  80. return
  81. }
  82. // ToMap converts a list of labels in a map of (key,value) pairs
  83. func ToMap(labels []string) map[string]string {
  84. m := make(map[string]string, len(labels))
  85. for _, l := range labels {
  86. k, v := KeyValue(l)
  87. m[k] = v
  88. }
  89. return m
  90. }
  91. // FromMap converts a map of (key,value) pairs in a lsit of labels
  92. func FromMap(m map[string]string) []string {
  93. l := make([]string, 0, len(m))
  94. for k, v := range m {
  95. s := k
  96. if v != "" {
  97. s = s + "=" + v
  98. }
  99. l = append(l, s)
  100. }
  101. return l
  102. }