Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/sources/gcppubsub/gcppubsub-serviceentry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2018 The Knative Authors
#
# 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
#
# http://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: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: gcppubsub-bus-receive-adapter-ext
spec:
hosts:
- "*.googleapis.com"
- "accounts.google.com"
ports:
- number: 443
name: https
protocol: HTTPS
location: MESH_EXTERNAL
8 changes: 3 additions & 5 deletions pkg/sources/gcppubsub/watcher_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ func MakeWatcherDeployment(namespace string, deploymentName string, serviceAccou
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
// Inject Istio so any connection made from the receive adapter
// goes through and is enforced by Istio. Currently for some
// reason turning this on means that the container can not
// reach GCP so leaving this false for now.
Annotations: map[string]string{sidecarIstioInjectAnnotation: "false"},
// goes through and is enforced by Istio.
Annotations: map[string]string{sidecarIstioInjectAnnotation: "true"},
},
Spec: corev1.PodSpec{
ServiceAccountName: serviceAccount,
Containers: []corev1.Container{
corev1.Container{
Name: deploymentName,
Name: "receive-adapter",
Image: image,
ImagePullPolicy: "Always",
Env: []corev1.EnvVar{
Expand Down
88 changes: 33 additions & 55 deletions sample/gcp_pubsub_function/README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
# gcp_pubsub_function

A simple function that receives Google Cloud Pub Sub events and prints out the
data field after decoding from base64 encoding. The function uses a Knative
route as an endpoint. This is also example of where we make use of a Receive
data field after decoding from base64 encoding. The Flow object creates underlying
primitive resources underneath: Feed, Channel and Subscription and wires them
together.
This is also example of where we make use of a Receive
Adapter that runs in the context of the namespace where the feed is created.
Since we wanted to demonstrate pull events, we create a deployment that
attaches to the specified GCP topic and then forwards them to the destination.

## Prerequisites

1. [Setup your development environment](../../DEVELOPMENT.md#getting-started)
1. [Start Knative](../../README.md#start-knative)
1. Decide on the DNS name that git can then call. Update knative/serving/config-domain.yaml domainSuffix.
For example I used aikas.org as my hostname, so my config-domain.yaml looks like so:

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: config-domain
namespace: knative-serving
data:
aikas.org: |
```

If you were already running the Knative controllers, you will need to
re-apply the configmap.

1. Install GCP Pub Sub as an event source
2. [Start Knative Serving](../../README.md#start-knative)
3. Install GCP Pub Sub as an event source

```shell
ko apply -f pkg/sources/gcppubsub/
```

1. Create a GCP Pub Sub topic
4. Create a GCP Pub Sub topic

```shell
gcloud pubsub topics create knative-demo
```

5. Create a cluster bus. You **must** change the Kind to ClusterBus in stub-sub.yaml file.

```
ko apply -f config/buses/stub/stub-bus.yaml
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

starting tomorrow, we'll have releases for the stub clusterbus

https://storage.googleapis.com/knative-releases/latest/release-eventing-cluterbus-stub.yaml

```



## Creating a Service Account

Because the Receive Adapter needs to run a deployment, you need to specify what
Service Account should be used in the target namespace for running the Receive
Adapter. Feed.Spec has a field that allows you to specify this. By default it
Adapter. FLow.Spec has a field that allows you to specify this. By default it
uses "default" for feed which typically has no privileges, but this feed
requires standing up a deployment, so you need to either use an existing
Service Account with appropriate privileges or create a new one. This example
Expand Down Expand Up @@ -78,48 +70,31 @@ kubectl get configurations gcp-pubsub-function -o yaml
# This will show the Revision that was created by our configuration:
kubectl get revisions -l serving.knative.dev/configuration=gcp-pubsub-function -o yaml

# This will show the available EventSources that you can generate a feed from:
# This will show the available EventSources that you can generate a flow from:
kubectl get eventsources -o yaml

# This will show the available EventTypes that you can generate a feed from:
# This will show the available EventTypes that you can generate a flow from:
kubectl get eventtypes -o yaml
```

To make this service accessible to GCP, we first need to determine its ingress
address (might have to wait a little while until 'EXTERNAL-IP' gets assigned):

```shell
kubectl get svc knative-ingressgateway -n istio-system

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
knative-ingressgateway LoadBalancer 10.23.247.74 35.203.155.229 80:32380/TCP,443:32390/TCP,32400:32400/TCP 2d
```

Once the `EXTERNAL-IP` gets assigned to the cluster, you need to assign a DNS name
for that IP address. This DNS address needs to be:
`gcp-pubsub-function.default.<domainsuffix you created>` so for me, I would
create a DNS entry from: `gcp-pubsub-function.default.aikas.org` pointing to
`35.203.155.229`. See also, [Using GCP
DNS](https://support.google.com/domains/answer/3290350).

So, you'd need to create an A record for
`gcp-pubsub-function.default.aikas.org` pointing to `35.203.155.229`.
## Creating a Flow

To now send events to the `gcp_pubsub_function` for GCP PubSub messages with the function
we created above, you need to create a Feed object. Modify
`sample/gcp_pubsub_function/feed.yaml` to specify the topic and project id you
we created above, you need to create a Flow object. Modify
`sample/gcp_pubsub_function/flow.yaml` to specify the topic and project id you
want.

For example, if I wanted to receive notifications to: project:
`quantum-reducer-434` topic: `knative-demo`, my Feed object would look like the
`quantum-reducer-434` topic: `knative-demo`, my flow object would look like the
one below.

You can also specify a different Service Account to use for the feed / receive
You can also specify a different Service Account to use for the flow / receive
watcher by changing the spec.serviceAccountName to something else.

```yaml
apiVersion: feeds.knative.dev/v1alpha1
kind: Feed
apiVersion: flows.knative.dev/v1alpha1
kind: Flow
metadata:
name: gcppubsub-example
namespace: default
Expand All @@ -133,13 +108,16 @@ spec:
projectID: quantum-reducer-434
topic: knative-demo
action:
routeName: gcp-pubsub-function
target:
kind: Route
apiVersion: serving.knative.dev/v1alpha1
name: gcp-pubsub-function
```

Then create the feed so that you can see changes:
Then create the flow so that you can see changes:

```shell
ko apply -f sample/gcp_pubsub_function/feed.yaml
ko apply -f sample/gcp_pubsub_function/flow.yaml
```


Expand Down Expand Up @@ -190,13 +168,13 @@ $ kubectl logs gcp-pubsub-function-00001-deployment-68864b8c7d-rgx2w user-contai
2018/05/22 19:16:59 Received data: "test message"
```

## Removing a feed
## Removing a flow

Remove the feed and things get cleaned up (including removing the
Remove the flow and things get cleaned up (including removing the
subscription to GCP PubSub):

```shell
kubectl delete feeds gcppubsub-example
kubectl delete flows gcppubsub-example
```

And now your subscription from above has been removed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: feeds.knative.dev/v1alpha1
kind: Feed
apiVersion: flows.knative.dev/v1alpha1
kind: Flow
metadata:
name: gcppubsub-example
namespace: default
Expand All @@ -27,4 +27,7 @@ spec:
projectID: quantum-reducer-434
topic: knative-demo
action:
routeName: gcp-pubsub-function
target:
kind: Route
apiVersion: serving.knative.dev/v1alpha1
name: gcp-pubsub-function