disk_pov_image.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. imageapi "yunion.io/x/onecloud/pkg/apis/image"
  21. "yunion.io/x/onecloud/pkg/compute/options"
  22. "yunion.io/x/onecloud/pkg/httperrors"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. imagemod "yunion.io/x/onecloud/pkg/mcclient/modules/image"
  26. )
  27. type povImage struct {
  28. }
  29. func newDiskPostOverlayImage() iDiskPostOverlay {
  30. return &povImage{}
  31. }
  32. func (p povImage) validateData(ctx context.Context, userCred mcclient.TokenCredential, pov *apis.ContainerVolumeMountDiskPostOverlay) error {
  33. img := pov.Image
  34. if img.Id == "" {
  35. return httperrors.NewMissingParameterError("image id")
  36. }
  37. s := auth.GetAdminSession(ctx, options.Options.Region)
  38. obj, err := imagemod.Images.Get(s, img.Id, nil)
  39. if err != nil {
  40. return errors.Wrapf(err, "Get image by id %s", img.Id)
  41. }
  42. imgObj := new(imageapi.ImageDetails)
  43. if err := obj.Unmarshal(imgObj); err != nil {
  44. return errors.Wrap(err, "unmarshal image details")
  45. }
  46. pov.Image.Id = imgObj.Id
  47. props := imgObj.Properties
  48. usedByStr, ok := props[imageapi.IMAGE_USED_BY_POST_OVERLAY]
  49. if !ok {
  50. return errors.Wrapf(err, "Get %s", imageapi.IMAGE_USED_BY_POST_OVERLAY)
  51. }
  52. if usedByStr != "true" {
  53. return errors.Errorf("image isn't used by post overlay")
  54. }
  55. pathMapStr := props[imageapi.IMAGE_INTERNAL_PATH_MAP]
  56. pathMapObj, err := jsonutils.ParseString(pathMapStr)
  57. if err != nil {
  58. return errors.Wrapf(err, "json parse path_map: %s", pathMapStr)
  59. }
  60. pathMap := make(map[string]string)
  61. if err := pathMapObj.Unmarshal(pathMap); err != nil {
  62. return errors.Wrapf(err, "unmarshal pathMapObj")
  63. }
  64. if len(pov.Image.PathMap) == 0 {
  65. pov.Image.PathMap = pathMap
  66. }
  67. if len(pov.Image.HostLowerMap) != 0 {
  68. for hostPath, _ := range pov.Image.HostLowerMap {
  69. _, ok := pov.Image.PathMap[hostPath]
  70. if !ok {
  71. return httperrors.NewNotFoundError("host_path %s of host_lower_map doesn't found in path_map", hostPath)
  72. }
  73. }
  74. }
  75. return nil
  76. }
  77. func (p povImage) getContainerTargetDirs(ov *apis.ContainerVolumeMountDiskPostOverlay) []string {
  78. pathMap := ov.Image.PathMap
  79. ctrPaths := []string{}
  80. for _, ctrPath := range pathMap {
  81. ctrPaths = append(ctrPaths, ctrPath)
  82. }
  83. return ctrPaths
  84. }