Skip to content

Commit

Permalink
Deleting a customrun on the basis of name
Browse files Browse the repository at this point in the history
  • Loading branch information
Senjuti256 committed Aug 14, 2024
1 parent 512f13e commit b991e46
Show file tree
Hide file tree
Showing 7 changed files with 658 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/cmd/tkn_customrun.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ Manage CustomRuns
### SEE ALSO

* [tkn](tkn.md) - CLI for tekton pipelines
* [tkn customrun delete](tkn_customrun_delete.md) - Delete CustomRuns having a specific name in a namespace
* [tkn customrun list](tkn_customrun_list.md) - Lists CustomRuns in a namespace

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

Delete CustomRuns having a specific name in a namespace

### Usage

```
tkn customrun delete
```

### Synopsis

Delete CustomRuns having a specific name in a namespace

### Examples

Delete CustomRun with name 'foo' in namespace 'bar':

tkn customrun delete foo -n bar

or

tkn cr 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)
-h, --help help for delete
-o, --output string Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath, jsonpath-as-json, jsonpath-file).
--show-managed-fields If true, keep the managedFields when printing objects in JSON or YAML format.
--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, --no-color disable coloring (default: false)
```

### SEE ALSO

* [tkn customrun](tkn_customrun.md) - Manage CustomRuns

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


.SH NAME
.PP
tkn\-customrun\-delete \- Delete CustomRuns having a specific name in a namespace


.SH SYNOPSIS
.PP
\fBtkn customrun delete\fP


.SH DESCRIPTION
.PP
Delete CustomRuns having a specific name 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\-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\-as\-json, jsonpath\-file).

.PP
\fB\-\-show\-managed\-fields\fP[=false]
If true, keep the managedFields when printing objects in JSON or YAML format.

.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\-\-no\-color\fP[=false]
disable coloring (default: false)


.SH EXAMPLE
.PP
Delete CustomRun with name 'foo' in namespace 'bar':

.PP
.RS

.nf
tkn customrun delete foo \-n bar

.fi
.RE

.PP
or

.PP
.RS

.nf
tkn cr rm foo \-n bar

.fi
.RE


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

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-customrun\-list(1)\fP
\fBtkn(1)\fP, \fBtkn\-customrun\-delete(1)\fP, \fBtkn\-customrun\-list(1)\fP
1 change: 1 addition & 0 deletions pkg/cmd/customrun/customrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Command(p cli.Params) *cobra.Command {

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

Expand Down
127 changes: 127 additions & 0 deletions pkg/cmd/customrun/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright © 2024 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"
"os"

"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"go.uber.org/multierr"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func crExists(args []string, p cli.Params) ([]string, error) {
availableCrs := make([]string, 0)
c, err := p.Clients()
if err != nil {
return availableCrs, err
}
var errorList error
ns := p.Namespace()
for _, name := range args {
var cr *v1beta1.CustomRun
err := actions.GetV1(customrunGroupResource, c, name, ns, metav1.GetOptions{}, &cr)
if err != nil {
errorList = multierr.Append(errorList, err)
if !errors.IsNotFound(err) {
// Log the error but don't stop execution
continue
}
// CustomRun not found, skip to the next
fmt.Fprintf(os.Stderr, "CustomRun %s not found in namespace %s\n", name, ns)
continue
}
availableCrs = append(availableCrs, name)
}
return availableCrs, nil
}

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",
Aliases: []string{"rm"},
Short: "Delete CustomRuns in a namespace",
Example: eg,
Args: cobra.MinimumNArgs(1), // Requires at least one argument (customrun-name)
Annotations: map[string]string{
"commandType": "main",
},
RunE: func(cmd *cobra.Command, args []string) error {
crNames := args
s := &cli.Stream{
In: cmd.InOrStdin(),
Out: cmd.OutOrStdout(),
Err: cmd.OutOrStderr(),
}

return deleteCustomRuns(s, p, crNames)

},
}

f.AddFlags(c)
return c
}

func deleteCustomRuns(s *cli.Stream, p cli.Params, crNames []string) error {
cs, err := p.Clients()
if err != nil {
return fmt.Errorf("failed to create tekton client: %w", err)
}
namespace := p.Namespace()
for _, crName := range crNames {
// Check if CustomRun exists before attempting deletion
exists, _ := crExists([]string{crName}, p)
if len(exists) == 0 {
fmt.Fprintf(s.Err, "CustomRun %s not found in namespace %s\n", crName, namespace)
continue
}

// Proceed with deletion
err := deleteCustomRun(cs, namespace, crName)
if err == nil {
fmt.Fprintf(s.Out, "CustomRun '%s' deleted successfully from namespace '%s'\n", crName, namespace)
} else {
fmt.Fprintf(s.Err, "failed to delete CustomRun %s: %v\n", crName, err)
return err
}
}
return nil
}

func deleteCustomRun(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
}
Loading

0 comments on commit b991e46

Please sign in to comment.