volumes.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 resources
  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 _, s := range s.Secrets {
  73. name := fmt.Sprintf("%s-%s", project.Name, s.Source)
  74. target := path.Join("/run/secrets", or(s.Target, path.Join(s.Source, s.Source)))
  75. specs = append(specs, secretMount(name, target))
  76. }
  77. for i, c := range s.Configs {
  78. name := fmt.Sprintf("config-%d", i)
  79. target := or(c.Target, "/"+c.Source)
  80. subPath := name
  81. readOnly := true
  82. specs = append(specs, volumeSpec{
  83. source: configVolume(c, project.Configs[name], subPath),
  84. mount: volumeMount(name, target, readOnly, subPath),
  85. })
  86. }
  87. return specs, nil
  88. }
  89. func or(v string, defaultValue string) string {
  90. if v != "" && v != "." {
  91. return v
  92. }
  93. return defaultValue
  94. }
  95. func toVolumeMounts(project *types.Project, s types.ServiceConfig) ([]apiv1.VolumeMount, error) {
  96. var mounts []apiv1.VolumeMount
  97. specs, err := toVolumeSpecs(project, s)
  98. if err != nil {
  99. return nil, err
  100. }
  101. for _, spec := range specs {
  102. mounts = append(mounts, spec.mount)
  103. }
  104. return mounts, nil
  105. }
  106. func toVolumes(project *types.Project, s types.ServiceConfig) ([]apiv1.Volume, error) {
  107. var volumes []apiv1.Volume
  108. specs, err := toVolumeSpecs(project, s)
  109. if err != nil {
  110. return nil, err
  111. }
  112. for _, spec := range specs {
  113. if spec.source == nil {
  114. spec.source = emptyVolumeInMemory()
  115. }
  116. volumes = append(volumes, apiv1.Volume{
  117. Name: spec.mount.Name,
  118. VolumeSource: *spec.source,
  119. })
  120. }
  121. return volumes, nil
  122. }
  123. func gitVolume(path string) *apiv1.VolumeSource {
  124. return &apiv1.VolumeSource{
  125. GitRepo: &apiv1.GitRepoVolumeSource{
  126. Repository: filepath.ToSlash(path),
  127. },
  128. }
  129. }
  130. func hostPathVolume(path string) *apiv1.VolumeSource {
  131. return &apiv1.VolumeSource{
  132. HostPath: &apiv1.HostPathVolumeSource{
  133. Path: path,
  134. },
  135. }
  136. }
  137. func defaultMode(mode *uint32) *int32 {
  138. var defaultMode *int32
  139. if mode != nil {
  140. signedMode := int32(*mode)
  141. defaultMode = &signedMode
  142. }
  143. return defaultMode
  144. }
  145. func secretMount(name, target string) volumeSpec {
  146. readOnly := true
  147. filename := filepath.Base(target)
  148. dir := filepath.Dir(target)
  149. return volumeSpec{
  150. source: &apiv1.VolumeSource{
  151. Secret: &apiv1.SecretVolumeSource{
  152. SecretName: name,
  153. Items: []apiv1.KeyToPath{
  154. {
  155. Key: name,
  156. Path: filename,
  157. },
  158. },
  159. },
  160. },
  161. mount: apiv1.VolumeMount{
  162. Name: filename,
  163. MountPath: dir,
  164. ReadOnly: readOnly,
  165. },
  166. }
  167. }
  168. func volumeMount(name, path string, readOnly bool, subPath string) apiv1.VolumeMount {
  169. return apiv1.VolumeMount{
  170. Name: name,
  171. MountPath: path,
  172. ReadOnly: readOnly,
  173. SubPath: subPath,
  174. }
  175. }
  176. func configVolume(config types.ServiceConfigObjConfig, topLevelConfig types.ConfigObjConfig, subPath string) *apiv1.VolumeSource {
  177. return &apiv1.VolumeSource{
  178. ConfigMap: &apiv1.ConfigMapVolumeSource{
  179. LocalObjectReference: apiv1.LocalObjectReference{
  180. Name: config.Source,
  181. },
  182. Items: []apiv1.KeyToPath{
  183. {
  184. Key: toKey(topLevelConfig.File),
  185. Path: subPath,
  186. Mode: defaultMode(config.Mode),
  187. },
  188. },
  189. },
  190. }
  191. }
  192. func toKey(file string) string {
  193. if file != "" {
  194. return path.Base(file)
  195. }
  196. return "file" // TODO: hard-coded key for external configs
  197. }
  198. func emptyVolumeInMemory() *apiv1.VolumeSource {
  199. return &apiv1.VolumeSource{
  200. EmptyDir: &apiv1.EmptyDirVolumeSource{
  201. Medium: apiv1.StorageMediumMemory,
  202. },
  203. }
  204. }