Skip to content
Closed
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
2 changes: 1 addition & 1 deletion config/buses/stub/stub-bus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: channels.knative.dev/v1alpha1
kind: Bus
kind: ClusterBus
metadata:
name: stub
spec:
Expand Down
3 changes: 1 addition & 2 deletions sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ functionality.

* [Github Pull Request Handler](./github) - A simple handler for Github Pull Requests
* [GCP PubSub Receiver Handler](./gcp_pubsub_function) - A simple handler for processing GCP PubSub events
* [K8S events Handler](./k8s_events_function) - A simple handler for processing k8s events in the cluster
* [K8S events Handler using Flow](./flow) - Example using Flow object that wires k8s events through the bus
* [K8S events Handler](./k8s_events_function) - A simple handler for processing k8s events in the cluster
57 changes: 0 additions & 57 deletions sample/flow/README.md

This file was deleted.

21 changes: 13 additions & 8 deletions sample/gcp_pubsub_function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ attaches to the specified GCP topic and then forwards them to the destination.
```shell
ko apply -f pkg/sources/gcppubsub/
```

Once deployed, you can inspect the created resources with `kubectl` commands:

```
# 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 flow from:
kubectl get eventtypes -o yaml
```

4. Create a GCP Pub Sub topic

```shell
Expand Down Expand Up @@ -46,16 +57,14 @@ suffice just fine.


```shell
ko apply -f sample/gcp_pubsub_function/serviceaccount.yaml
ko apply -f sample/gcp_pubsub_function/serviceaccountbinding.yaml
ko apply -f sample/gcp_pubsub_function/auth.yaml
```

## Running

You can deploy this to Knative from the root directory via:
```shell
ko apply -f sample/gcp_pubsub_function/route.yaml
ko apply -f sample/gcp_pubsub_function/configuration.yaml
ko apply -f sample/gcp_pubsub_function/function.yaml
```

Once deployed, you can inspect the created resources with `kubectl` commands:
Expand All @@ -70,11 +79,7 @@ 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 flow from:
kubectl get eventsources -o yaml

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# 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: v1
kind: ServiceAccount
metadata:
name: feed-sa
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:
Expand All @@ -22,4 +28,4 @@ subjects:
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
apiGroup: rbac.authorization.k8s.io
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ spec:
spec:
container:
image: github.com/knative/eventing/sample/gcp_pubsub_function
---
apiVersion: serving.knative.dev/v1alpha1
kind: Route
metadata:
name: gcp-pubsub-function
namespace: default
spec:
traffic:
- configurationName: gcp-pubsub-function
percent: 100
23 changes: 0 additions & 23 deletions sample/gcp_pubsub_function/route.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions sample/gcp_pubsub_function/serviceaccount.yaml

This file was deleted.

143 changes: 30 additions & 113 deletions sample/k8s_events_function/README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
# k8s_events_function

A simple function that receives Kubernetes events and prints them out the after decoding
from base64 encoding. This example targets a Knative Route, there's another example [using
channels](./README_CHANNEL.md). 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 there's no push
events, we create a deployment that attaches to k8s events for a given namespace and then
forwards them to the destination.

## Prerequisites

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

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

The flow sample includes a function that listens for k8s events on a cluster and a flow
object that wires it into the function

## Deploy

If you were already running the knative controllers, you will need reapply this config map so the cluster
will see the changes.
First, install the Stub bus, if not already installed. You **must** change the Kind to ClusterBus

```
ko apply -f config/buses/stub/stub-bus.yaml
```

4. Install k8s events as an event source
## Install k8s events as an event source
```shell
ko apply -f pkg/sources/k8sevents/
```

Once deployed, you can inspect the created resources with `kubectl` commands:

```
# This will show the available EventSources that you can generate a feed from:
kubectl get eventsources -oyaml

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

## 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 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 creates a Service Account and grants
priviledges or create a new one. This example creates a Service Account and grants
it cluster admin access, and you probably wouldn't want to do that in production
settings, but for this example it will suffice just fine.

```shell
ko apply -f sample/k8s_events_function/serviceaccount.yaml
ko apply -f sample/k8s_events_function/serviceaccountbinding.yaml
ko apply -f sample/k8s_events_function/auth.yaml
```

## Deploy function
Then, deploy the k8s-events function

## Running

You can deploy this to Knative from the root directory via:
```shell
ko apply -f sample/k8s_events_function/function.yaml
```
Expand All @@ -67,91 +59,16 @@ kubectl get configurations -o yaml
# This will show the Revision that was created by our configuration:
kubectl get revisions -o yaml

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

# This will show the available EventTypes that you can generate a feed from:
kubectl get eventtypes -oyaml

```

To make this function accessible to our receive adapter via a Route, we first need to determine
its ingress address (might have to wait a little while until 'ADDRESS' gets assigned):
## Create a flow wiring this into k8s events
```shell
$ watch kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
k8s-events-function-ela-ingress k8s-events-function.default.aikas.org,*.k8s-events-function.default.aikas.org 104.197.125.124 80 48s

```

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

So, you'd need to create an A record for k8s-events-function.default.aikas.org pointing to 104.197.125.124

To now send the feed of k8s system events to the function we created above, you need to
create a Feed object. Note that if you are using a different Service Account than created
in the example above, you also need to specify that Service Account in the feed.spec.serviceAccountName.
Modify sample/k8s_events_function/feed.yaml to specify the namespace you want to
watch events for ('default' in this example):

```yaml
apiVersion: feeds.knative.dev/v1alpha1
kind: Feed
metadata:
name: k8s-events-example
namespace: default
spec:
serviceAccountName: feed-sa
trigger:
eventType: receiveevent
resource: k8sevents/receiveevent
service: k8sevents
parameters:
namespace: default
action:
routeName: k8s-events-example
```

Then create the feed so that you can see changes

```shell
kubectl create -f sample/k8s_events_function/feed.yaml
```


This will create a receive_adapter that runs in the cluster and receives native k8s events
and pushes them to the consuming function. You can check the status by looking at the
namespace you deployed the feed into. In the case above it would be default.

```shell
$kubectl get pods

NAME READY STATUS RESTARTS AGE
sub-a3095905-f9c8-4f32-87ac-3c8fec9b51f9-85db55dc48-2mbm9 1/1 Running 0 1m

ko apply -f sample/k8s_events_function/flow.yaml
```

Then look at the logs for the function:

```shell
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
k8s-events-function-00001-deployment-68864b8c7d-rgx2w 3/3 Running 0 12m


# Replace k8s-events-function with the pod name from above:
$ kubectl logs k8s-events-function user-container
```

## Removing a feed

Remove the feed and things get cleaned up (including removing the receive adapter to k8s events)

## Check the logs of the function
```shell
kubectl delete feeds k8s-events-example
kubectl logs k8s-events-00001-deployment-78fb756c8b-rfwl4 user-container
```

## Cleaning up
Expand Down
Loading