Chapter 3. Knative CLI plugins
The Knative (kn) CLI supports the use of plugins, which enable you to extend the functionality of your kn installation by adding custom commands and other shared commands that are not part of the core distribution. Knative (kn) CLI plugins are used in the same way as the main kn functionality.
Currently, Red Hat supports the kn-source-kafka plugin and the kn-event plugin.
3.1. Building events by using the kn-event plugin Copy linkLink copied to clipboard!
You can use the builder-like interface of the kn event build command to build an event. You can then send that event at a later time or use it in another context.
Prerequisites
-
You have installed the Knative (
kn) CLI.
Procedure
Build an event:
$ kn event build --field <field-name>=<value> --type <type-name> --id <id> --output <format>where:
-
The
--fieldflag adds data to the event as a field-value pair. You can use it multiple times. -
The
--typeflag enables you to specify a string that designates the type of the event. -
The
--idflag specifies the ID of the event. You can use the
jsonoryamlarguments with the--outputflag to change the output format of the event.All of these flags are optional.
Building a simple event
$ kn event build -o yamlResultant event in the YAML format
data: {} datacontenttype: application/json id: 81a402a2-9c29-4c27-b8ed-246a253c9e58 source: kn-event/v0.4.0 specversion: "1.0" time: "2021-10-15T10:42:57.713226203Z" type: dev.knative.cli.plugin.event.genericBuilding a sample transaction event
$ kn event build \ --field operation.type=local-wire-transfer \ --field operation.amount=2345.40 \ --field operation.from=87656231 \ --field operation.to=2344121 \ --field automated=true \ --field signature='FGzCPLvYWdEgsdpb3qXkaVp7Da0=' \ --type org.example.bank.bar \ --id $(head -c 10 < /dev/urandom | base64 -w 0) \ --output jsonResultant event in the JSON format
{ "specversion": "1.0", "id": "RjtL8UH66X+UJg==", "source": "kn-event/v0.4.0", "type": "org.example.bank.bar", "datacontenttype": "application/json", "time": "2021-10-15T10:43:23.113187943Z", "data": { "automated": true, "operation": { "amount": "2345.40", "from": 87656231, "to": 2344121, "type": "local-wire-transfer" }, "signature": "FGzCPLvYWdEgsdpb3qXkaVp7Da0=" } }
-
The
3.2. Sending events by using the kn-event plugin Copy linkLink copied to clipboard!
You can use the kn event send command to send an event. The events can be sent either to publicly available addresses or to addressable resources inside a cluster, such as Kubernetes services, as well as Knative services, brokers, and channels. The command uses the same builder-like interface as the kn event build command.
Prerequisites
-
You have installed the Knative (
kn) CLI.
Procedure
Send an event:
$ kn event send \ --field <field_name>=<value> \ --type <type_name> \ --id <id> \ --to <url_or_cluster_resource> \ --namespace <namespace>where:
-
The
--fieldflag adds data to the event as a field-value pair. You can use it multiple times. -
The
--typeflag enables you to specify a string that designates the type of the event. -
The
--idflag specifies the ID of the event. -
The
--toflag specifies the destination of the event. The
--namespaceflag specifies the namespace. If omitted, the namespace is taken from the current context.All of these flags are optional, except for the destination specification.
-
The
You can use the following destination formats for the --to flag:
-
--to broker:<broker>: Specifies a broker -
--to channel:<channel>: Specifies a channel -
--to ksvc:<service>or--to <service>: Specifies a Knative service in the current namespace -
--to ksvc:<service>:<namespace>: Specifies a Knative service in another namespace -
--to svc:<service>:<namespace>: Specifies a Kubernetes service in another namespace -
--to special.eventing.dev/v1alpha1/channels:<channel>: SpecifiesGroupVersionResourceofv1alpha1channel -
--to https://example.receiver.uri: Specifies an HTTP URL
If you do not provide a prefix, the destination defaults to a Knative service in the current namespace.
Sending an event to a URL
$ kn event send \
--field player.id=6354aa60-ddb1-452e-8c13-24893667de20 \
--field player.game=2345 \
--field points=456 \
--type org.example.gaming.foo \
--to http://ce-api.foo.example.com/
Sending and event to an in-cluster resource
$ kn event send \
--type org.example.kn.ping \
--id $(uuidgen) \
--field event.type=test \
--field event.data=98765 \
--to ksvc:event-display