container.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package convert
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/Sirupsen/logrus"
  6. container "github.com/docker/docker/api/types/container"
  7. mounttypes "github.com/docker/docker/api/types/mount"
  8. types "github.com/docker/docker/api/types/swarm"
  9. swarmapi "github.com/docker/swarmkit/api"
  10. "github.com/docker/swarmkit/protobuf/ptypes"
  11. )
  12. func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {
  13. containerSpec := types.ContainerSpec{
  14. Image: c.Image,
  15. Labels: c.Labels,
  16. Command: c.Command,
  17. Args: c.Args,
  18. Hostname: c.Hostname,
  19. Env: c.Env,
  20. Dir: c.Dir,
  21. User: c.User,
  22. Groups: c.Groups,
  23. TTY: c.TTY,
  24. OpenStdin: c.OpenStdin,
  25. Hosts: c.Hosts,
  26. Secrets: secretReferencesFromGRPC(c.Secrets),
  27. }
  28. if c.DNSConfig != nil {
  29. containerSpec.DNSConfig = &types.DNSConfig{
  30. Nameservers: c.DNSConfig.Nameservers,
  31. Search: c.DNSConfig.Search,
  32. Options: c.DNSConfig.Options,
  33. }
  34. }
  35. // Mounts
  36. for _, m := range c.Mounts {
  37. mount := mounttypes.Mount{
  38. Target: m.Target,
  39. Source: m.Source,
  40. Type: mounttypes.Type(strings.ToLower(swarmapi.Mount_MountType_name[int32(m.Type)])),
  41. ReadOnly: m.ReadOnly,
  42. }
  43. if m.BindOptions != nil {
  44. mount.BindOptions = &mounttypes.BindOptions{
  45. Propagation: mounttypes.Propagation(strings.ToLower(swarmapi.Mount_BindOptions_MountPropagation_name[int32(m.BindOptions.Propagation)])),
  46. }
  47. }
  48. if m.VolumeOptions != nil {
  49. mount.VolumeOptions = &mounttypes.VolumeOptions{
  50. NoCopy: m.VolumeOptions.NoCopy,
  51. Labels: m.VolumeOptions.Labels,
  52. }
  53. if m.VolumeOptions.DriverConfig != nil {
  54. mount.VolumeOptions.DriverConfig = &mounttypes.Driver{
  55. Name: m.VolumeOptions.DriverConfig.Name,
  56. Options: m.VolumeOptions.DriverConfig.Options,
  57. }
  58. }
  59. }
  60. if m.TmpfsOptions != nil {
  61. mount.TmpfsOptions = &mounttypes.TmpfsOptions{
  62. SizeBytes: m.TmpfsOptions.SizeBytes,
  63. Mode: m.TmpfsOptions.Mode,
  64. }
  65. }
  66. containerSpec.Mounts = append(containerSpec.Mounts, mount)
  67. }
  68. if c.StopGracePeriod != nil {
  69. grace, _ := ptypes.Duration(c.StopGracePeriod)
  70. containerSpec.StopGracePeriod = &grace
  71. }
  72. if c.Healthcheck != nil {
  73. containerSpec.Healthcheck = healthConfigFromGRPC(c.Healthcheck)
  74. }
  75. return containerSpec
  76. }
  77. func secretReferencesToGRPC(sr []*types.SecretReference) []*swarmapi.SecretReference {
  78. refs := make([]*swarmapi.SecretReference, 0, len(sr))
  79. for _, s := range sr {
  80. ref := &swarmapi.SecretReference{
  81. SecretID: s.SecretID,
  82. SecretName: s.SecretName,
  83. }
  84. if s.File != nil {
  85. ref.Target = &swarmapi.SecretReference_File{
  86. File: &swarmapi.SecretReference_FileTarget{
  87. Name: s.File.Name,
  88. UID: s.File.UID,
  89. GID: s.File.GID,
  90. Mode: s.File.Mode,
  91. },
  92. }
  93. }
  94. refs = append(refs, ref)
  95. }
  96. return refs
  97. }
  98. func secretReferencesFromGRPC(sr []*swarmapi.SecretReference) []*types.SecretReference {
  99. refs := make([]*types.SecretReference, 0, len(sr))
  100. for _, s := range sr {
  101. target := s.GetFile()
  102. if target == nil {
  103. // not a file target
  104. logrus.Warnf("secret target not a file: secret=%s", s.SecretID)
  105. continue
  106. }
  107. refs = append(refs, &types.SecretReference{
  108. File: &types.SecretReferenceFileTarget{
  109. Name: target.Name,
  110. UID: target.UID,
  111. GID: target.GID,
  112. Mode: target.Mode,
  113. },
  114. SecretID: s.SecretID,
  115. SecretName: s.SecretName,
  116. })
  117. }
  118. return refs
  119. }
  120. func containerToGRPC(c types.ContainerSpec) (*swarmapi.ContainerSpec, error) {
  121. containerSpec := &swarmapi.ContainerSpec{
  122. Image: c.Image,
  123. Labels: c.Labels,
  124. Command: c.Command,
  125. Args: c.Args,
  126. Hostname: c.Hostname,
  127. Env: c.Env,
  128. Dir: c.Dir,
  129. User: c.User,
  130. Groups: c.Groups,
  131. TTY: c.TTY,
  132. OpenStdin: c.OpenStdin,
  133. Hosts: c.Hosts,
  134. Secrets: secretReferencesToGRPC(c.Secrets),
  135. }
  136. if c.DNSConfig != nil {
  137. containerSpec.DNSConfig = &swarmapi.ContainerSpec_DNSConfig{
  138. Nameservers: c.DNSConfig.Nameservers,
  139. Search: c.DNSConfig.Search,
  140. Options: c.DNSConfig.Options,
  141. }
  142. }
  143. if c.StopGracePeriod != nil {
  144. containerSpec.StopGracePeriod = ptypes.DurationProto(*c.StopGracePeriod)
  145. }
  146. // Mounts
  147. for _, m := range c.Mounts {
  148. mount := swarmapi.Mount{
  149. Target: m.Target,
  150. Source: m.Source,
  151. ReadOnly: m.ReadOnly,
  152. }
  153. if mountType, ok := swarmapi.Mount_MountType_value[strings.ToUpper(string(m.Type))]; ok {
  154. mount.Type = swarmapi.Mount_MountType(mountType)
  155. } else if string(m.Type) != "" {
  156. return nil, fmt.Errorf("invalid MountType: %q", m.Type)
  157. }
  158. if m.BindOptions != nil {
  159. if mountPropagation, ok := swarmapi.Mount_BindOptions_MountPropagation_value[strings.ToUpper(string(m.BindOptions.Propagation))]; ok {
  160. mount.BindOptions = &swarmapi.Mount_BindOptions{Propagation: swarmapi.Mount_BindOptions_MountPropagation(mountPropagation)}
  161. } else if string(m.BindOptions.Propagation) != "" {
  162. return nil, fmt.Errorf("invalid MountPropagation: %q", m.BindOptions.Propagation)
  163. }
  164. }
  165. if m.VolumeOptions != nil {
  166. mount.VolumeOptions = &swarmapi.Mount_VolumeOptions{
  167. NoCopy: m.VolumeOptions.NoCopy,
  168. Labels: m.VolumeOptions.Labels,
  169. }
  170. if m.VolumeOptions.DriverConfig != nil {
  171. mount.VolumeOptions.DriverConfig = &swarmapi.Driver{
  172. Name: m.VolumeOptions.DriverConfig.Name,
  173. Options: m.VolumeOptions.DriverConfig.Options,
  174. }
  175. }
  176. }
  177. if m.TmpfsOptions != nil {
  178. mount.TmpfsOptions = &swarmapi.Mount_TmpfsOptions{
  179. SizeBytes: m.TmpfsOptions.SizeBytes,
  180. Mode: m.TmpfsOptions.Mode,
  181. }
  182. }
  183. containerSpec.Mounts = append(containerSpec.Mounts, mount)
  184. }
  185. if c.Healthcheck != nil {
  186. containerSpec.Healthcheck = healthConfigToGRPC(c.Healthcheck)
  187. }
  188. return containerSpec, nil
  189. }
  190. func healthConfigFromGRPC(h *swarmapi.HealthConfig) *container.HealthConfig {
  191. interval, _ := ptypes.Duration(h.Interval)
  192. timeout, _ := ptypes.Duration(h.Timeout)
  193. return &container.HealthConfig{
  194. Test: h.Test,
  195. Interval: interval,
  196. Timeout: timeout,
  197. Retries: int(h.Retries),
  198. }
  199. }
  200. func healthConfigToGRPC(h *container.HealthConfig) *swarmapi.HealthConfig {
  201. return &swarmapi.HealthConfig{
  202. Test: h.Test,
  203. Interval: ptypes.DurationProto(h.Interval),
  204. Timeout: ptypes.DurationProto(h.Timeout),
  205. Retries: int32(h.Retries),
  206. }
  207. }