* Intial work porting the old event-flow to the new object model. * hack/update-deps.sh * Minor typos * Update the function to point at the correct topic. |
||
---|---|---|
.. | ||
generator | ||
README.md | ||
channel.yaml | ||
gcp-pubsub-source.yaml | ||
subscription.yaml |
README.md
Sample: Binding running services to an IoT core
This sample shows how to bind a running service to an IoT core using GCP PubSub as the event source. With minor modifications, it can be used to bind a running service to anything that sends events via GCP PubSub.
Note: All commands are given relative to the root of this repository.
Deployment Steps
Environment Variables
To make the following commands easier, we are going to set the various variables here and use them later.
Variables you must Change
export IOTCORE_PROJECT="s9-demo"
Variables you may Change
export CHANNEL_NAME="iot-demo"
export IOTCORE_REGISTRY="iot-demo"
export IOTCORE_DEVICE="iot-demo-client"
export IOTCORE_REGION="us-central1"
export IOTCORE_TOPIC_DATA="iot-demo-pubsub-topic"
export IOTCORE_TOPIC_DEVICE="iot-demo-device-pubsub-topic"
Prerequisites
Local
- Clone Knative Eventing Sources locally.
Kubernetes
- Have a running Kubernetes cluster with
kubectl
pointing at it.
GCP
-
Create a Google Cloud Project.
-
Have gcloud installed and pointing at that project.
-
Enable the 'Cloud Pub/Sub API' on that project.
gcloud services enable pubsub.googleapis.com
-
Create the two GCP PubSub
topic
s.gcloud pubsub topics create $IOTCORE_TOPIC_DATA gcloud pubsub topics create $IOTCORE_TOPIC_DEVICE
-
Setup Knative Eventing.
-
Install the in-memory
ClusterChannelProvisioner
.- Note that you can skip this if you choose to use a different type of
Channel
. If so, you will need to modifychannel.yaml
before deploying it.
- Note that you can skip this if you choose to use a different type of
GCP PubSub Source
-
Create a GCP Service Account.
-
Determine the Service Account to use, or create a new one.
-
Give that Service Account the 'Pub/Sub Editor' role on your GCP project.
-
Download a new JSON private key for that Service Account.
-
Create two secrets with the downloaded key (one for the Source, one for the Receive Adapter):
kubectl -n knative-sources create secret generic gcppubsub-source-key --from-file=key.json=PATH_TO_KEY_FILE.json kubectl -n default create secret generic google-cloud-key --from-file=key.json=PATH_TO_KEY_FILE.json
-
-
Deploy the
GcpPubSubSource
controller as part of eventing-source's controller.pushd $HOME/go/src/github.com/knative/eventing-sources ko apply -f config/default-gcppubsub.yaml popd
Deploying
Channel
-
Create a
Channel
.sed "s/CHANNEL_NAME/$CHANNEL_NAME/" eventing/samples/iot-core/channel.yaml | kubectl -n default apply -f -
GCP PubSub Source
-
Deploy
gcp-pubsub-source.yaml
.sed -e "s/MY_GCP_PROJECT/$IOTCORE_PROJECT/" \ -e "s/TOPIC_NAME/$IOTCORE_TOPIC_DATA/" \ -e "s/CHANNEL_NAME/$CHANNEL_NAME/" \ eventing/samples/iot-core/gcp-pubsub-source.yaml | kubectl apply -f -
Subscription
Even though the Source
isn't completely ready yet, we can setup the
Subscription
for all events coming out of it.
-
Deploy
subscription.yaml
.sed "s/CHANNEL_NAME/$CHANNEL_NAME/" \ eventing/samples/iot-core/subscription.yaml | ko apply -f -
- This uses a very simple Knative Service to see that events are flowing. Feel free to replace it.
IoT Core
We now have everything setup on the Knative side. We will now setup the IoT Core.
-
Create a device registry:
gcloud iot registries create $IOTCORE_REGISTRY \ --project=$IOTCORE_PROJECT \ --region=$IOTCORE_REGION \ --event-notification-config=topic=$IOTCORE_TOPIC_DATA \ --state-pubsub-topic=$IOTCORE_TOPIC_DEVICE
-
Create the certificates.
openssl req -x509 -nodes -newkey rsa:2048 \ -keyout device.key.pem \ -out device.crt.pem \ -days 365 \ -subj "/CN=unused" curl https://pki.google.com/roots.pem > ./root-ca.pem
-
Register a device using the generated certificates.
gcloud iot devices create $IOTCORE_DEVICE \ --project=$IOTCORE_PROJECT \ --region=$IOTCORE_REGION \ --registry=$IOTCORE_REGISTRY \ --public-key path=./device.crt.pem,type=rsa-x509-pem
Running
We now have everything installed and ready to go. We will generate events and see them in the subscriber.
-
Setup
kail
to tail the logs of the subscriber.kail -d message-dumper -c user-container
-
In a separate terminal, run the following program to generate events.
go run github.com/knative/docs/eventing/samples/iot-core/generator \ -project $IOTCORE_PROJECT \ -region $IOTCORE_REGION \ -registry $IOTCORE_REGISTRY \ -device $IOTCORE_DEVICE \ -ca "$PWD/root-ca.pem" \ -key "$PWD/device.key.pem" \ -src "iot-core demo" \ -events 10