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

Commit bd78eb6

Browse files
rhusslance
authored andcommitted
chore: Fix usage with ConfigMap
1 parent d87ca59 commit bd78eb6

File tree

6 files changed

+70
-44
lines changed

6 files changed

+70
-44
lines changed

Diff for: cmd/manager/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1515
_ "k8s.io/client-go/plugin/pkg/client/auth"
1616

17-
"github.com/lance/js-function-operator/pkg/apis"
18-
"github.com/lance/js-function-operator/pkg/controller"
17+
"github.com/openshift-cloud-functions/js-function-operator/pkg/apis"
18+
"github.com/openshift-cloud-functions/js-function-operator/pkg/controller"
1919

2020
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
2121
"github.com/operator-framework/operator-sdk/pkg/leader"

Diff for: deploy/crds/faas_v1alpha1_jsfunction_cr.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ spec:
66
# Add fields here
77
func: |
88
const http = require('http');
9-
const port = 8080;
9+
const port = 8181;
1010
1111
const handler = (request, response) => {
1212
console.log(request.url);

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: quay.io/lanceball/js-function-operator:v0.0.1
19+
image: docker.io/rhuss/js-function-operator:v0.0.1
2020
command:
2121
- js-function-operator
2222
imagePullPolicy: Always

Diff for: pkg/apis/addtoscheme_faas_v1alpha1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package apis
22

33
import (
4-
"github.com/lance/js-function-operator/pkg/apis/faas/v1alpha1"
4+
"github.com/openshift-cloud-functions/js-function-operator/pkg/apis/faas/v1alpha1"
55
)
66

77
func init() {

Diff for: pkg/controller/add_jsfunction.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package controller
22

33
import (
4-
"github.com/lance/js-function-operator/pkg/controller/jsfunction"
4+
"github.com/openshift-cloud-functions/js-function-operator/pkg/controller/jsfunction"
55
)
66

77
func init() {

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

+64-38
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package jsfunction
22

33
import (
44
"context"
5-
"strings"
5+
"fmt"
66

77
knv1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
88
knv1beta1 "github.com/knative/serving/pkg/apis/serving/v1beta1"
99

10-
faasv1alpha1 "github.com/lance/js-function-operator/pkg/apis/faas/v1alpha1"
10+
faasv1alpha1 "github.com/openshift-cloud-functions/js-function-operator/pkg/apis/faas/v1alpha1"
1111

1212
corev1 "k8s.io/api/core/v1"
1313
"k8s.io/apimachinery/pkg/api/errors"
@@ -104,7 +104,24 @@ func (r *ReconcileJSFunction) Reconcile(request reconcile.Request) (reconcile.Re
104104
err = r.client.Get(context.TODO(), types.NamespacedName{Name: function.Name, Namespace: function.Namespace}, found)
105105
if err != nil && errors.IsNotFound(err) {
106106
// No service for this function exists. Create a new one
107-
service := r.serviceForFunction(function)
107+
108+
// Create configmap first
109+
configMap, err := r.configMapWithFunction(function)
110+
if err != nil {
111+
return reconcile.Result{}, err
112+
}
113+
err = r.client.Create(context.TODO(), configMap)
114+
if err != nil {
115+
reqLogger.Error(err, "Failed to create new ConfigMap holding function.", "Service.Namespace", configMap.Namespace, "ConfigMap.Name", configMap.Name)
116+
return reconcile.Result{}, err
117+
}
118+
119+
// Create service, mounting the config map
120+
service, err := r.serviceForFunction(function, configMap.Name)
121+
if err != nil {
122+
return reconcile.Result{}, err
123+
}
124+
108125
reqLogger.Info("Creating a new knative Service", "Service.Namespace", service.Namespace, "Service.Name", service.Name)
109126
err = r.client.Create(context.TODO(), service)
110127
if err != nil {
@@ -126,35 +143,23 @@ func (r *ReconcileJSFunction) Reconcile(request reconcile.Request) (reconcile.Re
126143
return reconcile.Result{}, nil
127144
}
128145

129-
func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction) *knv1alpha1.Service {
146+
func (r *ReconcileJSFunction) configMapWithFunction(f *faasv1alpha1.JSFunction) (*corev1.ConfigMap, error) {
130147
// Create a config map containing the user code
131148
configMap := &corev1.ConfigMap{
132149
ObjectMeta: metav1.ObjectMeta{
133150
Name: f.Name,
151+
Namespace: f.Namespace,
134152
},
135153
Data: map[string]string{"index.js": f.Spec.Func},
136154
}
137155
if err := controllerutil.SetControllerReference(f, configMap, r.scheme); err != nil {
138-
log.Error(err, "Failed to set controller reference for function ConfigMap")
139-
}
140-
141-
volumeName := strings.Join([]string{f.Name, "source"}, "-")
142-
var _ = &corev1.Volume{
143-
Name: volumeName,
144-
VolumeSource: corev1.VolumeSource{
145-
ConfigMap: &corev1.ConfigMapVolumeSource{
146-
LocalObjectReference: corev1.LocalObjectReference{
147-
Name: configMap.Name,
148-
},
149-
},
150-
},
156+
return nil, err
151157
}
158+
return configMap, nil
159+
}
152160

161+
func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction, configMapName string) (*knv1alpha1.Service, error) {
153162
service := &knv1alpha1.Service{
154-
TypeMeta: metav1.TypeMeta{
155-
APIVersion: "serving.knative.dev/v1beta1",
156-
Kind: "Service",
157-
},
158163
ObjectMeta: metav1.ObjectMeta{
159164
Name: f.Name,
160165
Namespace: f.Namespace,
@@ -167,21 +172,7 @@ func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction) *kn
167172
},
168173
Spec: knv1alpha1.RevisionSpec{
169174
RevisionSpec: knv1beta1.RevisionSpec{
170-
PodSpec: corev1.PodSpec{
171-
Containers: []corev1.Container{{
172-
Image: "quay.io/lanceball/js-runtime",
173-
Name: strings.Join([]string{"nodejs", f.Name}, "-"),
174-
Ports: []corev1.ContainerPort{{
175-
ContainerPort: 8080,
176-
}},
177-
VolumeMounts: []corev1.VolumeMount{
178-
corev1.VolumeMount{
179-
Name: volumeName,
180-
MountPath: "/home/node/usr",
181-
},
182-
},
183-
}},
184-
},
175+
PodSpec: createPodSpec(f.Name, configMapName),
185176
},
186177
},
187178
},
@@ -192,8 +183,43 @@ func (r *ReconcileJSFunction) serviceForFunction(f *faasv1alpha1.JSFunction) *kn
192183

193184
// Set JSFunction instance as the owner and controller
194185
if err := controllerutil.SetControllerReference(f, service, r.scheme); err != nil {
195-
log.Error(err, "Failed to set controller reference for function Service")
186+
return nil, err
196187
}
197188

198-
return service
189+
return service, nil
190+
}
191+
192+
func createPodSpec(functionName, configMapName string) corev1.PodSpec {
193+
volumeName := fmt.Sprintf("%s-source", functionName)
194+
return corev1.PodSpec{
195+
Containers: []corev1.Container{{
196+
Image: "docker.io/rhuss/js-runtime",
197+
Name: fmt.Sprintf("nodejs-%s", functionName),
198+
Ports: []corev1.ContainerPort{{
199+
ContainerPort: 8181,
200+
}},
201+
VolumeMounts: []corev1.VolumeMount{
202+
{
203+
Name: volumeName,
204+
MountPath: "/home/node/usr",
205+
},
206+
},
207+
}},
208+
Volumes: []corev1.Volume{
209+
createConfigMapVolume(volumeName, configMapName),
210+
},
211+
}
212+
}
213+
214+
func createConfigMapVolume(volumeName, configMapName string) corev1.Volume {
215+
return corev1.Volume {
216+
Name: volumeName,
217+
VolumeSource: corev1.VolumeSource {
218+
ConfigMap: &corev1.ConfigMapVolumeSource{
219+
LocalObjectReference: corev1.LocalObjectReference{
220+
Name: configMapName,
221+
},
222+
},
223+
},
224+
}
199225
}

0 commit comments

Comments
 (0)