From e3c31c4c8f1f66e72b00967897b6b31ae1a3918c Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Thu, 29 Jul 2021 09:02:18 -0700 Subject: [PATCH 1/2] Use `knative.dev/pkg/changeset` This replaces the need for `./pkg/version` and a flag with the automatic inference of the version of the deployed code from the git metadata symlinked into `kodata/` This package is already used to automatically decorate the zap loggers, and a few other places (iirc). /kind cleanup --- cmd/controller/main.go | 3 -- config/controller.yaml | 2 - pkg/pod/pod.go | 8 +++- pkg/pod/pod_test.go | 22 ++++++++--- pkg/pod/testdata/HEAD | 1 + pkg/reconciler/taskrun/taskrun_test.go | 39 ++++++++++++------- pkg/reconciler/taskrun/testdata/HEAD | 1 + .../taskrun/testdata/refs/heads/v1.2.3 | 1 + pkg/version/version.go | 21 ---------- test/taskrun_test.go | 6 +-- 10 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 pkg/pod/testdata/HEAD create mode 100644 pkg/reconciler/taskrun/testdata/HEAD create mode 100644 pkg/reconciler/taskrun/testdata/refs/heads/v1.2.3 delete mode 100644 pkg/version/version.go diff --git a/cmd/controller/main.go b/cmd/controller/main.go index f435a629d23..755d147144a 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -26,7 +26,6 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun" "github.com/tektoncd/pipeline/pkg/reconciler/taskrun" - "github.com/tektoncd/pipeline/pkg/version" corev1 "k8s.io/api/core/v1" "k8s.io/client-go/rest" filteredinformerfactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered" @@ -51,7 +50,6 @@ var ( prImage = flag.String("pr-image", "", "The container image containing our PR binary.") imageDigestExporterImage = flag.String("imagedigest-exporter-image", "", "The container image containing our image digest exporter binary.") namespace = flag.String("namespace", corev1.NamespaceAll, "Namespace to restrict informer to. Optional, defaults to all namespaces.") - versionGiven = flag.String("version", "devel", "Version of Tekton running") threadsPerController = flag.Int("threads-per-controller", controller.DefaultThreadsPerController, "Threads (goroutines) to create per controller") disableHighAvailability = flag.Bool("disable-ha", false, "Whether to disable high-availability functionality for this component. This flag will be deprecated "+ "and removed when we have promoted this feature to stable, so do not pass it without filing an "+ @@ -61,7 +59,6 @@ var ( func main() { cfg := sharedmain.ParseAndGetConfigOrDie() controller.DefaultThreadsPerController = *threadsPerController - version.SetVersion(*versionGiven) images := pipeline.Images{ EntrypointImage: *entrypointImage, NopImage: *nopImage, diff --git a/config/controller.yaml b/config/controller.yaml index c1de705d7d8..b1b8d3def1b 100644 --- a/config/controller.yaml +++ b/config/controller.yaml @@ -65,8 +65,6 @@ spec: - name: tekton-pipelines-controller image: ko://github.com/tektoncd/pipeline/cmd/controller args: [ - # Version, to be replace at release time - "-version", "devel", # These images are built on-demand by `ko resolve` and are replaced # by image references by digest. "-kubeconfig-writer-image", "ko://github.com/tektoncd/pipeline/cmd/kubeconfigwriter", diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index 02a65c91145..cf38c10fa03 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -26,12 +26,12 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/names" - "github.com/tektoncd/pipeline/pkg/version" "github.com/tektoncd/pipeline/pkg/workspace" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" + "knative.dev/pkg/changeset" ) const ( @@ -290,7 +290,11 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1beta1.TaskRun, taskSpec } podAnnotations := taskRun.Annotations - podAnnotations[ReleaseAnnotation] = version.PipelineVersion + version, err := changeset.Get() + if err != nil { + return nil, err + } + podAnnotations[ReleaseAnnotation] = version if shouldAddReadyAnnotationOnPodCreate(ctx, taskSpec.Sidecars) { podAnnotations[readyAnnotation] = readyAnnotationValue diff --git a/pkg/pod/pod_test.go b/pkg/pod/pod_test.go index 16147e44518..784a5c789a2 100644 --- a/pkg/pod/pod_test.go +++ b/pkg/pod/pod_test.go @@ -19,6 +19,7 @@ package pod import ( "context" "fmt" + "os" "path/filepath" "strconv" "strings" @@ -31,13 +32,13 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/pod" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - "github.com/tektoncd/pipeline/pkg/version" "github.com/tektoncd/pipeline/test/diff" "github.com/tektoncd/pipeline/test/names" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" fakek8s "k8s.io/client-go/kubernetes/fake" + "knative.dev/pkg/changeset" logtesting "knative.dev/pkg/logging/testing" "knative.dev/pkg/system" @@ -57,8 +58,19 @@ var ( featureFlagDisableHomeEnvKey = "disable-home-env-overwrite" featureFlagDisableWorkingDirKey = "disable-working-directory-overwrite" featureFlagSetReadyAnnotationOnPodCreate = "enable-ready-annotation-on-pod-create" + + fakeVersion string ) +func init() { + os.Setenv("KO_DATA_PATH", "./testdata/") + commit, err := changeset.Get() + if err != nil { + panic(err) + } + fakeVersion = commit +} + func TestPodBuild(t *testing.T) { secretsVolume := corev1.Volume{ Name: "tekton-internal-secret-volume-multi-creds-9l9zj", @@ -1464,11 +1476,11 @@ _EOF_ var trAnnotations map[string]string if c.trAnnotation == nil { trAnnotations = map[string]string{ - ReleaseAnnotation: version.PipelineVersion, + ReleaseAnnotation: fakeVersion, } } else { trAnnotations = c.trAnnotation - trAnnotations[ReleaseAnnotation] = version.PipelineVersion + trAnnotations[ReleaseAnnotation] = fakeVersion } tr := &v1beta1.TaskRun{ ObjectMeta: metav1.ObjectMeta{ @@ -1618,11 +1630,11 @@ func TestPodBuildwithAlphaAPIEnabled(t *testing.T) { var trAnnotations map[string]string if c.trAnnotation == nil { trAnnotations = map[string]string{ - ReleaseAnnotation: version.PipelineVersion, + ReleaseAnnotation: fakeVersion, } } else { trAnnotations = c.trAnnotation - trAnnotations[ReleaseAnnotation] = version.PipelineVersion + trAnnotations[ReleaseAnnotation] = fakeVersion } tr := &v1beta1.TaskRun{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/pod/testdata/HEAD b/pkg/pod/testdata/HEAD new file mode 100644 index 00000000000..422bdddf214 --- /dev/null +++ b/pkg/pod/testdata/HEAD @@ -0,0 +1 @@ +a728ce3c3d717fe52ff0b1eb71d357460498115c \ No newline at end of file diff --git a/pkg/reconciler/taskrun/taskrun_test.go b/pkg/reconciler/taskrun/taskrun_test.go index 2e86cc2020a..a836f48212b 100644 --- a/pkg/reconciler/taskrun/taskrun_test.go +++ b/pkg/reconciler/taskrun/taskrun_test.go @@ -22,6 +22,7 @@ import ( "fmt" "net/http/httptest" "net/url" + "os" "regexp" "strings" "testing" @@ -40,7 +41,6 @@ import ( "github.com/tektoncd/pipeline/pkg/reconciler/events/cloudevent" ttesting "github.com/tektoncd/pipeline/pkg/reconciler/testing" "github.com/tektoncd/pipeline/pkg/reconciler/volumeclaim" - "github.com/tektoncd/pipeline/pkg/version" "github.com/tektoncd/pipeline/pkg/workspace" "github.com/tektoncd/pipeline/test" "github.com/tektoncd/pipeline/test/diff" @@ -56,6 +56,7 @@ import ( ktesting "k8s.io/client-go/testing" "k8s.io/client-go/tools/record" "knative.dev/pkg/apis" + "knative.dev/pkg/changeset" cminformer "knative.dev/pkg/configmap/informer" "knative.dev/pkg/controller" "knative.dev/pkg/kmeta" @@ -235,8 +236,18 @@ var ( actualOps = append(actualOps, ops...) return tb.PodInitContainer("place-tools", "override-with-entrypoint:latest", actualOps...) } + fakeVersion string ) +func init() { + os.Setenv("KO_DATA_PATH", "./testdata/") + commit, err := changeset.Get() + if err != nil { + panic(err) + } + fakeVersion = commit +} + func getRunName(tr *v1beta1.TaskRun) string { return strings.Join([]string{tr.Namespace, tr.Name}, "/") } @@ -435,7 +446,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, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -477,7 +488,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, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-with-sa"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -607,7 +618,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, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), 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"), @@ -651,7 +662,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, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-working-dir"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -995,7 +1006,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-run-success-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1041,7 +1052,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-sa-run-success-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-with-sa"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1087,7 +1098,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-substitution-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-task-with-substitution"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-substitution"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1213,7 +1224,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-taskspec-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-taskspec"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), tb.PodOwnerReference("TaskRun", "test-taskrun-with-taskspec", @@ -1279,7 +1290,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-cluster-task-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(clusterTaskNameLabelKey, "test-cluster-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-cluster-task"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1325,7 +1336,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-resource-spec-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), 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", @@ -1392,7 +1403,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-pod-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-with-pod"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), @@ -1437,7 +1448,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-with-credentials-variable-pod-9l9zj", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), 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", @@ -1482,7 +1493,7 @@ func TestReconcile(t *testing.T) { }, wantPod: tb.Pod("test-taskrun-bundle-pod-abcde", tb.PodNamespace("foo"), - tb.PodAnnotation(podconvert.ReleaseAnnotation, version.PipelineVersion), + tb.PodAnnotation(podconvert.ReleaseAnnotation, fakeVersion), tb.PodLabel(taskNameLabelKey, "test-task"), tb.PodLabel(taskRunNameLabelKey, "test-taskrun-bundle"), tb.PodLabel("app.kubernetes.io/managed-by", "tekton-pipelines"), diff --git a/pkg/reconciler/taskrun/testdata/HEAD b/pkg/reconciler/taskrun/testdata/HEAD new file mode 100644 index 00000000000..b68fb28159e --- /dev/null +++ b/pkg/reconciler/taskrun/testdata/HEAD @@ -0,0 +1 @@ +ref: refs/heads/v1.2.3 \ No newline at end of file diff --git a/pkg/reconciler/taskrun/testdata/refs/heads/v1.2.3 b/pkg/reconciler/taskrun/testdata/refs/heads/v1.2.3 new file mode 100644 index 00000000000..422bdddf214 --- /dev/null +++ b/pkg/reconciler/taskrun/testdata/refs/heads/v1.2.3 @@ -0,0 +1 @@ +a728ce3c3d717fe52ff0b1eb71d357460498115c \ No newline at end of file diff --git a/pkg/version/version.go b/pkg/version/version.go deleted file mode 100644 index 096b9f48bcf..00000000000 --- a/pkg/version/version.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2019 The Tekton Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package version - -var PipelineVersion = "" - -func SetVersion(version string) { - PipelineVersion = version -} diff --git a/test/taskrun_test.go b/test/taskrun_test.go index 02a46ebcd29..42046970f57 100644 --- a/test/taskrun_test.go +++ b/test/taskrun_test.go @@ -123,9 +123,9 @@ func TestTaskRunFailure(t *testing.T) { } releaseAnnotation, ok := taskrun.Annotations[pod.ReleaseAnnotation] - // Nightly release tag looks like v20210323-0593c7bef3 - nightlyReleaseRegexp := regexp.MustCompile("^v[0-9]{8}-[0-9a-z]{10}$") - if !ok || !(releaseAnnotation == "devel" || nightlyReleaseRegexp.MatchString(releaseAnnotation)) { + // This should always contain a commit truncated to ~7 characters (based on knative.dev/pkg/changeset) + commitIDRegexp := regexp.MustCompile(`^[a-f0-9]{7}$`) + if !ok || !commitIDRegexp.MatchString(releaseAnnotation) { t.Fatalf("expected Taskrun to be annotated with %s=devel or with nightly release tag, got %s=%s", pod.ReleaseAnnotation, pod.ReleaseAnnotation, releaseAnnotation) } } From 78fc154a019de57dc74d49f0849716f87c2883b3 Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Thu, 29 Jul 2021 11:38:53 -0700 Subject: [PATCH 2/2] Drop sed line --- tekton/publish.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tekton/publish.yaml b/tekton/publish.yaml index 0cb20624815..c524be856c9 100644 --- a/tekton/publish.yaml +++ b/tekton/publish.yaml @@ -129,7 +129,7 @@ spec: done # Rewrite "devel" to params.versionTag - sed -i -e 's/\(pipeline.tekton.dev\/release\): "devel"/\1: "$(params.versionTag)"/g' -e 's/\(app.kubernetes.io\/version\): "devel"/\1: "$(params.versionTag)"/g' -e 's/\(version\): "devel"/\1: "$(params.versionTag)"/g' -e 's/\("-version"\), "devel"/\1, "$(params.versionTag)"/g' ${PROJECT_ROOT}/config/*.yaml + sed -i -e 's/\(pipeline.tekton.dev\/release\): "devel"/\1: "$(params.versionTag)"/g' -e 's/\(app.kubernetes.io\/version\): "devel"/\1: "$(params.versionTag)"/g' -e 's/\(version\): "devel"/\1: "$(params.versionTag)"/g' ${PROJECT_ROOT}/config/*.yaml # Publish images and create release.yaml mkdir -p $OUTPUT_RELEASE_DIR