|
| 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