Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve get pipelines version func #947

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,45 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
pipelinesControllerSelector string = "app.kubernetes.io/part-of=tekton-pipelines,app.kubernetes.io/component=controller,app.kubernetes.io/name=controller"
oldpipelinesControllerSelector string = "app.kubernetes.io/component=controller,app.kubernetes.io/name=tekton-pipelines"
)

// GetPipelineVersion Get pipeline version, functions imported from Dashboard
func GetPipelineVersion(c *cli.Clients) (string, error) {
version := ""
namespaces := []string{"tekton-pipelines", "openshift-pipelines"}
deploymentsList, err := getDeployments(c)

listOptions := metav1.ListOptions{
LabelSelector: "app.kubernetes.io/component=controller,app.kubernetes.io/name=tekton-pipelines",
if err != nil {
return "", err
}

var deployments []v1.Deployment
for _, namespace := range namespaces {
d, _ := c.Kube.AppsV1().Deployments(namespace).List(listOptions)
deployments = append(deployments, d.Items...)
}
version := findVersion(deploymentsList.Items)

if len(deployments) > 0 {
version = findVersion(deployments)
if version == "" {
return "", fmt.Errorf("Error getting the tekton pipelines deployment version. Version is unknown")
}

if version == "" {
d, _ := c.Kube.AppsV1().Deployments("").List(listOptions)
deployments = append(deployments, d.Items...)
version = findVersion(deployments)
return version, nil
}

// Get deployments for either Tekton Triggers, Tekton Dashboard or Tekton Pipelines
func getDeployments(c *cli.Clients) (*v1.DeploymentList, error) {
deployments, err := c.Kube.AppsV1().Deployments("").List(metav1.ListOptions{LabelSelector: pipelinesControllerSelector})
if err != nil {
return nil, err
}

if version == "" {
return "", fmt.Errorf("Error getting the tekton pipelines deployment version. Version is unknown")
// NOTE: If the new labels selector returned nothing, try with old labels selector
// The old labels selectors are deprecated and should be removed at some point
if deployments == nil || len(deployments.Items) == 0 {
deployments, err = c.Kube.AppsV1().Deployments("").List(metav1.ListOptions{LabelSelector: oldpipelinesControllerSelector})
if err != nil {
return nil, err
}
}

return version, nil
return deployments, err
}

func findVersion(deployments []v1.Deployment) string {
Expand All @@ -62,7 +71,12 @@ func findVersion(deployments []v1.Deployment) string {
deploymentAnnotations := deployment.Spec.Template.GetAnnotations()

// For master of Tekton Pipelines
version = deploymentLabels["pipeline.tekton.dev/release"]
version = deploymentLabels["app.kubernetes.io/version"]

// For Tekton Pipelines 0.11.*
if version == "" {
version = deploymentLabels["pipeline.tekton.dev/release"]
}

// For Tekton Pipelines 0.10.0 + 0.10.1 tekton.dev/release has been set as annotation
if version == "" {
Expand Down
50 changes: 34 additions & 16 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ import (
)

func TestGetPipelineVersion(t *testing.T) {
oldDeploymentLabels := map[string]string{
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/name": "tekton-pipelines",
}

newDeploymentLabels := map[string]string{
"app.kubernetes.io/part-of": "tekton-pipelines",
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/name": "controller",
}

testParams := []struct {
name string
namespace string
Expand All @@ -36,25 +47,35 @@ func TestGetPipelineVersion(t *testing.T) {
deployment: &v1.Deployment{},
want: "",
}, {
name: "controller in different namespace",
name: "controller in different namespace (old labels)",
namespace: "test",
deployment: getDeploymentData("dep", "", nil, map[string]string{"tekton.dev/release": "v0.10.0"}),
deployment: getDeploymentData("dep", "", oldDeploymentLabels, nil, map[string]string{"tekton.dev/release": "v0.10.0"}),
want: "v0.10.0",
}, {
name: "deployment spec does not have labels and annotations specific to version",
name: "deployment spec does not have labels and annotations specific to version (old labels)",
namespace: "tekton-pipelines",
deployment: getDeploymentData("dep1", "pipeline/cmd/controller:v0.9.0@sha256:5d23", nil, nil),
deployment: getDeploymentData("dep1", "pipeline/cmd/controller:v0.9.0@sha256:5d23", oldDeploymentLabels, nil, nil),
want: "v0.9.0",
}, {
name: "deployment spec have annotation specific to version",
name: "deployment spec have annotation specific to version (old labels)",
namespace: "openshift-pipelines",
deployment: getDeploymentData("dep2", "", nil, map[string]string{"tekton.dev/release": "v0.10.0"}),
deployment: getDeploymentData("dep2", "", oldDeploymentLabels, nil, map[string]string{"tekton.dev/release": "v0.10.0"}),
want: "v0.10.0",
}, {
name: "deployment spec have labels specific to master version",
name: "deployment spec have labels specific to version (old labels)",
namespace: "tekton-pipelines",
deployment: getDeploymentData("dep3", "", map[string]string{"pipeline.tekton.dev/release": "master"}, nil),
want: "master",
deployment: getDeploymentData("dep3", "", oldDeploymentLabels, map[string]string{"pipeline.tekton.dev/release": "v0.11.0"}, nil),
want: "v0.11.0",
}, {
name: "controller in different namespace (new labels)",
namespace: "test",
deployment: getDeploymentData("dep4", "", newDeploymentLabels, map[string]string{"app.kubernetes.io/version": "master-test"}, nil),
want: "master-test",
}, {
name: "deployment spec have labels specific to master version (new labels)",
namespace: "tekton-pipelines",
deployment: getDeploymentData("dep5", "", newDeploymentLabels, map[string]string{"app.kubernetes.io/version": "master-tekton-pipelines"}, nil),
want: "master-tekton-pipelines",
}}
for _, tp := range testParams {
t.Run(tp.name, func(t *testing.T) {
Expand All @@ -73,20 +94,17 @@ func TestGetPipelineVersion(t *testing.T) {
}
}

func getDeploymentData(name, image string, labels, annotations map[string]string) *v1.Deployment {
func getDeploymentData(name, image string, deploymentLabels, podTemplateLabels, annotations map[string]string) *v1.Deployment {
return &v1.Deployment{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/name": "tekton-pipelines",
},
Name: name,
Labels: deploymentLabels,
},
Spec: v1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Labels: podTemplateLabels,
Annotations: annotations,
},
Spec: corev1.PodSpec{
Expand Down