Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

Commit 2c7ed7d

Browse files
committed
feat: add a TaskRun to build a function runtime image
1 parent f01e94a commit 2c7ed7d

File tree

6 files changed

+175
-6
lines changed

6 files changed

+175
-6
lines changed

Diff for: Gopkg.lock

+27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: cmd/manager/main.go

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
knv1alpha1 "knative.dev/serving/pkg/apis/serving/v1alpha1"
1313
knv1beta1 "knative.dev/serving/pkg/apis/serving/v1beta1"
1414

15+
// Import tekton types
16+
pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
17+
1518
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1619
_ "k8s.io/client-go/plugin/pkg/client/auth"
1720

@@ -123,6 +126,11 @@ func main() {
123126
os.Exit(1)
124127
}
125128

129+
if err := pipeline.AddToScheme(mgr.GetScheme()); err != nil {
130+
log.Error(err, "Can't register the tekton pipeline scheme")
131+
os.Exit(1)
132+
}
133+
126134
// Setup all Controllers
127135
if err := controller.AddToManager(mgr); err != nil {
128136
log.Error(err, "")

Diff for: deploy/build/js-function-build-task.yaml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
apiVersion: tekton.dev/v1alpha1
2+
kind: Task
3+
metadata:
4+
name: js-function-build-runtime
5+
spec:
6+
inputs:
7+
params:
8+
- name: FUNCTION_NAME
9+
description: The name of the function being built
10+
default: 'user-function'
11+
- name: TLSVERIFY
12+
description: Verify the TLS on the registry endpoint (default false)
13+
default: 'false'
14+
outputs:
15+
resources:
16+
- name: image
17+
type: image
18+
steps:
19+
- name: copy-source
20+
image: docker.io/alpine
21+
command: ['cp', '-fpv', '/fn-source/index.js', '/fn-source/package.json', '/home/node/usr/']
22+
volumeMounts:
23+
- name: sourcedir
24+
mountPath: /fn-source
25+
- name: gen-source
26+
mountPath: /home/node/usr
27+
securityContext:
28+
privileged: true
29+
- name: generate
30+
image: quay.io/openshift-pipeline/s2i
31+
workingdir: '/home/node/usr'
32+
command: ['s2i', 'build', '.', 'quay.io/lanceball/js-runtime', '--as-dockerfile', '/home/node/build/Dockerfile.gen']
33+
volumeMounts:
34+
- name: gen-source
35+
mountPath: /home/node/usr
36+
- name: buildpath
37+
mountPath: /home/node/build
38+
securityContext:
39+
privileged: true
40+
- name: build
41+
image: quay.io/buildah/stable
42+
workingdir: /home/node/build
43+
command: ['buildah', 'bud', '--tls-verify=${inputs.params.TLSVERIFY}', '--layers', '-f', '/Dockerfile.gen', '-t', '${outputs.resources.image.url}', '.']
44+
volumeMounts:
45+
- name: varlibcontainers
46+
mountPath: /var/lib/containers
47+
- name: buildpath
48+
mountPath: /home/node/build
49+
securityContext:
50+
privileged: true
51+
- name: push
52+
image: quay.io/buildah/stable
53+
command: ['buildah', 'push', '--tls-verify=${inputs.params.TLSVERIFY}', '${outputs.resources.image.url}', 'docker://${outputs.resources.image.url}']
54+
volumeMounts:
55+
- name: varlibcontainers
56+
mountPath: /var/lib/containers
57+
securityContext:
58+
privileged: true
59+
60+
volumes:
61+
- name: varlibcontainers
62+
emptyDir: {}
63+
- name: gen-source
64+
emptyDir: {}
65+
- name: buildpath
66+
emptyDir: {}
67+
- name: sourcedir
68+
configMap:
69+
name: ${inputs.params.FUNCTION_NAME}

Diff for: deploy/operator.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ spec:
1616
containers:
1717
- name: js-function-operator
1818
# Replace this with the built image name
19-
image: docker.io/lanceball/js-function-operator:v0.0.2
19+
image: quay.io/lanceball/js-function-operator:v0.0.2
2020
command:
2121
- js-function-operator
2222
imagePullPolicy: Always

Diff for: deploy/role.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ rules:
3131
- services
3232
verbs:
3333
- '*'
34+
- apiGroups:
35+
- tekton.dev
36+
resources:
37+
- tasks
38+
verbs:
39+
- '*'
3440
- apiGroups:
3541
- monitoring.coreos.com
3642
resources:

Diff for: pkg/controller/jsfunction/jsfunction_controller.go

+64-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
78
kneventing "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
89
knv1alpha1 "knative.dev/serving/pkg/apis/serving/v1alpha1"
910
knv1beta1 "knative.dev/serving/pkg/apis/serving/v1beta1"
@@ -134,8 +135,19 @@ func (r *ReconcileJSFunction) Reconcile(request reconcile.Request) (reconcile.Re
134135
return reconcile.Result{}, err
135136
}
136137

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+
137149
// Create service, mounting the config map
138-
service, err := r.serviceForFunction(function, configMap.Name)
150+
service, err := r.serviceForFunction(function, configMap.Name, runtimeImageForFunction(function))
139151
if err != nil {
140152
return reconcile.Result{}, err
141153
}
@@ -216,6 +228,49 @@ func (r *ReconcileJSFunction) Reconcile(request reconcile.Request) (reconcile.Re
216228
return reconcile.Result{}, nil
217229
}
218230

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+
219274
func (r *ReconcileJSFunction) configMapWithFunction(f *faasv1alpha1.JSFunction) (*corev1.ConfigMap, error) {
220275

221276
data := map[string]string{"index.js": f.Spec.Func}
@@ -238,7 +293,7 @@ func (r *ReconcileJSFunction) configMapWithFunction(f *faasv1alpha1.JSFunction)
238293
return configMap, nil
239294
}
240295

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) {
242297
service := &knv1alpha1.Service{
243298
ObjectMeta: metav1.ObjectMeta{
244299
Name: f.Name,
@@ -252,7 +307,7 @@ func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction, con
252307
},
253308
Spec: knv1alpha1.RevisionSpec{
254309
RevisionSpec: knv1beta1.RevisionSpec{
255-
PodSpec: createPodSpec(f.Name, configMapName),
310+
PodSpec: createPodSpec(f.Name, configMapName, imageName),
256311
},
257312
},
258313
},
@@ -269,11 +324,11 @@ func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction, con
269324
return service, nil
270325
}
271326

272-
func createPodSpec(functionName, configMapName string) corev1.PodSpec {
327+
func createPodSpec(functionName, configMapName string, imageName string) corev1.PodSpec {
273328
volumeName := fmt.Sprintf("%s-source", functionName)
274329
return corev1.PodSpec{
275330
Containers: []corev1.Container{{
276-
Image: "docker.io/zroubalik/js-runtime",
331+
Image: imageName,
277332
Name: fmt.Sprintf("nodejs-%s", functionName),
278333
Ports: []corev1.ContainerPort{{
279334
ContainerPort: 8080,
@@ -357,3 +412,7 @@ func (r *ReconcileJSFunction) subscriptionForFunction(f *faasv1alpha1.JSFunction
357412

358413
return subscription, nil
359414
}
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

Comments
 (0)