From b12d220acbcc679033814aaeda0983da0c049c10 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 10 Dec 2019 15:47:36 +0100 Subject: [PATCH] =?UTF-8?q?Move=20contexts=20to=20v1alpha1=20and=20add=20t?= =?UTF-8?q?ests=20=F0=9F=8E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The package it is on doesn't really matter, so adding it to the latests api version. Also adding quick tests for this file. Signed-off-by: Vincent Demeester --- cmd/webhook/main.go | 3 +- .../pipeline/v1alpha1/pipelinerun_defaults.go | 3 +- .../v1alpha1/pipelinerun_defaults_test.go | 3 +- .../pipeline/v1alpha1/taskrun_defaults.go | 3 +- .../v1alpha1/taskrun_defaults_test.go | 3 +- .../{v1alpha1 => v1alpha2}/contexts.go | 2 +- pkg/apis/pipeline/v1alpha2/contexts_test.go | 60 +++++++++++++++++++ pkg/reconciler/pipelinerun/pipelinerun.go | 3 +- pkg/reconciler/taskrun/taskrun.go | 3 +- 9 files changed, 75 insertions(+), 8 deletions(-) rename pkg/apis/pipeline/{v1alpha1 => v1alpha2}/contexts.go (99%) create mode 100644 pkg/apis/pipeline/v1alpha2/contexts_test.go diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index 81751898637..0a614e3cab8 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -23,6 +23,7 @@ import ( apiconfig "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" tklogging "github.com/tektoncd/pipeline/pkg/logging" "github.com/tektoncd/pipeline/pkg/system" "go.uber.org/zap" @@ -104,7 +105,7 @@ func main() { // Decorate contexts with the current state of the config. ctxFunc := func(ctx context.Context) context.Context { - return v1alpha1.WithDefaultConfigurationName(store.ToContext(ctx)) + return v1alpha2.WithDefaultConfigurationName(store.ToContext(ctx)) } controller, err := webhook.New(kubeClient, options, admissionControllers, logger, ctxFunc) diff --git a/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults.go b/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults.go index 0924ccb4159..cf26acad943 100644 --- a/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults.go +++ b/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults.go @@ -21,6 +21,7 @@ import ( "time" "github.com/tektoncd/pipeline/pkg/apis/config" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) @@ -35,7 +36,7 @@ func (prs *PipelineRunSpec) SetDefaults(ctx context.Context) { cfg := config.FromContextOrDefaults(ctx) if prs.Timeout == nil { var timeout *metav1.Duration - if IsUpgradeViaDefaulting(ctx) { + if v1alpha2.IsUpgradeViaDefaulting(ctx) { // This case is for preexisting `TaskRun` before 0.5.0, so let's // add the old default timeout. // Most likely those TaskRun passing here are already done and/or already running diff --git a/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults_test.go b/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults_test.go index 44c90c83042..ac276545ce4 100644 --- a/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults_test.go +++ b/pkg/apis/pipeline/v1alpha1/pipelinerun_defaults_test.go @@ -25,6 +25,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" logtesting "knative.dev/pkg/logging/testing" @@ -97,7 +98,7 @@ func TestPipelineRunDefaulting(t *testing.T) { Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute}, }, }, - wc: v1alpha1.WithUpgradeViaDefaulting, + wc: v1alpha2.WithUpgradeViaDefaulting, }, { name: "PipelineRef default config context", in: &v1alpha1.PipelineRun{ diff --git a/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go b/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go index c785a51ad2f..e221a99435c 100644 --- a/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go +++ b/pkg/apis/pipeline/v1alpha1/taskrun_defaults.go @@ -21,6 +21,7 @@ import ( "time" "github.com/tektoncd/pipeline/pkg/apis/config" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) @@ -39,7 +40,7 @@ func (trs *TaskRunSpec) SetDefaults(ctx context.Context) { if trs.Timeout == nil { var timeout *metav1.Duration - if IsUpgradeViaDefaulting(ctx) { + if v1alpha2.IsUpgradeViaDefaulting(ctx) { // This case is for preexisting `TaskRun` before 0.5.0, so let's // add the old default timeout. // Most likely those TaskRun passing here are already done and/or already running diff --git a/pkg/apis/pipeline/v1alpha1/taskrun_defaults_test.go b/pkg/apis/pipeline/v1alpha1/taskrun_defaults_test.go index 04952310e9e..0ece9fd0269 100644 --- a/pkg/apis/pipeline/v1alpha1/taskrun_defaults_test.go +++ b/pkg/apis/pipeline/v1alpha1/taskrun_defaults_test.go @@ -24,6 +24,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" logtesting "knative.dev/pkg/logging/testing" @@ -138,7 +139,7 @@ func TestTaskRunDefaulting(t *testing.T) { Timeout: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute}, }, }, - wc: v1alpha1.WithUpgradeViaDefaulting, + wc: v1alpha2.WithUpgradeViaDefaulting, }, { name: "TaskRef default config context", in: &v1alpha1.TaskRun{ diff --git a/pkg/apis/pipeline/v1alpha1/contexts.go b/pkg/apis/pipeline/v1alpha2/contexts.go similarity index 99% rename from pkg/apis/pipeline/v1alpha1/contexts.go rename to pkg/apis/pipeline/v1alpha2/contexts.go index 1b7daa482b3..54a95823688 100644 --- a/pkg/apis/pipeline/v1alpha1/contexts.go +++ b/pkg/apis/pipeline/v1alpha2/contexts.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package v1alpha1 +package v1alpha2 import "context" diff --git a/pkg/apis/pipeline/v1alpha2/contexts_test.go b/pkg/apis/pipeline/v1alpha2/contexts_test.go new file mode 100644 index 00000000000..094d6b62279 --- /dev/null +++ b/pkg/apis/pipeline/v1alpha2/contexts_test.go @@ -0,0 +1,60 @@ +/* +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 v1alpha2 + +import ( + "context" + "testing" +) + +func TestContexts(t *testing.T) { + ctx := context.Background() + tests := []struct { + name string + ctx context.Context + check func(context.Context) bool + want bool + }{{ + name: "has default config name", + ctx: WithDefaultConfigurationName(ctx), + check: HasDefaultConfigurationName, + want: true, + }, { + name: "doesn't have default config name", + ctx: ctx, + check: HasDefaultConfigurationName, + want: false, + }, { + name: "are upgrading via defaulting", + ctx: WithUpgradeViaDefaulting(ctx), + check: IsUpgradeViaDefaulting, + want: true, + }, { + name: "aren't upgrading via defaulting", + ctx: ctx, + check: IsUpgradeViaDefaulting, + want: false, + }} + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := tc.check(tc.ctx) + if tc.want != got { + t.Errorf("check() = %v, wanted %v", tc.want, got) + } + }) + } +} diff --git a/pkg/reconciler/pipelinerun/pipelinerun.go b/pkg/reconciler/pipelinerun/pipelinerun.go index fa369abb8ee..6bcf4535a5b 100644 --- a/pkg/reconciler/pipelinerun/pipelinerun.go +++ b/pkg/reconciler/pipelinerun/pipelinerun.go @@ -27,6 +27,7 @@ import ( apisconfig "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" "github.com/tektoncd/pipeline/pkg/artifacts" listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/reconciler" @@ -230,7 +231,7 @@ func (c *Reconciler) getPipelineFunc(tr *v1alpha1.PipelineRun) resources.GetPipe func (c *Reconciler) reconcile(ctx context.Context, pr *v1alpha1.PipelineRun) error { // We may be reading a version of the object that was stored at an older version // and may not have had all of the assumed default specified. - pr.SetDefaults(v1alpha1.WithUpgradeViaDefaulting(ctx)) + pr.SetDefaults(v1alpha2.WithUpgradeViaDefaulting(ctx)) getPipelineFunc := c.getPipelineFunc(pr) pipelineMeta, pipelineSpec, err := resources.GetPipelineData(pr, getPipelineFunc) diff --git a/pkg/reconciler/taskrun/taskrun.go b/pkg/reconciler/taskrun/taskrun.go index 4fde0e2cd6c..d48e2edb818 100644 --- a/pkg/reconciler/taskrun/taskrun.go +++ b/pkg/reconciler/taskrun/taskrun.go @@ -28,6 +28,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/config" "github.com/tektoncd/pipeline/pkg/apis/pipeline" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2" listers "github.com/tektoncd/pipeline/pkg/client/listers/pipeline/v1alpha1" podconvert "github.com/tektoncd/pipeline/pkg/pod" "github.com/tektoncd/pipeline/pkg/reconciler" @@ -223,7 +224,7 @@ func (c *Reconciler) getTaskFunc(tr *v1alpha1.TaskRun) (resources.GetTask, v1alp func (c *Reconciler) reconcile(ctx context.Context, tr *v1alpha1.TaskRun) error { // We may be reading a version of the object that was stored at an older version // and may not have had all of the assumed default specified. - tr.SetDefaults(v1alpha1.WithUpgradeViaDefaulting(ctx)) + tr.SetDefaults(v1alpha2.WithUpgradeViaDefaulting(ctx)) // If the taskrun is cancelled, kill resources and update status if tr.IsCancelled() {