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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Prototype a MQTT Source #7919

wants to merge 8 commits into from

Conversation

ctmphuongg
Copy link
Contributor

@ctmphuongg ctmphuongg commented May 10, 2024

Fixes #7207

Proposed Changes

(The main working files are cmd/mqttsource/main.go, config/tools/mqttsource/mqttsource.yaml, the rest is packages installed to use mqtt_paho, generated by using go get paho and go mod vendor)

  • Install packages for mqtt_paho
  • Prototype an mqtt container source

Notes: For testing

  1. MQTT needs a source to listen from. For local testing, run this command to open the mosquitto mqtt broker for source.
docker run -it --rm --name mosquitto -p 1883:1883 eclipse-mosquitto:2.0 mosquitto -c /mosquitto-no-auth.conf
  1. Keep the mqttsource running by go build the file and run
./main -source localhost:1883 -eventType mqtt.event -eventSource mqtt://localhost:1883 -topic "test-topic"
  1. Send a message to the same topic, using
mosquitto_pub -t 'test-topic' -m '{"specversion" : "1.0","type" :"com.example.someevent", "id" : "1234-1234-1234","source" : "/mycontext/subcontext","data":{"msg":"hello world!"}}' -D PUBLISH user-property Content-Type application/cloudevents+json; charset=utf-8
  1. The message will be logged and sent to sink.

@knative-prow knative-prow bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 10, 2024
Copy link

knative-prow bot commented May 10, 2024

Hi @ctmphuongg. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@knative-prow knative-prow bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels May 10, 2024
Copy link

knative-prow bot commented May 10, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ctmphuongg
Once this PR has been reviewed and has the lgtm label, please assign pierdipi for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

codecov bot commented May 10, 2024

Codecov Report

Attention: Patch coverage is 0% with 56 lines in your changes are missing coverage. Please review.

Project coverage is 69.27%. Comparing base (7e1c082) to head (d456eb6).
Report is 61 commits behind head on main.

Files Patch % Lines
cmd/mqttsource/main.go 0.00% 56 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7919      +/-   ##
==========================================
+ Coverage   69.22%   69.27%   +0.05%     
==========================================
  Files         339      345       +6     
  Lines       19494    16050    -3444     
==========================================
- Hits        13494    11119    -2375     
+ Misses       5337     4245    -1092     
- Partials      663      686      +23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Cali0707
Copy link
Member

/cc @matzew

Not sure if you have any initial feedback here...

@knative-prow knative-prow bot requested a review from matzew May 13, 2024 19:24
@ctmphuongg
Copy link
Contributor Author

/cc @Leo6Leo

@knative-prow knative-prow bot requested a review from Leo6Leo May 14, 2024 15:07
@ctmphuongg ctmphuongg marked this pull request as ready for review May 15, 2024 15:58
@knative-prow knative-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 15, 2024
@ctmphuongg ctmphuongg requested a review from Leo6Leo May 15, 2024 15:58
Copy link
Member

@Leo6Leo Leo6Leo left a comment

Choose a reason for hiding this comment

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

Great job @ctmphuongg ! The PR looks great overall, and I have tested it locally, everything works perfectly.

Currently the CI is failing due to the check failure on codegen. Please run

./hack/update-codegen.sh 

to fix the problem!

cmd/mqttsource/main.go Outdated Show resolved Hide resolved
cmd/mqttsource/main.go Outdated Show resolved Hide resolved
config/tools/mqttsource/mqttsource.yaml Outdated Show resolved Hide resolved
@Leo6Leo
Copy link
Member

Leo6Leo commented May 27, 2024

/ok-to-test

@knative-prow knative-prow bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 27, 2024
@Leo6Leo
Copy link
Member

Leo6Leo commented May 28, 2024

Thanks for fixing the review comment! @ctmphuongg
/lgtm
/cc @pierDipi @creydr @Cali0707

@knative-prow knative-prow bot requested review from creydr and pierDipi May 28, 2024 13:48
@knative-prow knative-prow bot added the lgtm Indicates that a PR is ready to be merged. label May 28, 2024
Copy link
Member

@Cali0707 Cali0707 left a comment

Choose a reason for hiding this comment

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

Hey @ctmphuongg awesome start! I left a few comments regarding error handling/setting CE attribute properly.

Otherwise, we generally use camel case variable names, not snake case in Knative. So, I went through and suggested a new variable name on the first occurrence of each variable I saw that was snake case. I would recommend going through and renaming those in your IDE instead of just committing my suggestions, as I didn't actually correct every instance of the variables, just the first

log.Printf("Received event: %s", event)
data := event.Data()
newEvent := cloudevents.NewEvent(cloudevents.VersionV1)
newEvent.SetType(eventType)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we use the event type from the input event, rather than overriding it on our end?

Suggested change
newEvent.SetType(eventType)
newEvent.SetType(event.Type())

newEvent.SetType(eventType)
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?

func main() {
flag.Parse()

k_sink := os.Getenv("K_SINK")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
k_sink := os.Getenv("K_SINK")
kSink := os.Getenv("K_SINK")

"net"
"os"

mqtt_paho "github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
mqtt_paho "github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2"
mqttv2 "github.com/cloudevents/sdk-go/protocol/mqtt_paho/v2"

},
}

p_receive, err := mqtt_paho.New(ctx, config, mqtt_paho.WithSubscribe(subscribeOpt))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
p_receive, err := mqtt_paho.New(ctx, config, mqtt_paho.WithSubscribe(subscribeOpt))
mqttReceiver, err := mqtt_paho.New(ctx, config, mqtt_paho.WithSubscribe(subscribeOpt))

}
defer p_receive.Close(ctx)

c_receive, err := cloudevents.NewClient(p_receive)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
c_receive, err := cloudevents.NewClient(p_receive)
ceReceiver, err := cloudevents.NewClient(p_receive)

log.Fatalf("failed to create client: %v", err)
}

c_send, err := cloudevents.NewClientHTTP()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
c_send, err := cloudevents.NewClientHTTP()
ceClient, err := cloudevents.NewClientHTTP()

@knative-prow knative-prow bot removed the lgtm Indicates that a PR is ready to be merged. label May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prototype a MQTT Source
3 participants