forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.go
131 lines (107 loc) · 3.99 KB
/
util.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright 2018 The Kubernetes Authors.
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 bucket
import (
"fmt"
"math/rand"
"time"
"github.com/coreos/pkg/capnslog"
bktv1alpha1 "github.com/kube-object-storage/lib-bucket-provisioner/pkg/apis/objectbucket.io/v1alpha1"
"github.com/kube-object-storage/lib-bucket-provisioner/pkg/provisioner"
"k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
cephclientset "github.com/rook/rook/pkg/client/clientset/versioned/typed/ceph.rook.io/v1"
)
var logger = capnslog.NewPackageLogger("github.com/rook/rook", "op-bucket-prov")
const (
genUserLen = 8
cephUser = "cephUser"
provisionerName = "ceph.rook.io/bucket"
prefixObjectStoreSvc = "rook-ceph-rgw"
accessKeyIdKey = "accessKeyID"
secretSecretKeyKey = "secretAccessKey"
objectStoreName = "objectStoreName"
objectStoreNamespace = "objectStoreNamespace"
)
func NewBucketController(cfg *rest.Config, p *Provisioner) (*provisioner.Provisioner, error) {
const allNamespaces = ""
logger.Infof("Ceph Bucket Provisioner launched")
return provisioner.NewProvisioner(cfg, provisionerName, p, allNamespaces)
}
// Return the secret namespace and name from the passed storage class.
func getSecretNamespaceAndName(sc *storagev1.StorageClass) (string, string) {
const (
scSecretNameKey = "secretName"
scSecretNSKey = "secretNamespace"
)
return sc.Parameters[scSecretNSKey], sc.Parameters[scSecretNameKey]
}
func getAccessKeyId(secret *v1.Secret) string {
return string(secret.Data[accessKeyIdKey])
}
func getSecretAccessKey(secret *v1.Secret) string {
return string(secret.Data[secretSecretKeyKey])
}
func getObjectStoreName(sc *storagev1.StorageClass) string {
return sc.Parameters[objectStoreName]
}
func getObjectStoreNameSpace(sc *storagev1.StorageClass) string {
return sc.Parameters[objectStoreNamespace]
}
func getBucketName(ob *bktv1alpha1.ObjectBucket) string {
return ob.Spec.Endpoint.BucketName
}
func isStaticBucket(sc *storagev1.StorageClass) (string, bool) {
const key = "bucketName"
val, ok := sc.Parameters[key]
return val, ok
}
func getCephUser(ob *bktv1alpha1.ObjectBucket) string {
return ob.Spec.AdditionalState[cephUser]
}
func getObjectStore(c cephclientset.CephV1Interface, namespace, name string) (*cephv1.CephObjectStore, error) {
// Verify the object store API object actually exists
store, err := c.CephObjectStores(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return nil, fmt.Errorf("cephObjectStore not found: %v", err)
}
return nil, fmt.Errorf("error getting cephObjectStore: %v", err)
}
return store, err
}
func getService(c kubernetes.Interface, namespace, name string) (*v1.Service, error) {
// Verify the object store's service actually exists
svc, err := c.CoreV1().Services(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return nil, fmt.Errorf("cephObjectStore service not found: %v", err)
}
return nil, fmt.Errorf("error getting cephObjectStore service: %v", err)
}
return svc, nil
}
func randomString(n int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[r.Intn(len(letterRunes))]
}
return string(b)
}