Skip to content
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

Prototype a MQTT Source #7919

Merged
merged 10 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions cmd/mqttsource/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

Check failure on line 1 in cmd/mqttsource/main.go

View workflow job for this annotation

GitHub Actions / style / Golang / Boilerplate Check (go)

[Go headers] reported by reviewdog 🐶 missing boilerplate: Raw Output: cmd/mqttsource/main.go:1: missing boilerplate: /* Copyright 2024 The Knative Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"flag"
"log"
"net"
"os"

mqtt_paho "github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2"
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/eclipse/paho.golang/paho"
)

var (
sink string
source string

// CloudEvents specific parameters
eventType string
eventSource string

topic string
clientid string
)

func init() {
flag.StringVar(&sink, "sink", "", "the host url to send messages to")
flag.StringVar(&source, "source", "", "the url to get messages from")
flag.StringVar(&eventType, "eventType", "mqtt-event", "the event-type (CloudEvents)")
flag.StringVar(&eventSource, "eventSource", "", "the event-source (CloudEvents)")

flag.StringVar(&topic, "topic", "mqtt-topic", "MQTT topic subscribe to")
flag.StringVar(&clientid, "clientid", "receiver-client-id", "MQTT source client id")
}

func main() {
flag.Parse()

k_sink := os.Getenv("K_SINK")
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
if k_sink != "" {
sink = k_sink
}

// "source" flag must not be empty for operation.
if source == "" {
log.Fatal("A valid MQTT broker URL must be defined.")
}

// The event's source defaults to the MQTT broker URL.
if eventSource == "" {
eventSource = source
}

ctx := cloudevents.ContextWithTarget(context.Background(), sink)

conn, err := net.Dial("tcp", source)
if err != nil {
log.Fatalf("failed to connect to MQTT broker: %s", err.Error())
}

config := &paho.ClientConfig{
ClientID: clientid,
Conn: conn,
}

subscribeOpt := &paho.Subscribe{
Subscriptions: []paho.SubscribeOptions{
{Topic: topic,
QoS: 0},
},
}

p_receive, err := mqtt_paho.New(ctx, config, mqtt_paho.WithSubscribe(subscribeOpt))
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("failed to create protocol: %s", err.Error())
}
defer p_receive.Close(ctx)

c_receive, err := cloudevents.NewClient(p_receive)
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("failed to create client: %v", err)
}

c_send, err := cloudevents.NewClientHTTP()
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("Failed to create a http cloudevent client: %s", err.Error())
}

log.Printf("MQTT source start consuming messages from %s\n", source)
err = c_receive.StartReceiver(ctx, func(ctx context.Context, event cloudevents.Event) {
receive(ctx, event, c_send)
})
if err != nil {
log.Fatalf("failed to start receiver: %s", err)
} else {
log.Printf("MQTT source stopped\n")
}

}

func receive(ctx context.Context, event cloudevents.Event, c cloudevents.Client) {
log.Printf("%s", event)
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
data := event.Data()
newEvent := cloudevents.NewEvent(cloudevents.VersionV1)
newEvent.SetType(eventType)
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
newEvent.SetSource(eventSource)
newEvent.SetID(event.ID())
_ = newEvent.SetData(cloudevents.ApplicationJSON, data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably handle the error here, if we fail to set the data does it make sense to dispatch the event?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll probably just log the error and return so the event would not be sent?

if result := c.Send(ctx, newEvent); !cloudevents.IsACK(result) {
log.Printf("sending event to channel failed: %v", result)
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
}
}
39 changes: 39 additions & 0 deletions config/tools/mqttsource/mqttsource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2021 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is a very simple Knative Eventing Source that produces periodic CloudEvents.
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
apiVersion: sources.knative.dev/v1
kind: ContainerSource
metadata:
name: mqttsource
spec:
template:
spec:
containers:
- image: ko://knative.dev/eventing/cmd/mqttsource
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module knative.dev/eventing

Check failure on line 1 in go.mod

View workflow job for this annotation

GitHub Actions / verify / Verify Deps and Codegen

Please run ./hack/update-codegen.sh. diff --git a/go.mod b/go.mod index 441be9f..4b7e302 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,11 @@ require ( github.com/ahmetb/gen-crd-api-reference-docs v0.3.1-0.20210420163308-c1402a70e2f1 github.com/cloudevents/conformance v0.2.0 github.com/cloudevents/sdk-go/observability/opencensus/v2 v2.15.2 + github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20240508060731-1ed9471c98bd github.com/cloudevents/sdk-go/sql/v2 v2.15.2 github.com/cloudevents/sdk-go/v2 v2.15.2 github.com/coreos/go-oidc/v3 v3.9.0 + github.com/eclipse/paho.golang v0.12.0 github.com/go-jose/go-jose/v3 v3.0.3 github.com/golang/protobuf v1.5.4 github.com/google/go-cmp v0.6.0 @@ -70,9 +72,7 @@ require ( github.com/blendle/zapdriver v1.3.1 // indirect github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20240508060731-1ed9471c98bd // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/eclipse/paho.golang v0.12.0 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/evanphx/json-patch v4.12.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.9.0 // indirect

go 1.21

Expand Down Expand Up @@ -70,7 +70,9 @@
github.com/blendle/zapdriver v1.3.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20240508060731-1ed9471c98bd // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eclipse/paho.golang v0.12.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ github.com/cloudevents/conformance v0.2.0 h1:NvSXOKlagcsOWMEbi8U7Ex/0oQ4JZE1HQ45
github.com/cloudevents/conformance v0.2.0/go.mod h1:rHKDwylBH89Rns6U3wL9ww8bg9/4GbwRCDNuyoC6bcc=
github.com/cloudevents/sdk-go/observability/opencensus/v2 v2.15.2 h1:AbtPqiUDzKup5JpTZzO297/QXgL/TAdpdXQCNwLzlaM=
github.com/cloudevents/sdk-go/observability/opencensus/v2 v2.15.2/go.mod h1:ZbYLE+yaEQ2j4vbRc9qzvGmg30A9LhwFt/1bSebNnbU=
github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20240508060731-1ed9471c98bd h1:MGVlnkCz/b0vjfkM5tSVLD+4oaUnYuVEjiW6lAMJ9IM=
github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2 v2.0.0-20240508060731-1ed9471c98bd/go.mod h1:s+KZsVZst0bVW6vuKYb8CH49CcSJDO09+ZiIeKzJmqE=
github.com/cloudevents/sdk-go/sql/v2 v2.15.2 h1:TNaTeWIbDaci89xgXbmmNVGccawQOvEfWYLWrr7Fk/k=
github.com/cloudevents/sdk-go/sql/v2 v2.15.2/go.mod h1:us+PSk8OXdk8pDbRfvxy5w8ub5goKE7UP9PjKDY7TPw=
github.com/cloudevents/sdk-go/v2 v2.15.2 h1:54+I5xQEnI73RBhWHxbI1XJcqOFOVJN85vb41+8mHUc=
Expand All @@ -111,6 +113,8 @@ github.com/dgryski/go-gk v0.0.0-20200319235926-a69029f61654/go.mod h1:qm+vckxRlD
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/eclipse/paho.golang v0.12.0 h1:EXQFJbJklDnUqW6lyAknMWRhM2NgpHxwrrL8riUmp3Q=
github.com/eclipse/paho.golang v0.12.0/go.mod h1:TSDCUivu9JnoR9Hl+H7sQMcHkejWH2/xKK1NJGtLbIE=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
Expand Down
201 changes: 201 additions & 0 deletions vendor/github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading