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 Command to Remove EventListener #564

Merged
merged 1 commit into from
Jan 2, 2020
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/tkn_eventlistener.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ Manage eventlisteners
### SEE ALSO

* [tkn](tkn.md) - CLI for tekton pipelines
* [tkn eventlistener delete](tkn_eventlistener_delete.md) - Delete an EventListener in a namespace
* [tkn eventlistener list](tkn_eventlistener_list.md) - Lists eventlisteners in a namespace

50 changes: 50 additions & 0 deletions docs/cmd/tkn_eventlistener_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## tkn eventlistener delete

Delete an EventListener in a namespace

***Aliases**: rm*

### Usage

```
tkn eventlistener delete
```

### Synopsis

Delete an EventListener in a namespace

### Examples

Delete an EventListener of name 'foo' in namespace 'bar'

tkn eventlistener delete foo -n bar

or

tkn el rm foo -n bar


### Options

```
--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)
-f, --force Whether to force deletion (default: false)
-h, --help help for delete
-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

```
-c, --context string name of the kubeconfig context to use (default: kubectl config current-context)
-k, --kubeconfig string kubectl config file (default: $HOME/.kube/config)
-n, --namespace string namespace to use (default: from $KUBECONFIG)
-C, --nocolour disable colouring (default: false)
```

### SEE ALSO

* [tkn eventlistener](tkn_eventlistener.md) - Manage eventlisteners

90 changes: 90 additions & 0 deletions docs/man/man1/tkn-eventlistener-delete.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
.TH "TKN\-EVENTLISTENER\-DELETE" "1" "" "Auto generated by spf13/cobra" ""
.nh
.ad l


.SH NAME
.PP
tkn\-eventlistener\-delete \- Delete an EventListener in a namespace


.SH SYNOPSIS
.PP
\fBtkn eventlistener delete\fP


.SH DESCRIPTION
.PP
Delete an EventListener in a namespace


.SH OPTIONS
.PP
\fB\-\-allow\-missing\-template\-keys\fP[=true]
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.

.PP
\fB\-f\fP, \fB\-\-force\fP[=false]
Whether to force deletion (default: false)

.PP
\fB\-h\fP, \fB\-\-help\fP[=false]
help for delete

.PP
\fB\-o\fP, \fB\-\-output\fP=""
Output format. One of: json|yaml|name|go\-template|go\-template\-file|template|templatefile|jsonpath|jsonpath\-file.

.PP
\fB\-\-template\fP=""
Template string or path to template file to use when \-o=go\-template, \-o=go\-template\-file. The template format is golang templates [
\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]].


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
\fB\-c\fP, \fB\-\-context\fP=""
name of the kubeconfig context to use (default: kubectl config current\-context)

.PP
\fB\-k\fP, \fB\-\-kubeconfig\fP=""
kubectl config file (default: $HOME/.kube/config)

.PP
\fB\-n\fP, \fB\-\-namespace\fP=""
namespace to use (default: from $KUBECONFIG)

.PP
\fB\-C\fP, \fB\-\-nocolour\fP[=false]
disable colouring (default: false)


.SH EXAMPLE
.PP
Delete an EventListener of name 'foo' in namespace 'bar'

.PP
.RS

.nf
tkn eventlistener delete foo \-n bar

.fi
.RE

.PP
or

.PP
.RS

.nf
tkn el rm foo \-n bar

.fi
.RE


.SH SEE ALSO
.PP
\fBtkn\-eventlistener(1)\fP
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-eventlistener.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Manage eventlisteners

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-eventlistener\-list(1)\fP
\fBtkn(1)\fP, \fBtkn\-eventlistener\-delete(1)\fP, \fBtkn\-eventlistener\-list(1)\fP
87 changes: 87 additions & 0 deletions pkg/cmd/eventlistener/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright © 2019 The Tekton 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 eventlistener

import (
"fmt"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/helper/options"
"github.com/tektoncd/cli/pkg/helper/validate"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)

func deleteCommand(p cli.Params) *cobra.Command {
opts := &options.DeleteOptions{Resource: "eventlistener", ForceDelete: false}
f := cliopts.NewPrintFlags("delete")
eg := `Delete an EventListener of name 'foo' in namespace 'bar'

tkn eventlistener delete foo -n bar

or

tkn el rm foo -n bar
`

c := &cobra.Command{
Use: "delete",
Aliases: []string{"rm"},
Short: "Delete an EventListener in a namespace",
Example: eg,
Args: cobra.MinimumNArgs(1),
SilenceUsage: true,
Annotations: map[string]string{
"commandType": "main",
},
RunE: func(cmd *cobra.Command, args []string) error {
s := &cli.Stream{
In: cmd.InOrStdin(),
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}

if err := validate.NamespaceExists(p); err != nil {
return err
}

if err := opts.CheckOptions(s, args[0]); err != nil {
return err
}

return deleteEventListener(s, p, args[0])
},
}
f.AddFlags(c)
c.Flags().BoolVarP(&opts.ForceDelete, "force", "f", false, "Whether to force deletion (default: false)")

_ = c.MarkZshCompPositionalArgumentCustom(1, "__tkn_get_eventlistener")
return c
}

func deleteEventListener(s *cli.Stream, p cli.Params, elName string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

if err := cs.Triggers.TektonV1alpha1().EventListeners(p.Namespace()).Delete(elName, &metav1.DeleteOptions{}); err != nil {
return fmt.Errorf("failed to delete eventlistener %q: %s", elName, err)
khrm marked this conversation as resolved.
Show resolved Hide resolved
}

fmt.Fprintf(s.Out, "EventListener deleted: %s\n", elName)
return nil
}
129 changes: 129 additions & 0 deletions pkg/cmd/eventlistener/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright © 2019 The Tekton 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 eventlistener

import (
"io"
"strings"
"testing"

"github.com/tektoncd/cli/pkg/test"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
triggertest "github.com/tektoncd/triggers/test"
tb "github.com/tektoncd/triggers/test/builder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestEvetListenerDelete(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

seeds := make([]triggertest.Clients, 0)
for i := 0; i < 3; i++ {
els := []*v1alpha1.EventListener{tb.EventListener("el-1", "ns")}
ctx, _ := rtesting.SetupFakeContext(t)
cs := triggertest.SeedResources(t, ctx, triggertest.Resources{EventListeners: els, Namespaces: ns})
seeds = append(seeds, cs)
}

testParams := []struct {
name string
command []string
input triggertest.Clients
inputStream io.Reader
wantError bool
want string
}{
{
name: "Invalid namespace",
command: []string{"rm", "el-1", "-n", "invalid"},
input: seeds[0],
inputStream: nil,
wantError: true,
want: "namespaces \"invalid\" not found",
},
{
name: "With force delete flag (shorthand)",
command: []string{"rm", "el-1", "-n", "ns", "-f"},
input: seeds[0],
inputStream: nil,
wantError: false,
want: "EventListener deleted: el-1\n",
},
{
name: "With force delete flag",
command: []string{"rm", "el-1", "-n", "ns", "--force"},
input: seeds[1],
inputStream: nil,
wantError: false,
want: "EventListener deleted: el-1\n",
},
{
name: "Without force delete flag, reply no",
command: []string{"rm", "el-1", "-n", "ns"},
input: seeds[2],
inputStream: strings.NewReader("n"),
wantError: true,
want: "canceled deleting eventlistener \"el-1\"",
khrm marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "Without force delete flag, reply yes",
command: []string{"rm", "el-1", "-n", "ns"},
input: seeds[2],
inputStream: strings.NewReader("y"),
wantError: false,
want: "Are you sure you want to delete eventlistener \"el-1\" (y/n): EventListener deleted: el-1\n",
khrm marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "Remove non existent resource",
command: []string{"rm", "nonexistent", "-n", "ns"},
input: seeds[2],
inputStream: strings.NewReader("y"),
wantError: true,
want: "failed to delete eventlistener \"nonexistent\": eventlisteners.tekton.dev \"nonexistent\" not found",
khrm marked this conversation as resolved.
Show resolved Hide resolved
},
}

for _, tp := range testParams {
t.Run(tp.name, func(t *testing.T) {
p := &test.Params{Triggers: tp.input.Triggers, Kube: tp.input.Kube}
eventListener := Command(p)

if tp.inputStream != nil {
eventListener.SetIn(tp.inputStream)
}

out, err := test.ExecuteCommand(eventListener, tp.command...)
if tp.wantError {
if err == nil {
t.Errorf("Error expected here")
}
test.AssertOutput(t, tp.want, err.Error())
} else {
if err != nil {
t.Errorf("Unexpected Error")
}
test.AssertOutput(t, tp.want, out)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/cmd/eventlistener/eventlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func Command(p cli.Params) *cobra.Command {

flags.AddTektonOptions(cmd)
cmd.AddCommand(
deleteCommand(p),
listCommand(p),
)

Expand Down