Skip to content

Commit 9f2e483

Browse files
committed
Clean up validation for task and pipeline refs
This commit creates separate files for testing validation of Task and Pipeline references, and moves test cases related to this validation into those files. It also updates the error message for when a bundle is included in a reference without the "enable-tekton-oci-bundles" flag set to "true" to instruct the user to set that flag. In addition, it updates the error message for when resolvers are set without the "enable-api-fields" flag set to "alpha" to instruct the user to set that flag.
1 parent 74e7567 commit 9f2e483

10 files changed

+524
-463
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2022 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
// PipelineRef can be used to refer to a specific instance of a Pipeline.
20+
type PipelineRef struct {
21+
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
22+
Name string `json:"name,omitempty"`
23+
// API version of the referent
24+
// +optional
25+
APIVersion string `json:"apiVersion,omitempty"`
26+
// Bundle url reference to a Tekton Bundle.
27+
// +optional
28+
Bundle string `json:"bundle,omitempty"`
29+
30+
// ResolverRef allows referencing a Pipeline in a remote location
31+
// like a git repo. This field is only supported when the alpha
32+
// feature gate is enabled.
33+
// +optional
34+
ResolverRef `json:",omitempty"`
35+
}

pkg/apis/pipeline/v1beta1/pipelineref_validation.go

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,71 +18,58 @@ package v1beta1
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/google/go-containerregistry/pkg/name"
2324
"github.com/tektoncd/pipeline/pkg/apis/config"
25+
"github.com/tektoncd/pipeline/pkg/apis/version"
2426
"knative.dev/pkg/apis"
2527
)
2628

2729
// Validate ensures that a supplied PipelineRef field is populated
2830
// correctly. No errors are returned for a nil PipelineRef.
2931
func (ref *PipelineRef) Validate(ctx context.Context) (errs *apis.FieldError) {
30-
cfg := config.FromContextOrDefaults(ctx)
3132
if ref == nil {
3233
return
3334
}
34-
if cfg.FeatureFlags.EnableAPIFields == config.AlphaAPIFields {
35-
errs = errs.Also(ref.validateAlphaRef(ctx))
36-
} else {
37-
errs = errs.Also(ref.validateInTreeRef(ctx))
38-
}
39-
return
40-
}
41-
42-
// validateInTreeRef returns errors if the given pipelineRef is not
43-
// valid for Pipelines' built-in resolution machinery.
44-
func (ref *PipelineRef) validateInTreeRef(ctx context.Context) (errs *apis.FieldError) {
45-
cfg := config.FromContextOrDefaults(ctx)
46-
if ref.Resolver != "" {
47-
errs = errs.Also(apis.ErrDisallowedFields("resolver"))
48-
}
49-
if ref.Resource != nil {
50-
errs = errs.Also(apis.ErrDisallowedFields("resource"))
51-
}
52-
if ref.Name == "" {
53-
errs = errs.Also(apis.ErrMissingField("name"))
54-
}
55-
if cfg.FeatureFlags.EnableTektonOCIBundles {
56-
if ref.Bundle != "" && ref.Name == "" {
57-
errs = errs.Also(apis.ErrMissingField("name"))
58-
}
59-
if ref.Bundle != "" {
60-
if _, err := name.ParseReference(ref.Bundle); err != nil {
61-
errs = errs.Also(apis.ErrInvalidValue("invalid bundle reference", "bundle", err.Error()))
62-
}
63-
}
64-
} else if ref.Bundle != "" {
65-
errs = errs.Also(apis.ErrDisallowedFields("bundle"))
66-
}
67-
return
68-
}
6935

70-
// validateAlphaRef ensures that the user has passed either a
71-
// valid remote resource reference or a valid in-tree resource reference,
72-
// but not both.
73-
func (ref *PipelineRef) validateAlphaRef(ctx context.Context) (errs *apis.FieldError) {
7436
switch {
75-
case ref.Resolver == "" && ref.Resource != nil:
76-
errs = errs.Also(apis.ErrMissingField("resolver"))
77-
case ref.Resolver == "":
78-
errs = errs.Also(ref.validateInTreeRef(ctx))
79-
default:
37+
case ref.Resolver != "":
38+
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "resolver", config.AlphaAPIFields).ViaField("resolver"))
8039
if ref.Name != "" {
8140
errs = errs.Also(apis.ErrMultipleOneOf("name", "resolver"))
8241
}
8342
if ref.Bundle != "" {
8443
errs = errs.Also(apis.ErrMultipleOneOf("bundle", "resolver"))
8544
}
45+
case ref.Resource != nil:
46+
errs = errs.Also(version.ValidateEnabledAPIFields(ctx, "resource", config.AlphaAPIFields).ViaField("resource"))
47+
if ref.Name != "" {
48+
errs = errs.Also(apis.ErrMultipleOneOf("name", "resource"))
49+
}
50+
if ref.Bundle != "" {
51+
errs = errs.Also(apis.ErrMultipleOneOf("bundle", "resource"))
52+
}
53+
if ref.Resolver == "" {
54+
errs = errs.Also(apis.ErrMissingField("resolver"))
55+
}
56+
case ref.Name == "":
57+
errs = errs.Also(apis.ErrMissingField("name"))
58+
case ref.Bundle != "":
59+
errs = errs.Also(validateBundleFeatureFlag(ctx, "bundle", true).ViaField("bundle"))
60+
if _, err := name.ParseReference(ref.Bundle); err != nil {
61+
errs = errs.Also(apis.ErrInvalidValue("invalid bundle reference", "bundle", err.Error()))
62+
}
8663
}
8764
return
8865
}
66+
67+
func validateBundleFeatureFlag(ctx context.Context, featureName string, wantValue bool) *apis.FieldError {
68+
flagValue := config.FromContextOrDefaults(ctx).FeatureFlags.EnableTektonOCIBundles
69+
if flagValue != wantValue {
70+
var errs *apis.FieldError
71+
message := fmt.Sprintf(`%s requires "enable-tekton-oci-bundles" feature gate to be %t but it is %t`, featureName, wantValue, flagValue)
72+
return errs.Also(apis.ErrGeneric(message))
73+
}
74+
return nil
75+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
Copyright 2020 The Tekton Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1_test
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/google/go-cmp/cmp"
24+
"github.com/tektoncd/pipeline/pkg/apis/config"
25+
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
26+
"github.com/tektoncd/pipeline/test/diff"
27+
corev1 "k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"knative.dev/pkg/apis"
30+
logtesting "knative.dev/pkg/logging/testing"
31+
)
32+
33+
func TestPipelineRef_Invalid(t *testing.T) {
34+
tests := []struct {
35+
name string
36+
ref *v1beta1.PipelineRef
37+
wantErr *apis.FieldError
38+
withContext func(context.Context) context.Context
39+
}{{
40+
name: "use of bundle without the feature flag set",
41+
ref: &v1beta1.PipelineRef{
42+
Name: "my-pipeline",
43+
Bundle: "docker.io/foo",
44+
},
45+
wantErr: apis.ErrGeneric("bundle requires \"enable-tekton-oci-bundles\" feature gate to be true but it is false"),
46+
}, {
47+
name: "bundle missing name",
48+
ref: &v1beta1.PipelineRef{
49+
Bundle: "docker.io/foo",
50+
},
51+
wantErr: apis.ErrMissingField("name"),
52+
withContext: enableTektonOCIBundles(t),
53+
}, {
54+
name: "invalid bundle reference",
55+
ref: &v1beta1.PipelineRef{
56+
Name: "my-pipeline",
57+
Bundle: "not a valid reference",
58+
},
59+
wantErr: apis.ErrInvalidValue("invalid bundle reference", "bundle", "could not parse reference: not a valid reference"),
60+
withContext: enableTektonOCIBundles(t),
61+
}, {
62+
name: "pipelineRef without Pipeline Name",
63+
ref: &v1beta1.PipelineRef{},
64+
wantErr: apis.ErrMissingField("name"),
65+
}, {
66+
name: "pipelineref resolver disallowed without alpha feature gate",
67+
ref: &v1beta1.PipelineRef{
68+
ResolverRef: v1beta1.ResolverRef{
69+
Resolver: "foo",
70+
},
71+
},
72+
wantErr: apis.ErrGeneric("resolver requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\""),
73+
}, {
74+
name: "pipelineref resource disallowed without alpha feature gate",
75+
ref: &v1beta1.PipelineRef{
76+
ResolverRef: v1beta1.ResolverRef{
77+
Resource: []v1beta1.ResolverParam{},
78+
},
79+
},
80+
wantErr: apis.ErrMissingField("resolver").Also(apis.ErrGeneric("resource requires \"enable-api-fields\" feature gate to be \"alpha\" but it is \"stable\"")),
81+
}, {
82+
name: "pipelineref resource disallowed without resolver",
83+
ref: &v1beta1.PipelineRef{
84+
ResolverRef: v1beta1.ResolverRef{
85+
Resource: []v1beta1.ResolverParam{},
86+
},
87+
},
88+
wantErr: apis.ErrMissingField("resolver"),
89+
withContext: enableAlphaAPIFields,
90+
}, {
91+
name: "pipelineref resolver disallowed in conjunction with pipelineref name",
92+
ref: &v1beta1.PipelineRef{
93+
Name: "foo",
94+
ResolverRef: v1beta1.ResolverRef{
95+
Resolver: "bar",
96+
},
97+
},
98+
wantErr: apis.ErrMultipleOneOf("name", "resolver"),
99+
withContext: enableAlphaAPIFields,
100+
}, {
101+
name: "pipelineref resolver disallowed in conjunction with pipelineref bundle",
102+
ref: &v1beta1.PipelineRef{
103+
Bundle: "foo",
104+
ResolverRef: v1beta1.ResolverRef{
105+
Resolver: "baz",
106+
},
107+
},
108+
wantErr: apis.ErrMultipleOneOf("bundle", "resolver"),
109+
withContext: enableAlphaAPIFields,
110+
}, {
111+
name: "pipelineref resource disallowed in conjunction with taskref name",
112+
ref: &v1beta1.PipelineRef{
113+
Name: "bar",
114+
ResolverRef: v1beta1.ResolverRef{
115+
Resource: []v1beta1.ResolverParam{{
116+
Name: "foo",
117+
Value: "bar",
118+
}},
119+
},
120+
},
121+
wantErr: apis.ErrMultipleOneOf("name", "resource").Also(apis.ErrMissingField("resolver")),
122+
withContext: enableAlphaAPIFields,
123+
}, {
124+
name: "pipelineref resource disallowed in conjunction with taskref bundle",
125+
ref: &v1beta1.PipelineRef{
126+
Bundle: "bar",
127+
ResolverRef: v1beta1.ResolverRef{
128+
Resource: []v1beta1.ResolverParam{{
129+
Name: "foo",
130+
Value: "bar",
131+
}},
132+
},
133+
},
134+
wantErr: apis.ErrMultipleOneOf("bundle", "resource").Also(apis.ErrMissingField("resolver")),
135+
withContext: enableAlphaAPIFields,
136+
}}
137+
138+
for _, tc := range tests {
139+
t.Run(tc.name, func(t *testing.T) {
140+
ctx := context.Background()
141+
if tc.withContext != nil {
142+
ctx = tc.withContext(ctx)
143+
}
144+
err := tc.ref.Validate(ctx)
145+
if d := cmp.Diff(tc.wantErr.Error(), err.Error()); d != "" {
146+
t.Error(diff.PrintWantGot(d))
147+
}
148+
})
149+
}
150+
}
151+
152+
func TestPipelineRef_Valid(t *testing.T) {
153+
tests := []struct {
154+
name string
155+
ref *v1beta1.PipelineRef
156+
wc func(context.Context) context.Context
157+
}{{
158+
name: "no pipelineRef",
159+
ref: nil,
160+
}, {
161+
name: "alpha feature: valid resolver",
162+
ref: &v1beta1.PipelineRef{ResolverRef: v1beta1.ResolverRef{Resolver: "git"}},
163+
wc: enableAlphaAPIFields,
164+
}, {
165+
name: "alpha feature: valid resolver with resource parameters",
166+
ref: &v1beta1.PipelineRef{ResolverRef: v1beta1.ResolverRef{Resolver: "git", Resource: []v1beta1.ResolverParam{{
167+
Name: "repo",
168+
Value: "https://github.com/tektoncd/pipeline.git",
169+
}, {
170+
Name: "branch",
171+
Value: "baz",
172+
}}}},
173+
wc: enableAlphaAPIFields,
174+
}}
175+
176+
for _, ts := range tests {
177+
t.Run(ts.name, func(t *testing.T) {
178+
ctx := context.Background()
179+
if ts.wc != nil {
180+
ctx = ts.wc(ctx)
181+
}
182+
if err := ts.ref.Validate(ctx); err != nil {
183+
t.Error(err)
184+
}
185+
})
186+
}
187+
}
188+
189+
func enableTektonOCIBundles(t *testing.T) func(context.Context) context.Context {
190+
return func(ctx context.Context) context.Context {
191+
s := config.NewStore(logtesting.TestLogger(t))
192+
s.OnConfigChanged(&corev1.ConfigMap{
193+
ObjectMeta: metav1.ObjectMeta{Name: config.GetFeatureFlagsConfigName()},
194+
Data: map[string]string{
195+
"enable-tekton-oci-bundles": "true",
196+
},
197+
})
198+
return s.ToContext(ctx)
199+
}
200+
}

pkg/apis/pipeline/v1beta1/pipelinerun_types.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,6 @@ const (
265265
PipelineRunSpecStatusPending = "PipelineRunPending"
266266
)
267267

268-
// PipelineRef can be used to refer to a specific instance of a Pipeline.
269-
// Copied from CrossVersionObjectReference: https://github.com/kubernetes/kubernetes/blob/169df7434155cbbc22f1532cba8e0a9588e29ad8/pkg/apis/autoscaling/types.go#L64
270-
type PipelineRef struct {
271-
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
272-
Name string `json:"name,omitempty"`
273-
// API version of the referent
274-
// +optional
275-
APIVersion string `json:"apiVersion,omitempty"`
276-
// Bundle url reference to a Tekton Bundle.
277-
// +optional
278-
Bundle string `json:"bundle,omitempty"`
279-
280-
// ResolverRef allows referencing a Pipeline in a remote location
281-
// like a git repo. This field is only supported when the alpha
282-
// feature gate is enabled.
283-
// +optional
284-
ResolverRef `json:",omitempty"`
285-
}
286-
287268
// PipelineRunStatus defines the observed state of PipelineRun
288269
type PipelineRunStatus struct {
289270
duckv1beta1.Status `json:",inline"`

0 commit comments

Comments
 (0)