opts.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package run
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "github.com/docker/docker/pkg/namesgenerator"
  19. "github.com/docker/go-connections/nat"
  20. "github.com/docker/api/containers"
  21. "github.com/docker/api/formatter"
  22. )
  23. // Opts contain run command options
  24. type Opts struct {
  25. Name string
  26. Publish []string
  27. Labels []string
  28. Volumes []string
  29. Cpus float64
  30. Memory formatter.MemBytes
  31. Detach bool
  32. }
  33. // ToContainerConfig convert run options to a container configuration
  34. func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
  35. if r.Name == "" {
  36. r.Name = getRandomName()
  37. }
  38. publish, err := r.toPorts()
  39. if err != nil {
  40. return containers.ContainerConfig{}, err
  41. }
  42. labels, err := toLabels(r.Labels)
  43. if err != nil {
  44. return containers.ContainerConfig{}, err
  45. }
  46. return containers.ContainerConfig{
  47. ID: r.Name,
  48. Image: image,
  49. Ports: publish,
  50. Labels: labels,
  51. Volumes: r.Volumes,
  52. MemLimit: r.Memory,
  53. CPULimit: r.Cpus,
  54. }, nil
  55. }
  56. func (r *Opts) toPorts() ([]containers.Port, error) {
  57. _, bindings, err := nat.ParsePortSpecs(r.Publish)
  58. if err != nil {
  59. return nil, err
  60. }
  61. var result []containers.Port
  62. for port, bind := range bindings {
  63. for _, portbind := range bind {
  64. var hostPort uint32
  65. if portbind.HostPort != "" {
  66. hp, err := strconv.Atoi(portbind.HostPort)
  67. if err != nil {
  68. return nil, err
  69. }
  70. hostPort = uint32(hp)
  71. } else {
  72. hostPort = uint32(port.Int())
  73. }
  74. result = append(result, containers.Port{
  75. HostPort: hostPort,
  76. ContainerPort: uint32(port.Int()),
  77. Protocol: port.Proto(),
  78. HostIP: portbind.HostIP,
  79. })
  80. }
  81. }
  82. return result, nil
  83. }
  84. func toLabels(labels []string) (map[string]string, error) {
  85. result := map[string]string{}
  86. for _, label := range labels {
  87. parts := strings.Split(label, "=")
  88. if len(parts) != 2 {
  89. return nil, fmt.Errorf("wrong label format %q", label)
  90. }
  91. result[parts[0]] = parts[1]
  92. }
  93. return result, nil
  94. }
  95. func getRandomName() string {
  96. // Azure supports hyphen but not underscore in names
  97. return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
  98. }