container_drivers.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 models
  15. import (
  16. "context"
  17. "sync"
  18. "yunion.io/x/onecloud/pkg/apis"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. )
  24. var (
  25. containerVolumeDrivers = newContainerDrivers()
  26. containerDeviceDrivers = newContainerDrivers()
  27. containerLifecycleDrivers = newContainerDrivers()
  28. )
  29. type containerDrivers struct {
  30. drivers *sync.Map
  31. }
  32. func newContainerDrivers() *containerDrivers {
  33. return &containerDrivers{
  34. drivers: new(sync.Map),
  35. }
  36. }
  37. func (cd *containerDrivers) GetWithError(typ string) (interface{}, error) {
  38. drv, ok := cd.drivers.Load(typ)
  39. if !ok {
  40. return drv, httperrors.NewNotFoundError("not found driver by type %q", typ)
  41. }
  42. return drv, nil
  43. }
  44. func (cd *containerDrivers) Get(typ string) interface{} {
  45. drv, err := cd.GetWithError(typ)
  46. if err != nil {
  47. panic(err.Error())
  48. }
  49. return drv
  50. }
  51. func (cd *containerDrivers) Register(typ string, drv interface{}) {
  52. cd.drivers.Store(typ, drv)
  53. }
  54. func registerContainerDriver[K ~string, D any](drvs *containerDrivers, typ K, drv D) {
  55. drvs.Register(string(typ), drv)
  56. }
  57. func getContainerDriver[K ~string, D any](drvs *containerDrivers, typ K) D {
  58. return drvs.Get(string(typ)).(D)
  59. }
  60. func getContainerDriverWithError[K ~string, D any](drvs *containerDrivers, typ K) (D, error) {
  61. drv, err := drvs.GetWithError(string(typ))
  62. if err != nil {
  63. return drv.(D), err
  64. }
  65. return drv.(D), nil
  66. }
  67. func RegisterContainerVolumeMountDriver(drv IContainerVolumeMountDriver) {
  68. registerContainerDriver(containerVolumeDrivers, drv.GetType(), drv)
  69. }
  70. func GetContainerVolumeMountDriver(typ apis.ContainerVolumeMountType) IContainerVolumeMountDriver {
  71. return getContainerDriver[apis.ContainerVolumeMountType, IContainerVolumeMountDriver](containerVolumeDrivers, typ)
  72. }
  73. func GetContainerVolumeMountDriverWithError(typ apis.ContainerVolumeMountType) (IContainerVolumeMountDriver, error) {
  74. return getContainerDriverWithError[apis.ContainerVolumeMountType, IContainerVolumeMountDriver](containerVolumeDrivers, typ)
  75. }
  76. func GetContainerRootFsDriverWithError(typ apis.ContainerVolumeMountType) (IContainerRootFsDriver, error) {
  77. return getContainerDriverWithError[apis.ContainerVolumeMountType, IContainerRootFsDriver](containerVolumeDrivers, typ)
  78. }
  79. func GetContainerRootFsDriver(typ apis.ContainerVolumeMountType) IContainerRootFsDriver {
  80. return getContainerDriver[apis.ContainerVolumeMountType, IContainerRootFsDriver](containerVolumeDrivers, typ)
  81. }
  82. type IContainerVolumeMountDriver interface {
  83. GetType() apis.ContainerVolumeMountType
  84. ValidatePodCreateData(ctx context.Context, userCred mcclient.TokenCredential, vm *apis.ContainerVolumeMount, input *api.ServerCreateInput) error
  85. ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, pod *SGuest, vm *apis.ContainerVolumeMount) (*apis.ContainerVolumeMount, error)
  86. }
  87. type IContainerRootFsDriver interface {
  88. IContainerVolumeMountDriver
  89. ToHostRootFs(rootFs *apis.ContainerRootfs) (*hostapi.ContainerRootfs, error)
  90. ValidateRootFsCreateData(ctx context.Context, userCred mcclient.TokenCredential, pod *SGuest, rootFs *apis.ContainerRootfs) error
  91. }
  92. type IContainerVolumeMountDiskDriver interface {
  93. IContainerVolumeMountDriver
  94. ValidatePostOverlay(ctx context.Context, userCred mcclient.TokenCredential, vm *apis.ContainerVolumeMount) error
  95. ValidatePostSingleOverlay(ctx context.Context, userCred mcclient.TokenCredential, pov *apis.ContainerVolumeMountDiskPostOverlay) error
  96. ValidatePostOverlayTargetDirs(ovs []*apis.ContainerVolumeMountDiskPostOverlay) error
  97. }
  98. func RegisterContainerDeviceDriver(drv IContainerDeviceDriver) {
  99. registerContainerDriver(containerDeviceDrivers, drv.GetType(), drv)
  100. }
  101. func GetContainerDeviceDriver(typ apis.ContainerDeviceType) IContainerDeviceDriver {
  102. return getContainerDriver[apis.ContainerDeviceType, IContainerDeviceDriver](containerDeviceDrivers, typ)
  103. }
  104. func GetContainerDeviceDriverWithError(typ apis.ContainerDeviceType) (IContainerDeviceDriver, error) {
  105. return getContainerDriverWithError[apis.ContainerDeviceType, IContainerDeviceDriver](containerDeviceDrivers, typ)
  106. }
  107. type IContainerDeviceDriver interface {
  108. GetType() apis.ContainerDeviceType
  109. ValidatePodCreateData(ctx context.Context, userCred mcclient.TokenCredential, dev *api.ContainerDevice, input *api.ServerCreateInput) error
  110. ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, pod *SGuest, dev *api.ContainerDevice) (*api.ContainerDevice, error)
  111. ToHostDevice(dev *api.ContainerDevice) (*hostapi.ContainerDevice, error)
  112. }
  113. type IContainerLifecyleDriver interface {
  114. GetType() apis.ContainerLifecyleHandlerType
  115. ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *apis.ContainerLifecyleHandler) error
  116. }
  117. func RegisterContainerLifecyleDriver(drv IContainerLifecyleDriver) {
  118. registerContainerDriver(containerLifecycleDrivers, drv.GetType(), drv)
  119. }
  120. func GetContainerLifecyleDriver(typ apis.ContainerLifecyleHandlerType) IContainerLifecyleDriver {
  121. return getContainerDriver[apis.ContainerLifecyleHandlerType, IContainerLifecyleDriver](containerLifecycleDrivers, typ)
  122. }
  123. func GetContainerLifecyleDriverWithError(typ apis.ContainerLifecyleHandlerType) (IContainerLifecyleDriver, error) {
  124. return getContainerDriverWithError[apis.ContainerLifecyleHandlerType, IContainerLifecyleDriver](containerLifecycleDrivers, typ)
  125. }