volumes.go 5.5 KB

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