From f59e1bec5c6b6566370a5dd5f35671a49f577524 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 7 Oct 2020 11:53:40 +0200 Subject: [PATCH] =?UTF-8?q?Take=20-version=20into=20account=20in=20the=20c?= =?UTF-8?q?ontroller=20=E2=98=95=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Set the `version.PipelineVersion` properly in the controller main so that we annotate the object with the version (instead of a blank string). Signed-off-by: Vincent Demeester --- cmd/controller/main.go | 2 +- pkg/pod/pod.go | 5 ++--- pkg/pod/pod_test.go | 5 +++-- pkg/reconciler/taskrun/taskrun_test.go | 25 +++++++++++++------------ pkg/version/version.go | 4 ++++ test/taskrun_test.go | 6 ++++++ 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/cmd/controller/main.go b/cmd/controller/main.go index a746bf5e93d..da01286aba3 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -60,7 +60,7 @@ var ( func main() { flag.Parse() - version.PipelineVersion = *versionGiven + version.SetVersion(*versionGiven) images := pipeline.Images{ EntrypointImage: *entrypointImage, NopImage: *nopImage, diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index 66c4601a14f..d28a3d67192 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -44,8 +44,7 @@ const ( // These are effectively const, but Go doesn't have such an annotation. var ( - ReleaseAnnotation = "pipeline.tekton.dev/release" - ReleaseAnnotationValue = version.PipelineVersion + ReleaseAnnotation = "pipeline.tekton.dev/release" groupVersionKind = schema.GroupVersionKind{ Group: v1beta1.SchemeGroupVersion.Group, @@ -251,7 +250,7 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1beta1.TaskRun, taskSpec } podAnnotations := taskRun.Annotations - podAnnotations[ReleaseAnnotation] = ReleaseAnnotationValue + podAnnotations[ReleaseAnnotation] = version.PipelineVersion if shouldAddReadyAnnotationOnPodCreate(ctx, taskSpec.Sidecars) { podAnnotations[readyAnnotation] = readyAnnotationValue diff --git a/pkg/pod/pod_test.go b/pkg/pod/pod_test.go index 7e45fbee61c..f966850d96a 100644 --- a/pkg/pod/pod_test.go +++ b/pkg/pod/pod_test.go @@ -30,6 +30,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/system" + "github.com/tektoncd/pipeline/pkg/version" "github.com/tektoncd/pipeline/test/diff" "github.com/tektoncd/pipeline/test/names" corev1 "k8s.io/api/core/v1" @@ -1169,11 +1170,11 @@ script-heredoc-randomly-generated-78c5n var trAnnotations map[string]string if c.trAnnotation == nil { trAnnotations = map[string]string{ - ReleaseAnnotation: ReleaseAnnotationValue, + ReleaseAnnotation: version.PipelineVersion, } } else { trAnnotations = c.trAnnotation - trAnnotations[ReleaseAnnotation] = ReleaseAnnotationValue + trAnnotations[ReleaseAnnotation] = version.PipelineVersion } tr := &v1beta1.TaskRun{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/reconciler/taskrun/taskrun_test.go b/pkg/reconciler/taskrun/taskrun_test.go index ca21e5c0f53..cb79aef0091 100644 --- a/pkg/reconciler/taskrun/taskrun_test.go +++ b/pkg/reconciler/taskrun/taskrun_test.go @@ -38,6 +38,7 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/volumeclaim" "github.com/tektoncd/pipeline/pkg/system" "github.com/tektoncd/pipeline/pkg/timeout" + "github.com/tektoncd/pipeline/pkg/version" "github.com/tektoncd/pipeline/pkg/workspace" test "github.com/tektoncd/pipeline/test" "github.com/tektoncd/pipeline/test/diff" @@ -427,7 +428,7 @@ func TestReconcile_ExplicitDefaultSA(t *testing.T) { taskRun: taskRunSuccess, wantPod: tb.Pod("test-taskrun-run-success-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -471,7 +472,7 @@ func TestReconcile_ExplicitDefaultSA(t *testing.T) { taskRun: taskRunWithSaSuccess, wantPod: tb.Pod("test-taskrun-with-sa-run-success-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-with-sa"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -603,7 +604,7 @@ func TestReconcile_FeatureFlags(t *testing.T) { featureFlag: "disable-home-env-overwrite", wantPod: tb.Pod("test-taskrun-run-home-env-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-task-with-env-var"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-home-env"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -648,7 +649,7 @@ func TestReconcile_FeatureFlags(t *testing.T) { featureFlag: "disable-working-directory-overwrite", wantPod: tb.Pod("test-taskrun-run-working-dir-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-working-dir"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -973,7 +974,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-run-success-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1021,7 +1022,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-sa-run-success-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-with-sa"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1069,7 +1070,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-substitution-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-task-with-substitution"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-substitution"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1204,7 +1205,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-taskspec-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-taskspec"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), tb.PodOwnerReference("TaskRun", "test-taskrun-with-taskspec", @@ -1276,7 +1277,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-cluster-task-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-cluster-task"), tb.PodLabel(clusterTaskNameLabelKey, "test-cluster-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-cluster-task"), @@ -1325,7 +1326,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-resource-spec-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-resource-spec"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), tb.PodOwnerReference("TaskRun", "test-taskrun-with-resource-spec", @@ -1398,7 +1399,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-pod-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-pod"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1445,7 +1446,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-credentials-variable-pod-9l9zj", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, podconvert.ReleaseAnnotationValue), + tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-credentials-variable"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), tb.PodOwnerReference("TaskRun", "test-taskrun-with-credentials-variable", diff --git a/pkg/version/version.go b/pkg/version/version.go index 34ae15ddb6c..096b9f48bcf 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -15,3 +15,7 @@ package version var PipelineVersion = "" + +func SetVersion(version string) { + PipelineVersion = version +} diff --git a/test/taskrun_test.go b/test/taskrun_test.go index 34b87b8fab4..42f952d7367 100644 --- a/test/taskrun_test.go +++ b/test/taskrun_test.go @@ -26,6 +26,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/pod" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -119,6 +120,11 @@ func TestTaskRunFailure(t *testing.T) { if d := cmp.Diff(taskrun.Status.Steps, expectedStepState, ignoreTerminatedFields, ignoreStepFields); d != "" { t.Fatalf("-got, +want: %v", d) } + + releaseAnnotation, ok := taskrun.Annotations[pod.ReleaseAnnotation] + if !ok || releaseAnnotation != "devel" { + t.Fatalf("expected Taskrun to be annotated with %s=devel, got %s=%s", pod.ReleaseAnnotation, pod.ReleaseAnnotation, releaseAnnotation) + } } func TestTaskRunStatus(t *testing.T) {