Skip to content

Commit

Permalink
Add ClusterTask export command
Browse files Browse the repository at this point in the history
Signed-off-by: vinamra28 <[email protected]>
  • Loading branch information
vinamra28 committed Apr 22, 2021
1 parent 87010b6 commit 502326c
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/cmd/tkn_clustertask.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Manage ClusterTasks
* [tkn](tkn.md) - CLI for tekton pipelines
* [tkn clustertask delete](tkn_clustertask_delete.md) - Delete ClusterTasks in a cluster
* [tkn clustertask describe](tkn_clustertask_describe.md) - Describe a ClusterTask
* [tkn clustertask export](tkn_clustertask_export.md) - Export a ClusterTask to Task
* [tkn clustertask list](tkn_clustertask_list.md) - Lists ClusterTasks
* [tkn clustertask start](tkn_clustertask_start.md) - Start ClusterTasks

41 changes: 41 additions & 0 deletions docs/cmd/tkn_clustertask_export.md
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

55 changes: 55 additions & 0 deletions docs/man/man1/tkn-clustertask-export.1
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
2 changes: 1 addition & 1 deletion docs/man/man1/tkn-clustertask.1
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ Manage ClusterTasks

.SH SEE ALSO
.PP
\fBtkn(1)\fP, \fBtkn\-clustertask\-delete(1)\fP, \fBtkn\-clustertask\-describe(1)\fP, \fBtkn\-clustertask\-list(1)\fP, \fBtkn\-clustertask\-start(1)\fP
\fBtkn(1)\fP, \fBtkn\-clustertask\-delete(1)\fP, \fBtkn\-clustertask\-describe(1)\fP, \fBtkn\-clustertask\-export(1)\fP, \fBtkn\-clustertask\-list(1)\fP, \fBtkn\-clustertask\-start(1)\fP
1 change: 1 addition & 0 deletions pkg/cmd/clustertask/clustertask.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Command(p cli.Params) *cobra.Command {
describeCommand(p),
listCommand(p),
startCommand(p),
exportCommand(p),
)
return cmd
}
143 changes: 143 additions & 0 deletions pkg/cmd/clustertask/export.go
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
}
Loading

0 comments on commit 502326c

Please sign in to comment.