Skip to content

Commit

Permalink
Move contexts to unversioned package
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyom authored and tekton-robot committed Jun 4, 2021
1 parent aea2ef4 commit ae8419c
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 73 deletions.
3 changes: 2 additions & 1 deletion cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"os"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewDefaultingAdmissionController(ctx context.Context, cmw configmap.Watcher
types,

// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
v1alpha1.WithUpgradeViaDefaulting,
contexts.WithUpgradeViaDefaulting,

// Whether to disallow unknown fields.
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1
package contexts

import "context"

Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/triggers/v1alpha1/cluster_interceptor_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package v1alpha1

import (
"context"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
)

// SetDefaults sets the defaults on the object.
func (it *ClusterInterceptor) SetDefaults(ctx context.Context) {
if IsUpgradeViaDefaulting(ctx) {
if contexts.IsUpgradeViaDefaulting(ctx) {
if svc := it.Spec.ClientConfig.Service; svc != nil {
if svc.Port == nil {
svc.Port = &defaultPort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"testing"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"knative.dev/pkg/ptr"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -66,7 +67,7 @@ func TestClusterInterceptorSetDefaults(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := tc.in
got.SetDefaults(triggersv1.WithUpgradeViaDefaulting(context.Background()))
got.SetDefaults(contexts.WithUpgradeViaDefaulting(context.Background()))
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf("ClusterInterceptor SetDefaults error: %s", diff)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/triggers/v1alpha1/event_listener_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package v1alpha1
import (
"context"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"knative.dev/pkg/logging"
)

// SetDefaults sets the defaults on the object.
func (el *EventListener) SetDefaults(ctx context.Context) {
if IsUpgradeViaDefaulting(ctx) {
if contexts.IsUpgradeViaDefaulting(ctx) {
// set defaults
if el.Spec.Resources.KubernetesResource != nil {
if el.Spec.Resources.KubernetesResource.Replicas != nil && *el.Spec.Resources.KubernetesResource.Replicas == 0 {
Expand Down
9 changes: 5 additions & 4 deletions pkg/apis/triggers/v1alpha1/event_listener_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
"knative.dev/pkg/ptr"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Triggers: []v1alpha1.EventListenerTrigger{{
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Resources: v1alpha1.Resources{
Expand All @@ -105,7 +106,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Resources: v1alpha1.Resources{
Expand All @@ -128,7 +129,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.EventListener{
Spec: v1alpha1.EventListenerSpec{
Triggers: []v1alpha1.EventListenerTrigger{{
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/triggers/v1alpha1/trigger_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package v1alpha1
import (
"context"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"knative.dev/pkg/logging"
)

type triggerSpecBindingArray []*TriggerSpecBinding

// SetDefaults sets the defaults on the object.
func (t *Trigger) SetDefaults(ctx context.Context) {
if !IsUpgradeViaDefaulting(ctx) {
if !contexts.IsUpgradeViaDefaulting(ctx) {
return
}
triggerSpecBindingArray(t.Spec.Bindings).defaultBindings()
Expand Down
13 changes: 7 additions & 6 deletions pkg/apis/triggers/v1alpha1/trigger_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
"github.com/tektoncd/triggers/test"
)
Expand All @@ -46,7 +47,7 @@ func TestTriggerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Bindings: []*v1alpha1.TriggerSpecBinding{{
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestTriggerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Interceptors: []*v1alpha1.TriggerInterceptor{{
Expand All @@ -114,7 +115,7 @@ func TestTriggerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Interceptors: []*v1alpha1.TriggerInterceptor{{
Expand Down Expand Up @@ -150,7 +151,7 @@ func TestTriggerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Interceptors: []*v1alpha1.TriggerInterceptor{{
Expand Down Expand Up @@ -186,7 +187,7 @@ func TestTriggerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Interceptors: []*v1alpha1.TriggerInterceptor{{
Expand Down Expand Up @@ -222,7 +223,7 @@ func TestTriggerSetDefaults(t *testing.T) {
}},
},
},
wc: v1alpha1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1alpha1.Trigger{
Spec: v1alpha1.TriggerSpec{
Interceptors: []*v1alpha1.TriggerInterceptor{{
Expand Down
38 changes: 0 additions & 38 deletions pkg/apis/triggers/v1beta1/contexts.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/apis/triggers/v1beta1/event_listener_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package v1beta1
import (
"context"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"knative.dev/pkg/logging"
"knative.dev/pkg/ptr"
)

// SetDefaults sets the defaults on the object.
func (el *EventListener) SetDefaults(ctx context.Context) {
if IsUpgradeViaDefaulting(ctx) {
if contexts.IsUpgradeViaDefaulting(ctx) {
// set defaults
if el.Spec.Resources.KubernetesResource != nil {
if el.Spec.Resources.KubernetesResource.Replicas != nil && *el.Spec.Resources.KubernetesResource.Replicas == 0 {
Expand Down
17 changes: 9 additions & 8 deletions pkg/apis/triggers/v1beta1/event_listener_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"github.com/tektoncd/triggers/pkg/apis/triggers/v1beta1"
corev1 "k8s.io/api/core/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
}},
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Triggers: []v1beta1.EventListenerTrigger{{
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
DeprecatedReplicas: ptr.Int32(0),
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Resources: v1beta1.Resources{
Expand All @@ -103,7 +104,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
},
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Resources: v1beta1.Resources{
Expand All @@ -122,7 +123,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
},
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Resources: v1beta1.Resources{
Expand All @@ -144,7 +145,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
DeprecatedPodTemplate: &v1beta1.PodTemplate{Tolerations: []corev1.Toleration{{Key: "key"}}},
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Resources: v1beta1.Resources{
Expand All @@ -168,7 +169,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
},
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Resources: v1beta1.Resources{
Expand All @@ -185,7 +186,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
DeprecatedReplicas: ptr.Int32(2),
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Resources: v1beta1.Resources{
Expand All @@ -208,7 +209,7 @@ func TestEventListenerSetDefaults(t *testing.T) {
}},
},
},
wc: v1beta1.WithUpgradeViaDefaulting,
wc: contexts.WithUpgradeViaDefaulting,
want: &v1beta1.EventListener{
Spec: v1beta1.EventListenerSpec{
Triggers: []v1beta1.EventListenerTrigger{{
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/triggers/v1beta1/trigger_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package v1beta1
import (
"context"

"github.com/tektoncd/triggers/pkg/apis/triggers/contexts"
"knative.dev/pkg/logging"
)

type triggerSpecBindingArray []*TriggerSpecBinding

// SetDefaults sets the defaults on the object.
func (t *Trigger) SetDefaults(ctx context.Context) {
if !IsUpgradeViaDefaulting(ctx) {
if !contexts.IsUpgradeViaDefaulting(ctx) {
return
}
triggerSpecBindingArray(t.Spec.Bindings).defaultBindings()
Expand Down
Loading

0 comments on commit ae8419c

Please sign in to comment.