interface.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package volume_mount
  15. import (
  16. "fmt"
  17. pdisk "github.com/shirou/gopsutil/v3/disk"
  18. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  21. "yunion.io/x/onecloud/pkg/hostman/guestman/desc"
  22. "yunion.io/x/onecloud/pkg/hostman/storageman"
  23. )
  24. var (
  25. drivers = make(map[apis.ContainerVolumeMountType]IVolumeMount)
  26. )
  27. func RegisterDriver(drv IVolumeMount) {
  28. drivers[drv.GetType()] = drv
  29. }
  30. func GetDriver(typ apis.ContainerVolumeMountType) IVolumeMount {
  31. drv, ok := drivers[typ]
  32. if !ok {
  33. panic(fmt.Sprintf("not found driver by type %s", typ))
  34. }
  35. return drv
  36. }
  37. type IPodInfo interface {
  38. GetName() string
  39. GetVolumesDir() string
  40. GetVolumesOverlayDir() string
  41. GetDisks() []*desc.SGuestDisk
  42. GetDiskMountPoint(disk storageman.IDisk) string
  43. GetRootFsMountPath(ctrId string) (string, error)
  44. }
  45. type IVolumeMount interface {
  46. GetType() apis.ContainerVolumeMountType
  47. GetRuntimeMountHostPath(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) (string, error)
  48. Mount(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error
  49. Unmount(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error
  50. }
  51. type IConnectedVolumeMount interface {
  52. IVolumeMount
  53. UnmountWithoutDisconnect(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error
  54. Connect(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) (string, bool, error)
  55. Disconnect(pod IPodInfo, ctrId string, vm *hostapi.ContainerVolumeMount) error
  56. }
  57. type ContainerVolumeMountUsage struct {
  58. Id string
  59. HostPath string
  60. MountPath string
  61. VolumeType string
  62. Usage *pdisk.UsageStat
  63. Tags map[string]string
  64. }
  65. type IUsageVolumeMount interface {
  66. IVolumeMount
  67. InjectUsageTags(usage *ContainerVolumeMountUsage, vol *hostapi.ContainerVolumeMount)
  68. }
  69. func GetRuntimeVolumeMountPropagation(input apis.ContainerMountPropagation) runtimeapi.MountPropagation {
  70. switch input {
  71. case apis.MOUNTPROPAGATION_PROPAGATION_PRIVATE:
  72. return runtimeapi.MountPropagation_PROPAGATION_PRIVATE
  73. case apis.MOUNTPROPAGATION_PROPAGATION_HOST_TO_CONTAINER:
  74. return runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER
  75. case apis.MOUNTPROPAGATION_PROPAGATION_BIDIRECTIONAL:
  76. return runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL
  77. }
  78. // private defaultly
  79. return runtimeapi.MountPropagation_PROPAGATION_PRIVATE
  80. }