Knative Eventing: Concepts, Hello‑World Example, and Integration with Cloud Storage & Vision API
This article explains Knative Eventing fundamentals, walks through a Hello‑World event pipeline using GCP Pub/Sub, shows the required YAML definitions and kubectl commands, and demonstrates how to connect Cloud Storage events to a Vision API service for image analysis.
In the first part of the series the author introduced Knative for quickly deploying and autoscaling serverless containers, focusing on HTTP‑triggered services. The second part dives into Knative Eventing, which enables loosely‑coupled, event‑driven services.
Knative Eventing consists of four main primitives: Source (or Producer) reads events from an external system, Channel stores events and fan‑out to subscribers, Subscription links a channel to a service (or another channel), and Service (Consumer) processes the events.
The article then presents a concrete "Hello World" example that reads messages from Google Cloud Pub/Sub, stores them in an in‑memory channel, and delivers them to a Knative service that logs the received payload.
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: GcpPubSubSource
metadata:
name: testing-source
spec:
gcpCredsSecret:
name: google-cloud-key
key: key.json
googleCloudProject: knative-atamel
topic: testing
sink:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: pubsub-testNext, the channel definition stores events in memory:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
metadata:
name: pubsub-test
spec:
provisioner:
apiVersion: eventing.knative.dev/v1alpha1
kind: ClusterChannelProvisioner
name: in-memory-channelThese resources are applied with:
kubectl apply -f gcp-pubsub-source.yaml
kubectl apply -f channel.yamlAfter confirming the source and channel pods are running, a Knative service is created and linked via a Subscription:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: message-dumper-csharp
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: docker.io/{username}/message-dumper-csharp:v1
---
apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
name: gcppubsub-source-sample-csharp
spec:
channel:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: pubsub-test
subscriber:
ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: message-dumper-csharpPublishing a test message with gcloud pubsub topics publish testing --message="Hello World" results in the service logging a Base64‑encoded payload, confirming the asynchronous event flow.
The article then shows how to extend the pattern to integrate Cloud Storage events with Google Vision API. When an image is uploaded to a bucket, a CloudEvent is emitted, captured by a Knative source, and routed to a service that calls the Vision API to extract labels.
var cloudEvent = JsonConvert.DeserializeObject<CloudEvent>(content);
var eventType = cloudEvent.Attributes["eventType"];
var storageUrl = ConstructStorageUrl(cloudEvent);
var visionClient = ImageAnnotatorClient.Create();
var labels = await visionClient.DetectLabelsAsync(Image.FromUri(storageUrl), maxResults: 10);The corresponding service and subscription are defined in YAML (omitted for brevity) and, once applied, any image uploaded to the bucket triggers the Vision API call, with the resulting labels appearing in the service logs.
Overall, the tutorial demonstrates how Knative Eventing can connect disparate cloud services—such as Pub/Sub, Cloud Storage, and Vision API—through declarative YAML resources and standard Kubernetes tooling.
Architects Research Society
A daily treasure trove for architects, expanding your view and depth. We share enterprise, business, application, data, technology, and security architecture, discuss frameworks, planning, governance, standards, and implementation, and explore emerging styles such as microservices, event‑driven, micro‑frontend, big data, data warehousing, IoT, and AI architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.