Skip to content

Harwayne/auto-container-source

Repository files navigation

Auto Container Source

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.

  1. auto-container-crd-metacontroller

    • Watches all CustomResourceDefinitions for those with the appropriate label and annotation. If found, create a MetaController CompositeController for that CRD.
      • A DecoratorController is what causes auto-container-crd-metacontroller to watch CRDs.
  2. auto-container-cr-reifier-metacontroller

    • Creates a ContainerSource for 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 by auto-container-crd-metacontroller point at this Deployment.

Installation

  1. Install Knative and Knative Eventing.

  2. Install Metacontroller.

    • I happen to be using 0.3.0.
  3. Set your docker user as an environment variable.

    export DOCKER_USER=foobar
  4. Install the two Deployments.

    ./apply-crd-decorator.sh
    ./apply-cr-reifier.sh

Example

See k8s-event-source.

About

Auto Container Source for Knative Eventing Sources

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published