flocker_util.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 flocker
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/util/rand"
  18. volumehelpers "k8s.io/cloud-provider/volume/helpers"
  19. flockerapi "github.com/clusterhq/flocker-go"
  20. "k8s.io/klog"
  21. )
  22. type flockerUtil struct{}
  23. func (util *flockerUtil) DeleteVolume(d *flockerVolumeDeleter) error {
  24. var err error
  25. if d.flockerClient == nil {
  26. d.flockerClient, err = d.plugin.newFlockerClient("")
  27. if err != nil {
  28. return err
  29. }
  30. }
  31. datasetUUID, err := d.GetDatasetUUID()
  32. if err != nil {
  33. return err
  34. }
  35. return d.flockerClient.DeleteDataset(datasetUUID)
  36. }
  37. func (util *flockerUtil) CreateVolume(c *flockerVolumeProvisioner) (datasetUUID string, volumeSizeGiB int, labels map[string]string, err error) {
  38. if c.flockerClient == nil {
  39. c.flockerClient, err = c.plugin.newFlockerClient("")
  40. if err != nil {
  41. return
  42. }
  43. }
  44. nodes, err := c.flockerClient.ListNodes()
  45. if err != nil {
  46. return
  47. }
  48. if len(nodes) < 1 {
  49. err = fmt.Errorf("no nodes found inside the flocker cluster to provision a dataset")
  50. return
  51. }
  52. // select random node
  53. node := nodes[rand.Intn(len(nodes))]
  54. klog.V(2).Infof("selected flocker node with UUID '%s' to provision dataset", node.UUID)
  55. capacity := c.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
  56. requestBytes := capacity.Value()
  57. volumeSizeGiB, err = volumehelpers.RoundUpToGiBInt(capacity)
  58. if err != nil {
  59. return
  60. }
  61. createOptions := &flockerapi.CreateDatasetOptions{
  62. MaximumSize: requestBytes,
  63. Metadata: map[string]string{
  64. "type": "k8s-dynamic-prov",
  65. "pvc": c.options.PVC.Name,
  66. },
  67. Primary: node.UUID,
  68. }
  69. datasetState, err := c.flockerClient.CreateDataset(createOptions)
  70. if err != nil {
  71. return
  72. }
  73. datasetUUID = datasetState.DatasetID
  74. klog.V(2).Infof("successfully created Flocker dataset with UUID '%s'", datasetUUID)
  75. return
  76. }