-
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
512f13e
commit 7cf046c
Showing
7 changed files
with
655 additions
and
1 deletion.
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,50 @@ | ||
## tkn customrun delete | ||
|
||
Delete CustomRuns in a namespace | ||
|
||
***Aliases**: rm* | ||
|
||
### Usage | ||
|
||
``` | ||
tkn customrun delete | ||
``` | ||
|
||
### Synopsis | ||
|
||
Delete CustomRuns 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 | ||
|
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,90 @@ | ||
.TH "TKN\-CUSTOMRUN\-DELETE" "1" "" "Auto generated by spf13/cobra" "" | ||
.nh | ||
.ad l | ||
|
||
|
||
.SH NAME | ||
.PP | ||
tkn\-customrun\-delete \- Delete CustomRuns in a namespace | ||
|
||
|
||
.SH SYNOPSIS | ||
.PP | ||
\fBtkn customrun delete\fP | ||
|
||
|
||
.SH DESCRIPTION | ||
.PP | ||
Delete CustomRuns 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 |
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
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,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) { | ||
fmt.Fprintf(os.Stderr, "Error checking CustomRun %s in namespace %s: %v\n", name, ns, err) | ||
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 | ||
} |
Oops, something went wrong.