opts.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/cli/opts"
  19. "github.com/docker/docker/pkg/namesgenerator"
  20. "github.com/docker/go-connections/nat"
  21. "github.com/docker/api/containers"
  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 opts.MemBytes
  31. }
  32. // ToContainerConfig convert run options to a container configuration
  33. func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, error) {
  34. if r.Name == "" {
  35. r.Name = getRandomName()
  36. }
  37. publish, err := r.toPorts()
  38. if err != nil {
  39. return containers.ContainerConfig{}, err
  40. }
  41. labels, err := toLabels(r.Labels)
  42. if err != nil {
  43. return containers.ContainerConfig{}, err
  44. }
  45. return containers.ContainerConfig{
  46. ID: r.Name,
  47. Image: image,
  48. Ports: publish,
  49. Labels: labels,
  50. Volumes: r.Volumes,
  51. MemLimit: r.Memory,
  52. CpuLimit: r.Cpus,
  53. }, nil
  54. }
  55. func (r *Opts) toPorts() ([]containers.Port, error) {
  56. _, bindings, err := nat.ParsePortSpecs(r.Publish)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var result []containers.Port
  61. for port, bind := range bindings {
  62. for _, portbind := range bind {
  63. var hostPort uint32
  64. if portbind.HostPort != "" {
  65. hp, err := strconv.Atoi(portbind.HostPort)
  66. if err != nil {
  67. return nil, err
  68. }
  69. hostPort = uint32(hp)
  70. } else {
  71. hostPort = uint32(port.Int())
  72. }
  73. result = append(result, containers.Port{
  74. HostPort: hostPort,
  75. ContainerPort: uint32(port.Int()),
  76. Protocol: port.Proto(),
  77. HostIP: portbind.HostIP,
  78. })
  79. }
  80. }
  81. return result, nil
  82. }
  83. func toLabels(labels []string) (map[string]string, error) {
  84. result := map[string]string{}
  85. for _, label := range labels {
  86. parts := strings.Split(label, "=")
  87. if len(parts) != 2 {
  88. return nil, fmt.Errorf("wrong label format %q", label)
  89. }
  90. result[parts[0]] = parts[1]
  91. }
  92. return result, nil
  93. }
  94. func getRandomName() string {
  95. // Azure supports hyphen but not underscore in names
  96. return strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
  97. }