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

Adding encoding flag and support for base64 data #1388

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cmd/kn_source_ping_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kn source ping create NAME --sink SINK
```
--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
-e, --encoding string Preferred encoding format. Valid values: text/base64
-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.
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_source_ping_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kn source ping update NAME
```
--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
-e, --encoding string Preferred encoding format. Valid values: text/base64
-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.
Expand Down
28 changes: 27 additions & 1 deletion pkg/kn/commands/source/ping/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ping

import (
"encoding/base64"
"errors"
"fmt"

Expand All @@ -26,6 +27,11 @@ import (
"knative.dev/client/pkg/util"
)

const (
base64Encoding = "base64"
textEncoding = "text"
)

// NewPingCreateCommand is for creating Ping source COs
func NewPingCreateCommand(p *commands.KnParams) *cobra.Command {
var updateFlags pingUpdateFlags
Expand Down Expand Up @@ -70,9 +76,29 @@ func NewPingCreateCommand(p *commands.KnParams) *cobra.Command {
}
ceOverridesToRemove := util.ParseMinusSuffix(ceOverridesMap)

var data, dataBase64 string

switch updateFlags.encoding {
vyasgun marked this conversation as resolved.
Show resolved Hide resolved
case "":
if _, err := base64.StdEncoding.DecodeString(updateFlags.data); err == nil {
dataBase64 = updateFlags.data
} else {
data = updateFlags.data
}
case base64Encoding:
dataBase64 = updateFlags.data
case textEncoding:
data = updateFlags.data
default:
err := fmt.Errorf("invalid value: %v", updateFlags.encoding)
vyasgun marked this conversation as resolved.
Show resolved Hide resolved
fmt.Fprintf(cmd.OutOrStderr(), "Ping source creation failed with error: %v", err)
return err
}

err = pingSourceClient.CreatePingSource(cmd.Context(), clientsourcesv1beta2.NewPingSourceBuilder(name).
Schedule(updateFlags.schedule).
Data(updateFlags.data).
Data(data).
DataBase64(dataBase64).
Sink(*destination).
CloudEventOverrides(ceOverridesMap, ceOverridesToRemove).
Build())
Expand Down
34 changes: 33 additions & 1 deletion pkg/kn/commands/source/ping/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSimpleCreatePingSource(t *testing.T) {
pingClient := clientsourcesv1beta2.NewMockKnPingSourceClient(t)

pingRecorder := pingClient.Recorder()
pingRecorder.CreatePingSource(createPingSource("testsource", "* * * * */2", "maxwell", "mysvc", map[string]string{"bla": "blub", "foo": "bar"}), nil)
pingRecorder.CreatePingSource(createPingSource("testsource", "* * * * */2", "maxwell", "", "mysvc", map[string]string{"bla": "blub", "foo": "bar"}), nil)

out, err := executePingSourceCommand(pingClient, dynamicClient, "create", "--sink", "ksvc:mysvc", "--schedule", "* * * * */2", "--data", "maxwell", "testsource", "--ce-override", "bla=blub", "--ce-override", "foo=bar")
assert.NilError(t, err, "Source should have been created")
Expand Down Expand Up @@ -69,3 +69,35 @@ func TestNoNameGivenError(t *testing.T) {
assert.ErrorContains(t, err, "require")
assert.Assert(t, util.ContainsAll(out, "Usage", "require", "name"))
}

func TestDataEncoding(t *testing.T) {
base64Val := "ZGF0YQ=="
mysvc := &servingv1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service", APIVersion: "serving.knative.dev/v1"},
ObjectMeta: metav1.ObjectMeta{Name: "mysvc", Namespace: "default"},
}
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", mysvc)

pingClient := clientsourcesv1beta2.NewMockKnPingSourceClient(t)

pingRecorder := pingClient.Recorder()

pingRecorder.CreatePingSource(createPingSource("testsource", "* * * * */2", "", base64Val, "mysvc", nil), nil)
out, err := executePingSourceCommand(pingClient, dynamicClient, "create", "--sink", "ksvc:mysvc", "--schedule", "* * * * */2", "--data", base64Val, "testsource")
assert.NilError(t, err, "Source should have been created")
assert.Assert(t, util.ContainsAll(out, "created", "default", "testsource"))

pingRecorder.CreatePingSource(createPingSource("testsource", "* * * * */2", "", base64Val, "mysvc", nil), nil)
out, err = executePingSourceCommand(pingClient, dynamicClient, "create", "--sink", "ksvc:mysvc", "--schedule", "* * * * */2", "--data", base64Val, "--encoding", "base64", "testsource")
assert.NilError(t, err, "Source should have been created")
assert.Assert(t, util.ContainsAll(out, "created", "default", "testsource"))

pingRecorder.CreatePingSource(createPingSource("testsource", "* * * * */2", base64Val, "", "mysvc", nil), nil)
out, err = executePingSourceCommand(pingClient, dynamicClient, "create", "--sink", "ksvc:mysvc", "--schedule", "* * * * */2", "--data", base64Val, "--encoding", "text", "testsource")
assert.NilError(t, err, "Source should have been created")
assert.Assert(t, util.ContainsAll(out, "created", "default", "testsource"))

out, err = executePingSourceCommand(pingClient, dynamicClient, "create", "--sink", "ksvc:mysvc", "--schedule", "* * * * */2", "--data", base64Val, "--encoding", "baseMock", "testsource")
assert.ErrorContains(t, err, "invalid")
assert.Assert(t, util.ContainsAll(out, "Usage", "text", "base64"))
}
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/ping/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDescribeRef(t *testing.T) {

pingRecorder := pingClient.Recorder()
pingRecorder.GetPingSource("testping",
createPingSource("testping", "*/2 * * * *", "test", "testsvc", map[string]string{"foo": "bar"}), nil)
createPingSource("testping", "*/2 * * * *", "test", "", "testsvc", map[string]string{"foo": "bar"}), nil)

out, err := executePingSourceCommand(pingClient, nil, "describe", "testping")
assert.NilError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/kn/commands/source/ping/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
type pingUpdateFlags struct {
schedule string
data string
encoding string
ceOverrides []string
}

Expand All @@ -42,6 +43,8 @@ func (c *pingUpdateFlags) addFlags(cmd *cobra.Command) {

cmd.Flags().StringVarP(&c.data, "data", "d", "", "Json data to send")

cmd.Flags().StringVarP(&c.encoding, "encoding", "e", "", "Preferred encoding format. Valid values: text/base64")
vyasgun marked this conversation as resolved.
Show resolved Hide resolved

cmd.Flags().StringArrayVar(&c.ceOverrides,
"ce-override",
[]string{},
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/ping/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestListPingSource(t *testing.T) {
pingClient := clientv1beta2.NewMockKnPingSourceClient(t)

pingRecorder := pingClient.Recorder()
cJSource := createPingSource("testsource", "* * * * */2", "maxwell", "mysvc", nil)
cJSource := createPingSource("testsource", "* * * * */2", "maxwell", "", "mysvc", nil)
cJSourceList := sourcesv1beta2.PingSourceList{}
cJSourceList.Items = []sourcesv1beta2.PingSource{*cJSource}

Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/commands/source/ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ func cleanupPingMockClient() {
pingSourceClientFactory = nil
}

func createPingSource(name, schedule, data, service string, ceOverridesMap map[string]string) *sourcesv1beta2.PingSource {
func createPingSource(name, schedule, data, dataBase64, service string, ceOverridesMap map[string]string) *sourcesv1beta2.PingSource {
sink := &duckv1.Destination{
Ref: &duckv1.KReference{Name: service, Kind: "Service", APIVersion: "serving.knative.dev/v1", Namespace: "default"},
}
return clientv1beta2.NewPingSourceBuilder(name).
Schedule(schedule).
Data(data).
DataBase64(dataBase64).
Sink(*sink).
CloudEventOverrides(ceOverridesMap, []string{}).
Build()
Expand Down
12 changes: 6 additions & 6 deletions pkg/kn/commands/source/ping/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func TestSimplePingUpdate(t *testing.T) {
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", mysvc1)
pingSourceClient := sourcesv1beta2.NewMockKnPingSourceClient(t)
pingRecorder := pingSourceClient.Recorder()
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "mysvc", nil), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "maxwell", "mysvc1", nil), nil)
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "maxwell", "", "mysvc1", nil), nil)

out, err := executePingSourceCommand(pingSourceClient, dynamicClient, "update", "--schedule", "* * * * */3", "testsource", "--sink", "mysvc1")
assert.NilError(t, err)
Expand All @@ -52,8 +52,8 @@ func TestSimplePingUpdateCEOverrides(t *testing.T) {
pingRecorder := pingSourceClient.Recorder()
ceOverrideMap := map[string]string{"bla": "blub", "foo": "bar"}
ceOverrideMapUpdated := map[string]string{"foo": "baz", "new": "ceoverride"}
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "mysvc", ceOverrideMap), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "updated-data", "mysvc", ceOverrideMapUpdated), nil)
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", ceOverrideMap), nil)
pingRecorder.UpdatePingSource(createPingSource("testsource", "* * * * */3", "updated-data", "", "mysvc", ceOverrideMapUpdated), nil)

out, err := executePingSourceCommand(pingSourceClient, nil, "update", "--schedule", "* * * * */3", "testsource", "--data", "updated-data", "--ce-override", "bla-", "--ce-override", "foo=baz", "--ce-override", "new=ceoverride")
assert.NilError(t, err)
Expand All @@ -77,7 +77,7 @@ func TestUpdateError(t *testing.T) {

func TestPingUpdateDeletionTimestampNotNil(t *testing.T) {
pingSourceClient := sourcesv1beta2.NewMockKnPingSourceClient(t)
present := createPingSource("test", "", "", "", nil)
present := createPingSource("test", "", "", "", "", nil)
present.DeletionTimestamp = &metav1.Time{Time: time.Now()}
pingRecorder := pingSourceClient.Recorder()
pingRecorder.GetPingSource("test", present, nil)
Expand All @@ -100,7 +100,7 @@ func TestPingUpdateNoSinkError(t *testing.T) {
pingClient := sourcesv1beta2.NewMockKnPingSourceClient(t)
pingRecorder := pingClient.Recorder()

pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "mysvc", nil), nil)
pingRecorder.GetPingSource("testsource", createPingSource("testsource", "* * * * */1", "maxwell", "", "mysvc", nil), nil)

out, err := executePingSourceCommand(pingClient, dynamicClient, "update", "testsource", "--sink", "ksvc1")
assert.ErrorContains(t, err, "not found")
Expand Down
5 changes: 5 additions & 0 deletions pkg/sources/v1beta2/ping_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ func (b *PingSourceBuilder) Data(data string) *PingSourceBuilder {
return b
}

func (b *PingSourceBuilder) DataBase64(data string) *PingSourceBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method PingSourceBuilder.DataBase64 should have comment or be unexported. More info.

vyasgun marked this conversation as resolved.
Show resolved Hide resolved
b.pingSource.Spec.DataBase64 = data
return b
}

func (b *PingSourceBuilder) Sink(sink duckv1.Destination) *PingSourceBuilder {
b.pingSource.Spec.Sink = sink
return b
Expand Down