Skip to content

Commit 8f3f158

Browse files
committed
Adding BucketAccess Add Functionality, Fix cmd controller and pass context to command
Fixes kubernetes-retired#10 Fixes kubernetes-retired#8
1 parent c330fbd commit 8f3f158

File tree

7 files changed

+457
-16
lines changed

7 files changed

+457
-16
lines changed

Diff for: cmd/controller-manager/controller-manager.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/spf13/viper"
1212

1313
bucketcontroller "github.com/kubernetes-sigs/container-object-storage-interface-api/controller"
14+
"github.com/kubernetes-sigs/container-object-storage-interface-controller/pkg/bucketaccessrequest"
1415
"github.com/kubernetes-sigs/container-object-storage-interface-controller/pkg/bucketrequest"
1516

1617
"github.com/golang/glog"
@@ -59,20 +60,20 @@ func init() {
5960
}
6061

6162
func main() {
62-
if err := cmd.Execute(); err != nil {
63-
glog.Fatal(err.Error())
64-
}
65-
66-
var cancel context.CancelFunc
63+
ctx, cancel := context.WithCancel(context.Background())
64+
defer cancel() // Just in case
6765

68-
_, cancel = context.WithCancel(cmd.Context())
6966
sigs := make(chan os.Signal, 1)
7067
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
7168

7269
go func() {
7370
<-sigs
7471
cancel()
7572
}()
73+
74+
if err := cmd.ExecuteContext(ctx); err != nil {
75+
glog.Fatal(err.Error())
76+
}
7677
}
7778

7879
func run(ctx context.Context, args []string) error {
@@ -81,5 +82,6 @@ func run(ctx context.Context, args []string) error {
8182
return err
8283
}
8384
ctrl.AddBucketRequestListener(bucketrequest.NewListener())
85+
ctrl.AddBucketAccessRequestListener(bucketaccessrequest.NewListener())
8486
return ctrl.Run(ctx)
8587
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/spf13/cobra v1.1.1
99
github.com/spf13/viper v1.7.1
1010
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
11+
k8s.io/api v0.19.4
1112
k8s.io/apiextensions-apiserver v0.19.4
1213
k8s.io/apimachinery v0.19.4
1314
k8s.io/client-go v0.19.4

Diff for: go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
259259
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
260260
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
261261
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
262-
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
263262
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
264263
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
265264
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
@@ -486,7 +485,6 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCc
486485
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
487486
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
488487
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
489-
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
490488
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
491489
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
492490
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
@@ -676,7 +674,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
676674
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
677675
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
678676
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
679-
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
680677
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
681678
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
682679
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -693,7 +690,6 @@ k8s.io/api v0.18.6/go.mod h1:eeyxr+cwCjMdLAmr2W3RyDI0VvTawSg/3RFFBEnmZGI=
693690
k8s.io/api v0.19.4 h1:I+1I4cgJYuCDgiLNjKx7SLmIbwgj9w7N7Zr5vSIdwpo=
694691
k8s.io/api v0.19.4/go.mod h1:SbtJ2aHCItirzdJ36YslycFNzWADYH3tgOhvBEFtZAk=
695692
k8s.io/apiextensions-apiserver v0.18.2/go.mod h1:q3faSnRGmYimiocj6cHQ1I3WpLqmDgJFlKL37fC4ZvY=
696-
k8s.io/apiextensions-apiserver v0.18.6 h1:vDlk7cyFsDyfwn2rNAO2DbmUbvXy5yT5GE3rrqOzaMo=
697693
k8s.io/apiextensions-apiserver v0.18.6/go.mod h1:lv89S7fUysXjLZO7ke783xOwVTm6lKizADfvUM/SS/M=
698694
k8s.io/apiextensions-apiserver v0.19.4 h1:D9ak9T012tb3vcGFWYmbQuj9SCC8YM4zhA4XZqsAQC4=
699695
k8s.io/apiextensions-apiserver v0.19.4/go.mod h1:B9rpH/nu4JBCtuUp3zTTk8DEjZUupZTBEec7/2zNRYw=

Diff for: pkg/bucketaccessrequest/bucketaccessrequest.go

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package bucketaccessrequest
2+
3+
import (
4+
"context"
5+
6+
apierrs "k8s.io/apimachinery/pkg/api/errors"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
9+
"github.com/kubernetes-sigs/container-object-storage-interface-api/apis/objectstorage.k8s.io/v1alpha1"
10+
bucketclientset "github.com/kubernetes-sigs/container-object-storage-interface-api/clientset"
11+
bucketcontroller "github.com/kubernetes-sigs/container-object-storage-interface-api/controller"
12+
"github.com/kubernetes-sigs/container-object-storage-interface-controller/pkg/util"
13+
kubeclientset "k8s.io/client-go/kubernetes"
14+
15+
"github.com/golang/glog"
16+
)
17+
18+
type bucketAccessRequestListener struct {
19+
kubeClient kubeclientset.Interface
20+
bucketClient bucketclientset.Interface
21+
}
22+
23+
func NewListener() bucketcontroller.BucketAccessRequestListener {
24+
return &bucketAccessRequestListener{}
25+
}
26+
27+
func (b *bucketAccessRequestListener) InitializeKubeClient(k kubeclientset.Interface) {
28+
b.kubeClient = k
29+
}
30+
31+
func (b *bucketAccessRequestListener) InitializeBucketClient(bc bucketclientset.Interface) {
32+
b.bucketClient = bc
33+
}
34+
35+
func (b *bucketAccessRequestListener) Add(ctx context.Context, obj *v1alpha1.BucketAccessRequest) error {
36+
glog.V(1).Infof("add called for bucketaccessrequest %s", obj.Name)
37+
bucketAccessRequest := obj
38+
39+
err := b.provisionBucketAccess(ctx, bucketAccessRequest)
40+
if err != nil {
41+
// Provisioning is 100% finished / not in progress.
42+
switch err {
43+
case util.ErrInvalidBucketAccessClass:
44+
glog.V(5).Infof("Bucket access class specified does not exist. Stop provisioning, removing bucket access request %s from bucket access requests in progress", bucketAccessRequest.UID)
45+
err = nil
46+
case util.ErrBucketAccessAlreadyExists:
47+
glog.V(5).Infof("Bucket access already exist for this bucket request. Stop provisioning, removing bucket access request %s from bucket access requests in progress", bucketAccessRequest.UID)
48+
err = nil
49+
default:
50+
glog.V(2).Infof("Final error received, removing bucket access request %s from bucket access requests in progress", bucketAccessRequest.Name)
51+
}
52+
return err
53+
}
54+
55+
glog.V(5).Infof("BucketAccessRequest processing succeeded, removing bucket access request %s from bucket access requests in progress", bucketAccessRequest.Name)
56+
return nil
57+
}
58+
59+
func (b *bucketAccessRequestListener) Update(ctx context.Context, old, new *v1alpha1.BucketAccessRequest) error {
60+
glog.V(1).Infof("update called for bucket %v", old)
61+
return nil
62+
}
63+
64+
func (b *bucketAccessRequestListener) Delete(ctx context.Context, obj *v1alpha1.BucketAccessRequest) error {
65+
glog.V(1).Infof("delete called for bucket %v", obj)
66+
return nil
67+
}
68+
69+
// provisionBucketAccess attempts to provision a BucketAccess for the given bucketAccessRequest.
70+
// Returns nil error only when the bucketaccess was provisioned. An error is return if we cannot create bucket access.
71+
// A normal error is returned when bucket acess was not provisioned and provisioning should be retried (requeue the bucketAccessRequest),
72+
// or a special error errBucketAccessAlreadyExists, errInvalidBucketAccessClass is returned when provisioning was impossible and
73+
// no further attempts to provision should be tried.
74+
func (b *bucketAccessRequestListener) provisionBucketAccess(ctx context.Context, bucketAccessRequest *v1alpha1.BucketAccessRequest) error {
75+
bucketAccessClassName := bucketAccessRequest.Spec.BucketAccessClassName
76+
77+
bucketaccess := b.FindBucketAccess(ctx, bucketAccessRequest)
78+
if bucketaccess != nil {
79+
// bucketaccess has been already provisioned, nothing to do.
80+
return util.ErrBucketAccessAlreadyExists
81+
}
82+
83+
bucketAccessClass, err := b.bucketClient.ObjectstorageV1alpha1().BucketAccessClasses().Get(ctx, bucketAccessClassName, metav1.GetOptions{})
84+
if bucketAccessClass == nil {
85+
// bucket has been already provisioned, nothing to do.
86+
return util.ErrInvalidBucketAccessClass
87+
}
88+
89+
bucketRequest, err := b.bucketClient.ObjectstorageV1alpha1().BucketRequests(bucketAccessRequest.Namespace).Get(ctx, bucketAccessRequest.Spec.BucketRequestName, metav1.GetOptions{})
90+
if bucketRequest == nil {
91+
// bucket request does not exist, we have to reject this provision.
92+
return util.ErrInvalidBucketRequest
93+
}
94+
if err != nil {
95+
return err
96+
}
97+
98+
if bucketRequest.Spec.BucketInstanceName == "" {
99+
return util.ErrWaitForBucketProvisioning
100+
}
101+
102+
bucketaccess = &v1alpha1.BucketAccess{}
103+
bucketaccess.Name = util.GetUUID()
104+
105+
bucketaccess.Spec.BucketInstanceName = bucketRequest.Spec.BucketInstanceName
106+
bucketaccess.Spec.BucketAccessRequest = bucketAccessRequest.Name
107+
bucketaccess.Spec.ServiceAccount = bucketAccessRequest.Spec.ServiceAccountName
108+
// bucketaccess.Spec.MintedSecretName - set by the driver
109+
bucketaccess.Spec.PolicyActionsConfigMapData, err = util.ReadConfigData(b.kubeClient, bucketAccessClass.PolicyActionsConfigMap)
110+
if err != nil {
111+
return err
112+
}
113+
// bucketaccess.Spec.Principal - set by the driver
114+
115+
bucketaccess.Spec.Provisioner = bucketAccessClass.Provisioner
116+
bucketaccess.Spec.Parameters = util.CopySS(bucketAccessClass.Parameters)
117+
118+
bucketaccess, err = b.bucketClient.ObjectstorageV1alpha1().BucketAccesses().Create(context.Background(), bucketaccess, metav1.CreateOptions{})
119+
if err == nil || apierrs.IsAlreadyExists(err) {
120+
glog.V(5).Infof("BucketAccess %s saved", bucketaccess.Name)
121+
return nil
122+
}
123+
124+
glog.Infof("Finished providioning bucket access for %v", bucketAccessRequest.Name)
125+
return nil
126+
}
127+
128+
func (b *bucketAccessRequestListener) FindBucketAccess(ctx context.Context, br *v1alpha1.BucketAccessRequest) *v1alpha1.BucketAccess {
129+
bucketAccessList, err := b.bucketClient.ObjectstorageV1alpha1().BucketAccesses().List(ctx, metav1.ListOptions{})
130+
if err != nil {
131+
return nil
132+
}
133+
if len(bucketAccessList.Items) > 0 {
134+
for _, bucketaccess := range bucketAccessList.Items {
135+
if bucketaccess.Spec.BucketAccessRequest == br.Name {
136+
return &bucketaccess
137+
}
138+
}
139+
}
140+
return nil
141+
}

0 commit comments

Comments
 (0)