Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
Function's "public" param added
Browse files Browse the repository at this point in the history
  • Loading branch information
tzununbekov committed Mar 25, 2021
1 parent 6408ead commit a32aa4e
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 63 deletions.
2 changes: 1 addition & 1 deletion config/200-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ metadata:
duck.knative.dev/addressable: "true"
rules:
- apiGroups:
- routing.triggermesh.io
- flow.triggermesh.io
resources:
- functions
- functions/status
Expand Down
3 changes: 3 additions & 0 deletions config/300-function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ spec:
entrypoint:
description: 'Function name to use as an entrypoint.'
type: string
public:
description: 'Should the function be publicly available.'
type: bool
sink:
description: 'Sink is a reference to an object that will resolve to
a uri to use as the sink.'
Expand Down
61 changes: 61 additions & 0 deletions config/samples/nodejs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2021 Triggermesh Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: sources.knative.dev/v1alpha2
kind: PingSource
metadata:
name: ping-source
spec:
schedule: "*/1 * * * *"
jsonData: '{"name":""}'
sink:
ref:
apiVersion: flow.triggermesh.io/v1alpha1
kind: Function
name: inline-node-function

---

apiVersion: flow.triggermesh.io/v1alpha1
kind: Function
metadata:
name: inline-node-function
spec:
runtime: node
entrypoint: transformName
code: |
module.exports.transformName = async (event) => {
if (event.name == "") {
event.name = "placeholder";
}
return event;
};
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: sockeye

---

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: sockeye
spec:
template:
spec:
containers:
- image: docker.io/n3wscott/sockeye:v0.7.0
name: user-container
71 changes: 15 additions & 56 deletions function.yaml → config/samples/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: sockeye
spec:
template:
spec:
containers:
- image: docker.io/n3wscott/sockeye:v0.7.0
name: user-container

---

apiVersion: flow.triggermesh.io/v1alpha1
kind: Function
metadata:
name: inline-python-function
spec:
runtime: python
public: true
entrypoint: foo
code: |
import urllib.request
Expand All @@ -36,59 +50,4 @@ spec:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: sockeye

---

apiVersion: flow.triggermesh.io/v1alpha1
kind: Function
metadata:
name: inline-node-function
spec:
runtime: node
entrypoint: sayHelloAsync
code: |
async function justWait() {
return new Promise((resolve, reject) => setTimeout(resolve, 100));
}
module.exports.sayHelloAsync = async (event) => {
if (event.name == "") {
event.name = "placeholder";
}
await justWait();
return event;
};
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: sockeye

---

apiVersion: flow.triggermesh.io/v1alpha1
kind: Function
metadata:
name: inline-ruby-function
spec:
runtime: ruby
entrypoint: endpoint
code: |
def endpoint(event:, context:)
hash = {date: Time.new}
{ statusCode: 200, body: JSON.generate(hash) }
end
---

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: sockeye
spec:
template:
spec:
containers:
- image: docker.io/n3wscott/sockeye:v0.7.0
name: user-container
name: sockeye
27 changes: 27 additions & 0 deletions config/samples/ruby.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2021 Triggermesh Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: flow.triggermesh.io/v1alpha1
kind: Function
metadata:
name: inline-ruby-function
spec:
runtime: ruby
public: true
entrypoint: endpoint
code: |
def endpoint(event:, context:)
hash = {date: Time.new}
{ statusCode: 200, body: JSON.generate(hash) }
end
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
k8s.io/api v0.19.7
k8s.io/apimachinery v0.20.5
k8s.io/client-go v0.19.7
knative.dev/pkg v0.0.0-20210318052054-dfeeb1817679
knative.dev/networking v0.0.0-20210324061918-44a3b919bce1
knative.dev/pkg v0.0.0-20210323202917-b558677ab034
knative.dev/serving v0.21.0
)
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1190,12 +1190,13 @@ k8s.io/utils v0.0.0-20210111153108-fddb29f9d009/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
knative.dev/caching v0.0.0-20210215030244-1212288570f0/go.mod h1:rAPalJe9Lx3jHffJpackk5WjZYl3j2QvXUgw0GPllxQ=
knative.dev/hack v0.0.0-20210203173706-8368e1f6eacf/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/hack v0.0.0-20210317214554-58edbdc42966/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/networking v0.0.0-20210215030235-088986a1c2a3 h1:viMEi8ob8FdeiIsiv0ww6eKgamS+YkTP3+u95qF4n/s=
knative.dev/networking v0.0.0-20210215030235-088986a1c2a3/go.mod h1:pmAMQjMqQUxpK0UyjE71KljMs6rwDMVIAlvrZsU3I6Y=
knative.dev/networking v0.0.0-20210324061918-44a3b919bce1 h1:+ehpTEwj++yNfWGVk8uF2CMWcteoi4dEdcTOAwz3/oU=
knative.dev/networking v0.0.0-20210324061918-44a3b919bce1/go.mod h1:MdAWrWH1pktqcuc+hDzzLcvksUpNzpo9SscJte0WtnE=
knative.dev/pkg v0.0.0-20210212203835-448ae657fb5f/go.mod h1:TJSdebQOWX5N2bszohOYVi0H1QtXbtlYLuMghAFBMhY=
knative.dev/pkg v0.0.0-20210215165523-84c98f3c3e7a/go.mod h1:TJSdebQOWX5N2bszohOYVi0H1QtXbtlYLuMghAFBMhY=
knative.dev/pkg v0.0.0-20210318052054-dfeeb1817679 h1:5yTTe0oW2/a13DU2b4U42h7lSHZLWy32a/8DEkkH7C4=
knative.dev/pkg v0.0.0-20210318052054-dfeeb1817679/go.mod h1:o1aTlbc+h60DP9P6nd/WmP99Ny9Ls3U/JIjL4xMgqpw=
knative.dev/pkg v0.0.0-20210323202917-b558677ab034 h1:Uj1gJL+G6cEInJ7dbCSrtxAhtxoLLjIjRCA7xeQyUrQ=
knative.dev/pkg v0.0.0-20210323202917-b558677ab034/go.mod h1:o1aTlbc+h60DP9P6nd/WmP99Ny9Ls3U/JIjL4xMgqpw=
knative.dev/serving v0.21.0 h1:bdg0JqNovzlMQB2IxXU/iJKF2zPZH6/yXiwiUtr7vtY=
knative.dev/serving v0.21.0/go.mod h1:PU9k1Y6YMG27XQldEu5agNkcebvSafUXKXPircQYCsE=
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/function/v1alpha1/function_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var (
type FunctionSpec struct {
Runtime string `json:"runtime"`
Entrypoint string `json:"entrypoint"`
Public bool `json:"public"`
Code string `json:"code"`
Sink *duckv1.Destination `json:"sink"`
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/reconciler/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, o *functionv1alpha1.Func

o.Status.Address = &duckv1.Addressable{
URL: &apis.URL{
Scheme: ksvc.Status.Address.URL.Scheme,
Host: ksvc.Status.Address.URL.Host,
Scheme: ksvc.Status.URL.Scheme,
Host: ksvc.Status.URL.Host,
},
}
o.Status.MarkServiceAvailable()
Expand Down Expand Up @@ -207,6 +207,7 @@ func (r *Reconciler) reconcileKnService(ctx context.Context, f *functionv1alpha1
resources.KnSvcEnvVar("CE_TYPE", eventType),
resources.KnSvcEnvVar("CE_SOURCE", f.SelfLink),
resources.KnSvcEnvVar("CE_SUBJECT", handler),
resources.KnSvcVisibility(f.Spec.Public),
resources.KnSvcLabel(map[string]string{labelKey: f.Name}),
resources.KnSvcOwner(f),
)
Expand Down
24 changes: 24 additions & 0 deletions pkg/reconciler/function/resources/knservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

network "knative.dev/networking/pkg"
"knative.dev/pkg/kmeta"
"knative.dev/serving/pkg/apis/serving"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)

Expand Down Expand Up @@ -89,6 +91,12 @@ func KnSvcOwner(o kmeta.OwnerRefable) knSvcOption {

func KnSvcLabel(labels map[string]string) knSvcOption {
return func(svc *servingv1.Service) {
if svc.Labels != nil {
for k, v := range labels {
svc.Labels[k] = v
}
return
}
svc.SetLabels(labels)
}
}
Expand Down Expand Up @@ -129,6 +137,22 @@ func KnSvcEntrypoint(command string) knSvcOption {
}
}

func KnSvcVisibility(public bool) knSvcOption {
return func(svc *servingv1.Service) {
if public {
return
}
if svc.Labels != nil {
svc.Labels[network.VisibilityLabelKey] = serving.VisibilityClusterLocal
return
}
labels := map[string]string{
network.VisibilityLabelKey: serving.VisibilityClusterLocal,
}
svc.Labels = labels
}
}

func envVarsFrom(svc *servingv1.Service) *[]corev1.EnvVar {
return &firstContainer(svc).Env
}
Expand Down

0 comments on commit a32aa4e

Please sign in to comment.