Knative Eventing Sugar Controller

Knative Eventing Sugar Controller will react to configured labels to produce or control eventing resources in a cluster or namespace. This allows cluster operators and developers to focus on creating fewer resources, and the underlying eventing infrastructure is created on-demand, and cleaned up when no longer needed.

Installing

The Sugar Controller is disabled by default and can be enabled by configuring config-sugar ConfigMap. See below for a simple example and Configure Sugar Controller for more details.

Automatic Broker Creation

One way to create a Broker is to manually apply a resource to a cluster using the default settings:

  1. Copy the following YAML into a file:

    1. apiVersion: eventing.knative.dev/v1
    2. kind: Broker
    3. metadata:
    4. name: default
    5. namespace: default
  2. Apply the YAML file by running the command:

    1. kubectl apply -f <filename>.yaml

    Where <filename> is the name of the file you created in the previous step.

There might be cases where automated Broker creation is desirable, such as on namespace creation, or on Trigger creation. The Sugar controller enables those use-cases. The following sample configuration of the sugar-config ConfigMap enables Sugar Controller for select Namespaces & all Triggers.

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: config-sugar
  5. namespace: knative-eventing
  6. labels:
  7. eventing.knative.dev/release: devel
  8. data:
  9. # Specify a label selector to selectively apply sugaring to certain namespaces
  10. namespace-selector: |
  11. matchExpressions:
  12. - key: "my.custom.injection.key"
  13. operator: "In"
  14. values: ["enabled"]
  15. # Use an empty object to enable for all triggers
  16. trigger-selector: |
  17. {}
  • When a Namespace is created with label my.custom.injection.key: enabled , the Sugar controller will create a Broker named “default” in that namespace.
  • When a Trigger is created, the Sugar controller will create a Broker named “default” in the Trigger’s namespace.

When a Broker is deleted and but the referenced label selectors are in use, the Sugar Controller will automatically recreate a default Broker.

Namespace Examples

Creating a “default” Broker when creating a Namespace:

  1. Copy the following YAML into a file:

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: example
    5. labels:
    6. my.custom.injection.key: enabled
  2. Apply the YAML file by running the command:

    1. kubectl apply -f <filename>.yaml

    Where <filename> is the name of the file you created in the previous step.

To automatically create a Broker after a namespace exists, label the Namespace:

  1. kubectl label namespace default my.custom.injection.key=enabled

If the Broker named “default” already exists in the Namespace, the Sugar Controller will do nothing.

Trigger Example

Create a “default” Broker in the Trigger’s Namespace when creating a Trigger:

  1. kubectl apply -f - << EOF
  2. apiVersion: eventing.knative.dev/v1
  3. kind: Trigger
  4. metadata:
  5. name: hello-sugar
  6. namespace: hello
  7. spec:
  8. broker: default
  9. subscriber:
  10. ref:
  11. apiVersion: v1
  12. kind: Service
  13. name: event-display
  14. EOF

This will make a Broker called “default” in the Namespace “hello”, and attempt to send events to the “event-display” service.

If the Broker named “default” already exists in the Namespace, the Sugar Controller will do nothing and the Trigger will not own the existing Broker.