host.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 device
  15. import (
  16. "context"
  17. "strings"
  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/compute/models"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. func init() {
  26. models.RegisterContainerDeviceDriver(newHostDevice())
  27. }
  28. type hostDevice struct {
  29. }
  30. func newHostDevice() models.IContainerDeviceDriver {
  31. return &hostDevice{}
  32. }
  33. func (h hostDevice) GetType() apis.ContainerDeviceType {
  34. return apis.CONTAINER_DEVICE_TYPE_HOST
  35. }
  36. func (h hostDevice) ValidatePodCreateData(ctx context.Context, userCred mcclient.TokenCredential, dev *api.ContainerDevice, input *api.ServerCreateInput) error {
  37. _, err := h.ValidateCreateData(ctx, userCred, nil, dev)
  38. return err
  39. }
  40. func (h hostDevice) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, pod *models.SGuest, dev *api.ContainerDevice) (*api.ContainerDevice, error) {
  41. host := dev.Host
  42. if host == nil {
  43. return nil, httperrors.NewNotEmptyError("host is nil")
  44. }
  45. if host.HostPath == "" {
  46. return nil, httperrors.NewNotEmptyError("host_path is empty")
  47. }
  48. if host.ContainerPath == "" {
  49. return nil, httperrors.NewNotEmptyError("container_path is empty")
  50. }
  51. if host.Permissions == "" {
  52. return nil, httperrors.NewNotEmptyError("permissions is empty")
  53. }
  54. for _, p := range strings.Split(host.Permissions, "") {
  55. switch p {
  56. case "r", "w", "m":
  57. default:
  58. return nil, httperrors.NewInputParameterError("wrong permission %s", p)
  59. }
  60. }
  61. return dev, nil
  62. }
  63. func (h hostDevice) ToHostDevice(dev *api.ContainerDevice) (*hostapi.ContainerDevice, error) {
  64. return &hostapi.ContainerDevice{
  65. Type: apis.CONTAINER_DEVICE_TYPE_HOST,
  66. ContainerPath: dev.Host.ContainerPath,
  67. Permissions: dev.Host.Permissions,
  68. Host: &hostapi.ContainerHostDevice{HostPath: dev.Host.HostPath},
  69. }, nil
  70. }