diff --git a/config/nav.yml b/config/nav.yml index 73408f4ff..cb2cafe37 100644 --- a/config/nav.yml +++ b/config/nav.yml @@ -222,7 +222,6 @@ nav: - CloudStorageSource: eventing/samples/cloud-storage-source/README.md - GitHub source: eventing/samples/github-source/README.md - GitLab source: eventing/samples/gitlab-source/README.md - - IoT core: eventing/samples/iot-core/README.md # Reference docs - Reference: - Serving: reference/api/serving-api.md diff --git a/docs/eventing/samples/iot-core/README.md b/docs/eventing/samples/iot-core/README.md deleted file mode 100644 index 0a2850305..000000000 --- a/docs/eventing/samples/iot-core/README.md +++ /dev/null @@ -1,226 +0,0 @@ -# Binding running services to an IoT core - -This sample shows how to bind a running service to an -[IoT core](https://cloud.google.com/iot-core/) using -[GCP PubSub](https://cloud.google.com/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 - -```bash -export IOTCORE_PROJECT="s9-demo" -``` - -#### Variables you may Change - -```bash -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 - -#### Kubernetes - -1. Have a running Kubernetes cluster with `kubectl` pointing at it. - -#### GCP - -1. Create a - [Google Cloud Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects). - -1. Have [gcloud](https://cloud.google.com/sdk/gcloud/) installed and pointing - at that project. - -1. Enable the `Cloud Pub/Sub API` on that project. - - ```bash - gcloud services enable pubsub.googleapis.com - ``` - -1. Create the two GCP PubSub `topic`s. - - ```bash - gcloud pubsub topics create $IOTCORE_TOPIC_DATA - gcloud pubsub topics create $IOTCORE_TOPIC_DEVICE - ``` - -1. Set up [Knative Eventing](../../../eventing/README.md). - -#### GCP PubSub Source - -1. Create a GCP - [Service Account](https://console.cloud.google.com/iam-admin/serviceaccounts/project). - - 1. Determine the Service Account to use, or create a new one. - 1. Give that Service Account the 'Pub/Sub Editor' role on your GCP project. - 1. Download a new JSON private key for that Service Account. - 1. Create two secrets with the downloaded key (one for the Source, one for - the Receive Adapter): - - ```bash - kubectl --namespace knative-sources create secret generic gcppubsub-source-key --from-file=key.json=PATH_TO_KEY_FILE.json - kubectl --namespace default create secret generic google-cloud-key --from-file=key.json=PATH_TO_KEY_FILE.json - ``` - -1. Deploy the `GcpPubSubSource` controller as part of eventing-source's - controller. - - ```bash - kubectl apply --filename https://github.com/knative/eventing-contrib/releases/download/v0.8.2/gcppubsub.yaml - ``` - -### Deploying - -#### Broker - -To install the default Broker: - -1. Copy the following YAML into a file: - - ```yaml - apiVersion: eventing.knative.dev/v1 - kind: Broker - metadata: - name: default - ``` - -1. Apply the YAML file by running the command: - - ```bash - kubectl apply -f .yaml - ``` - Where `` is the name of the file you created in the previous step. - - -#### GCP PubSub Source - -1. Deploy `gcp-pubsub-source.yaml`. - - ```bash - sed -e "s/PROJECT_ID/$IOTCORE_PROJECT/" \ - -e "s/TOPIC_NAME/$IOTCORE_TOPIC_DATA/" \ - docs/eventing/samples/iot-core/gcp-pubsub-source.yaml | - kubectl apply --filename - - ``` - -#### Trigger - -Even though the `Source` isn't completely ready yet, we can setup the `Trigger` -for all events coming out of it. - -1. Deploy `trigger.yaml`. - - ```bash - kubectl apply -f docs/eventing/samples/iot-core/trigger.yaml - ``` - - - 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. - -1. Create a device registry: - - ```bash - 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 - ``` - -1. Create the certificates. - - ```bash - 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 - ``` - -1. Register a device using the generated certificates. - - ```bash - 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. - -1. Run the following program to generate events: - - ```bash - go run github.com/knative/docs/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 - ``` - -1. Inspect the logs of the subscriber: - - ```bash - kubectl logs --selector serving.knative.dev/service=event-display -c user-container - ``` - - You should see something along the similar to: - - ```{ .bash .no-copy } - {"ID":"481014114648052","Data":"eyJzb3VyY2VfaWQiOiJpb3QtY29yZSBkZW1vIiwiZXZlbnRfaWQiOiJlaWQtMzI3MjJiMzItZWU5Mi00YzZlLWEzOTgtNDlmYjRkYWYyNGE1IiwiZXZlbnRfdHMiOjE1NTM3MTczOTYsIm1ldHJpYyI6MC4xMzY1MjI5OH0=","Attributes":{"deviceId":"iot-demo-client","deviceNumId":"2754785852315736","deviceRegistryId":"iot-demo","deviceRegistryLocation":"us-central1","projectId":"s9-demo","subFolder":""},"PublishTime":"2019-03-27T20:09:56.685Z"} - ``` - -### Cleanup - -To cleanup the knative resources: - -1. Remove the `GcpPubSubSource`: - - ```bash - sed -e "s/PROJECT_ID/$IOTCORE_PROJECT/" \ - -e "s/TOPIC_NAME/$IOTCORE_TOPIC_DATA/" \ - docs/eventing/samples/iot-core/gcp-pubsub-source.yaml | - kubectl delete --filename - - ``` - -1. Remove the Trigger: - - ```bash - kubectl delete --filename docs/eventing/samples/iot-core/trigger.yaml - ``` - -1. Remove the `GcpPubSubSource` controller: - - ```bash - kubectl delete --filename https://github.com/knative/eventing-contrib/releases/download/v0.8.2/gcppubsub.yaml - ``` diff --git a/docs/eventing/samples/iot-core/gcp-pubsub-source.yaml b/docs/eventing/samples/iot-core/gcp-pubsub-source.yaml deleted file mode 100644 index ed29e9190..000000000 --- a/docs/eventing/samples/iot-core/gcp-pubsub-source.yaml +++ /dev/null @@ -1,18 +0,0 @@ -# Replace the following before applying this file: -# TOPIC_NAME: Replace with the GCP PubSub Topic name. -# MY_GCP_PROJECT: Replace with the GCP Project's ID. - -apiVersion: sources.knative.dev/v1alpha1 -kind: GcpPubSubSource -metadata: - name: TOPIC_NAME-source -spec: - gcpCredsSecret: - name: google-cloud-key - key: key.json - googleCloudProject: PROJECT_ID - topic: TOPIC_NAME - sink: - apiVersion: eventing.knative.dev/v1 - kind: Broker - name: default diff --git a/docs/eventing/samples/iot-core/generator/main.go b/docs/eventing/samples/iot-core/generator/main.go deleted file mode 100644 index d0cb64f2b..000000000 --- a/docs/eventing/samples/iot-core/generator/main.go +++ /dev/null @@ -1,170 +0,0 @@ -/* -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. -*/ - -package main - -import ( - "crypto/tls" - "crypto/x509" - "encoding/json" - "flag" - "fmt" - "io/ioutil" - "log" - "math/rand" - "time" - - MQTT "github.com/eclipse/paho.mqtt.golang" - jwt "github.com/form3tech-oss/jwt-go" - uuid "github.com/google/uuid" -) - -const ( - host = "mqtt.googleapis.com" - port = "8883" - idPrefix = "eid" -) - -var ( - deviceID = flag.String("device", "", "Cloud IoT Core Device ID") - projectID = flag.String("project", "", "GCP Project ID") - registryID = flag.String("registry", "", "Cloud IoT Registry ID (short form)") - region = flag.String("region", "us-central1", "GCP Region") - numEvents = flag.Int("events", 10, "Number of events to sent") - eventSrc = flag.String("src", "", "Event source") - certsCA = flag.String("ca", "root-ca.pem", "Download https://pki.google.com/roots.pem") - privateKey = flag.String("key", "", "Path to private key file") -) - -func main() { - flag.Parse() - - log.Println("Loading Google's roots...") - certpool := x509.NewCertPool() - pemCerts, err := ioutil.ReadFile(*certsCA) - if err == nil { - certpool.AppendCertsFromPEM(pemCerts) - } - - config := &tls.Config{ - RootCAs: certpool, - ClientAuth: tls.NoClientCert, - ClientCAs: nil, - InsecureSkipVerify: true, - Certificates: []tls.Certificate{}, - MinVersion: tls.VersionTLS12, - } - - clientID := fmt.Sprintf("projects/%v/locations/%v/registries/%v/devices/%v", - *projectID, - *region, - *registryID, - *deviceID, - ) - - log.Println("Creating MQTT client options...") - opts := MQTT.NewClientOptions() - - broker := fmt.Sprintf("ssl://%v:%v", host, port) - log.Printf("Broker '%v'", broker) - - opts.AddBroker(broker) - opts.SetClientID(clientID).SetTLSConfig(config) - opts.SetUsername("unused") - - token := jwt.New(jwt.SigningMethodRS256) - token.Claims = jwt.StandardClaims{ - Audience: []string{*projectID}, - IssuedAt: time.Now().Unix(), - ExpiresAt: time.Now().Add(24 * time.Hour).Unix(), - } - - log.Println("Loading private key...") - keyBytes, err := ioutil.ReadFile(*privateKey) - if err != nil { - log.Fatal(err) - } - - log.Println("Parsing private key...") - key, err := jwt.ParseRSAPrivateKeyFromPEM(keyBytes) - if err != nil { - log.Fatal(err) - } - - log.Println("Signing token") - tokenString, err := token.SignedString(key) - if err != nil { - log.Fatal(err) - } - - log.Println("Setting password...") - opts.SetPassword(tokenString) - - opts.SetDefaultPublishHandler(func(client MQTT.Client, msg MQTT.Message) { - fmt.Printf("[handler] Topic: %v\n", msg.Topic()) - fmt.Printf("[handler] Payload: %v\n", msg.Payload()) - }) - - log.Println("Connecting...") - client := MQTT.NewClient(opts) - if token := client.Connect(); token.Wait() && token.Error() != nil { - log.Fatal(token.Error()) - } - - topic := fmt.Sprintf("/devices/%s/events", *deviceID) - log.Println("Publishing messages...") - for i := 0; i < *numEvents; i++ { - data := makeEvent() - log.Printf("Publishing to topic '%s': %v", topic, data) - token := client.Publish( - topic, - 0, - false, - data) - token.WaitTimeout(5 * time.Second) - if token.Error() != nil { - log.Printf("Error publishing: %s", token.Error()) - } - } - - log.Println("Disconnecting...") - client.Disconnect(250) - - log.Println("Done") -} - -func makeEvent() string { - - s1 := rand.NewSource(time.Now().UnixNano()) - r1 := rand.New(s1) - - event := struct { - SourceID string `json:"source_id"` - EventID string `json:"event_id"` - EventTs int64 `json:"event_ts"` - Metric float32 `json:"metric"` - }{ - SourceID: *eventSrc, - EventID: fmt.Sprintf("%s-%s", idPrefix, uuid.New().String()), - EventTs: time.Now().UTC().Unix(), - Metric: r1.Float32(), - } - - data, _ := json.Marshal(event) - - return string(data) - -} diff --git a/docs/eventing/samples/iot-core/trigger.yaml b/docs/eventing/samples/iot-core/trigger.yaml deleted file mode 100644 index cc72c3c76..000000000 --- a/docs/eventing/samples/iot-core/trigger.yaml +++ /dev/null @@ -1,26 +0,0 @@ -apiVersion: eventing.knative.dev/v1 -kind: Trigger -metadata: - name: iot-demo - namespace: default -spec: - broker: default - subscriber: - ref: - apiVersion: serving.knative.dev/v1 - kind: Service - name: event-display - ---- -# This is a very simple Knative Service that writes the input request to its log. - -apiVersion: serving.knative.dev/v1 -kind: Service -metadata: - name: event-display - namespace: default -spec: - template: - spec: - containers: - image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display