4
4
"context"
5
5
"fmt"
6
6
7
+ pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
7
8
kneventing "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
8
9
knv1alpha1 "knative.dev/serving/pkg/apis/serving/v1alpha1"
9
10
knv1beta1 "knative.dev/serving/pkg/apis/serving/v1beta1"
@@ -134,8 +135,19 @@ func (r *ReconcileJSFunction) Reconcile(request reconcile.Request) (reconcile.Re
134
135
return reconcile.Result {}, err
135
136
}
136
137
138
+ reqLogger .Info ("Creating TaskRun for function build." )
139
+ build , err := r .buildForFunction (function )
140
+ if err != nil {
141
+ return reconcile.Result {}, err
142
+ }
143
+ err = r .client .Create (context .TODO (), build )
144
+ if err != nil {
145
+ reqLogger .Error (err , "Failed to create TaskRun for function build." , "Service.Namespace" , build .Namespace , "ConfigMap.Name" , build .Name )
146
+ return reconcile.Result {}, err
147
+ }
148
+
137
149
// Create service, mounting the config map
138
- service , err := r .serviceForFunction (function , configMap .Name )
150
+ service , err := r .serviceForFunction (function , configMap .Name , runtimeImageForFunction ( function ) )
139
151
if err != nil {
140
152
return reconcile.Result {}, err
141
153
}
@@ -216,6 +228,49 @@ func (r *ReconcileJSFunction) Reconcile(request reconcile.Request) (reconcile.Re
216
228
return reconcile.Result {}, nil
217
229
}
218
230
231
+ func (r * ReconcileJSFunction ) buildForFunction (f * faasv1alpha1.JSFunction ) (* pipeline.TaskRun , error ) {
232
+ imageName := runtimeImageForFunction (f )
233
+ taskRun := & pipeline.TaskRun {
234
+ ObjectMeta : metav1.ObjectMeta {
235
+ Name : fmt .Sprintf ("%s-build" , f .Name ),
236
+ Namespace : f .Namespace ,
237
+ },
238
+ Spec : pipeline.TaskRunSpec {
239
+ ServiceAccount : "js-function-operator" ,
240
+ TaskRef : & pipeline.TaskRef {
241
+ Name : "js-function-build-runtime" ,
242
+ },
243
+ Inputs : pipeline.TaskRunInputs {
244
+ Params : []pipeline.Param {{
245
+ Name : "FUNCTION_NAME" ,
246
+ Value : pipeline.ArrayOrString {
247
+ Type : "string" ,
248
+ StringVal : f .Name ,
249
+ },
250
+ }},
251
+ },
252
+ Outputs : pipeline.TaskRunOutputs {
253
+ Resources : []pipeline.TaskResourceBinding {
254
+ {
255
+ Name : "image" ,
256
+ ResourceSpec : & pipeline.PipelineResourceSpec {
257
+ Type : "image" ,
258
+ Params : []pipeline.ResourceParam {{
259
+ Name : "url" ,
260
+ Value : imageName ,
261
+ }},
262
+ },
263
+ },
264
+ },
265
+ },
266
+ },
267
+ }
268
+ if err := controllerutil .SetControllerReference (f , taskRun , r .scheme ); err != nil {
269
+ return nil , err
270
+ }
271
+ return taskRun , nil
272
+ }
273
+
219
274
func (r * ReconcileJSFunction ) configMapWithFunction (f * faasv1alpha1.JSFunction ) (* corev1.ConfigMap , error ) {
220
275
221
276
data := map [string ]string {"index.js" : f .Spec .Func }
@@ -238,7 +293,7 @@ func (r *ReconcileJSFunction) configMapWithFunction(f *faasv1alpha1.JSFunction)
238
293
return configMap , nil
239
294
}
240
295
241
- func (r * ReconcileJSFunction ) serviceForFunction (f * faasv1alpha1.JSFunction , configMapName string ) (* knv1alpha1.Service , error ) {
296
+ func (r * ReconcileJSFunction ) serviceForFunction (f * faasv1alpha1.JSFunction , configMapName string , imageName string ) (* knv1alpha1.Service , error ) {
242
297
service := & knv1alpha1.Service {
243
298
ObjectMeta : metav1.ObjectMeta {
244
299
Name : f .Name ,
@@ -252,7 +307,7 @@ func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction, con
252
307
},
253
308
Spec : knv1alpha1.RevisionSpec {
254
309
RevisionSpec : knv1beta1.RevisionSpec {
255
- PodSpec : createPodSpec (f .Name , configMapName ),
310
+ PodSpec : createPodSpec (f .Name , configMapName , imageName ),
256
311
},
257
312
},
258
313
},
@@ -269,11 +324,11 @@ func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction, con
269
324
return service , nil
270
325
}
271
326
272
- func createPodSpec (functionName , configMapName string ) corev1.PodSpec {
327
+ func createPodSpec (functionName , configMapName string , imageName string ) corev1.PodSpec {
273
328
volumeName := fmt .Sprintf ("%s-source" , functionName )
274
329
return corev1.PodSpec {
275
330
Containers : []corev1.Container {{
276
- Image : "docker.io/zroubalik/js-runtime" ,
331
+ Image : imageName ,
277
332
Name : fmt .Sprintf ("nodejs-%s" , functionName ),
278
333
Ports : []corev1.ContainerPort {{
279
334
ContainerPort : 8080 ,
@@ -357,3 +412,7 @@ func (r *ReconcileJSFunction) subscriptionForFunction(f *faasv1alpha1.JSFunction
357
412
358
413
return subscription , nil
359
414
}
415
+
416
+ func runtimeImageForFunction (f * faasv1alpha1.JSFunction ) string {
417
+ return fmt .Sprintf ("image-registry.openshift-image-registry.svc:5000/%s/%s-runtime" , f .Namespace , f .Name )
418
+ }
0 commit comments