@@ -30,8 +30,9 @@ import (
30
30
"github.com/pkg/errors"
31
31
"github.com/satori/go.uuid"
32
32
"github.com/spf13/cobra"
33
- meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
34
"k8s.io/apimachinery/pkg/runtime"
35
+ "k8s.io/apimachinery/pkg/watch"
35
36
"k8s.io/client-go/kubernetes"
36
37
"k8s.io/client-go/pkg/api/v1"
37
38
kalpha "k8s.io/client-go/pkg/apis/settings/v1alpha1"
@@ -53,7 +54,7 @@ func createBindingObject(consumer, provider, bindingName, instance string, param
53
54
return nil , err
54
55
}
55
56
b := & v1beta1.ServiceBinding {
56
- ObjectMeta : meta_v1 .ObjectMeta {
57
+ ObjectMeta : metav1 .ObjectMeta {
57
58
Name : bindingName ,
58
59
Annotations : map [string ]string {"consumer" : consumer , "provider" : provider },
59
60
},
@@ -66,25 +67,25 @@ func createBindingObject(consumer, provider, bindingName, instance string, param
66
67
return b , nil
67
68
}
68
69
69
- func podPreset (objectName , secretName , producerSvcName , consumerSvcName string ) * kalpha.PodPreset {
70
+ func podPreset (objectName , secretName , providerSvcName , consumerSvcName string ) * kalpha.PodPreset {
70
71
podPreset := kalpha.PodPreset {
71
- ObjectMeta : meta_v1 .ObjectMeta {
72
+ ObjectMeta : metav1 .ObjectMeta {
72
73
Name : objectName ,
73
74
Labels : map [string ]string {
74
75
"group" : "mobile" ,
75
- "service" : producerSvcName ,
76
+ "service" : providerSvcName ,
76
77
},
77
78
},
78
79
Spec : kalpha.PodPresetSpec {
79
- Selector : meta_v1 .LabelSelector {
80
+ Selector : metav1 .LabelSelector {
80
81
MatchLabels : map [string ]string {
81
82
"run" : consumerSvcName ,
82
- producerSvcName : "enabled" ,
83
+ providerSvcName : "enabled" ,
83
84
},
84
85
},
85
86
Volumes : []v1.Volume {
86
87
{
87
- Name : producerSvcName ,
88
+ Name : providerSvcName ,
88
89
VolumeSource : v1.VolumeSource {
89
90
Secret : & v1.SecretVolumeSource {
90
91
SecretName : secretName ,
@@ -94,8 +95,8 @@ func podPreset(objectName, secretName, producerSvcName, consumerSvcName string)
94
95
},
95
96
VolumeMounts : []v1.VolumeMount {
96
97
{
97
- Name : producerSvcName ,
98
- MountPath : "/etc/secrets/" + producerSvcName ,
98
+ Name : providerSvcName ,
99
+ MountPath : "/etc/secrets/" + providerSvcName ,
99
100
},
100
101
},
101
102
},
@@ -112,7 +113,7 @@ mobile --namespace=myproject create integration <consuming_service_instance_id>
112
113
oc plugin mobile create integration <consuming_service_instance_id> <providing_service_instance_id>
113
114
` ,
114
115
RunE : func (cmd * cobra.Command , args []string ) error {
115
- if len (args ) < 2 {
116
+ if len (args ) != 2 {
116
117
return cmd .Usage ()
117
118
}
118
119
namespace , err := currentNamespace (cmd .Flags ())
@@ -122,51 +123,88 @@ oc plugin mobile create integration <consuming_service_instance_id> <providing_s
122
123
123
124
consumerSvcInstName := args [0 ]
124
125
providerSvcInstName := args [1 ]
125
- providerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (providerSvcInstName , meta_v1 .GetOptions {})
126
+ providerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (providerSvcInstName , metav1 .GetOptions {})
126
127
if err != nil {
127
- return err
128
+ return errors . WithStack ( err )
128
129
}
129
- consumerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (consumerSvcInstName , meta_v1 .GetOptions {})
130
+ consumerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (consumerSvcInstName , metav1 .GetOptions {})
130
131
if err != nil {
131
- return err
132
+ return errors . WithStack ( err )
132
133
}
133
- //todo remove the need for these by updating the apbs to read the secrets themselves (https://github.com/feedhenry/keycloak-apb/issues/37)
134
+ // todo remove the need for these by updating the apbs to read the secrets themselves (https://github.com/feedhenry/keycloak-apb/issues/37)
134
135
consumerSvc := getService (namespace , consumerSvcInst .Labels ["serviceName" ], bc .k8Client ) // the consumer service
135
136
providerSvc := getService (namespace , providerSvcInst .Labels ["serviceName" ], bc .k8Client ) // the provider service
136
137
bindParams := buildBindParams (providerSvc , consumerSvc )
137
138
objectName := objectName (consumerSvcInstName , providerSvcInstName )
138
139
binding , err := createBindingObject (consumerSvc .Name , providerSvc .Name , objectName , providerSvcInst .Name , bindParams , objectName )
139
140
if err != nil {
140
- return err
141
+ return errors . WithStack ( err )
141
142
}
142
- if _ , err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).Create (binding ); err != nil {
143
- return err
143
+ sb , err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).Create (binding )
144
+ if err != nil {
145
+ return errors .WithStack (err )
144
146
}
145
147
preset := podPreset (objectName , objectName , providerSvc .Name , consumerSvc .Name )
146
148
if _ , err := bc .k8Client .SettingsV1alpha1 ().PodPresets (namespace ).Create (preset ); err != nil {
147
149
return errors .Wrap (err , "failed to create pod preset for service " )
148
150
}
149
151
redeploy , err := cmd .PersistentFlags ().GetBool ("auto-redeploy" )
150
152
if err != nil {
151
- return err
153
+ return errors . WithStack ( err )
152
154
}
153
155
if ! redeploy {
154
156
fmt .Println ("you will need to redeploy your service/pod to pick up the changes" )
155
157
return nil
156
158
}
157
- //update the deployment with an annotation
158
- dep , err := bc .k8Client .AppsV1beta1 ().Deployments (namespace ).Get (consumerSvc .Name , meta_v1 .GetOptions {})
159
+ // update the deployment with an annotation
160
+ dep , err := bc .k8Client .AppsV1beta1 ().Deployments (namespace ).Get (consumerSvc .Name , metav1 .GetOptions {})
159
161
if err != nil {
160
162
return errors .Wrap (err , "failed to get deployment for service " + consumerSvcInstName )
161
163
}
162
164
dep .Spec .Template .Labels [providerSvc .Name ] = "enabled"
163
165
if _ , err := bc .k8Client .AppsV1beta1 ().Deployments (namespace ).Update (dep ); err != nil {
164
- return errors .Wrap (err , "failed up update deployment for " + consumerSvcInstName )
166
+ return errors .Wrap (err , "failed to update deployment for service " + consumerSvcInstName )
167
+ }
168
+
169
+ // exit immediately
170
+ noWait , err := cmd .PersistentFlags ().GetBool ("no-wait" )
171
+ if err != nil {
172
+ return errors .WithStack (err )
173
+ }
174
+ if noWait {
175
+ return nil
176
+ }
177
+
178
+ // else watch
179
+ id := sb .Spec .ExternalID
180
+ w , err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).Watch (metav1.ListOptions {LabelSelector : "id=" + id })
181
+ if err != nil {
182
+ return errors .WithStack (err )
183
+ }
184
+ for u := range w .ResultChan () {
185
+ o := u .Object .(* v1beta1.ServiceBinding )
186
+ switch u .Type {
187
+ case watch .Error :
188
+ w .Stop ()
189
+ return errors .New ("unexpected error watching service binding " + err .Error ())
190
+ case watch .Modified :
191
+ for _ , c := range o .Status .Conditions {
192
+ fmt .Println ("status: " + c .Message )
193
+ if c .Type == "Ready" && c .Status == "True" {
194
+ w .Stop ()
195
+ }
196
+ if c .Type == "Failed" {
197
+ w .Stop ()
198
+ return errors .New ("Failed to create integration: " + c .Message )
199
+ }
200
+ }
201
+ }
165
202
}
166
203
167
204
return nil
168
205
},
169
206
}
207
+ cmd .PersistentFlags ().Bool ("no-wait" , false , "--no-wait will cause the command to exit immediately after a successful response instead of waiting until the binding is complete" )
170
208
cmd .PersistentFlags ().Bool ("auto-redeploy" , false , "--auto-redeploy=true will cause a backing deployment to be rolled out" )
171
209
return cmd
172
210
}
@@ -184,7 +222,7 @@ mobile --namespace=myproject delete integration <consuming_service_instance_id>
184
222
oc plugin mobile delete integration <consuming_service_instance_id> <providing_service_instance_id>
185
223
` ,
186
224
RunE : func (cmd * cobra.Command , args []string ) error {
187
- if len (args ) < 2 {
225
+ if len (args ) != 2 {
188
226
return cmd .Usage ()
189
227
}
190
228
namespace , err := currentNamespace (cmd .Flags ())
@@ -194,39 +232,39 @@ oc plugin mobile delete integration <consuming_service_instance_id> <providing_s
194
232
consumerSvcInstName := args [0 ]
195
233
providerSvcInstName := args [1 ]
196
234
197
- consumerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (consumerSvcInstName , meta_v1 .GetOptions {})
235
+ consumerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (consumerSvcInstName , metav1 .GetOptions {})
198
236
if err != nil {
199
- return err
237
+ return errors . WithStack ( err )
200
238
}
201
- providerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (providerSvcInstName , meta_v1 .GetOptions {})
239
+ providerSvcInst , err := bc .scClient .ServicecatalogV1beta1 ().ServiceInstances (namespace ).Get (providerSvcInstName , metav1 .GetOptions {})
202
240
if err != nil {
203
- return err
241
+ return errors . WithStack ( err )
204
242
}
205
243
consumerSvcName := consumerSvcInst .Labels ["serviceName" ]
206
244
providerSvcName := providerSvcInst .Labels ["serviceName" ]
207
245
objectName := objectName (consumerSvcInstName , providerSvcInstName )
208
- if err := bc .k8Client .SettingsV1alpha1 ().PodPresets (namespace ).Delete (objectName , meta_v1 .NewDeleteOptions (0 )); err != nil {
209
- return err
246
+ if err := bc .k8Client .SettingsV1alpha1 ().PodPresets (namespace ).Delete (objectName , metav1 .NewDeleteOptions (0 )); err != nil {
247
+ return errors . WithStack ( err )
210
248
}
211
- if err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).Delete (objectName , meta_v1 .NewDeleteOptions (0 )); err != nil {
212
- return err
249
+ if err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).Delete (objectName , metav1 .NewDeleteOptions (0 )); err != nil {
250
+ return errors . WithStack ( err )
213
251
}
214
252
redeploy , err := cmd .PersistentFlags ().GetBool ("auto-redeploy" )
215
253
if err != nil {
216
- return err
254
+ return errors . WithStack ( err )
217
255
}
218
256
if ! redeploy {
219
257
fmt .Println ("you will need to redeploy your service to pick up the changes" )
220
258
return nil
221
259
}
222
260
//update the deployment with an annotation
223
- dep , err := bc .k8Client .AppsV1beta1 ().Deployments (namespace ).Get (consumerSvcName , meta_v1 .GetOptions {})
261
+ dep , err := bc .k8Client .AppsV1beta1 ().Deployments (namespace ).Get (consumerSvcName , metav1 .GetOptions {})
224
262
if err != nil {
225
263
return errors .Wrap (err , "failed to get deployment for service " + consumerSvcInstName )
226
264
}
227
265
delete (dep .Spec .Template .Labels , providerSvcName )
228
266
if _ , err := bc .k8Client .AppsV1beta1 ().Deployments (namespace ).Update (dep ); err != nil {
229
- return errors .Wrap (err , "failed up update deployment for " + consumerSvcInstName )
267
+ return errors .Wrap (err , "failed to update deployment for service " + consumerSvcInstName )
230
268
}
231
269
return nil
232
270
},
@@ -256,13 +294,13 @@ func (bc *IntegrationCmd) ListIntegrationsCmd() *cobra.Command {
256
294
if err != nil {
257
295
return errors .Wrap (err , "failed to get namespace" )
258
296
}
259
- sbList , err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).List (meta_v1 .ListOptions {})
297
+ sbList , err := bc .scClient .ServicecatalogV1beta1 ().ServiceBindings (namespace ).List (metav1 .ListOptions {})
260
298
if err != nil {
261
- return err
299
+ return errors . WithStack ( err )
262
300
}
263
301
outType := outputType (cmd .Flags ())
264
302
if err := bc .Out .Render ("list" + cmd .Name (), outType , sbList ); err != nil {
265
- return err
303
+ return errors . WithStack ( err )
266
304
}
267
305
return nil
268
306
},
0 commit comments