Skip to content

Commit 313d587

Browse files
committed
Feat: enhance opc version command to display openshift pipelines product version
Signed-off-by: Shubham Bhardwaj <[email protected]>
1 parent 1253f94 commit 313d587

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

pkg/version.go

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,116 @@
11
package opc
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"html/template"
8+
"strings"
79

810
_ "embed"
911

1012
paccli "github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
13+
"github.com/spf13/cobra"
14+
"github.com/tektoncd/cli/pkg/cli"
1115
tkncli "github.com/tektoncd/cli/pkg/cli"
1216
tknversion "github.com/tektoncd/cli/pkg/version"
13-
14-
"github.com/spf13/cobra"
17+
corev1 "k8s.io/api/core/v1"
18+
"k8s.io/apimachinery/pkg/api/errors"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1520
)
1621

1722
var serverFlag = "server"
1823

24+
const operatorInfo string = "tekton-operator-info"
25+
1926
//go:embed version.json
2027
var versionFile string
2128

2229
//go:embed version.tmpl
2330
var versionTmpl string
2431

32+
var defaultNamespaces = []string{"tekton-pipelines", "openshift-pipelines", "tekton-chains", "tekton-operator", "openshift-operators"}
33+
2534
type versions struct {
26-
Opc string `json:"opc"`
27-
Tkn string `json:"tkn"`
28-
Pac string `json:"pac"`
29-
Results string `json:"results"`
30-
ManualApprovalGate string `json:"manualapprovalgate"`
35+
Opc string `json:"opc"`
36+
Tkn string `json:"tkn"`
37+
Pac string `json:"pac"`
38+
Results string `json:"results"`
39+
ManualApprovalGate string `json:"manualapprovalgate"`
40+
OpenShiftPipelines string `json:"openshiftpipelines"`
41+
}
42+
43+
func getConfigMap(c *cli.Clients, name, ns string) (*corev1.ConfigMap, error) {
44+
45+
var (
46+
err error
47+
configMap *corev1.ConfigMap
48+
)
49+
50+
if ns != "" {
51+
configMap, err = c.Kube.CoreV1().ConfigMaps(ns).Get(context.Background(), name, metav1.GetOptions{})
52+
if err != nil {
53+
return nil, err
54+
}
55+
return configMap, nil
56+
}
57+
58+
for _, n := range defaultNamespaces {
59+
configMap, err = c.Kube.CoreV1().ConfigMaps(n).Get(context.Background(), name, metav1.GetOptions{})
60+
if err != nil {
61+
if errors.IsNotFound(err) {
62+
continue
63+
}
64+
if strings.Contains(err.Error(), fmt.Sprintf(`cannot get resource "configmaps" in API group "" in the namespace "%s"`, n)) {
65+
continue
66+
}
67+
return nil, err
68+
}
69+
if configMap != nil {
70+
break
71+
}
72+
}
73+
74+
if configMap == nil {
75+
return nil, fmt.Errorf("ConfigMap with name %s not found in the namespace %s", name, ns)
76+
}
77+
return configMap, nil
78+
}
79+
80+
func GetRedHatOpenShiftPipelinesVersion(c *cli.Clients, ns string) (string, error) {
81+
configMap, err := getConfigMap(c, operatorInfo, ns)
82+
if err != nil {
83+
return "", nil // Not found or inaccessible, return no version
84+
}
85+
86+
// 1. Check for a dedicated "product" field
87+
if product, exists := configMap.Data["product"]; exists && product != "" {
88+
return product, nil
89+
}
90+
91+
// 2. Check for embedded version in the "version" field
92+
if version, exists := configMap.Data["version"]; exists && version != "" {
93+
if strings.Contains(version, "(") && strings.Contains(version, ")") {
94+
parts := strings.SplitN(version, "(", 2)
95+
if len(parts) > 1 {
96+
productVersion := strings.TrimSuffix(strings.TrimSpace(parts[1]), ")")
97+
if strings.HasPrefix(productVersion, "Red Hat OpenShift Pipelines") {
98+
productVersion = strings.TrimSpace(
99+
strings.TrimPrefix(productVersion, "Red Hat OpenShift Pipelines"),
100+
)
101+
return productVersion, nil
102+
}
103+
return productVersion, nil
104+
}
105+
}
106+
}
107+
108+
// 3. Fallback to "rhProduct" field
109+
if rhProduct, exists := configMap.Data["rhProduct"]; exists && rhProduct != "" {
110+
return rhProduct, nil
111+
}
112+
113+
return "", nil
31114
}
32115

33116
func getLiveInformations(iostreams *paccli.IOStreams) error {
@@ -61,6 +144,10 @@ func getLiveInformations(iostreams *paccli.IOStreams) error {
61144
if hubVersion != "" {
62145
fmt.Fprintf(iostreams.Out, "Hub version: %s\n", hubVersion)
63146
}
147+
productVersion, _ := GetRedHatOpenShiftPipelinesVersion(cs, namespace)
148+
if productVersion != "" {
149+
fmt.Fprintf(iostreams.Out, "OpenShift Pipelines: %s\n", productVersion)
150+
}
64151
return nil
65152
}
66153

@@ -94,6 +181,8 @@ func VersionCommand(ioStreams *paccli.IOStreams) *cobra.Command {
94181
fmt.Fprintln(ioStreams.Out, v.Results)
95182
case "manualapprovalgate":
96183
fmt.Fprintln(ioStreams.Out, v.ManualApprovalGate)
184+
case "openShiftpipelines":
185+
fmt.Fprintln(ioStreams.Out, v.OpenShiftPipelines)
97186
default:
98187
return fmt.Errorf("unknown component: %v", args[1])
99188
}

pkg/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"pac": "0.35.0", "tkn": "0.41.0", "results": "0.15.0", "manualapprovalgate": "0.6.0", "opc": "devel"}
1+
{"pac": "0.35.0", "tkn": "0.41.0", "results": "0.15.0", "manualapprovalgate": "0.6.0", "opc": "devel", "openshiftpipelines": "test"}

pkg/version.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Tekton CLI: {{ .Tkn }}
33
Pipelines as Code CLI: {{ .Pac }}
44
Tekton Results CLI: {{ .Results }}
55
Manual Approval Gate CLI: {{ .ManualApprovalGate }}
6+
OpenShift Pipelines: {{ .OpenShiftPipelines }}

0 commit comments

Comments
 (0)