-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds install command in tkn hub, has 2 subcommand task and pipeline. by default latest version is installed. '--version' flag can be used to pass a specific version. '--from' flag can be used to pass catalog name. If a resource is already installed with the passed name, will return an error. Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
- Loading branch information
SM43
committed
Nov 23, 2020
1 parent
be7952f
commit 6dcca4f
Showing
17 changed files
with
1,197 additions
and
117 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,177 @@ | ||
// Copyright © 2020 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 install | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/tektoncd/hub/api/pkg/cli/app" | ||
"github.com/tektoncd/hub/api/pkg/cli/flag" | ||
"github.com/tektoncd/hub/api/pkg/cli/hub" | ||
"github.com/tektoncd/hub/api/pkg/cli/installer" | ||
"github.com/tektoncd/hub/api/pkg/cli/kube" | ||
"github.com/tektoncd/hub/api/pkg/cli/printer" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
type options struct { | ||
cli app.CLI | ||
from string | ||
version string | ||
kind string | ||
args []string | ||
kc kube.Config | ||
cs kube.ClientSet | ||
resource hub.ResourceResult | ||
} | ||
|
||
var cmdExamples string = ` | ||
Install a %S of name 'foo': | ||
tkn hub install %s foo | ||
or | ||
Install a %S of name 'foo' of version '0.3' from Catalog 'Tekton': | ||
tkn hub install %s foo --version 0.3 --from tekton | ||
` | ||
|
||
func Command(cli app.CLI) *cobra.Command { | ||
|
||
opts := &options{cli: cli} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "install", | ||
Short: "Install a resource from a catalog by its kind, name and version", | ||
Long: ``, | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
SilenceUsage: true, | ||
} | ||
cmd.AddCommand( | ||
commandForKind("task", opts), | ||
commandForKind("pipeline", opts), | ||
) | ||
|
||
cmd.PersistentFlags().StringVar(&opts.from, "from", "tekton", "Name of Catalog to which resource belongs.") | ||
cmd.PersistentFlags().StringVar(&opts.version, "version", "", "Version of Resource") | ||
|
||
cmd.PersistentFlags().StringVarP(&opts.kc.Path, "kubeconfig", "k", "", "kubectl config file (default: $HOME/.kube/config)") | ||
cmd.PersistentFlags().StringVarP(&opts.kc.Context, "context", "c", "", "name of the kubeconfig context to use (default: kubectl config current-context)") | ||
cmd.PersistentFlags().StringVarP(&opts.kc.Namespace, "namespace", "n", "", "namespace to use (default: from $KUBECONFIG)") | ||
|
||
return cmd | ||
} | ||
|
||
// commandForKind creates a cobra.Command that when run sets | ||
// opts.Kind and opts.Args and invokes opts.run | ||
func commandForKind(kind string, opts *options) *cobra.Command { | ||
|
||
return &cobra.Command{ | ||
Use: kind, | ||
Short: "Install " + kind + " by name, catalog and version", | ||
Long: ``, | ||
SilenceUsage: true, | ||
Example: examples(kind), | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.kind = kind | ||
opts.args = args | ||
return opts.run() | ||
}, | ||
} | ||
} | ||
|
||
func (opts *options) run() error { | ||
|
||
if err := opts.validate(); err != nil { | ||
return err | ||
} | ||
|
||
hubClient := opts.cli.Hub() | ||
opts.resource = hubClient.GetResource(hub.ResourceOption{ | ||
Name: opts.name(), | ||
Catalog: opts.from, | ||
Kind: opts.kind, | ||
Version: opts.version, | ||
}) | ||
|
||
manifest, err := opts.resource.Manifest() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// This allows fake clients to be inserted while testing | ||
if opts.cs == nil { | ||
opts.cs, err = kube.NewClientSet(opts.kc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
installer := installer.New(opts.cs) | ||
res, err := installer.Install(manifest, opts.cs.Namespace()) | ||
if err != nil { | ||
return opts.checkError(err) | ||
} | ||
|
||
out := opts.cli.Stream().Out | ||
return printer.New(out).String(msg(res)) | ||
} | ||
|
||
func msg(res *unstructured.Unstructured) string { | ||
version := res.GetLabels()["app.kubernetes.io/version"] | ||
return fmt.Sprintf("%s %s(%s) installed in %s namespace", | ||
strings.Title(res.GetKind()), res.GetName(), version, res.GetNamespace()) | ||
} | ||
|
||
func (opts *options) validate() error { | ||
return flag.ValidateVersion(opts.version) | ||
} | ||
|
||
func (opts *options) name() string { | ||
return strings.TrimSpace(opts.args[0]) | ||
} | ||
|
||
func (opts *options) checkError(err error) error { | ||
|
||
if errors.IsAlreadyExists(err) { | ||
return fmt.Errorf("%s %s already exists in %s namespace", | ||
strings.Title(opts.kind), opts.name(), opts.cs.Namespace()) | ||
} | ||
|
||
if strings.Contains(err.Error(), "mutation failed: cannot decode incoming new object") { | ||
version, vErr := opts.resource.MinPipelinesVersion() | ||
if vErr != nil { | ||
return vErr | ||
} | ||
return fmt.Errorf("%v \nMake sure the pipeline version you are running is not lesser than %s and %s have correct spec fields", | ||
err, version, opts.kind) | ||
} | ||
return err | ||
} | ||
|
||
func examples(kind string) string { | ||
replacer := strings.NewReplacer("%s", kind, "%S", strings.Title(kind)) | ||
return replacer.Replace(cmdExamples) | ||
} |
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,148 @@ | ||
// Copyright © 2020 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 install | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
res "github.com/tektoncd/hub/api/gen/resource" | ||
"github.com/tektoncd/hub/api/pkg/cli/test" | ||
cb "github.com/tektoncd/hub/api/pkg/cli/test/builder" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
pipelinev1beta1test "github.com/tektoncd/pipeline/test" | ||
"gopkg.in/h2non/gock.v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/dynamic/fake" | ||
) | ||
|
||
var resVersion = &res.ResourceVersionData{ | ||
ID: 11, | ||
Version: "0.3", | ||
DisplayName: "foo-bar", | ||
Description: "v0.3 Task to run foo", | ||
MinPipelinesVersion: "0.12", | ||
RawURL: "http://raw.github.url/foo/0.3/foo.yaml", | ||
WebURL: "http://web.github.com/foo/0.3/foo.yaml", | ||
UpdatedAt: "2020-01-01 12:00:00 +0000 UTC", | ||
Resource: &res.ResourceData{ | ||
ID: 1, | ||
Name: "foo", | ||
Kind: "Task", | ||
Catalog: &res.Catalog{ | ||
ID: 1, | ||
Name: "tekton", | ||
Type: "community", | ||
}, | ||
Rating: 4.8, | ||
Tags: []*res.Tag{ | ||
&res.Tag{ | ||
ID: 3, | ||
Name: "cli", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func TestInstall_NewResource(t *testing.T) { | ||
cli := test.NewCLI() | ||
|
||
defer gock.Off() | ||
|
||
resVersion := &res.ResourceVersion{Data: resVersion} | ||
res := res.NewViewedResourceVersion(resVersion, "default") | ||
gock.New(test.API). | ||
Get("/resource/tekton/task/foo/0.3"). | ||
Reply(200). | ||
JSON(&res.Projected) | ||
|
||
gock.New("http://raw.github.url"). | ||
Get("/foo/0.3/foo.yaml"). | ||
Reply(200). | ||
File("./testdata/foo-v0.3.yaml") | ||
|
||
buf := new(bytes.Buffer) | ||
cli.SetStream(buf, buf) | ||
|
||
version := "v1beta1" | ||
dynamic := fake.NewSimpleDynamicClient(runtime.NewScheme()) | ||
|
||
cs, _ := test.SeedV1beta1TestData(t, pipelinev1beta1test.Data{}) | ||
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) | ||
|
||
opts := &options{ | ||
cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), | ||
cli: cli, | ||
kind: "task", | ||
args: []string{"foo"}, | ||
from: "tekton", | ||
version: "0.3", | ||
} | ||
|
||
err := opts.run() | ||
assert.NoError(t, err) | ||
assert.Equal(t, "Task foo(0.1) installed in hub namespace\n", buf.String()) | ||
assert.Equal(t, gock.IsDone(), true) | ||
} | ||
|
||
func TestInstall_ResourceAlreadyExistError(t *testing.T) { | ||
cli := test.NewCLI() | ||
|
||
defer gock.Off() | ||
|
||
resVersion := &res.ResourceVersion{Data: resVersion} | ||
res := res.NewViewedResourceVersion(resVersion, "default") | ||
gock.New(test.API). | ||
Get("/resource/tekton/task/foo/0.3"). | ||
Reply(200). | ||
JSON(&res.Projected) | ||
|
||
gock.New("http://raw.github.url"). | ||
Get("/foo/0.3/foo.yaml"). | ||
Reply(200). | ||
File("./testdata/foo-v0.3.yaml") | ||
|
||
buf := new(bytes.Buffer) | ||
cli.SetStream(buf, buf) | ||
|
||
existingTask := &v1beta1.Task{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "foo", | ||
Namespace: "hub", | ||
}, | ||
} | ||
|
||
version := "v1beta1" | ||
dynamic := fake.NewSimpleDynamicClient(runtime.NewScheme(), cb.UnstructuredV1beta1T(existingTask, version)) | ||
|
||
cs, _ := test.SeedV1beta1TestData(t, pipelinev1beta1test.Data{Tasks: []*v1beta1.Task{existingTask}}) | ||
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task"}) | ||
|
||
opts := &options{ | ||
cs: test.FakeClientSet(cs.Pipeline, dynamic, "hub"), | ||
cli: cli, | ||
kind: "task", | ||
args: []string{"foo"}, | ||
from: "tekton", | ||
version: "0.3", | ||
} | ||
|
||
err := opts.run() | ||
assert.Error(t, err) | ||
assert.EqualError(t, err, "Task foo already exists in hub namespace") | ||
assert.Equal(t, gock.IsDone(), true) | ||
} |
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,14 @@ | ||
--- | ||
apiVersion: tekton.dev/v1beta1 | ||
kind: Task | ||
metadata: | ||
name: foo | ||
labels: | ||
app.kubernetes.io/version: '0.3' | ||
annotations: | ||
tekton.dev/pipelines.minVersion: '0.13.1' | ||
tekton.dev/tags: cli | ||
tekton.dev/displayName: 'foo-bar' | ||
spec: | ||
description: >- | ||
v0.3 Task to run foo |
Oops, something went wrong.