volumes.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kubernetes
  15. import (
  16. "fmt"
  17. "path"
  18. "path/filepath"
  19. "strings"
  20. "github.com/compose-spec/compose-go/types"
  21. "github.com/pkg/errors"
  22. apiv1 "k8s.io/api/core/v1"
  23. )
  24. const dockerSock = "/var/run/docker.sock"
  25. type volumeSpec struct {
  26. mount apiv1.VolumeMount
  27. source *apiv1.VolumeSource
  28. }
  29. func toVolumeSpecs(project *types.Project, s types.ServiceConfig) ([]volumeSpec, error) {
  30. var specs []volumeSpec
  31. for i, m := range s.Volumes {
  32. var source *apiv1.VolumeSource
  33. name := fmt.Sprintf("mount-%d", i)
  34. subpath := ""
  35. if m.Source == dockerSock && m.Target == dockerSock {
  36. subpath = "docker.sock"
  37. source = hostPathVolume("/var/run")
  38. } else if strings.HasSuffix(m.Source, ".git") {
  39. source = gitVolume(m.Source)
  40. } else if m.Type == "volume" {
  41. if m.Source != "" {
  42. name = strings.ReplaceAll(m.Source, "_", "-")
  43. }
  44. } else {
  45. // bind mount
  46. if !filepath.IsAbs(m.Source) {
  47. return nil, errors.Errorf("%s: only absolute paths can be specified in mount source", m.Source)
  48. }
  49. if m.Source == "/" {
  50. source = hostPathVolume("/")
  51. } else {
  52. parent, file := filepath.Split(m.Source)
  53. if parent != "/" {
  54. parent = strings.TrimSuffix(parent, "/")
  55. }
  56. source = hostPathVolume(parent)
  57. subpath = file
  58. }
  59. }
  60. specs = append(specs, volumeSpec{
  61. source: source,
  62. mount: volumeMount(name, m.Target, m.ReadOnly, subpath),
  63. })
  64. }
  65. for i, m := range s.Tmpfs {
  66. name := fmt.Sprintf("tmp-%d", i)
  67. specs = append(specs, volumeSpec{
  68. source: emptyVolumeInMemory(),
  69. mount: volumeMount(name, m, false, ""),
  70. })
  71. }
  72. for i, s := range s.Secrets {
  73. name := fmt.Sprintf("secret-%d", i)
  74. target := path.Join("/run/secrets", or(s.Target, s.Source))
  75. subPath := name
  76. readOnly := true
  77. specs = append(specs, volumeSpec{
  78. source: secretVolume(s, project.Secrets[name], subPath),
  79. mount: volumeMount(name, target, readOnly, subPath),
  80. })
  81. }
  82. for i, c := range s.Configs {
  83. name := fmt.Sprintf("config-%d", i)
  84. target := or(c.Target, "/"+c.Source)
  85. subPath := name
  86. readOnly := true
  87. specs = append(specs, volumeSpec{
  88. source: configVolume(c, project.Configs[name], subPath),
  89. mount: volumeMount(name, target, readOnly, subPath),
  90. })
  91. }
  92. return specs, nil
  93. }
  94. func or(v string, defaultValue string) string {
  95. if v != "" && v != "." {
  96. return v
  97. }
  98. return defaultValue
  99. }
  100. func toVolumeMounts(project *types.Project, s types.ServiceConfig) ([]apiv1.VolumeMount, error) {
  101. var mounts []apiv1.VolumeMount
  102. specs, err := toVolumeSpecs(project, s)
  103. if err != nil {
  104. return nil, err
  105. }
  106. for _, spec := range specs {
  107. mounts = append(mounts, spec.mount)
  108. }
  109. return mounts, nil
  110. }
  111. func toVolumes(project *types.Project, s types.ServiceConfig) ([]apiv1.Volume, error) {
  112. var volumes []apiv1.Volume
  113. specs, err := toVolumeSpecs(project, s)
  114. if err != nil {
  115. return nil, err
  116. }
  117. for _, spec := range specs {
  118. if spec.source == nil {
  119. spec.source = emptyVolumeInMemory()
  120. }
  121. volumes = append(volumes, apiv1.Volume{
  122. Name: spec.mount.Name,
  123. VolumeSource: *spec.source,
  124. })
  125. }
  126. return volumes, nil
  127. }
  128. func gitVolume(path string) *apiv1.VolumeSource {
  129. return &apiv1.VolumeSource{
  130. GitRepo: &apiv1.GitRepoVolumeSource{
  131. Repository: filepath.ToSlash(path),
  132. },
  133. }
  134. }
  135. func hostPathVolume(path string) *apiv1.VolumeSource {
  136. return &apiv1.VolumeSource{
  137. HostPath: &apiv1.HostPathVolumeSource{
  138. Path: path,
  139. },
  140. }
  141. }
  142. func defaultMode(mode *uint32) *int32 {
  143. var defaultMode *int32
  144. if mode != nil {
  145. signedMode := int32(*mode)
  146. defaultMode = &signedMode
  147. }
  148. return defaultMode
  149. }
  150. func secretVolume(config types.ServiceSecretConfig, topLevelConfig types.SecretConfig, subPath string) *apiv1.VolumeSource {
  151. return &apiv1.VolumeSource{
  152. Secret: &apiv1.SecretVolumeSource{
  153. SecretName: config.Source,
  154. Items: []apiv1.KeyToPath{
  155. {
  156. Key: toKey(topLevelConfig.File),
  157. Path: subPath,
  158. Mode: defaultMode(config.Mode),
  159. },
  160. },
  161. },
  162. }
  163. }
  164. func volumeMount(name, path string, readOnly bool, subPath string) apiv1.VolumeMount {
  165. return apiv1.VolumeMount{
  166. Name: name,
  167. MountPath: path,
  168. ReadOnly: readOnly,
  169. SubPath: subPath,
  170. }
  171. }
  172. func configVolume(config types.ServiceConfigObjConfig, topLevelConfig types.ConfigObjConfig, subPath string) *apiv1.VolumeSource {
  173. return &apiv1.VolumeSource{
  174. ConfigMap: &apiv1.ConfigMapVolumeSource{
  175. LocalObjectReference: apiv1.LocalObjectReference{
  176. Name: config.Source,
  177. },
  178. Items: []apiv1.KeyToPath{
  179. {
  180. Key: toKey(topLevelConfig.File),
  181. Path: subPath,
  182. Mode: defaultMode(config.Mode),
  183. },
  184. },
  185. },
  186. }
  187. }
  188. func toKey(file string) string {
  189. if file != "" {
  190. return path.Base(file)
  191. }
  192. return "file" // TODO: hard-coded key for external configs
  193. }
  194. func emptyVolumeInMemory() *apiv1.VolumeSource {
  195. return &apiv1.VolumeSource{
  196. EmptyDir: &apiv1.EmptyDirVolumeSource{
  197. Medium: apiv1.StorageMediumMemory,
  198. },
  199. }
  200. }