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 Namespace Check to Task Delete Command #350

Merged
merged 1 commit into from
Oct 10, 2019
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
10 changes: 10 additions & 0 deletions pkg/cmd/task/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
validate "github.com/tektoncd/cli/pkg/helper/validate"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
cliopts "k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -60,6 +61,15 @@ tkn t rm foo -n bar
Err: cmd.OutOrStderr(),
}

cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

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

if err := checkOptions(opts, s, p, args[0]); err != nil {
return err
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/cmd/task/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
pipelinetest "github.com/tektoncd/pipeline/test"
tb "github.com/tektoncd/pipeline/test/builder"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -62,6 +63,13 @@ func TestTaskDelete(t *testing.T) {
),
),
},
Namespaces: []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
},
})
seeds = append(seeds, cs)
}
Expand Down Expand Up @@ -130,11 +138,19 @@ func TestTaskDelete(t *testing.T) {
wantError: false,
want: "Task deleted: task\nTaskRun deleted: task-run-1\nTaskRun deleted: task-run-2\n",
},
{
name: "Try to delete task from invalid namespace",
command: []string{"rm", "task", "-n", "invalid", "-f"},
input: seeds[4],
inputStream: nil,
wantError: true,
want: "namespaces \"invalid\" not found",
},
}

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

if tp.inputStream != nil {
Expand Down
19 changes: 11 additions & 8 deletions pkg/cmd/task/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/formatted"
validate "github.com/tektoncd/cli/pkg/helper/validate"
"github.com/tektoncd/cli/pkg/printer"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
Expand All @@ -48,6 +49,15 @@ func listCommand(p cli.Params) *cobra.Command {
"commandType": "main",
},
RunE: func(cmd *cobra.Command, args []string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client")
}

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

output, err := cmd.LocalFlags().GetString("output")
if err != nil {
fmt.Fprint(os.Stderr, "Error: output option not set properly \n")
Expand All @@ -70,16 +80,9 @@ func listCommand(p cli.Params) *cobra.Command {
}

func printTaskDetails(s *cli.Stream, p cli.Params) error {

cs, err := p.Clients()
if err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

If there's a way to avoid the second error check here, would be nice. Was originally passing the clients as a param to printTaskDetails, but I'm not opposed to two checks.

Copy link
Member

Choose a reason for hiding this comment

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

Agree, I was also thinking of

validate.All( validate.Clients(p), validate.NamespaceExists(p)) 

where validate.KubeClient(p) returns a validator function like: func validator() error and validate.All just checks none of the functions return an error`

That way all commands would just have to invoke just

if   err := validate.All( validate.Clients(p), validate.Foobar(x, y, z)); err != nil {
   return err
}

WDYT?

BTW, I am not suggesting this change in this PR :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I think that's really clever. I'll keep this in mind a try to refactor it.

return err
}

// Check if namespace exists. Return error if namespace specified with -n doesn't exist or if user doesn't have permissions to view.
_, err = cs.Kube.CoreV1().Namespaces().Get(p.Namespace(), metav1.GetOptions{})
if err != nil {
return err
return fmt.Errorf("failed to create tekton client")
}

tasks, err := listAllTasks(cs.Tekton, p.Namespace())
Expand Down
32 changes: 32 additions & 0 deletions pkg/helper/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © 2019 The Tekton Authors.
Copy link
Member Author

Choose a reason for hiding this comment

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

Idea here would be to use this class for common validation methods across the CLI.

//
// 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 validate

import (
k8s "k8s.io/client-go/kubernetes"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Check if namespace exists. Returns error if namespace specified with -n doesn't exist or if user doesn't have permissions to view.
func NamespaceExists(kube k8s.Interface, ns string) error {

_, err := kube.CoreV1().Namespaces().Get(ns, metav1.GetOptions{})
if err != nil {
return err
}

return nil
}
54 changes: 54 additions & 0 deletions pkg/helper/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 validate

import (
"testing"

"github.com/tektoncd/cli/pkg/test"
pipelinetest "github.com/tektoncd/pipeline/test"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNamespaceExists_Invalid_Namespace(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
},
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{Namespaces: ns})
p := &test.Params{Kube: cs.Kube}

err := NamespaceExists(p.Kube, "foo")
test.AssertOutput(t, "namespaces \"foo\" not found", err.Error())
}

func TestNamespaceExists_Valid_Namespace(t *testing.T) {
ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
},
},
}
cs, _ := test.SeedTestData(t, pipelinetest.Data{Namespaces: ns})
p := &test.Params{Kube: cs.Kube}

err := NamespaceExists(p.Kube, "default")
test.AssertOutput(t, nil, err)
}