|
1 | 1 | package opc |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "html/template" |
| 8 | + "strings" |
7 | 9 |
|
8 | 10 | _ "embed" |
9 | 11 |
|
10 | 12 | paccli "github.com/openshift-pipelines/pipelines-as-code/pkg/cli" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/tektoncd/cli/pkg/cli" |
11 | 15 | tkncli "github.com/tektoncd/cli/pkg/cli" |
12 | 16 | 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" |
15 | 20 | ) |
16 | 21 |
|
17 | 22 | var serverFlag = "server" |
18 | 23 |
|
| 24 | +const operatorInfo string = "tekton-operator-info" |
| 25 | + |
19 | 26 | //go:embed version.json |
20 | 27 | var versionFile string |
21 | 28 |
|
22 | 29 | //go:embed version.tmpl |
23 | 30 | var versionTmpl string |
24 | 31 |
|
| 32 | +var defaultNamespaces = []string{"tekton-pipelines", "openshift-pipelines", "tekton-chains", "tekton-operator", "openshift-operators"} |
| 33 | + |
25 | 34 | 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 |
31 | 114 | } |
32 | 115 |
|
33 | 116 | func getLiveInformations(iostreams *paccli.IOStreams) error { |
@@ -61,6 +144,10 @@ func getLiveInformations(iostreams *paccli.IOStreams) error { |
61 | 144 | if hubVersion != "" { |
62 | 145 | fmt.Fprintf(iostreams.Out, "Hub version: %s\n", hubVersion) |
63 | 146 | } |
| 147 | + productVersion, _ := GetRedHatOpenShiftPipelinesVersion(cs, namespace) |
| 148 | + if productVersion != "" { |
| 149 | + fmt.Fprintf(iostreams.Out, "OpenShift Pipelines: %s\n", productVersion) |
| 150 | + } |
64 | 151 | return nil |
65 | 152 | } |
66 | 153 |
|
@@ -94,6 +181,8 @@ func VersionCommand(ioStreams *paccli.IOStreams) *cobra.Command { |
94 | 181 | fmt.Fprintln(ioStreams.Out, v.Results) |
95 | 182 | case "manualapprovalgate": |
96 | 183 | fmt.Fprintln(ioStreams.Out, v.ManualApprovalGate) |
| 184 | + case "openShiftpipelines": |
| 185 | + fmt.Fprintln(ioStreams.Out, v.OpenShiftPipelines) |
97 | 186 | default: |
98 | 187 | return fmt.Errorf("unknown component: %v", args[1]) |
99 | 188 | } |
|
0 commit comments