Skip to content

Commit

Permalink
add --all option to delete all the revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaustubh-pande committed Feb 10, 2021
1 parent 72aeb1c commit 26c1697
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
|===
| | Description | PR

| ✨
| Add --all option to delete all the revisions
| https://github.com/knative/client/pull/1217[#1217]
| 🐣
| Making a toReference public to enable reuse from kn-plugins
| https://github.com/knative/client/pull/1203[#1203]
Expand Down
4 changes: 4 additions & 0 deletions docs/cmd/kn_revision_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ kn revision delete NAME [NAME ...]
# Delete a revision 'svc1-abcde' in default namespace
kn revision delete svc1-abcde
# Delete all revisions
kn revision delete --all
```

### Options

```
--all Delete all revisions.
-h, --help help for delete
-n, --namespace string Specify the namespace to operate in.
--no-wait Do not wait for 'revision delete' operation to be completed. (default true)
Expand Down
7 changes: 7 additions & 0 deletions lib/test/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,10 @@ func RevisionListWithService(r *KnRunResultCollector, serviceNames ...string) {
}
}
}

// RevisionDeleteWithAllOption verifies deleting all revision in sync mode
func RevisionDeleteWithAllOption(r *KnRunResultCollector, revName string) {
out := r.KnTest().Kn().Run("revision", "delete", "--all")
assert.Check(r.T(), util.ContainsAll(out.Stdout, "Revision", revName, "deleted", "namespace", r.KnTest().Kn().Namespace()))
r.AssertNoError(out)
}
43 changes: 41 additions & 2 deletions pkg/kn/commands/revision/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/spf13/cobra"
v1 "knative.dev/client/pkg/serving/v1"

"knative.dev/client/pkg/kn/commands"
)
Expand All @@ -34,11 +35,24 @@ func NewRevisionDeleteCommand(p *commands.KnParams) *cobra.Command {
Short: "Delete revisions",
Example: `
# Delete a revision 'svc1-abcde' in default namespace
kn revision delete svc1-abcde`,
kn revision delete svc1-abcde
# Delete all revisions
kn revision delete --all`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
all, err := cmd.Flags().GetBool("all")
if err != nil {
return err
}
argsLen := len(args)
if argsLen < 1 && !all {
return errors.New("'kn revision delete' requires one or more revision name")
}

if argsLen > 0 && all {
return errors.New("'kn revision delete' with --all flag requires no arguments")
}

namespace, err := p.GetNamespace(cmd)
if err != nil {
return err
Expand All @@ -48,6 +62,17 @@ func NewRevisionDeleteCommand(p *commands.KnParams) *cobra.Command {
return err
}

if all {
args, err = getRevisionNames(client)
if err != nil {
return err
}
if len(args) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No Revisions found.\n")
return nil
}
}

errs := []string{}
for _, name := range args {
timeout := time.Duration(0)
Expand All @@ -67,7 +92,21 @@ func NewRevisionDeleteCommand(p *commands.KnParams) *cobra.Command {
return nil
},
}
flags := RevisionDeleteCommand.Flags()
flags.Bool("all", false, "Delete all revisions.")
commands.AddNamespaceFlags(RevisionDeleteCommand.Flags(), false)
waitFlags.AddConditionWaitFlags(RevisionDeleteCommand, commands.WaitDefaultTimeout, "delete", "revision", "deleted")
return RevisionDeleteCommand
}

func getRevisionNames(client v1.KnServingClient) ([]string, error) {
revisionList, err := client.ListRevisions()
if err != nil {
return []string{}, err
}
revisionNames := []string{}
for _, revision := range revisionList.Items {
revisionNames = append(revisionNames, revision.Name)
}
return revisionNames, nil
}
111 changes: 111 additions & 0 deletions pkg/kn/commands/revision/delete_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2020 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 revision

import (
"errors"
"testing"

"gotest.tools/assert"

clientservingv1 "knative.dev/client/pkg/serving/v1"
"knative.dev/client/pkg/util"
"knative.dev/client/pkg/util/mock"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)

func TestRevisionDeleteAllMock(t *testing.T) {
// New mock client
client := clientservingv1.NewMockKnServiceClient(t)

// Recording:
r := client.Recorder()

// Wait for delete event
r.DeleteRevision("foo", mock.Any(), nil)
r.DeleteRevision("bar", mock.Any(), nil)
r.DeleteRevision("baz", mock.Any(), nil)

revision1 := createMockRevisionWithParams("foo", "svc1", "1", "50", "")
revision2 := createMockRevisionWithParams("bar", "svc1", "1", "50", "")
revision3 := createMockRevisionWithParams("baz", "svc1", "1", "50", "")
revisionList := &servingv1.RevisionList{Items: []servingv1.Revision{*revision1, *revision2, *revision3}}
r.ListRevisions(mock.Any(), revisionList, nil)

output, err := executeRevisionCommand(client, "delete", "--all")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "deleted", "foo", "bar", "baz", "default"))

r.Validate()
}

func TestRevisionDeleteCheckErrorForNotFoundRevisionsMock(t *testing.T) {
// New mock client
client := clientservingv1.NewMockKnServiceClient(t)

// Recording:
r := client.Recorder()

r.DeleteRevision("foo", mock.Any(), nil)
r.DeleteRevision("bar", mock.Any(), errors.New("revisions.serving.knative.dev \"bar\" not found"))
r.DeleteRevision("baz", mock.Any(), errors.New("revisions.serving.knative.dev \"baz\" not found"))

output, err := executeRevisionCommand(client, "delete", "foo", "bar", "baz")
if err == nil {
t.Fatal("Expected revision not found error, returned nil")
}
assert.Assert(t, util.ContainsAll(output, "'foo' deleted", "\"bar\" not found", "\"baz\" not found"))

r.Validate()
}

func TestRevisionDeleteAllErrorFromArgMock(t *testing.T) {
// New mock client
client := clientservingv1.NewMockKnServiceClient(t)

_, err := executeRevisionCommand(client, "delete", "foo", "--all")
assert.Error(t, err, "'kn revision delete' with --all flag requires no arguments")
}

func TestRevisionDeleteAllNoRevisionsMock(t *testing.T) {
// New mock client
client := clientservingv1.NewMockKnServiceClient(t)

// Recording:
r := client.Recorder()
revisionList := &servingv1.RevisionList{Items: []servingv1.Revision{}}
r.ListRevisions(mock.Any(), revisionList, nil)

output, err := executeRevisionCommand(client, "delete", "--all")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "No", "Revisions", "found"))

r.Validate()
}

func TestRevisionDeleteNoNameMock(t *testing.T) {
// New mock client
client := clientservingv1.NewMockKnServiceClient(t)

// Recording:
r := client.Recorder()

_, err := executeRevisionCommand(client, "delete")
assert.ErrorContains(t, err, "'kn revision delete' requires one or more revision name")

r.Validate()

}
22 changes: 0 additions & 22 deletions pkg/kn/commands/revision/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
clienttesting "k8s.io/client-go/testing"

"knative.dev/client/pkg/kn/commands"
clientservingv1 "knative.dev/client/pkg/serving/v1"
"knative.dev/client/pkg/util"
"knative.dev/client/pkg/util/mock"
"knative.dev/client/pkg/wait"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
)
Expand Down Expand Up @@ -104,23 +102,3 @@ func getRevisionDeleteEvents(name string) []watch.Event {
{Type: watch.Deleted, Object: &servingv1.Revision{ObjectMeta: metav1.ObjectMeta{Name: name}}},
}
}

func TestRevisionDeleteCheckErrorForNotFoundRevisionsMock(t *testing.T) {
// New mock client
client := clientservingv1.NewMockKnServiceClient(t)

// Recording:
r := client.Recorder()

r.DeleteRevision("foo", mock.Any(), nil)
r.DeleteRevision("bar", mock.Any(), errors.New("revisions.serving.knative.dev \"bar\" not found."))
r.DeleteRevision("baz", mock.Any(), errors.New("revisions.serving.knative.dev \"baz\" not found."))

output, err := executeRevisionCommand(client, "delete", "foo", "bar", "baz")
if err == nil {
t.Fatal("Expected revision not found error, returned nil")
}
assert.Assert(t, util.ContainsAll(output, "'foo' deleted", "\"bar\" not found", "\"baz\" not found"))

r.Validate()
}
3 changes: 3 additions & 0 deletions test/e2e/revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func TestRevision(t *testing.T) {
nonexistRevision := "hello-nonexist"
test.RevisionMultipleDelete(r, existRevision1, existRevision2, nonexistRevision)

t.Log("delete all revision from hello service and return no error")
test.RevisionDeleteWithAllOption(r, "hello")

t.Log("delete latest revision from hello service and return no error")
revName = test.FindRevision(r, "hello")
test.RevisionDelete(r, revName)
Expand Down

0 comments on commit 26c1697

Please sign in to comment.