quobyte_util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package quobyte
  14. import (
  15. "net"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "k8s.io/api/core/v1"
  20. volumehelpers "k8s.io/cloud-provider/volume/helpers"
  21. quobyteapi "github.com/quobyte/api"
  22. "k8s.io/klog"
  23. )
  24. type quobyteVolumeManager struct {
  25. config *quobyteAPIConfig
  26. }
  27. func (manager *quobyteVolumeManager) createVolume(provisioner *quobyteVolumeProvisioner, createQuota bool) (quobyte *v1.QuobyteVolumeSource, size int, err error) {
  28. capacity := provisioner.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
  29. volumeSize, err := volumehelpers.RoundUpToGiBInt(capacity)
  30. if err != nil {
  31. return nil, 0, err
  32. }
  33. // Quobyte has the concept of Volumes which doen't have a specific size (they can grow unlimited)
  34. // to simulate a size constraint we set here a Quota for logical space
  35. volumeRequest := &quobyteapi.CreateVolumeRequest{
  36. Name: provisioner.volume,
  37. RootUserID: provisioner.user,
  38. RootGroupID: provisioner.group,
  39. TenantID: provisioner.tenant,
  40. ConfigurationName: provisioner.config,
  41. }
  42. quobyteClient := manager.createQuobyteClient()
  43. volumeUUID, err := quobyteClient.CreateVolume(volumeRequest)
  44. if err != nil {
  45. return &v1.QuobyteVolumeSource{}, volumeSize, err
  46. }
  47. // Set Quota for Volume with specified byte size
  48. if createQuota {
  49. err = quobyteClient.SetVolumeQuota(volumeUUID, uint64(capacity.Value()))
  50. if err != nil {
  51. return &v1.QuobyteVolumeSource{}, volumeSize, err
  52. }
  53. }
  54. klog.V(4).Infof("Created Quobyte volume %s", provisioner.volume)
  55. return &v1.QuobyteVolumeSource{
  56. Registry: provisioner.registry,
  57. Volume: provisioner.volume,
  58. User: provisioner.user,
  59. Group: provisioner.group,
  60. }, volumeSize, nil
  61. }
  62. func (manager *quobyteVolumeManager) deleteVolume(deleter *quobyteVolumeDeleter) error {
  63. return manager.createQuobyteClient().DeleteVolumeByName(deleter.volume, deleter.tenant)
  64. }
  65. func (manager *quobyteVolumeManager) createQuobyteClient() *quobyteapi.QuobyteClient {
  66. return quobyteapi.NewQuobyteClient(
  67. manager.config.quobyteAPIServer,
  68. manager.config.quobyteUser,
  69. manager.config.quobytePassword,
  70. )
  71. }
  72. func (mounter *quobyteMounter) pluginDirIsMounted(pluginDir string) (bool, error) {
  73. mounts, err := mounter.mounter.List()
  74. if err != nil {
  75. return false, err
  76. }
  77. for _, mountPoint := range mounts {
  78. if strings.HasPrefix(mountPoint.Type, "quobyte") {
  79. continue
  80. }
  81. if mountPoint.Path == pluginDir {
  82. klog.V(4).Infof("quobyte: found mountpoint %s in /proc/mounts", mountPoint.Path)
  83. return true, nil
  84. }
  85. }
  86. return false, nil
  87. }
  88. func (mounter *quobyteMounter) correctTraillingSlash(regStr string) string {
  89. return filepath.Clean(regStr) + string(os.PathSeparator)
  90. }
  91. func validateRegistry(registry string) bool {
  92. if len(registry) == 0 {
  93. return false
  94. }
  95. for _, hostPortPair := range strings.Split(registry, ",") {
  96. if _, _, err := net.SplitHostPort(hostPortPair); err != nil {
  97. return false
  98. }
  99. }
  100. return true
  101. }