client.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2019 The Rook Authors. All rights reserved.
  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 clients
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. "github.com/rook/rook/pkg/daemon/ceph/client"
  19. "github.com/rook/rook/tests/framework/installer"
  20. "github.com/rook/rook/tests/framework/utils"
  21. "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. )
  24. // ClientOperation is a wrapper for k8s rook file operations
  25. type ClientOperation struct {
  26. k8sh *utils.K8sHelper
  27. manifests installer.CephManifests
  28. }
  29. // CreateClientOperation Constructor to create ClientOperation - client to perform rook file system operations on k8s
  30. func CreateClientOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *ClientOperation {
  31. return &ClientOperation{k8sh, manifests}
  32. }
  33. // Create creates a client in Rook
  34. func (c *ClientOperation) Create(name, namespace string, caps map[string]string) error {
  35. logger.Infof("creating the client via CRD")
  36. if err := c.k8sh.ResourceOperation("apply", c.manifests.GetClient(name, caps)); err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. // Delete deletes a client in Rook
  42. func (c *ClientOperation) Delete(name, namespace string) error {
  43. ctx := context.TODO()
  44. options := &metav1.DeleteOptions{}
  45. logger.Infof("Deleting filesystem %s in namespace %s", name, namespace)
  46. err := c.k8sh.RookClientset.CephV1().CephClients(namespace).Delete(ctx, name, *options)
  47. if err != nil && !errors.IsNotFound(err) {
  48. return err
  49. }
  50. logger.Infof("Deleted client %s in namespace %s", name, namespace)
  51. return nil
  52. }
  53. // Get shows user created in Rook
  54. func (c *ClientOperation) Get(clusterInfo *client.ClusterInfo, clientName string) (key string, error error) {
  55. context := c.k8sh.MakeContext()
  56. key, err := client.AuthGetKey(context, clusterInfo, clientName)
  57. if err != nil {
  58. return "", fmt.Errorf("failed to get client %s: %+v", clientName, err)
  59. }
  60. return key, nil
  61. }
  62. // Update updates provided user capabilities
  63. func (c *ClientOperation) Update(clusterInfo *client.ClusterInfo, clientName string, caps map[string]string) (updatedcaps map[string]string, error error) {
  64. context := c.k8sh.MakeContext()
  65. logger.Infof("updating the client via CRD")
  66. if err := c.k8sh.ResourceOperation("apply", c.manifests.GetClient(clientName, caps)); err != nil {
  67. return nil, err
  68. }
  69. for i := 0; i < 30; i++ {
  70. updatedcaps, _ = client.AuthGetCaps(context, clusterInfo, "client."+clientName)
  71. if caps["mon"] == updatedcaps["mon"] {
  72. logger.Infof("Finished updating the client via CRD")
  73. return updatedcaps, nil
  74. }
  75. logger.Info("Waiting for client CRD to finish updating caps")
  76. time.Sleep(2 * time.Second)
  77. }
  78. return nil, fmt.Errorf("Unable to update client")
  79. }