Auto Container Source
December 18, 2018 ยท View on GitHub
This repo uses MetaController to automatically search for all CustomResourceDefinitions that have the label AutoContainerSource=Is-Controlling-This-CRD and annotation AutoContainerSourceImage=<DockerImage>. It will then reconcile all instances of those CRDs, by creating a ContainerSource for each. It uses the image found in the CRD's annotation. Any CRD spec fields directly understood by the ContainerSource will be passed as the ContainerSource's spec (i.e. serviceAccountName and sink). All other CRD spec fields are sent as environment variables on the ContainerSource (whose value is the JSON string representing their value in the CRD spec).
For example:
If I had the following CRD:
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: K8sEventSource
metadata:
name: k8seventsource-sample
namespace: default
spec:
namespace: default
serviceAccountName: k8seventsource
sink:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: k8ssource
randomField: 123
randomOtherField:
baz:
- 1
- hello
- 2
- world
foo: bar
qux:
hi: there
Would generate the following ContainerSource:
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: ContainerSource
metadata:
name: k8seventsource-sample-ea3211d5-0312-11e9-8193-42010a80019e
namespace: default
spec:
env:
- name: namespace
value: '"default"'
- name: randomField
value: "123"
- name: randomOtherField
value: '{"baz": [1, "hello", 2, "world"], "foo": "bar", "qux": {"hi": "there"}}'
image: gcr.io/harwayne2/cmd-bf183bbaaf47d0b67277bbc91fa2b42b@sha256:4f91ac63e5cc4f45a0d806be04468110d92ad3c2c71bdd265df321b50ad0f55b
serviceAccountName: k8seventsource
sink:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: k8ssource
Overview
This runs two distinct Kubernetes Deployments and Services.
-
auto-container-crd-metacontroller- Watches all
CustomResourceDefinitions for those with the appropriate label and annotation. If found, create a MetaControllerCompositeControllerfor that CRD.- A
DecoratorControlleris what causesauto-container-crd-metacontrollerto watch CRDs.
- A
- Watches all
-
auto-container-cr-reifier-metacontroller- Creates a
ContainerSourcefor each instantiation of a CRD with the appropriate label and annotation. Syncs the image and arguments down as well as the status up. CompositeControllers generated byauto-container-crd-metacontrollerpoint at thisDeployment.
- Creates a
Installation
-
Install Knative and Knative Eventing.
-
Install Metacontroller.
- I happen to be using 0.3.0.
-
Set your docker user as an environment variable.
export DOCKER_USER=foobar -
Install the two
Deployments../apply-crd-decorator.sh ./apply-cr-reifier.sh
Example
See k8s-event-source.