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

Add 'kn func emit' command #332

Merged
merged 1 commit into from
May 13, 2021
Merged

Conversation

lance
Copy link
Member

@lance lance commented May 6, 2021

Adds the kn func emit command. Usage is:

Emit event

Emits a CloudEvent, sending it to the deployed function.

Usage:
  func emit [flags]

Examples:

# Send a CloudEvent to the deployed function with no data and default values
# for source, type and ID
kn func emit

# Send a CloudEvent to the deployed function with the data found in ./test.json
kn func emit --file ./test.json

# Send a CloudEvent to the function running locally with a CloudEvent containing
# "Hello World!" as the data field, with a content type of "text/plain"
kn func emit --data "Hello World!" --content-type "text/plain" -s local

# Send a CloudEvent to the function running locally with an event type of "my.event"
kn func emit --type my.event --sink local

# Send a CloudEvent to the deployed function found at /path/to/fn with an id of "fn.test"
kn func emit --path /path/to/fn -i fn.test

# Send a CloudEvent to an arbitrary endpoint
kn func emit --sink "http://my.event.broker.com"


Flags:
  -c, --content-type string   The MIME Content-Type for the CloudEvent data  (Env: $FUNC_CONTENT_TYPE) (default "application/json")
  -d, --data string           Any arbitrary string to be sent as the CloudEvent data. Ignored if --file is provided  (Env: $FUNC_DATA)
  -f, --file string           Path to a local file containing CloudEvent data to be sent  (Env: $FUNC_FILE)
  -h, --help                  help for emit
  -i, --id string             CloudEvent ID (Env: $FUNC_ID) (default "205b5fc6-33b9-42ee-999a-04ca96c59b24")
  -p, --path string           Path to the project directory. Ignored when --sink is provided (Env: $FUNC_PATH) (default "/home/lanceball/src/github.com/boson-project/func")
  -k, --sink string           Send the CloudEvent to the function running at [sink]. The special value "local" can be used to send the event to a function running on the local host. When provided, the --path flag is ignored  (Env: $FUNC_SINK)
  -s, --source string         CloudEvent source (Env: $FUNC_SOURCE) (default "/boson/fn")
  -t, --type string           CloudEvent type  (Env: $FUNC_TYPE) (default "boson.fn")

Global Flags:
  -v, --verbose   print verbose logs

Still needed:

  • CLI command implementation
  • Client tests
  • Configurable content type
  • Fix lint errors
  • Add documentation

Signed-off-by: Lance Ball lball@redhat.com

cloudevents/emitter.go Outdated Show resolved Hide resolved

cancel() // stop the client

if got.Source() != "/boson/fn" {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we used github.com/stretchr/testify/assert for prettier assertions?

Copy link
Member

Choose a reason for hiding this comment

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

I like what assertion frameworks do in some languages, and have used several throughout the years, but (and I stress this is just a humble opinion), I have come to not like them. especially in Go. Having watched them steadily descend into madness repeatedly throughout the years, they are something I now avoid.

The standard built in language logic operators are are not only accessible to everyone without the need to learn a new API, but they also age better, and tend to teach the reader of the tests about the language, and the package being tested, better as one reads the tests. The code is being presented in a context similar to how it will be used in their code, rather than a somewhat contrived context of an almost, but not quite, natural language assertion syntax that is often just qirky enough to be confusing.

I am happy to go along with the majority opinion on this one, if others want to use an assertions framework I am game, but I feel compelled to make my jaded curmudgeon of a voice heard. I am also open to being educated on why they are worth the aforementioned problems!

Copy link
Member Author

Choose a reason for hiding this comment

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

I am easy either way, but I don't think a new assertion library should be added in this PR. Willing to rebase on top of one that does though. :)

cmd/emit.go Outdated Show resolved Hide resolved
Copy link
Member

@lkingland lkingland left a comment

Choose a reason for hiding this comment

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

Lookin' pretty good for a draft IMHO

cloudevents/emitter.go Outdated Show resolved Hide resolved
cloudevents/emitter.go Outdated Show resolved Hide resolved
cloudevents/emitter.go Outdated Show resolved Hide resolved
cloudevents/emitter.go Outdated Show resolved Hide resolved
cloudevents/emitter_test.go Outdated Show resolved Hide resolved
cmd/emit.go Show resolved Hide resolved
cmd/emit.go Outdated Show resolved Hide resolved
@lance lance force-pushed the lance/ocf-233-add-mock-event branch from 4a77c29 to 128e420 Compare May 11, 2021 19:00
@lance lance self-assigned this May 12, 2021
@lance lance force-pushed the lance/ocf-233-add-mock-event branch from 741ce38 to 506774f Compare May 12, 2021 20:39
@lance lance marked this pull request as ready for review May 12, 2021 20:40
@lance
Copy link
Member Author

lance commented May 12, 2021

@boson-project/core I think this is ready for a review. PTAL.

Copy link
Contributor

@zroubalik zroubalik left a comment

Choose a reason for hiding this comment

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

Looking good! Minor nit on warning when both data and file is set.

cmd/emit.go Outdated Show resolved Hide resolved
This commit adds an Emitter to be used by the CLI commands
for sending CloudEvents to functions, either locally, on
the cluster, or at a specified endpoint.

Signed-off-by: Lance Ball <lball@redhat.com>
@lance lance force-pushed the lance/ocf-233-add-mock-event branch from 7868604 to 2109669 Compare May 13, 2021 16:25
@lance lance merged commit 49594d9 into knative:main May 13, 2021
@lance lance deleted the lance/ocf-233-add-mock-event branch May 13, 2021 16:44
Comment on lines +102 to +103
// TODO: This made me wonder whether we should switch to a real logging library
fmt.Printf("WARN: Found both --data and --file. Using file: %v\n", config.File)
Copy link
Member

Choose a reason for hiding this comment

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

@lance I would take the purist approach and say that there is no way this is a state the developer intended, but is due to either a misconfiguration or misunderstanding, and should thus be an error.

jrangelramos pushed a commit to jrangelramos/func that referenced this pull request Oct 7, 2022
* fix: make socatImage linker setable

* Make socatImage var linker setable
* Remove socatImage override via envvar

Signed-off-by: Matej Vasek <mvasek@redhat.com>

* fixup: make socatImage public

Signed-off-by: Matej Vasek <mvasek@redhat.com>

Signed-off-by: Matej Vasek <mvasek@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants