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 --ce-override flag for apiserver and ping sources #865

Merged
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
|===
| | Description | PR

| 🎁
| Add --ce-override flag for apiserver and ping sources
| https://github.com/knative/client/pull/865[#865]

| 🎁
| Add --requests and --limits flags for resource requirements
| https://github.com/knative/client/pull/859[#859]
Expand Down
19 changes: 10 additions & 9 deletions docs/cmd/kn_source_apiserver_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ kn source apiserver create NAME --resource RESOURCE --service-account ACCOUNTNAM
### Options

```
-h, --help help for create
--mode string The mode the receive adapter controller runs under:,
"Reference" sends only the reference to the resource,
"Resource" send the full resource. (default "Reference")
-n, --namespace string Specify the namespace to operate in.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:LabelSelector, e.g. "Event:v1:key=value".
"LabelSelector" is a list of comma separated key value pairs. "LabelSelector" can be omitted, e.g. "Event:v1".
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
--ce-override stringArray Cloud Event overrides to apply before sending event to sink. Example: '--ce-override key=value' You may be provide this flag multiple times. To unset, append "-" to the key (e.g. --ce-override key-).
-h, --help help for create
--mode string The mode the receive adapter controller runs under:,
"Reference" sends only the reference to the resource,
"Resource" send the full resource. (default "Reference")
-n, --namespace string Specify the namespace to operate in.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:LabelSelector, e.g. "Event:v1:key=value".
"LabelSelector" is a list of comma separated key value pairs. "LabelSelector" can be omitted, e.g. "Event:v1".
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
```

### Options inherited from parent commands
Expand Down
19 changes: 10 additions & 9 deletions docs/cmd/kn_source_apiserver_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ kn source apiserver update NAME --resource RESOURCE --service-account ACCOUNTNAM
### Options

```
-h, --help help for update
--mode string The mode the receive adapter controller runs under:,
"Reference" sends only the reference to the resource,
"Resource" send the full resource. (default "Reference")
-n, --namespace string Specify the namespace to operate in.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:LabelSelector, e.g. "Event:v1:key=value".
"LabelSelector" is a list of comma separated key value pairs. "LabelSelector" can be omitted, e.g. "Event:v1".
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
--ce-override stringArray Cloud Event overrides to apply before sending event to sink. Example: '--ce-override key=value' You may be provide this flag multiple times. To unset, append "-" to the key (e.g. --ce-override key-).
-h, --help help for update
--mode string The mode the receive adapter controller runs under:,
"Reference" sends only the reference to the resource,
"Resource" send the full resource. (default "Reference")
-n, --namespace string Specify the namespace to operate in.
--resource stringArray Specification for which events to listen, in the format Kind:APIVersion:LabelSelector, e.g. "Event:v1:key=value".
"LabelSelector" is a list of comma separated key value pairs. "LabelSelector" can be omitted, e.g. "Event:v1".
--service-account string Name of the service account to use to run this source
-s, --sink string Addressable sink for events
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_binding_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kn source binding create NAME --subject SUBJECT --sink SINK --ce-override KEY=VA
### Options

```
--ce-override stringArray Cloud Event overrides to apply before sending event to sink in the format '--ce-override key=value'. --ce-override can be provide multiple times
--ce-override stringArray Cloud Event overrides to apply before sending event to sink. Example: '--ce-override key=value' You may be provide this flag multiple times. To unset, append "-" to the key (e.g. --ce-override key-).
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
-s, --sink string Addressable sink for events
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_binding_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kn source binding update NAME --subject SCHEDULE --sink SINK --ce-override OVERR
### Options

```
--ce-override stringArray Cloud Event overrides to apply before sending event to sink in the format '--ce-override key=value'. --ce-override can be provide multiple times
--ce-override stringArray Cloud Event overrides to apply before sending event to sink. Example: '--ce-override key=value' You may be provide this flag multiple times. To unset, append "-" to the key (e.g. --ce-override key-).
-h, --help help for update
-n, --namespace string Specify the namespace to operate in.
-s, --sink string Addressable sink for events
Expand Down
11 changes: 6 additions & 5 deletions docs/cmd/kn_source_ping_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ kn source ping create NAME --schedule SCHEDULE --sink SINK --data DATA [flags]
### Options

```
-d, --data string Json data to send
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
--schedule string Optional schedule specification in crontab format (e.g. '*/2 * * * *' for every two minutes. By default fire every minute.
-s, --sink string Addressable sink for events
--ce-override stringArray Cloud Event overrides to apply before sending event to sink. Example: '--ce-override key=value' You may be provide this flag multiple times. To unset, append "-" to the key (e.g. --ce-override key-).
-d, --data string Json data to send
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
--schedule string Optional schedule specification in crontab format (e.g. '*/2 * * * *' for every two minutes. By default fire every minute.
-s, --sink string Addressable sink for events
```

### Options inherited from parent commands
Expand Down
11 changes: 6 additions & 5 deletions docs/cmd/kn_source_ping_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ kn source ping update NAME --schedule SCHEDULE --sink SERVICE --data DATA [flags
### Options

```
-d, --data string Json data to send
-h, --help help for update
-n, --namespace string Specify the namespace to operate in.
--schedule string Optional schedule specification in crontab format (e.g. '*/2 * * * *' for every two minutes. By default fire every minute.
-s, --sink string Addressable sink for events
--ce-override stringArray Cloud Event overrides to apply before sending event to sink. Example: '--ce-override key=value' You may be provide this flag multiple times. To unset, append "-" to the key (e.g. --ce-override key-).
-d, --data string Json data to send
-h, --help help for update
-n, --namespace string Specify the namespace to operate in.
--schedule string Optional schedule specification in crontab format (e.g. '*/2 * * * *' for every two minutes. By default fire every minute.
-s, --sink string Addressable sink for events
```

### Options inherited from parent commands
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/commands/source/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func cleanupAPIServerMockClient() {
apiServerSourceClientFactory = nil
}

func createAPIServerSource(name, resourceKind, resourceVersion, serviceAccount, mode, service string) *v1alpha2.ApiServerSource {
func createAPIServerSource(name, resourceKind, resourceVersion, serviceAccount, mode, service string, ceOverrides map[string]string) *v1alpha2.ApiServerSource {
resources := []v1alpha2.APIVersionKindSelector{{
APIVersion: resourceVersion,
Kind: resourceKind,
Expand All @@ -102,5 +102,6 @@ func createAPIServerSource(name, resourceKind, resourceVersion, serviceAccount,
ServiceAccount(serviceAccount).
EventMode(mode).
Sink(sink).
CloudEventOverrides(ceOverrides, []string{}).
Build()
}
20 changes: 14 additions & 6 deletions pkg/kn/commands/source/apiserver/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
"knative.dev/client/pkg/sources/v1alpha2"
"knative.dev/client/pkg/util"
)

// NewAPIServerCreateCommand for creating source
Expand Down Expand Up @@ -62,16 +63,23 @@ func NewAPIServerCreateCommand(p *commands.KnParams) *cobra.Command {
"because: %s", name, namespace, err)
}

b := v1alpha2.NewAPIServerSourceBuilder(name).
ServiceAccount(updateFlags.ServiceAccountName).
EventMode(updateFlags.Mode).
Sink(*objectRef)

resources, err := updateFlags.getAPIServerVersionKindSelector()
if err != nil {
return err
}
b.Resources(resources)

ceOverridesMap, err := util.MapFromArrayAllowingSingles(updateFlags.ceOverrides, "=")
if err != nil {
return err
}
ceOverridesToRemove := util.ParseMinusSuffix(ceOverridesMap)

b := v1alpha2.NewAPIServerSourceBuilder(name).
ServiceAccount(updateFlags.ServiceAccountName).
EventMode(updateFlags.Mode).
Sink(*objectRef).
Resources(resources).
CloudEventOverrides(ceOverridesMap, ceOverridesToRemove)

err = apiSourceClient.CreateAPIServerSource(b.Build())

Expand Down
4 changes: 2 additions & 2 deletions pkg/kn/commands/source/apiserver/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func TestCreateApiServerSource(t *testing.T) {
apiServerClient := v1alpha2.NewMockKnAPIServerSourceClient(t)

apiServerRecorder := apiServerClient.Recorder()
apiServerRecorder.CreateAPIServerSource(createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", "testsvc"), nil)
apiServerRecorder.CreateAPIServerSource(createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", "testsvc", map[string]string{"bla": "blub", "foo": "bar"}), nil)

out, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "create", "testsource", "--resource", "Event:v1", "--service-account", "testsa", "--sink", "svc:testsvc", "--mode", "Reference")
out, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "create", "testsource", "--resource", "Event:v1", "--service-account", "testsa", "--sink", "svc:testsvc", "--mode", "Reference", "--ce-override", "bla=blub", "--ce-override", "foo=bar")
assert.NilError(t, err, "ApiServer source should be created")
util.ContainsAll(out, "created", "default", "testsource")

Expand Down
17 changes: 17 additions & 0 deletions pkg/kn/commands/source/apiserver/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package apiserver
import (
"errors"
"fmt"
"sort"

"github.com/spf13/cobra"
v1alpha2 "knative.dev/eventing/pkg/apis/sources/v1alpha2"
Expand Down Expand Up @@ -71,6 +72,10 @@ func NewAPIServerDescribeCommand(p *commands.KnParams) *cobra.Command {
return err
}

if apiSource.Spec.CloudEventOverrides != nil && apiSource.Spec.CloudEventOverrides.Extensions != nil {
writeCeOverrides(dw, apiSource.Spec.CloudEventOverrides.Extensions)
}

writeResources(dw, apiSource.Spec.Resources)
dw.WriteLine()
if err := dw.Flush(); err != nil {
Expand Down Expand Up @@ -122,3 +127,15 @@ func writeAPIServerSource(dw printers.PrefixWriter, source *v1alpha2.ApiServerSo
dw.WriteAttribute("ServiceAccountName", source.Spec.ServiceAccountName)
dw.WriteAttribute("EventMode", source.Spec.EventMode)
}

func writeCeOverrides(dw printers.PrefixWriter, ceOverrides map[string]string) {
subDw := dw.WriteAttribute("CloudEvent Overrides", "")
var keys []string
for k := range ceOverrides {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
subDw.WriteAttribute(k, ceOverrides[k])
}
}
4 changes: 2 additions & 2 deletions pkg/kn/commands/source/apiserver/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func TestSimpleDescribe(t *testing.T) {
apiServerClient := v1alpha2.NewMockKnAPIServerSourceClient(t, "mynamespace")

apiServerRecorder := apiServerClient.Recorder()
sampleSource := createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", "testsvc")
sampleSource := createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", "testsvc", map[string]string{"foo": "bar"})
apiServerRecorder.GetAPIServerSource("testsource", sampleSource, nil)

out, err := executeAPIServerSourceCommand(apiServerClient, nil, "describe", "testsource")
assert.NilError(t, err)
util.ContainsAll(out, "testsource", "testsa", "Reference", "testsvc", "Service", "Resources", "Event", "v1", "false", "Conditions")
util.ContainsAll(out, "testsource", "testsa", "Reference", "testsvc", "Service", "Resources", "Event", "v1", "false", "Conditions", "foo", "bar")

apiServerRecorder.Validate()
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/kn/commands/source/apiserver/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type APIServerSourceUpdateFlags struct {
ServiceAccountName string
Mode string
Resources []string
ceOverrides []string
}

// getAPIServerVersionKindSelector is to construct an array of resources.
Expand Down Expand Up @@ -161,6 +162,13 @@ func (f *APIServerSourceUpdateFlags) Add(cmd *cobra.Command) {
[]string{},
`Specification for which events to listen, in the format Kind:APIVersion:LabelSelector, e.g. "Event:v1:key=value".
"LabelSelector" is a list of comma separated key value pairs. "LabelSelector" can be omitted, e.g. "Event:v1".`)
cmd.Flags().StringArrayVar(&f.ceOverrides,
"ce-override",
[]string{},
"Cloud Event overrides to apply before sending event to sink. "+
"Example: '--ce-override key=value' "+
"You may be provide this flag multiple times. "+
"To unset, append \"-\" to the key (e.g. --ce-override key-).")
}

// APIServerSourceListHandlers handles printing human readable table for `kn source apiserver list` command's output
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/apiserver/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestListAPIServerSource(t *testing.T) {
apiServerClient := v1alpha22.NewMockKnAPIServerSourceClient(t)

apiServerRecorder := apiServerClient.Recorder()
sampleSource := createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", "testsvc")
sampleSource := createAPIServerSource("testsource", "Event", "v1", "testsa", "Reference", "testsvc", nil)
sampleSourceList := v1alpha2.ApiServerSourceList{}
sampleSourceList.Items = []v1alpha2.ApiServerSource{*sampleSource}

Expand Down
20 changes: 15 additions & 5 deletions pkg/kn/commands/source/apiserver/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
"knative.dev/client/pkg/sources/v1alpha2"
"knative.dev/client/pkg/util"
)

// NewAPIServerUpdateCommand for managing source update
func NewAPIServerUpdateCommand(p *commands.KnParams) *cobra.Command {
var apiServerUpdateFlags APIServerSourceUpdateFlags
var updateFlags APIServerSourceUpdateFlags
var sinkFlags flags.SinkFlags

cmd := &cobra.Command{
Expand Down Expand Up @@ -69,15 +70,15 @@ func NewAPIServerUpdateCommand(p *commands.KnParams) *cobra.Command {

b := v1alpha2.NewAPIServerSourceBuilderFromExisting(source)
if cmd.Flags().Changed("service-account") {
b.ServiceAccount(apiServerUpdateFlags.ServiceAccountName)
b.ServiceAccount(updateFlags.ServiceAccountName)
}

if cmd.Flags().Changed("mode") {
b.EventMode(apiServerUpdateFlags.Mode)
b.EventMode(updateFlags.Mode)
}

if cmd.Flags().Changed("resource") {
updateExisting, err := apiServerUpdateFlags.updateExistingAPIVersionKindSelectorArray(source.Spec.Resources)
updateExisting, err := updateFlags.updateExistingAPIVersionKindSelectorArray(source.Spec.Resources)
if err != nil {
return err
}
Expand All @@ -92,6 +93,15 @@ func NewAPIServerUpdateCommand(p *commands.KnParams) *cobra.Command {
b.Sink(*objectRef)
}

if cmd.Flags().Changed("ce-override") {
ceOverridesMap, err := util.MapFromArrayAllowingSingles(updateFlags.ceOverrides, "=")
if err != nil {
return err
}
ceOverridesToRemove := util.ParseMinusSuffix(ceOverridesMap)
b.CloudEventOverrides(ceOverridesMap, ceOverridesToRemove)
}

err = sourcesClient.UpdateAPIServerSource(b.Build())
if err == nil {
fmt.Fprintf(cmd.OutOrStdout(), "ApiServer source '%s' updated in namespace '%s'.\n", args[0], namespace)
Expand All @@ -101,7 +111,7 @@ func NewAPIServerUpdateCommand(p *commands.KnParams) *cobra.Command {
},
}
commands.AddNamespaceFlags(cmd.Flags(), false)
apiServerUpdateFlags.Add(cmd)
updateFlags.Add(cmd)
sinkFlags.Add(cmd)
return cmd
}
8 changes: 4 additions & 4 deletions pkg/kn/commands/source/apiserver/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func TestApiServerSourceUpdate(t *testing.T) {

apiServerRecorder := apiServerClient.Recorder()

present := createAPIServerSource("testsource", "Event", "v1", "testsa1", "Reference", "svc1")
present := createAPIServerSource("testsource", "Event", "v1", "testsa1", "Reference", "svc1", map[string]string{"bla": "blub", "foo": "bar"})
apiServerRecorder.GetAPIServerSource("testsource", present, nil)

updated := createAPIServerSource("testsource", "Event", "v1", "testsa2", "Reference", "svc2")
updated := createAPIServerSource("testsource", "Event", "v1", "testsa2", "Reference", "svc2", map[string]string{"foo": "baz"})
apiServerRecorder.UpdateAPIServerSource(updated, nil)

output, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "update", "testsource", "--service-account", "testsa2", "--sink", "svc:svc2")
output, err := executeAPIServerSourceCommand(apiServerClient, dynamicClient, "update", "testsource", "--service-account", "testsa2", "--sink", "svc:svc2", "--ce-override", "bla-", "--ce-override", "foo=baz")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "testsource", "updated", "default"))

Expand All @@ -54,7 +54,7 @@ func TestApiServerSourceUpdateDeletionTimestampNotNil(t *testing.T) {
apiServerClient := v1alpha2.NewMockKnAPIServerSourceClient(t)
apiServerRecorder := apiServerClient.Recorder()

present := createAPIServerSource("testsource", "Event", "v1", "testsa1", "Ref", "svc1")
present := createAPIServerSource("testsource", "Event", "v1", "testsa1", "Ref", "svc1", nil)
present.DeletionTimestamp = &metav1.Time{Time: time.Now()}
apiServerRecorder.GetAPIServerSource("testsource", present, nil)

Expand Down
13 changes: 0 additions & 13 deletions pkg/kn/commands/source/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"knative.dev/client/pkg/kn/commands"
clientsourcesv1alpha1 "knative.dev/client/pkg/sources/v1alpha2"
"knative.dev/client/pkg/util"
)

// NewBindingCommand is the root command for all binding related commands
Expand Down Expand Up @@ -131,15 +130,3 @@ func subjectToString(ref tracker.Reference) string {
}
return ret
}

// updateCeOverrides updates the values of the --ce-override flags if given
func updateCeOverrides(bindingFlags bindingUpdateFlags, bindingBuilder *clientsourcesv1alpha1.SinkBindingBuilder) error {
if bindingFlags.ceOverrides != nil {
ceOverrideMap, err := util.MapFromArray(bindingFlags.ceOverrides, "=")
if err != nil {
return err
}
bindingBuilder.AddCloudEventOverrides(ceOverrideMap)
}
return nil
}
7 changes: 3 additions & 4 deletions pkg/kn/commands/source/binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ func createSinkBinding(name, service string, subjectGvk schema.GroupVersionKind,
Sink(&sink).
SubjectGVK(&subjectGvk).
SubjectName(subjectName).
SubjectNamespace("default")
if ceOverrides != nil {
builder.AddCloudEventOverrides(ceOverrides)
}
SubjectNamespace("default").
CloudEventOverrides(ceOverrides, []string{})

binding, _ := builder.Build()
return binding
}
Expand Down
Loading