-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blog Post for PR: Prototype a MQTT Source #7919 #6062
Open
ctmphuongg
wants to merge
3
commits into
knative:main
Choose a base branch
from
ctmphuongg:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Introducing MQTT Source with ContainerSource CRD | ||
|
||
**Author: [Phuong Cao](https://www.linkedin.com/in/phuong-cao-pc/)** | ||
|
||
Knative Eventing is taking a step forward in IoT integration with the introduction of an MQTT Source. This new feature contributes to addressing a growing need in the IoT space, allowing developers to seamlessly transform MQTT messages into CloudEvents and ingest them into the Knative Eventing system. | ||
|
||
## Why MQTT Source? | ||
|
||
With the proliferation of IoT devices and edge computing, there's an increasing number of devices emitting MQTT messages to brokers. Until now, processing these messages within Knative Eventing required additional steps. The new MQTT Source bridges this gap, enabling direct ingestion and processing of MQTT messages in a cloud-native event-driven architecture. | ||
|
||
## Feature Description: | ||
|
||
Instead of developing a full-fledged controller from scratch, we've taken a simpler and more efficient approach. The MQTT Source is implemented as a container image that can be used with the existing ContainerSource CRD. This approach is similar to what we've done for the WebSocket protocol, allowing us to transform MQTT messages received into CloudEvents and send them to specified brokers. | ||
|
||
## Using the feature: | ||
|
||
1. Set up an MQTT broker (for testing, you can use Mosquitto): | ||
|
||
```shell | ||
docker run -it --rm --name mosquitto -p 1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf | ||
``` | ||
|
||
2. Set up sink: | ||
The MQTT Source needs a sink to receive the CloudEvent message it sends. For testing purposes, we can use an event display service to receive and display those messages. Add this event_display.yaml file. | ||
|
||
``` | ||
apiVersion: serving.knative.dev/v1 | ||
kind: Service | ||
metadata: | ||
name: event-display | ||
namespace: default | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- # This corresponds to | ||
# https://github.com/knative/eventing/tree/main/cmd/event_display/main.go | ||
image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display | ||
``` | ||
|
||
Then keep this event_display running: | ||
|
||
```shell | ||
kubectl apply event-display.yaml | ||
``` | ||
|
||
## Deploy the MQTT Source | ||
|
||
We will first need to build the Container Source using command: | ||
|
||
```shell | ||
ko build cmd/mqttsource/main.go | ||
``` | ||
|
||
Then, use the following yaml file in config/tools/mqtt_source. Here, you can specify a different topic and event source by adding args. The default source would be localhost:1883, assuming you have started the mosquitto MQTT broker, and the default topic would be mqtt-topic | ||
|
||
``` | ||
apiVersion: sources.knative.dev/v1 | ||
kind: ContainerSource | ||
metadata: | ||
name: mqttsource | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- image: ko://knative.dev/eventing/cmd/mqttsource | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, instead of using the |
||
securityContext: | ||
allowPrivilegeEscalation: false | ||
readOnlyRootFilesystem: true | ||
runAsNonRoot: true | ||
capabilities: | ||
drop: | ||
- ALL | ||
seccompProfile: | ||
type: RuntimeDefault | ||
|
||
|
||
sink: | ||
ref: | ||
apiVersion: serving.knative.dev/v1 | ||
kind: Service | ||
name: event-display | ||
``` | ||
|
||
```shell | ||
ko apply -f config/tools/mqttsource/mqttsource.yaml | ||
``` | ||
|
||
4. Send testing messages: | ||
Use this command to trigger the mosquitto broker to send a MQTT message to the MQTT Source. | ||
|
||
```shell | ||
mosquitto_pub -t '<topic-name>' -m '{"specversion" : "1.0","type" :"com.example.someevent", "id" : "1234-1234-1234","source" : "/mycontext/subcontext","data":{"msg":<message>"}}' -D PUBLISH user-property Content-Type application/cloudevents+json; charset=utf-8 | ||
``` | ||
|
||
The MQTT Source will receive this message, transform it into a CloudEvent, and send it to the specified sink in your Knative Eventing system. The event received will also be logged to the console. You can view the message if it is successfully sent by viewing the logs of event_display. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, instead of using a local dev command, can we point users to a released image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just resolved the other comments! However, I'm not too sure how I can get the released image here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pierDipi @Leo6Leo any ideas on how to get a nightly image for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/knative/docs/blob/main/.github/workflows/knative-docs-image.yaml
We use the GitHub action to build and publish the image to Knative's release image registry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Leo6Leo we need it in eventing, not docs sadly...
Would you be interested in looking into getting the job in eventing to build+publish the mqtt source image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get the image built and released we need to add
mqttsource
to the list here (in Eventing) https://github.com/knative/eventing/blob/e4b6d6861c5b6b3696ec8d9ab82ec73e04e0a781/hack/generate-yamls.sh#L57