-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: vinamra28 <[email protected]>
- Loading branch information
Showing
9 changed files
with
569 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,41 @@ | ||
## tkn clustertask export | ||
|
||
Export a ClusterTask to Task | ||
|
||
### Usage | ||
|
||
``` | ||
tkn clustertask export [RESOURCES...] | ||
``` | ||
|
||
### Synopsis | ||
|
||
Export a ClusterTask to Task | ||
|
||
### Examples | ||
|
||
Export a ClusterTask 'foo' to Task in namespace 'ns': | ||
tkn clustertask export foo --as-task --namespace ns | ||
or | ||
tkn ct export foo --as-task | ||
|
||
|
||
### Options | ||
|
||
``` | ||
--as-task Promote a ClusterTask to Task in a particular namespace | ||
-h, --help help for export | ||
``` | ||
|
||
### 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) | ||
-C, --no-color disable coloring (default: false) | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [tkn clustertask](tkn_clustertask.md) - Manage ClusterTasks | ||
|
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,55 @@ | ||
.TH "TKN\-CLUSTERTASK\-EXPORT" "1" "" "Auto generated by spf13/cobra" "" | ||
.nh | ||
.ad l | ||
|
||
|
||
.SH NAME | ||
.PP | ||
tkn\-clustertask\-export \- Export a ClusterTask to Task | ||
|
||
|
||
.SH SYNOPSIS | ||
.PP | ||
\fBtkn clustertask export [RESOURCES...]\fP | ||
|
||
|
||
.SH DESCRIPTION | ||
.PP | ||
Export a ClusterTask to Task | ||
|
||
|
||
.SH OPTIONS | ||
.PP | ||
\fB\-\-as\-task\fP[=false] | ||
Promote a ClusterTask to Task in a particular namespace | ||
|
||
.PP | ||
\fB\-h\fP, \fB\-\-help\fP[=false] | ||
help for export | ||
|
||
|
||
.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\-C\fP, \fB\-\-no\-color\fP[=false] | ||
disable coloring (default: false) | ||
|
||
|
||
.SH EXAMPLE | ||
.PP | ||
Export a ClusterTask 'foo' to Task in namespace 'ns': | ||
tkn clustertask export foo \-\-as\-task \-\-namespace ns | ||
or | ||
tkn ct export foo \-\-as\-task | ||
|
||
|
||
.SH SEE ALSO | ||
.PP | ||
\fBtkn\-clustertask(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,143 @@ | ||
// Copyright © 2021 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 clustertask | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/tektoncd/cli/pkg/cli" | ||
"github.com/tektoncd/cli/pkg/clustertask" | ||
ctactions "github.com/tektoncd/cli/pkg/clustertask" | ||
"github.com/tektoncd/cli/pkg/formatted" | ||
"github.com/tektoncd/cli/pkg/options" | ||
"github.com/tektoncd/cli/pkg/task" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type exportOptions struct { | ||
ToTask bool | ||
clustertask *v1beta1.ClusterTask | ||
} | ||
|
||
// NameArg validates that the first argument is a valid clustertask name | ||
func checkNameArgs(args []string, p cli.Params, opt *exportOptions) error { | ||
if len(args) == 0 { | ||
return errNoClusterTask | ||
} | ||
|
||
c, err := p.Clients() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name := args[0] | ||
ct, err := ctactions.Get(c, name, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf(errInvalidClusterTask, name) | ||
} | ||
opt.clustertask = ct | ||
|
||
return nil | ||
} | ||
|
||
func exportCommand(p cli.Params) *cobra.Command { | ||
opts := &exportOptions{} | ||
eg := `Export a ClusterTask 'foo' to Task in namespace 'ns': | ||
tkn clustertask export foo --as-task --namespace ns | ||
or | ||
tkn ct export foo --as-task | ||
` | ||
c := &cobra.Command{ | ||
Use: "export [RESOURCES...]", | ||
ValidArgsFunction: formatted.ParentCompletion, | ||
Short: "Export a ClusterTask to Task", | ||
Example: eg, | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
SilenceUsage: true, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
return checkNameArgs(args, p, opts) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
s := &cli.Stream{ | ||
Out: cmd.OutOrStdout(), | ||
Err: cmd.OutOrStderr(), | ||
} | ||
if !opts.ToTask { | ||
return errors.New("--as-task flag not passed") | ||
} | ||
return exportClusterTaskToTask(s, p, args[0]) | ||
}, | ||
} | ||
c.Flags().BoolVarP(&opts.ToTask, "as-task", "", false, "Promote a ClusterTask to Task in a particular namespace") | ||
return c | ||
} | ||
|
||
func exportClusterTaskToTask(s *cli.Stream, p cli.Params, ctname string) error { | ||
|
||
opts := &options.DeleteOptions{Resource: "ClusterTask", ForceDelete: true, DeleteRelated: false} | ||
|
||
cs, err := p.Clients() | ||
if err != nil { | ||
return fmt.Errorf("failed to create tekton client") | ||
} | ||
|
||
ct, err := clustertask.Get(cs, ctname, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get ClusterTask %s: %v", ctname, err) | ||
} | ||
|
||
t := clustertaskToTask(ct) | ||
|
||
newT, err := task.Create(cs, t, metav1.CreateOptions{}, p.Namespace()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(s.Out, "ClusterTask %s promoted to Task in namespace %s\n", newT.Name, p.Namespace()) | ||
|
||
err = deleteClusterTasks(opts, s, p, []string{ctname}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func clustertaskToTask(ct *v1beta1.ClusterTask) *v1beta1.Task { | ||
t := &v1beta1.Task{} | ||
|
||
// Copy required Metadata from Task to ClusterTask | ||
t.ObjectMeta = metav1.ObjectMeta{ | ||
Name: ct.Name, | ||
Labels: ct.Labels, | ||
Annotations: ct.Annotations, | ||
GenerateName: ct.GenerateName, | ||
OwnerReferences: ct.OwnerReferences, | ||
} | ||
t.TypeMeta = metav1.TypeMeta{ | ||
APIVersion: ct.APIVersion, | ||
Kind: "Task", | ||
} | ||
// Copy the Specs from ClusterTask to Task | ||
t.Spec = ct.Spec | ||
|
||
return t | ||
} |
Oops, something went wrong.