-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for
func subscribe
for creating mutiple triggers, ba…
…sed on event filters (#2001) * Adding support for `func subscribe` for creating mutiple triggers, based on event filters Signed-off-by: Matthias Wessendorf <[email protected]> * Update cmd/subscribe.go Co-authored-by: Luke Kingland <[email protected]> * Update cmd/subscribe.go Co-authored-by: Luke Kingland <[email protected]> * Update cmd/subscribe.go Co-authored-by: Luke Kingland <[email protected]> * Update cmd/subscribe.go Co-authored-by: Luke Kingland <[email protected]> * Update cmd/subscribe.go Co-authored-by: Luke Kingland <[email protected]> * Update cmd/subscribe.go Co-authored-by: Luke Kingland <[email protected]> * removing unused import Signed-off-by: Matthias Wessendorf <[email protected]> * running make Signed-off-by: Matthias Wessendorf <[email protected]> * Some import ogranization Signed-off-by: Matthias Wessendorf <[email protected]> * Change argument syntax Signed-off-by: Matthias Wessendorf <[email protected]> * changes Signed-off-by: Matthias Wessendorf <[email protected]> * Adding some emoji text Signed-off-by: Matthias Wessendorf <[email protected]> * 💄 move subscriptions underneath the deploy element Signed-off-by: Matthias Wessendorf <[email protected]> * adding silly emoji to build Signed-off-by: Matthias Wessendorf <[email protected]> * Adding some simple/copied/modified test Signed-off-by: Matthias Wessendorf <[email protected]> * Running 'make schema-generate' Signed-off-by: Matthias Wessendorf <[email protected]> * Update function Signed-off-by: Matthias Wessendorf <[email protected]> * Little unit test Signed-off-by: Matthias Wessendorf <[email protected]> * Adding a bit more help text Signed-off-by: Matthias Wessendorf <[email protected]> * misspell instruction Signed-off-by: Matthias Wessendorf <[email protected]> --------- Signed-off-by: Matthias Wessendorf <[email protected]> Co-authored-by: Luke Kingland <[email protected]>
- Loading branch information
Showing
10 changed files
with
354 additions
and
0 deletions.
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.