-
Notifications
You must be signed in to change notification settings - Fork 138
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
Adding support for func subscribe
for creating mutiple triggers, based on event filters
#2001
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a7661bc
Adding support for `func subscribe` for creating mutiple triggers, ba…
matzew b059ec3
Update cmd/subscribe.go
matzew 723de4a
Update cmd/subscribe.go
matzew 7bab721
Update cmd/subscribe.go
matzew 4321b8d
Update cmd/subscribe.go
matzew fd44fc4
Update cmd/subscribe.go
matzew 69eee2a
Update cmd/subscribe.go
matzew aa65ae5
removing unused import
matzew e790c86
running make
matzew 84b8308
Some import ogranization
matzew 9c0ce9c
Change argument syntax
matzew bfcc517
changes
matzew 0555958
Adding some emoji text
matzew 972f87f
:lipstick: move subscriptions underneath the deploy element
matzew 057755f
adding silly emoji to build
matzew 5a23f47
Adding some simple/copied/modified test
matzew e5f1c5d
Running 'make schema-generate'
matzew 1437cab
Update function
matzew 810a003
Little unit test
matzew 6dcacfc
Adding a bit more help text
matzew c30a5c4
misspell instruction
matzew 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
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,106 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ory/viper" | ||
"github.com/spf13/cobra" | ||
fn "knative.dev/func/pkg/functions" | ||
) | ||
|
||
func NewSubscribeCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "subscribe", | ||
Short: "Subscribe a function to events", | ||
Long: `Subscribe a function to events | ||
|
||
Subscribe the function to a set of events, matching a set of filters for Cloud Event metadata | ||
and a Knative Broker from where the events are consumed. | ||
`, | ||
Example: ` | ||
# Subscribe the function to the 'default' broker where events have 'type' of 'com.example' | ||
and an 'extension' attribute for the value 'my-extension-value'. | ||
{{rootCmdUse}} subscribe --filter type=com.example --filter extension=my-extension-value | ||
|
||
# Subscribe the function to the 'my-broker' broker where events have 'type' of 'com.example' | ||
and an 'extension' attribute for the value 'my-extension-value'. | ||
{{rootCmdUse}} subscribe --filter type=com.example --filter extension=my-extension-value --source my-broker | ||
`, | ||
SuggestFor: []string{"subcsribe"}, //nolint:misspell | ||
PreRunE: bindEnv("filter", "source"), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runSubscribe(cmd, args) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringArrayP("filter", "f", []string{}, "Filter for the Cloud Event metadata") | ||
|
||
cmd.Flags().StringP("source", "s", "default", "The source, like a Knative Broker") | ||
|
||
return cmd | ||
} | ||
|
||
func runSubscribe(cmd *cobra.Command, args []string) (err error) { | ||
var ( | ||
cfg subscibeConfig | ||
f fn.Function | ||
) | ||
cfg = newSubscribeConfig(cmd) | ||
|
||
if f, err = fn.NewFunction(""); err != nil { | ||
return | ||
} | ||
if !f.Initialized() { | ||
return fn.NewErrNotInitialized(f.Root) | ||
} | ||
if !f.Initialized() { | ||
return fn.NewErrNotInitialized(f.Root) | ||
} | ||
|
||
// add subscription to function | ||
f.Deploy.Subscriptions = append(f.Deploy.Subscriptions, fn.KnativeSubscription{ | ||
Source: cfg.Source, | ||
Filters: extractFilterMap(cfg), | ||
}) | ||
|
||
// pump it | ||
return f.Write() | ||
|
||
} | ||
|
||
func extractFilterMap(cfg subscibeConfig) map[string]string { | ||
subscriptionFilters := make(map[string]string) | ||
for _, filter := range cfg.Filter { | ||
kv := strings.Split(filter, "=") | ||
if len(kv) != 2 { | ||
fmt.Println("Invalid pair:", filter) | ||
continue | ||
} | ||
key := kv[0] | ||
value := kv[1] | ||
subscriptionFilters[key] = value | ||
} | ||
return subscriptionFilters | ||
} | ||
|
||
type subscibeConfig struct { | ||
Filter []string | ||
Source string | ||
} | ||
|
||
func newSubscribeConfig(cmd *cobra.Command) (c subscibeConfig) { | ||
c = subscibeConfig{ | ||
Filter: viper.GetStringSlice("filter"), | ||
Source: viper.GetString("source"), | ||
} | ||
// NOTE: .Filter should be viper.GetStringSlice, but this returns unparsed | ||
// results and appears to be an open issue since 2017: | ||
// https://github.com/spf13/viper/issues/380 | ||
var err error | ||
if c.Filter, err = cmd.Flags().GetStringArray("filter"); err != nil { | ||
fmt.Fprintf(cmd.OutOrStdout(), "error reading filter arguments: %v", err) | ||
} | ||
|
||
return | ||
} |
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,73 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
fn "knative.dev/func/pkg/functions" | ||
) | ||
|
||
func TestSubscribeWithAll(t *testing.T) { | ||
root := fromTempDirectory(t) | ||
|
||
_, err := fn.New().Init(fn.Function{Runtime: "go", Root: root}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cmd := NewSubscribeCmd() | ||
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"}) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Now load the function and ensure that the subscription is set correctly. | ||
f, err := fn.NewFunction(root) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if f.Deploy.Subscriptions == nil { | ||
t.Fatal("Expected subscription to be present ") | ||
} | ||
if f.Deploy.Subscriptions[0].Source != "my-broker" { | ||
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source) | ||
} | ||
|
||
if f.Deploy.Subscriptions[0].Filters["foo"] != "go" { | ||
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"]) | ||
} | ||
} | ||
|
||
func TestSubscribeWithNoExplicitSourceAll(t *testing.T) { | ||
root := fromTempDirectory(t) | ||
|
||
_, err := fn.New().Init(fn.Function{Runtime: "go", Root: root}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cmd := NewSubscribeCmd() | ||
cmd.SetArgs([]string{"--filter", "foo=go"}) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Now load the function and ensure that the subscription is set correctly. | ||
f, err := fn.NewFunction(root) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if f.Deploy.Subscriptions == nil { | ||
t.Fatal("Expected subscription to be present ") | ||
} | ||
if f.Deploy.Subscriptions[0].Source != "default" { | ||
t.Fatalf("Expected subscription for broker to be 'default', but got '%v'", f.Deploy.Subscriptions[0].Source) | ||
} | ||
|
||
if f.Deploy.Subscriptions[0].Filters["foo"] != "go" { | ||
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"]) | ||
} | ||
} |
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,42 @@ | ||
## func subscribe | ||
|
||
Subscribe a function to events | ||
|
||
### Synopsis | ||
|
||
Subscribe a function to events | ||
|
||
Subscribe the function to a set of events, matching a set of filters for Cloud Event metadata | ||
and a Knative Broker from where the events are consumed. | ||
|
||
|
||
``` | ||
func subscribe | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
|
||
# Subscribe the function to the 'default' broker where events have 'type' of 'com.example' | ||
and an 'extension' attribute for the value 'my-extension-value'. | ||
func subscribe --filter type=com.example --filter extension=my-extension-value | ||
|
||
# Subscribe the function to the 'my-broker' broker where events have 'type' of 'com.example' | ||
and an 'extension' attribute for the value 'my-extension-value'. | ||
func subscribe --filter type=com.example --filter extension=my-extension-value --source my-broker | ||
|
||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-f, --filter stringArray Filter for the Cloud Event metadata | ||
-h, --help help for subscribe | ||
-s, --source string The source, like a Knative Broker (default "default") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [func](func.md) - func manages Knative Functions | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
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.
For testing support, this should pass through the
newClient
client factory; but I don't think that should hold up the PR.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.
I will add additional PR. thanks for the review on this