-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
client.go
92 lines (77 loc) · 3.07 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Copyright 2019 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clients
import (
"fmt"
"time"
"github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/tests/framework/installer"
"github.com/rook/rook/tests/framework/utils"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ClientOperation is a wrapper for k8s rook file operations
type ClientOperation struct {
k8sh *utils.K8sHelper
manifests installer.CephManifests
}
// CreateClientOperation Constructor to create ClientOperation - client to perform rook file system operations on k8s
func CreateClientOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *ClientOperation {
return &ClientOperation{k8sh, manifests}
}
// Create creates a client in Rook
func (c *ClientOperation) Create(name, namespace string, caps map[string]string) error {
logger.Infof("creating the client via CRD")
if err := c.k8sh.ResourceOperation("apply", c.manifests.GetClient(namespace, name, caps)); err != nil {
return err
}
return nil
}
// Delete deletes a client in Rook
func (c *ClientOperation) Delete(name, namespace string) error {
options := &metav1.DeleteOptions{}
logger.Infof("Deleting filesystem %s in namespace %s", name, namespace)
err := c.k8sh.RookClientset.CephV1().CephClients(namespace).Delete(name, options)
if err != nil && !errors.IsNotFound(err) {
return err
}
logger.Infof("Deleted client %s in namespace %s", name, namespace)
return nil
}
// Get shows user created in Rook
func (c *ClientOperation) Get(namespace string, clientName string) (key string, error error) {
context := c.k8sh.MakeContext()
key, err := client.AuthGetKey(context, namespace, clientName)
if err != nil {
return "", fmt.Errorf("failed to get client %s: %+v", clientName, err)
}
return key, nil
}
// Update updates provided user capabilities
func (c *ClientOperation) Update(namespace string, clientName string, caps map[string]string) (updatedcaps map[string]string, error error) {
context := c.k8sh.MakeContext()
logger.Infof("updating the client via CRD")
if err := c.k8sh.ResourceOperation("apply", c.manifests.GetClient(namespace, clientName, caps)); err != nil {
return nil, err
}
for i := 0; i < 30; i++ {
updatedcaps, _ = client.AuthGetCaps(context, namespace, "client."+clientName)
if caps["mon"] == updatedcaps["mon"] {
logger.Infof("Finished updating the client via CRD")
return updatedcaps, nil
}
logger.Info("Waiting for client CRD to finish updating caps")
time.Sleep(2 * time.Second)
}
return nil, fmt.Errorf("Unable to update client")
}