Este conteúdo não está disponível no idioma selecionado.

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

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>
    Copy to Clipboard Toggle word wrap

    where:

    • The --field flag adds data to the event as a field-value pair. You can use it multiple times.
    • The --type flag enables you to specify a string that designates the type of the event.
    • The --id flag specifies the ID of the event.
    • You can use the json or yaml arguments with the --output flag to change the output format of the event.

      All of these flags are optional.

      Building a simple event

      $ kn event build -o yaml
      Copy to Clipboard Toggle word wrap

      Resultant 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.generic
      Copy to Clipboard Toggle word wrap

      Building 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 json
      Copy to Clipboard Toggle word wrap

      Resultant 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="
        }
      }
      Copy to Clipboard Toggle word wrap

3.2. Sending events by using the kn-event plugin

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>
    Copy to Clipboard Toggle word wrap

    where:

    • The --field flag adds data to the event as a field-value pair. You can use it multiple times.
    • The --type flag enables you to specify a string that designates the type of the event.
    • The --id flag specifies the ID of the event.
    • The --to flag specifies the destination of the event.
    • The --namespace flag specifies the namespace. If omitted, the namespace is taken from the current context.

      All of these flags are optional, except for the destination specification.

Note

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>: Specifies GroupVersionResource of v1alpha1 channel
  • --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/
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap

Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat