Skip to content

Commit 7bbd275

Browse files
authored
Merge pull request #7 from jeffvance/review
WIP: cleanup code for hand-off
2 parents 2dd6df3 + 23ac9fc commit 7bbd275

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

pkg/broker/broker.go

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package broker
1818

1919
import (
2020
"fmt"
21+
"github.com/golang/glog"
22+
"github.com/rs/xid"
2123
"io/ioutil"
2224
"net/http"
2325
"sync"
24-
"github.com/golang/glog"
25-
"github.com/rs/xid"
2626

2727
"github.com/kubernetes-incubator/service-catalog/pkg/brokerapi"
2828
"github.com/minio/minio-go"
@@ -35,10 +35,10 @@ const (
3535
BUCKET_ENDPOINT = "bucketEndpoint"
3636
BUCKET_NAME = "bucketName"
3737
BUCKET_ID = "bucketID"
38-
BUCKET_PWORD = "bucketPword"
38+
BUCKET_PASS = "bucketPass"
3939
)
4040

41-
type serviceInstance struct {
41+
type s3ServiceInstance struct {
4242
// k8s namespace
4343
Namespace string
4444
// binding credential created during Bind()
@@ -47,27 +47,27 @@ type serviceInstance struct {
4747

4848
type broker struct {
4949
// rwMutex controls concurrent R and RW access
50-
rwMutex sync.RWMutex
50+
rwMutex sync.RWMutex
5151
// instanceMap maps instanceIDs to the ID's userProvidedServiceInstance values
52-
instanceMap map[string]*serviceInstance
52+
instanceMap map[string]*s3ServiceInstance
5353
// client used to access s3 API
54-
s3Client *minio.Client
54+
s3Client *minio.Client
5555
// s3 server ip and port
56-
s3url string // includes ":port"
57-
s3ID string
58-
s3Pwd string
56+
s3url string // includes ":port"
57+
s3ID string
58+
s3Pass string
5959
// client used to access kubernetes
60-
kubeClient *clientset.Clientset
60+
kubeClient *clientset.Clientset
6161
}
6262

63-
// CreateBroker initializes the service broker. This function is called by server.Start()
63+
// Initialize the s3-gluster service broker. This function is called by `server.Start()`.
6464
func CreateBroker() Broker {
6565
const S3_BROKER_POD_LABEL = "glusterfs=s3-pod"
66-
var instanceMap = make(map[string]*serviceInstance)
67-
glog.Info("Generating new broker.")
66+
var instanceMap = make(map[string]*s3ServiceInstance)
67+
glog.Info("Generating new s3-gluster broker.")
6868
s3ip, err := getExternalIP()
6969
if err != nil {
70-
glog.Errorf("Failed to get external IP: %v", err)
70+
glog.Fatalf("Failed to get external IP: %v", err)
7171
}
7272

7373
// get the kubernetes client
@@ -76,8 +76,8 @@ func CreateBroker() Broker {
7676
glog.Fatalf("failed to get kubernetes client: %v\n", err)
7777
}
7878

79-
// get the s3 deployment pod created via the `gk-deploy` script
80-
// need to get this pod using label selectors since its name is generated
79+
// Get the s3 deployment pod created via the `gk-deploy` script. Need to
80+
// get this pod using label selectors since its name is generated.
8181
ns := "default"
8282
podList, err := cs.CoreV1().Pods(ns).List(metav1.ListOptions{
8383
LabelSelector: S3_BROKER_POD_LABEL,
@@ -116,17 +116,18 @@ func CreateBroker() Broker {
116116
if err != nil {
117117
glog.Fatalf("failed to get minio-s3 client: %v\n", err)
118118
}
119-
glog.Infof("New Broker for s3 endpoint: %s", s3endpoint)
119+
glog.Infof("New Broker for s3-gluster endpoint: %s", s3endpoint)
120120
return &broker{
121121
instanceMap: instanceMap,
122122
s3Client: s3c,
123123
s3url: s3endpoint,
124124
kubeClient: cs,
125125
s3ID: fmt.Sprintf("%s:%s", acct, user),
126-
s3Pwd: pass,
126+
s3Pass: pass,
127127
}
128128
}
129129

130+
// Implements the `Catalog` interface method.
130131
func (b *broker) Catalog() (*brokerapi.Catalog, error) {
131132
return &brokerapi.Catalog{
132133
Services: []*brokerapi.Service{
@@ -148,11 +149,15 @@ func (b *broker) Catalog() (*brokerapi.Catalog, error) {
148149
}, nil
149150
}
150151

152+
// The `GetServiceInstanceLastOperation` interface method is not implemented.
151153
func (b *broker) GetServiceInstanceLastOperation(instanceID, serviceID, planID, operation string) (*brokerapi.LastOperationResponse, error) {
152154
glog.Info("GetServiceInstanceLastOperation not yet implemented.")
153155
return nil, nil
154156
}
155157

158+
// Implements the `CreateServiceInstance` interface method by creating (provisioning) a s3 bucket.
159+
// Note: (nil, nil) is returned for success, meaning the CreateServiceInstanceResponse is ignored by
160+
// the caller.
156161
func (b *broker) CreateServiceInstance(instanceID string, req *brokerapi.CreateServiceInstanceRequest) (*brokerapi.CreateServiceInstanceResponse, error) {
157162
glog.Infof("CreateServiceInstance called. instanceID: %s", instanceID)
158163
b.rwMutex.Lock()
@@ -164,7 +169,7 @@ func (b *broker) CreateServiceInstance(instanceID string, req *brokerapi.CreateS
164169
}
165170
// Check required parameter "bucketName"
166171
bucketName, ok := req.Parameters["bucketName"].(string)
167-
if ! ok {
172+
if !ok {
168173
glog.Errorf("Bucket name not provided, generating random name.")
169174
bucketName = xid.New().String()
170175
}
@@ -181,24 +186,25 @@ func (b *broker) CreateServiceInstance(instanceID string, req *brokerapi.CreateS
181186
if err := b.provisionBucket(bucketName); err != nil {
182187
return nil, err
183188
}
184-
b.instanceMap[instanceID] = &serviceInstance{
189+
b.instanceMap[instanceID] = &s3ServiceInstance{
185190
Namespace: req.ContextProfile.Namespace,
186191
Credential: brokerapi.Credential{
187192
BUCKET_NAME: bucketName,
188193
BUCKET_ENDPOINT: b.s3url,
189194
BUCKET_ID: b.s3ID,
190-
BUCKET_PWORD: b.s3Pwd,
195+
BUCKET_PASS: b.s3Pass,
191196
},
192197
}
193198
return nil, nil
194199
}
195200

201+
// Implements the `RemoveServiceInstance` interface method.
196202
func (b *broker) RemoveServiceInstance(instanceID, serviceID, planID string, acceptsIncomplete bool) (*brokerapi.DeleteServiceInstanceResponse, error) {
197203
glog.Infof("RemoveServiceInstance called. instanceID: %s", instanceID)
198204
b.rwMutex.Lock()
199205
defer b.rwMutex.Unlock()
200206
instance, ok := b.instanceMap[instanceID]
201-
if ! ok {
207+
if !ok {
202208
glog.Errorf("InstanceID %q not found.", instanceID)
203209
return nil, fmt.Errorf("Broker cannot find instanceID %q.", instanceID)
204210
}
@@ -219,10 +225,11 @@ func (b *broker) RemoveServiceInstance(instanceID, serviceID, planID string, acc
219225
return nil, nil
220226
}
221227

228+
// Implements the `Bind` interface method.
222229
func (b *broker) Bind(instanceID, bindingID string, req *brokerapi.BindingRequest) (*brokerapi.CreateServiceBindingResponse, error) {
223230
glog.Infof("Bind called. instanceID: %q", instanceID)
224231
instance, ok := b.instanceMap[instanceID]
225-
if ! ok {
232+
if !ok {
226233
glog.Errorf("Instance ID %q not found.")
227234
return nil, fmt.Errorf("Instance ID %q not found.", instanceID)
228235
}
@@ -237,11 +244,13 @@ func (b *broker) Bind(instanceID, bindingID string, req *brokerapi.BindingReques
237244
}
238245

239246
// nothing to do here
247+
// The `UnBind` interface method is not implemented.
240248
func (b *broker) UnBind(instanceID, bindingID, serviceID, planID string) error {
241249
glog.Info("UnBind not yet implemented.")
242250
return nil
243251
}
244252

253+
// Creates an s3 compatible bucket of the passed-in name.
245254
func (b *broker) provisionBucket(bucketName string) error {
246255
glog.Infof("Creating bucket %q", bucketName)
247256
location := "" // ignored for now...
@@ -255,8 +264,9 @@ func (b *broker) provisionBucket(bucketName string) error {
255264
return nil
256265
}
257266

258-
// TODO (copejon) long way of checking for bucket name collision. gluster-swift does not support the api call that
259-
// minio.BucketExists() maps to; always fails with "400 bad request".
267+
// Returns true if the passed-in bucket exists.
268+
// TODO: long way of checking for bucket name collision. gluster-swift does not support
269+
// the api call that minio.BucketExists() maps to; always fails with "400 bad request".
260270
func (b *broker) checkBucketExists(bucketName string) (bool, error) {
261271
glog.Infof("Checking if bucket name %q already exists.", bucketName)
262272
buckets, err := b.s3Client.ListBuckets()
@@ -274,7 +284,7 @@ func (b *broker) checkBucketExists(bucketName string) (bool, error) {
274284
return exists, nil
275285
}
276286

277-
// getS3Client returns a minio api client
287+
// Returns a minio api client.
278288
func getS3Client(acct, user, pass, ip string) (*minio.Client, error) {
279289
glog.Infof("Creating s3 client based on: \"%s:%s\" on ip %s", acct, user, ip)
280290

@@ -287,7 +297,7 @@ func getS3Client(acct, user, pass, ip string) (*minio.Client, error) {
287297
return minioClient, nil
288298
}
289299

290-
// getKubeClient returns a k8s api client
300+
// Returns a k8s api client.
291301
func getKubeClient() (*clientset.Clientset, error) {
292302
glog.Info("Getting k8s API Client config")
293303
kubeClientConfig, err := k8sRest.InClusterConfig()
@@ -299,6 +309,7 @@ func getKubeClient() (*clientset.Clientset, error) {
299309
return cs, err
300310
}
301311

312+
// Returns the external ip of the gce master node.
302313
func getExternalIP() (string, error) {
303314
c := http.Client{}
304315
glog.Info("Requesting external IP.")
@@ -310,7 +321,7 @@ func getExternalIP() (string, error) {
310321
req.Header.Add("Metadata-Flavor", " Google")
311322
resp, err := c.Do(req)
312323
if err != nil {
313-
glog.Errorf("Failed to sent http request: %v", err)
324+
glog.Errorf("Failed to send http request: %v", err)
314325
return "", err
315326
}
316327
defer resp.Body.Close()

0 commit comments

Comments
 (0)