host_local.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "context"
  17. "yunion.io/x/pkg/util/sets"
  18. "yunion.io/x/onecloud/pkg/apis"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. )
  24. func init() {
  25. models.RegisterContainerVolumeMountDriver(newHostLocal())
  26. }
  27. type hostLocal struct{}
  28. func newHostLocal() models.IContainerVolumeMountDriver {
  29. return &hostLocal{}
  30. }
  31. func (h hostLocal) GetType() apis.ContainerVolumeMountType {
  32. return apis.CONTAINER_VOLUME_MOUNT_TYPE_HOST_PATH
  33. }
  34. func (h hostLocal) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, pod *models.SGuest, vm *apis.ContainerVolumeMount) (*apis.ContainerVolumeMount, error) {
  35. return vm, h.ValidatePodCreateData(ctx, userCred, vm, nil)
  36. }
  37. func (h hostLocal) ValidatePodCreateData(ctx context.Context, userCred mcclient.TokenCredential, vm *apis.ContainerVolumeMount, input *api.ServerCreateInput) error {
  38. hp := vm.HostPath
  39. if hp == nil {
  40. return httperrors.NewNotEmptyError("host_path is nil")
  41. }
  42. if hp.Type == "" {
  43. hp.Type = apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_FILE
  44. }
  45. if !sets.NewString(
  46. string(apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_FILE),
  47. string(apis.CONTAINER_VOLUME_MOUNT_HOST_PATH_TYPE_DIRECTORY)).Has(string(hp.Type)) {
  48. return httperrors.NewInputParameterError("unsupported type %s", hp.Type)
  49. }
  50. if hp.Path == "" {
  51. return httperrors.NewNotEmptyError("path is required")
  52. }
  53. return nil
  54. }