Skip to content

Commit

Permalink
Merge pull request #1 from salaboy/serving-controller
Browse files Browse the repository at this point in the history
Serving controller
  • Loading branch information
salaboy authored Jan 25, 2022
2 parents 014ca33 + 37a1d00 commit 0523d79
Show file tree
Hide file tree
Showing 500 changed files with 31,694 additions and 18,429 deletions.
408 changes: 408 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.EXPORT_ALL_VARIABLES:
SYSTEM_NAMESPACE ?= default
METRICS_DOMAIN ?= example.com
CLUSTER_NAME ?= knative-test
EVENTSINK ?= http://localhost:10000

install-knative:
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.1.0/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.1.0/serving-core.yaml
# install networking layer | kourier
kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.1.0/kourier.yaml
kubectl patch configmap/config-network \
--namespace knative-serving \
--type merge \
--patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'


install-crds:
for crd in config/crds; \
do \
kubectl apply -f "$$crd"; \
done

run-controllers:
go run cmd/controller/main.go

run-webhooks:
go run cmd/webhook/main.go

run-schema:
go run cmd/schema/main.go dump SimpleDeployment

cluster:
kind create cluster --name ${CLUSTER_NAME} --config kind-config.yaml

delete-cluster:
kind delete cluster --name ${CLUSTER_NAME}
83 changes: 78 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,85 @@
[![GoDoc](https://godoc.org/knative.dev/sample-controller?status.svg)](https://godoc.org/knative.dev/sample-controller)
[![Go Report Card](https://goreportcard.com/badge/knative/sample-controller)](https://goreportcard.com/report/knative/sample-controller)

Knative `sample-controller` defines a few simple resources that are validated by
webhook and managed by a controller to demonstrate the canonical style in which
Knative writes controllers.
Knative `servinng events controller` defines a controller for `knative serving` resource and listens for Knative Service events and generates corresponding `CloudEvents` taking the [tektoncd controller](https://github.com/tektoncd/experimental/tree/main/cloudevents) as a reference implementation.

To learn more about Knative, please visit our
[Knative docs](https://github.com/knative/docs) repository.
### Controller architecture

The following roughly defines the controller components:
1. We register 2 Informers:
* [ServingV1/Service](knative.dev/serving/pkg/client/injection/informers/serving/v1/service)
* [ServiceV1/Revision](knative.dev/serving/pkg/client/injection/informers/serving/v1/revision)
2. A `ServingV1/Service` reconciler
3. Embed the [CloudEvents](github.com/cloudevents/sdk-go/v2) SDK client.
4. We also register a CloudEvents 'receiver' which listens at port `8080`.

The following sequence diagram shows the interaction between the components

![sequence](sequence_diagram.png)

### Testing and running the controller
1. Setup a test cluster using KIND with `make cluster`
2. Install Knative with `make install-knative`
3. Install CRDs with `make install-crds`
4. Run the controller with `make run-controllers`

#### Scenario 1 - Serving object to CloudEvent
Now create a dummy Knative service
```
echo "apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
spec:
containers:
- image: gcr.io/google-samples/hello-app:1.0
" | kubectl apply -f -
```

We will see the corresponding events received by the controller and subsequently the emitted `cloudevent` received:
```
Context Attributes,
specversion: 1.0
type: cd.service.deployed.v1 # event of type deployed
source: default/hello
id: 1fc4b160-e859-4177-8847-b85c19fdbc18
time: 2022-01-20T16:56:39.010077Z
```

On updating the previously created `ksvc` object, we should see a corresponding event of type `upgraded`
```diff
- - image: gcr.io/google-samples/hello-app:1.0
+ - image: gcr.io/google-samples/hello-app:2.0
```
This generates the upgraded event and should be reflected in the event as well:
```
Context Attributes,
specversion: 1.0
type: cd.service.upgraded.v1 # event of type upgraded
source: default/hello
id: 23225ed1-3dcc-4772-a60d-72ab57bb1ba3
time: 2022-01-20T17:16:00.253051Z
```

#### Scenario 2 - CloudEvent triggering a Knative service creation
1. Clearing the previous `ksvc` object
```
kubectl delete ksvc hello
```
2. Trigger a cloud event of type `created` (`cd.service.created.v1`) with the following command:
```
go run cmd/cloudevent/produce.go
```
This should create a `ksvc` object in the cluster
> note that the event used here (`cd.service.created.v1`) is not yet defined by the CloudEvents sdk and is just hardcoded for demo purposes.
Check for the created Knative service by running:
```
kubectl get ksvc
```

If you are interested in contributing, see [CONTRIBUTING.md](./CONTRIBUTING.md)
and [DEVELOPMENT.md](./DEVELOPMENT.md).
34 changes: 34 additions & 0 deletions cmd/cloudevent/produce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"log"

cloudevents "github.com/cloudevents/sdk-go/v2"
"knative.dev/sample-controller/pkg/server/models"
)

func main() {
c, err := cloudevents.NewClientHTTP()
if err != nil {
log.Fatalf("failed to create client, %v", err)
}

// Create an Event.
event := cloudevents.NewEvent()
event.SetSource("example/uri")
event.SetType(models.CreateKService.String())
event.SetData(cloudevents.ApplicationJSON, map[string]string{
"name": "hello",
"namespace": "default",
"image": "gcr.io/google-samples/hello-app:1.0",
})

// Set a target.
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:10000/")

// Send that Event.
if result := c.Send(ctx, event); cloudevents.IsUndelivered(result) {
log.Fatalf("failed to send, %v", result)
}
}
11 changes: 7 additions & 4 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ limitations under the License.
package main

import (

// The set of controllers this controller process runs.
"knative.dev/sample-controller/pkg/reconciler/addressableservice"
"knative.dev/sample-controller/pkg/reconciler/simpledeployment"

"knative.dev/sample-controller/pkg/reconciler/serving"

// This defines the shared main for injected controllers.
"knative.dev/pkg/injection/sharedmain"
)

func main() {
sharedmain.Main("controller",
addressableservice.NewController,
simpledeployment.NewController,
// addressableservice.NewController,
// simpledeployment.NewController,
serving.NewController,
)

}
1 change: 0 additions & 1 deletion cmd/webhook/kodata/HEAD

This file was deleted.

1 change: 0 additions & 1 deletion cmd/webhook/kodata/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion cmd/webhook/kodata/VENDOR-LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion cmd/webhook/kodata/refs

This file was deleted.

129 changes: 0 additions & 129 deletions cmd/webhook/main.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module knative.dev/sample-controller
go 1.15

require (
go.uber.org/zap v1.19.1
github.com/cdfoundation/sig-events/cde/sdk/go v0.0.0-20211122192319-2ad36f58fc2c
github.com/cloudevents/sdk-go/v2 v2.8.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/satori/go.uuid v1.2.0
k8s.io/api v0.22.5
k8s.io/apimachinery v0.22.5
k8s.io/client-go v0.22.5
k8s.io/code-generator v0.22.5
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c
knative.dev/hack v0.0.0-20211222071919-abd085fc43de
knative.dev/hack/schema v0.0.0-20211222071919-abd085fc43de
knative.dev/pkg v0.0.0-20220104185830-52e42b760b54
knative.dev/serving v0.28.0
)
Loading

0 comments on commit 0523d79

Please sign in to comment.