Skip to content

Commit c36b6ce

Browse files
chmoueltekton-robot
authored andcommitted
Add pipeline version information
Add pipeline service version information to `tkn pipeline version`. The logic has been imported from dashboard https://git.io/JvC4h I don't have a full test for it, since the machinery to fake a Deployment is quite tedious to import/implement and this is not used anywhere else. Closes #463 Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
1 parent 728fd3c commit c36b6ce

File tree

7 files changed

+85
-7
lines changed

7 files changed

+85
-7
lines changed

pkg/cmd/root.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import (
3535
)
3636

3737
const usageTemplate = `Usage:{{if .Runnable}}
38-
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
38+
{{.UseLine}}{{end}}{{if .Ha
39+
sAvailableSubCommands}}
3940
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
4041
4142
Aliases:
@@ -89,7 +90,7 @@ func Root(p cli.Params) *cobra.Command {
8990
taskrun.Command(p),
9091
triggerbinding.Command(p),
9192
triggertemplate.Command(p),
92-
version.Command(),
93+
version.Command(p),
9394
)
9495

9596
return cmd
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Client version: v1.2.3
2+
Pipeline version: unknown
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A newer version (v0.0.2) of Tekton CLI is available, please check https://github.com/tektoncd/cli/releases/tag/v0.0.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You are running the latest version (v0.0.10) of Tekton CLI

pkg/cmd/version/version.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/blang/semver"
2727
"github.com/pkg/errors"
2828
"github.com/spf13/cobra"
29+
"github.com/tektoncd/cli/pkg/cli"
30+
"github.com/tektoncd/cli/pkg/helper/version"
2931
)
3032

3133
// NOTE: use go build -ldflags "-X github.com/tektoncd/cli/pkg/cmd/version.clientVersion=$(git describe)"
@@ -35,7 +37,7 @@ const devVersion = "dev"
3537
const latestReleaseURL = "https://api.github.com/repos/tektoncd/cli/releases/latest"
3638

3739
// Command returns version command
38-
func Command() *cobra.Command {
40+
func Command(p cli.Params) *cobra.Command {
3941
var check bool
4042

4143
var cmd = &cobra.Command{
@@ -47,6 +49,15 @@ func Command() *cobra.Command {
4749
RunE: func(cmd *cobra.Command, args []string) error {
4850
fmt.Fprintf(cmd.OutOrStdout(), "Client version: %s\n", clientVersion)
4951

52+
cs, err := p.Clients()
53+
if err == nil {
54+
version, _ := version.GetPipelineVersion(cs)
55+
if version == "" {
56+
version = "unknown"
57+
}
58+
fmt.Fprintf(cmd.OutOrStdout(), "Pipeline version: %s\n", version)
59+
}
60+
5061
if !check || clientVersion == devVersion {
5162
return nil
5263
}

pkg/cmd/version/version_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ import (
1818
"context"
1919
"crypto/tls"
2020
"encoding/json"
21+
"fmt"
2122
"net"
2223
"net/http"
2324
"net/http/httptest"
2425
"testing"
2526
"time"
2627

2728
"github.com/tektoncd/cli/pkg/test"
29+
pipelinetest "github.com/tektoncd/pipeline/test"
2830
"gotest.tools/v3/assert"
31+
"gotest.tools/v3/golden"
2932
)
3033

3134
func TestVersionGood(t *testing.T) {
@@ -68,16 +71,20 @@ func TestVersionGood(t *testing.T) {
6871
cli.httpClient = httpClient
6972
output, err := checkRelease(cli)
7073
assert.NilError(t, err)
71-
assert.Equal(t, s.expected, output)
74+
golden.Assert(t, output, fmt.Sprintf("%s.golden", t.Name()))
7275
})
7376
}
7477

7578
clientVersion = "v1.2.3"
76-
version := Command()
79+
80+
seedData, _ := test.SeedTestData(t, pipelinetest.Data{})
81+
82+
cs := pipelinetest.Clients{Kube: seedData.Kube}
83+
p := &test.Params{Kube: cs.Kube}
84+
version := Command(p)
7785
got, err := test.ExecuteCommand(version, "version", "")
7886
assert.NilError(t, err)
79-
assert.Equal(t, "Client version: "+clientVersion+"\n", got)
80-
87+
golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name()))
8188
}
8289

8390
func TestVersionBad(t *testing.T) {

pkg/helper/version/version.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/tektoncd/cli/pkg/cli"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
)
10+
11+
const pipelineNamespace = "tekton-pipelines"
12+
13+
// GetPipelineVersion Get pipeline version, functions imported from Dashboard
14+
func GetPipelineVersion(c *cli.Clients) (string, error) {
15+
version := ""
16+
17+
listOptions := metav1.ListOptions{
18+
LabelSelector: "app.kubernetes.io/component=controller,app.kubernetes.io/name=tekton-pipelines",
19+
}
20+
21+
deployments, err := c.Kube.AppsV1().Deployments(pipelineNamespace).List(listOptions)
22+
if err != nil {
23+
return "", err
24+
}
25+
26+
for _, deployment := range deployments.Items {
27+
deploymentAnnotations := deployment.Spec.Template.GetAnnotations()
28+
29+
// For master of Tekton Pipelines
30+
version = deploymentAnnotations["pipeline.tekton.dev/release"]
31+
32+
// For Tekton Pipelines 0.10.0 + 0.10.1
33+
if version == "" {
34+
version = deploymentAnnotations["tekton.dev/release"]
35+
}
36+
37+
// For Tekton Pipelines 0.9.0 - 0.9.2
38+
if version == "" {
39+
deploymentImage := deployment.Spec.Template.Spec.Containers[0].Image
40+
if strings.Contains(deploymentImage, "pipeline/cmd/controller") && strings.Contains(deploymentImage, ":") && strings.Contains(deploymentImage, "@") {
41+
s := strings.SplitAfter(deploymentImage, ":")
42+
if strings.Contains(s[1], "@") {
43+
t := strings.Split(s[1], "@")
44+
version = t[0]
45+
}
46+
}
47+
}
48+
}
49+
50+
if version == "" {
51+
return "", fmt.Errorf("Error getting the tekton pipelines deployment version. Version is unknown")
52+
}
53+
54+
return version, nil
55+
}

0 commit comments

Comments
 (0)