forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
provision.go
310 lines (262 loc) · 10.7 KB
/
provision.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package servicebroker
import (
"net/http"
"reflect"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apiserver/pkg/authentication/user"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/authorization"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/authorization/util"
"github.com/openshift/origin/pkg/openservicebroker/api"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
)
// ensureSecret ensures the existence of a Secret object containing the template
// configuration parameters.
func (b *Broker) ensureSecret(u user.Info, namespace string, instanceID string, preq *api.ProvisionRequest, didWork *bool) (*kapi.Secret, *api.Response) {
glog.V(4).Infof("Template service broker: ensureSecret")
secret := &kapi.Secret{
ObjectMeta: metav1.ObjectMeta{Name: instanceID},
Data: map[string][]byte{},
}
for k, v := range preq.Parameters {
if k != templateapi.RequesterUsernameParameterKey {
secret.Data[k] = []byte(v)
}
}
if err := util.Authorize(b.kc.Authorization().SubjectAccessReviews(), u, &authorization.ResourceAttributes{
Namespace: namespace,
Verb: "create",
Group: kapi.GroupName,
Resource: "secrets",
Name: secret.Name,
}); err != nil {
return nil, api.Forbidden(err)
}
createdSec, err := b.kc.Core().Secrets(namespace).Create(secret)
if err == nil {
*didWork = true
return createdSec, nil
}
if kerrors.IsAlreadyExists(err) {
if err := util.Authorize(b.kc.Authorization().SubjectAccessReviews(), u, &authorization.ResourceAttributes{
Namespace: namespace,
Verb: "get",
Group: kapi.GroupName,
Resource: "secrets",
Name: secret.Name,
}); err != nil {
return nil, api.Forbidden(err)
}
existingSec, err := b.kc.Core().Secrets(namespace).Get(secret.Name, metav1.GetOptions{})
if err == nil && reflect.DeepEqual(secret.Data, existingSec.Data) {
return existingSec, nil
}
return nil, api.NewResponse(http.StatusConflict, api.ProvisionResponse{}, nil)
}
if kerrors.IsForbidden(err) {
return nil, api.Forbidden(err)
}
return nil, api.InternalServerError(err)
}
// ensureTemplateInstance ensures the existence of a TemplateInstance object
// (this causes the template instance controller to instantiate the template in
// the namespace).
func (b *Broker) ensureTemplateInstance(u user.Info, namespace string, instanceID string, template *templateapi.Template, secret *kapi.Secret, didWork *bool) (*templateapi.TemplateInstance, *api.Response) {
glog.V(4).Infof("Template service broker: ensureTemplateInstance")
templateInstance := &templateapi.TemplateInstance{
ObjectMeta: metav1.ObjectMeta{Name: instanceID},
Spec: templateapi.TemplateInstanceSpec{
Template: *template,
Secret: &kapi.LocalObjectReference{Name: secret.Name},
Requester: &templateapi.TemplateInstanceRequester{
Username: u.GetName(),
},
},
}
if err := util.Authorize(b.kc.Authorization().SubjectAccessReviews(), u, &authorization.ResourceAttributes{
Namespace: namespace,
Verb: "create",
Group: templateapi.GroupName,
Resource: "templateinstances",
}); err != nil {
return nil, api.Forbidden(err)
}
createdTemplateInstance, err := b.templateclient.TemplateInstances(namespace).Create(templateInstance)
if err == nil {
*didWork = true
return createdTemplateInstance, nil
}
if kerrors.IsAlreadyExists(err) {
if err := util.Authorize(b.kc.Authorization().SubjectAccessReviews(), u, &authorization.ResourceAttributes{
Namespace: namespace,
Verb: "get",
Group: templateapi.GroupName,
Resource: "templateinstances",
Name: templateInstance.Name,
}); err != nil {
return nil, api.Forbidden(err)
}
existingTemplateInstance, err := b.templateclient.TemplateInstances(namespace).Get(templateInstance.Name, metav1.GetOptions{})
if err == nil && reflect.DeepEqual(templateInstance.Spec, existingTemplateInstance.Spec) {
return existingTemplateInstance, nil
}
return nil, api.NewResponse(http.StatusConflict, api.ProvisionResponse{}, nil)
}
if kerrors.IsForbidden(err) {
return nil, api.Forbidden(err)
}
return nil, api.InternalServerError(err)
}
// ensureBrokerTemplateInstanceUIDs ensures the UIDs of the namespaced Secret
// and TemplateInstance objects are set in the BrokerTemplateInstance object, as
// proof that we are done.
func (b *Broker) ensureBrokerTemplateInstanceUIDs(brokerTemplateInstance *templateapi.BrokerTemplateInstance, secret *kapi.Secret, templateInstance *templateapi.TemplateInstance, didWork *bool) (*templateapi.BrokerTemplateInstance, *api.Response) {
glog.V(4).Infof("Template service broker: ensureBrokerTemplateInstanceUIDs")
brokerTemplateInstance.Spec.Secret.UID = secret.UID
brokerTemplateInstance.Spec.TemplateInstance.UID = templateInstance.UID
// there is no SAR here because end users are not expected to have access to
// BrokerTemplateInstance objects.
brokerTemplateInstance, err := b.templateclient.BrokerTemplateInstances().Update(brokerTemplateInstance)
if err == nil {
*didWork = true
return brokerTemplateInstance, nil
}
return nil, api.InternalServerError(err)
}
// ensureBrokerTemplateInstance ensures the existence of BrokerTemplateInstance
// object (records intent, globally maps instanceID to namespaced Secret and
// TemplateInstance objects).
func (b *Broker) ensureBrokerTemplateInstance(namespace, instanceID string, didWork *bool) (*templateapi.BrokerTemplateInstance, *api.Response) {
glog.V(4).Infof("Template service broker: ensureBrokerTemplateInstance")
brokerTemplateInstance := &templateapi.BrokerTemplateInstance{
ObjectMeta: metav1.ObjectMeta{Name: instanceID},
Spec: templateapi.BrokerTemplateInstanceSpec{
TemplateInstance: kapi.ObjectReference{
Kind: "TemplateInstance",
Namespace: namespace,
Name: instanceID,
},
Secret: kapi.ObjectReference{
Kind: "Secret",
Namespace: namespace,
Name: instanceID,
},
},
}
// there is no SAR here because end users are not expected to have access to
// BrokerTemplateInstance objects.
newBrokerTemplateInstance, err := b.templateclient.BrokerTemplateInstances().Create(brokerTemplateInstance)
if err == nil {
*didWork = true
return newBrokerTemplateInstance, nil
}
if kerrors.IsAlreadyExists(err) {
existingBrokerTemplateInstance, err := b.templateclient.BrokerTemplateInstances().Get(brokerTemplateInstance.Name, metav1.GetOptions{})
if err == nil && reflect.DeepEqual(brokerTemplateInstance.Spec, existingBrokerTemplateInstance.Spec) {
return existingBrokerTemplateInstance, nil
}
return nil, api.NewResponse(http.StatusConflict, api.ProvisionResponse{}, nil)
}
return nil, api.InternalServerError(err)
}
// Provision instantiates a template from a ProvisionRequest, via the OpenShift
// TemplateInstance API.
func (b *Broker) Provision(instanceID string, preq *api.ProvisionRequest) *api.Response {
glog.V(4).Infof("Template service broker: Provision: instanceID %s", instanceID)
if errs := ValidateProvisionRequest(preq); len(errs) > 0 {
return api.BadRequest(errs.ToAggregate())
}
namespace := preq.Context.Namespace
impersonate := preq.Parameters[templateapi.RequesterUsernameParameterKey]
u := &user.DefaultInfo{Name: impersonate}
template, err := b.lister.GetByUID(preq.ServiceID)
if err != nil && !kerrors.IsNotFound(err) {
return api.BadRequest(err)
}
if template == nil {
// If the template is not found, it is just possible that it is because
// the cache is out of date. To be sure, fall back to O(N) search of
// templates in configured namespace(s).
glog.V(4).Infof("Template service broker: GetByUID didn't template %s", preq.ServiceID)
out:
for namespace := range b.templateNamespaces {
templates, err := b.lister.Templates(namespace).List(labels.Everything())
if err != nil {
return api.InternalServerError(err)
}
for _, t := range templates {
if string(t.UID) == preq.ServiceID {
template = t
break out
}
}
}
}
if template == nil {
glog.V(4).Infof("Template service broker: template %s not found", preq.ServiceID)
return api.BadRequest(kerrors.NewNotFound(templateapi.Resource("templates"), preq.ServiceID))
}
if _, ok := b.templateNamespaces[template.Namespace]; !ok {
return api.BadRequest(kerrors.NewNotFound(templateapi.Resource("templates"), preq.ServiceID))
}
// TODO: enable SAR for template - at the moment I think this doesn't work
// properly because group information isn't populated in u.
/*
if err := util.Authorize(b.kc.Authorization().SubjectAccessReviews(), u, &authorization.ResourceAttributes{
Namespace: template.Namespace,
Verb: "get",
Group: templateapi.GroupName,
Resource: "templates",
Name: template.Name,
}); err != nil {
return api.Forbidden(err)
}
*/
if err := util.Authorize(b.kc.Authorization().SubjectAccessReviews(), u, &authorization.ResourceAttributes{
Namespace: namespace,
Verb: "create",
Group: templateapi.GroupName,
Resource: "templateinstances",
Name: instanceID,
}); err != nil {
return api.Forbidden(err)
}
// The OSB API requires this function to be idempotent (restartable). Thus
// each sub-step is intended to succeed if it sets the desired state, or if
// the desired state is already set. didWork tracks if any actual change
// was made (if so, per the spec, StatusAccepted is returned, else
// StatusOK).
didWork := false
// The flow is as follows:
// 1. Ensure existence of BrokerTemplateInstance (records intent, globally
// maps instanceID to namespaced Secret and TemplateInstance objects).
// 2. Ensure existence of Secret containing template configuration
// parameters.
// 3. Ensure existence of TemplateInstance object (this causes the template
// instance controller to instantiate the template in the namespace).
// 4. Ensure the UIDs of the namespaced Secret and TemplateInstance objects
// are set in the BrokerTemplateInstance object, as proof that we are done.
brokerTemplateInstance, resp := b.ensureBrokerTemplateInstance(namespace, instanceID, &didWork)
if resp != nil {
return resp
}
secret, resp := b.ensureSecret(u, namespace, instanceID, preq, &didWork)
if resp != nil {
return resp
}
templateInstance, resp := b.ensureTemplateInstance(u, namespace, instanceID, template, secret, &didWork)
if resp != nil {
return resp
}
_, resp = b.ensureBrokerTemplateInstanceUIDs(brokerTemplateInstance, secret, templateInstance, &didWork)
if resp != nil {
return resp
}
if didWork {
return api.NewResponse(http.StatusAccepted, api.ProvisionResponse{Operation: api.OperationProvisioning}, nil)
}
return api.NewResponse(http.StatusOK, api.ProvisionResponse{Operation: api.OperationProvisioning}, nil)
}