Skip to content

Commit

Permalink
add trigger list
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Dec 17, 2019
1 parent 6ac25cd commit 85cbea1
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/cmd/kn_trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ kn trigger [flags]
* [kn trigger create](kn_trigger_create.md) - Create a trigger
* [kn trigger delete](kn_trigger_delete.md) - Delete a trigger.
* [kn trigger describe](kn_trigger_describe.md) - Describe a trigger.
* [kn trigger list](kn_trigger_list.md) - List available trigger.

47 changes: 47 additions & 0 deletions docs/cmd/kn_trigger_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## kn trigger list

List available trigger.

### Synopsis

List available trigger.

```
kn trigger list [name] [flags]
```

### Examples

```
# List all trigger
kn trigger list
# List all trigger in JSON output format
kn trigger list -o json
```

### Options

```
-A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for list
-n, --namespace string Specify the namespace to operate in.
--no-headers When using the default output format, don't print headers (default: print headers).
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
```

### Options inherited from parent commands

```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn trigger](kn_trigger.md) - Trigger command group

38 changes: 37 additions & 1 deletion pkg/eventing/v1alpha1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ package v1alpha1

import (
apis_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime"
kn_errors "knative.dev/client/pkg/errors"
"knative.dev/client/pkg/util"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"knative.dev/eventing/pkg/client/clientset/versioned/scheme"
client_v1alpha1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1alpha1"
)

const (
nameFieldKey = "metadata.name"
)

// KnEventingClient to Eventing Sources. All methods are relative to the
// namespace specified during construction
type KnEventingClient interface {
Expand All @@ -33,6 +39,8 @@ type KnEventingClient interface {
DeleteTrigger(name string) error
// GetTrigger is used to get an instance of trigger
GetTrigger(name string) (*v1alpha1.Trigger, error)
// ListTrigger returns list of trigger CRDs
ListTriggers() (*v1alpha1.TriggerList, error)
}

// KnEventingClient is a combination of Sources client interface and namespace
Expand Down Expand Up @@ -78,7 +86,35 @@ func (c *knEventingClient) GetTrigger(name string) (*v1alpha1.Trigger, error) {
return trigger, nil
}

func (c *knEventingClient) ListTriggers() (*v1alpha1.TriggerList, error) {
triggerList, err := c.client.Triggers(c.namespace).List(apis_v1.ListOptions{})
if err != nil {
return nil, kn_errors.GetError(err)
}
triggerListNew := triggerList.DeepCopy()
err = updateTriggerGvk(triggerListNew)
if err != nil {
return nil, err
}

triggerListNew.Items = make([]v1alpha1.Trigger, len(triggerList.Items))
for idx, trigger := range triggerList.Items {
triggerClone := trigger.DeepCopy()
err := updateTriggerGvk(triggerClone)
if err != nil {
return nil, err
}
triggerListNew.Items[idx] = *triggerClone
}
return triggerListNew, nil
}

// Return the client's namespace
func (c *knEventingClient) Namespace() string {
return c.namespace
}

// update with the v1alpha1 group + version
func updateTriggerGvk(obj runtime.Object) error {
return util.UpdateGroupVersionKindWithScheme(obj, v1alpha1.SchemeGroupVersion, scheme.Scheme)
}
15 changes: 13 additions & 2 deletions pkg/eventing/v1alpha1/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (sr *EventingRecorder) CreateTrigger(trigger interface{}, err error) {
sr.r.Add("CreateTrigger", []interface{}{trigger}, []interface{}{trigger, err})
}

// CreateTrigger performs a previously recorded action, failing if non has been registered
// CreateTrigger performs a previously recorded action
func (c *MockKnEventingClient) CreateTrigger(trigger *v1alpha1.Trigger) (*v1alpha1.Trigger, error) {
call := c.recorder.r.VerifyCall("CreateTrigger", trigger)
return call.Result[0].(*v1alpha1.Trigger), mock.ErrorOrNil(call.Result[1])
Expand All @@ -74,7 +74,7 @@ func (sr *EventingRecorder) GetTrigger(name interface{}, trigger *v1alpha1.Trigg
sr.r.Add("GetTrigger", []interface{}{name}, []interface{}{trigger, err})
}

// GetTrigger performs a previously recorded action, failing if non has been registered
// GetTrigger performs a previously recorded action
func (c *MockKnEventingClient) GetTrigger(name string) (*v1alpha1.Trigger, error) {
call := c.recorder.r.VerifyCall("GetTrigger", name)
return call.Result[0].(*v1alpha1.Trigger), mock.ErrorOrNil(call.Result[1])
Expand All @@ -91,6 +91,17 @@ func (c *MockKnEventingClient) DeleteTrigger(name string) error {
return mock.ErrorOrNil(call.Result[0])
}

// ListTriggers records a call for ListTriggers with the expected result and error (nil if none)
func (sr *EventingRecorder) ListTriggers(triggerList *v1alpha1.TriggerList, err error) {
sr.r.Add("ListTriggers", nil, []interface{}{triggerList, err})
}

// ListTriggers performs a previously recorded action
func (c *MockKnEventingClient) ListTriggers() (*v1alpha1.TriggerList, error) {
call := c.recorder.r.VerifyCall("ListTriggers")
return call.Result[0].(*v1alpha1.TriggerList), mock.ErrorOrNil(call.Result[1])
}

// Validate validates whether every recorded action has been called
func (sr *EventingRecorder) Validate() {
sr.r.CheckThatAllRecordedMethodsHaveBeenCalled()
Expand Down
2 changes: 2 additions & 0 deletions pkg/eventing/v1alpha1/client_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ func TestMockKnClient(t *testing.T) {
recorder.GetTrigger("hello", nil, nil)
recorder.CreateTrigger(&v1alpha1.Trigger{}, nil)
recorder.DeleteTrigger("hello", nil)
recorder.ListTriggers(nil, nil)

// Call all service
client.GetTrigger("hello")
client.CreateTrigger(&v1alpha1.Trigger{})
client.DeleteTrigger("hello")
client.ListTriggers()

// Validate
recorder.Validate()
Expand Down
21 changes: 21 additions & 0 deletions pkg/eventing/v1alpha1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ func TestGetTrigger(t *testing.T) {
assert.ErrorContains(t, err, "errorTrigger")
}

func TestListTrigger(t *testing.T) {
serving, client := setup()

t.Run("list trigger returns a list of triggers", func(t *testing.T) {
trigger1 := newTrigger("trigger-1")
trigger2 := newTrigger("trigger-2")

serving.AddReactor("list", "triggers",
func(a client_testing.Action) (bool, runtime.Object, error) {
assert.Equal(t, testNamespace, a.GetNamespace())
return true, &v1alpha1.TriggerList{Items: []v1alpha1.Trigger{*trigger1, *trigger2}}, nil
})

listTriggers, err := client.ListTriggers()
assert.NilError(t, err)
assert.Assert(t, len(listTriggers.Items) == 2)
assert.Equal(t, listTriggers.Items[0].Name, "trigger-1")
assert.Equal(t, listTriggers.Items[1].Name, "trigger-2")
})
}

func newTrigger(name string) *v1alpha1.Trigger {
obj := &v1alpha1.Trigger{
ObjectMeta: metav1.ObjectMeta{
Expand Down
84 changes: 84 additions & 0 deletions pkg/kn/commands/flags/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package flags

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"knative.dev/client/pkg/kn/commands"
hprinters "knative.dev/client/pkg/printers"
)

// ListFlagsPrinterHandler is the printer handler for HumanReadableFlags
type ListFlagsPrinterHandler interface {
ListPrinter(h hprinters.PrintHandler)
}

// ListFlags composes common printer flag structs
// used in the list command.
type ListFlags struct {
GenericPrintFlags *genericclioptions.PrintFlags
HumanReadableFlags *commands.HumanPrintFlags
PrinterHandler func(h hprinters.PrintHandler)
}

// AllowedFormats is the list of formats in which data can be displayed
func (f *ListFlags) AllowedFormats() []string {
formats := f.GenericPrintFlags.AllowedFormats()
formats = append(formats, f.HumanReadableFlags.AllowedFormats()...)
return formats
}

// ToPrinter attempts to find a composed set of ListTypesFlags suitable for
// returning a printer based on current flag values.
func (f *ListFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
// if there are flags specified for generic printing
if f.GenericPrintFlags.OutputFlagSpecified() {
p, err := f.GenericPrintFlags.ToPrinter()
if err != nil {
return nil, err
}
return p, nil
}

p, err := f.HumanReadableFlags.ToPrinter(f.PrinterHandler)
if err != nil {
return nil, err
}
return p, nil
}

// AddFlags receives a *cobra.Command reference and binds
// flags related to humanreadable and template printing.
func (f *ListFlags) AddFlags(cmd *cobra.Command) {
f.GenericPrintFlags.AddFlags(cmd)
f.HumanReadableFlags.AddFlags(cmd)
}

// NewListFlags returns flags associated with humanreadable,
// template, and "name" printing, with default values set.
func NewListFlags(printer func(h hprinters.PrintHandler)) *ListFlags {
return &ListFlags{
GenericPrintFlags: genericclioptions.NewPrintFlags(""),
HumanReadableFlags: commands.NewHumanPrintFlags(),
PrinterHandler: printer,
}
}

// EnsureWithNamespace ensures that humanreadable flags return
// a printer capable of printing with a "namespace" column.
func (f *ListFlags) EnsureWithNamespace() {
f.HumanReadableFlags.EnsureWithNamespace()
}
77 changes: 77 additions & 0 deletions pkg/kn/commands/trigger/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package trigger

import (
"fmt"

"github.com/spf13/cobra"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
)

// NewTriggerListCommand represents 'kn trigger list' command
func NewTriggerListCommand(p *commands.KnParams) *cobra.Command {
triggerListFlags := flags.NewListFlags(TriggerListHandlers)

triggerListCommand := &cobra.Command{
Use: "list [name]",
Short: "List available trigger.",
Example: `
# List all trigger
kn trigger list
# List all trigger in JSON output format
kn trigger list -o json`,
RunE: func(cmd *cobra.Command, args []string) error {
namespace, err := p.GetNamespace(cmd)
if err != nil {
return err
}
client, err := p.NewEventingClient(namespace)
if err != nil {
return err
}
triggerList, err := client.ListTriggers()
if err != nil {
return err
}
if len(triggerList.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No trigger found.\n")
return nil
}

// empty namespace indicates all-namespaces flag is specified
if namespace == "" {
triggerListFlags.EnsureWithNamespace()
}

printer, err := triggerListFlags.ToPrinter()
if err != nil {
return err
}

err = printer.PrintObj(triggerList, cmd.OutOrStdout())
if err != nil {
return err
}
return nil
},
}
commands.AddNamespaceFlags(triggerListCommand.Flags(), true)
triggerListFlags.AddFlags(triggerListCommand)
return triggerListCommand
}
Loading

0 comments on commit 85cbea1

Please sign in to comment.