-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deleting a customrun on the basis of name
- Loading branch information
1 parent
b11908e
commit 28169b3
Showing
3 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright © 2023 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 customrun | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"github.com/tektoncd/cli/pkg/cli" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
func deleteCommand(p cli.Params) *cobra.Command { | ||
f := genericclioptions.NewPrintFlags("delete") | ||
eg := `Delete CustomRun with name 'foo' in namespace 'bar': | ||
tkn customrun delete foo -n bar | ||
or | ||
tkn cr rm foo -n bar | ||
` | ||
|
||
c := &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete CustomRuns having a specific name in a namespace", | ||
Example: eg, | ||
Args: cobra.MinimumNArgs(1), // Requires at least one argument (customrun-name) | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
crNames := args | ||
s := &cli.Stream{ | ||
In: cmd.InOrStdin(), | ||
Out: cmd.OutOrStdout(), | ||
Err: cmd.OutOrStderr(), | ||
} | ||
cs, err := p.Clients() | ||
if err != nil { | ||
return fmt.Errorf("failed to create tekton client: %w", err) | ||
} | ||
|
||
namespace := p.Namespace() | ||
|
||
err = deleteCustomRuns(s, cs, namespace, crNames) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, crName := range crNames { | ||
fmt.Fprintf(s.Out, "CustomRun %s deleted successfully\n", crName) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
f.AddFlags(c) | ||
return c | ||
} | ||
|
||
func deleteCustomRuns(s *cli.Stream, clients *cli.Clients, namespace string, crNames []string) error { | ||
for _, crName := range crNames { | ||
err := deleteSingleCustomRun(clients, namespace, crName) | ||
if err != nil { | ||
fmt.Fprintf(s.Err, "failed to delete CustomRun %s: %v\n", crName, err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func deleteSingleCustomRun(cs *cli.Clients, namespace, crName string) error { | ||
err := cs.Dynamic.Resource(customrunGroupResource).Namespace(namespace).Delete(context.TODO(), crName, metav1.DeleteOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete CustomRun %s: %w", crName, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package customrun | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/tektoncd/cli/pkg/cli" | ||
"github.com/tektoncd/cli/pkg/test" | ||
testDynamic "github.com/tektoncd/cli/pkg/test/dynamic" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
pipelinetest "github.com/tektoncd/pipeline/test" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
) | ||
|
||
func TestDeleteSingleCustomRun(t *testing.T) { | ||
// Create a sample CustomRun | ||
cr := &v1beta1.CustomRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "customrun-1", | ||
Namespace: "foo", | ||
}, | ||
} | ||
|
||
test.SeedTestData(t, pipelinetest.Data{ | ||
CustomRuns: []*v1beta1.CustomRun{cr}, | ||
}) | ||
|
||
tdc := testDynamic.Options{} | ||
dynamicClient, err := tdc.Client() | ||
if err != nil { | ||
t.Errorf("unable to create dynamic client: %v", err) | ||
} | ||
|
||
// Create cli.Clients with the dynamic client | ||
clients := &cli.Clients{ | ||
Dynamic: dynamicClient, | ||
} | ||
|
||
// Namespace and CustomRun name for the test | ||
namespace := "foo" | ||
customRunName := "customrun-1" | ||
|
||
// Check that the CustomRun exists before deletion | ||
_, err = clients.Dynamic.Resource(customrunGroupResource).Namespace(namespace).Get(context.TODO(), customRunName, metav1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
// Call the function | ||
err = deleteSingleCustomRun(clients, namespace, customRunName) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
|
||
// Verify that the CustomRun has been deleted | ||
_, err = clients.Dynamic.Resource(customrunGroupResource).Namespace(namespace).Get(context.TODO(), customRunName, metav1.GetOptions{}) | ||
if err == nil { | ||
t.Errorf("expected error but got none") | ||
} | ||
|
||
// Attempt to delete a non-existent CustomRun | ||
err = deleteSingleCustomRun(clients, namespace, "non-existent-customrun") | ||
if err == nil { | ||
t.Errorf("expected error but got none") | ||
} | ||
} | ||
|
||
func TestDeleteMultipleCustomRuns(t *testing.T) { | ||
// Create sample CustomRuns | ||
cr1 := &v1beta1.CustomRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "customrun-1", | ||
Namespace: "foo", | ||
}, | ||
} | ||
cr2 := &v1beta1.CustomRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "customrun-2", | ||
Namespace: "foo", | ||
}, | ||
} | ||
|
||
test.SeedTestData(t, pipelinetest.Data{ | ||
CustomRuns: []*v1beta1.CustomRun{cr1, cr2}, | ||
}) | ||
|
||
tdc := testDynamic.Options{} | ||
dynamicClient, err := tdc.Client() | ||
if err != nil { | ||
t.Fatalf("unable to create dynamic client: %v", err) | ||
} | ||
|
||
// Create cli.Clients with the dynamic client | ||
clients := &cli.Clients{ | ||
Dynamic: dynamicClient, | ||
} | ||
|
||
// Namespace and CustomRun names for the test | ||
namespace := "foo" | ||
customRunNames := []string{"customrun-1", "customrun-2"} | ||
|
||
// Call the function to delete multiple CustomRuns | ||
s := &cli.Stream{} | ||
err = deleteCustomRuns(s, clients, namespace, customRunNames) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
// Verify that the CustomRuns have been deleted | ||
for _, customRunName := range customRunNames { | ||
_, err = clients.Dynamic.Resource(customrunGroupResource).Namespace(namespace).Get(context.TODO(), customRunName, metav1.GetOptions{}) | ||
if !apierrors.IsNotFound(err) { | ||
t.Errorf("expected not found error for %s but got: %v", customRunName, err) | ||
} | ||
} | ||
} | ||
|
||
func TestNamespaceSpecificDeletion(t *testing.T) { | ||
// Create CustomRuns in different namespaces | ||
cr1 := &v1beta1.CustomRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "customrun-1", | ||
Namespace: "foo", | ||
}, | ||
} | ||
cr2 := &v1beta1.CustomRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "customrun-2", | ||
Namespace: "bar", | ||
}, | ||
} | ||
|
||
test.SeedTestData(t, pipelinetest.Data{ | ||
CustomRuns: []*v1beta1.CustomRun{cr1, cr2}, | ||
}) | ||
|
||
tdc := testDynamic.Options{} | ||
dynamicClient, err := tdc.Client() | ||
if err != nil { | ||
t.Fatalf("unable to create dynamic client: %v", err) | ||
} | ||
|
||
// Create cli.Clients with the dynamic client | ||
clients := &cli.Clients{ | ||
Dynamic: dynamicClient, | ||
} | ||
|
||
// Namespace and CustomRun names for the test | ||
namespace := "foo" | ||
customRunNames := []string{"customrun-1"} | ||
|
||
// Call the function to delete CustomRuns in a specific namespace | ||
s := &cli.Stream{} | ||
err = deleteCustomRuns(s, clients, namespace, customRunNames) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
// Verify that the CustomRun in the "foo" namespace has been deleted | ||
_, err = clients.Dynamic.Resource(customrunGroupResource).Namespace(namespace).Get(context.TODO(), "customrun-1", metav1.GetOptions{}) | ||
if !apierrors.IsNotFound(err) { | ||
t.Errorf("expected not found error but got: %v", err) | ||
} | ||
|
||
// Verify that the CustomRun in the "bar" namespace still exists | ||
_, err = clients.Dynamic.Resource(customrunGroupResource).Namespace("bar").Get(context.TODO(), "customrun-2", metav1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
} |