Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions pkg/apis/pipeline/v1/container_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,33 +296,33 @@ func taskArtifactReferenceExists(src string) bool {
}

func validateStepResultReference(s Step) (errs *apis.FieldError) {
errs = errs.Also(errorIfStepResultReferenceinField(s.Name, "name"))
errs = errs.Also(errorIfStepResultReferenceinField(s.Image, "image"))
errs = errs.Also(errorIfStepResultReferenceinField(s.Script, "script"))
errs = errs.Also(errorIfStepResultReferenceinField(string(s.ImagePullPolicy), "imagePullPoliicy"))
errs = errs.Also(errorIfStepResultReferenceinField(s.WorkingDir, "workingDir"))
errs = errs.Also(errorIfStepResultReferencedInField(s.Name, "name"))
errs = errs.Also(errorIfStepResultReferencedInField(s.Image, "image"))
errs = errs.Also(errorIfStepResultReferencedInField(s.Script, "script"))
errs = errs.Also(errorIfStepResultReferencedInField(string(s.ImagePullPolicy), "imagePullPoliicy"))
errs = errs.Also(errorIfStepResultReferencedInField(s.WorkingDir, "workingDir"))
for _, e := range s.EnvFrom {
errs = errs.Also(errorIfStepResultReferenceinField(e.Prefix, "envFrom.prefix"))
errs = errs.Also(errorIfStepResultReferencedInField(e.Prefix, "envFrom.prefix"))
if e.ConfigMapRef != nil {
errs = errs.Also(errorIfStepResultReferenceinField(e.ConfigMapRef.LocalObjectReference.Name, "envFrom.configMapRef"))
errs = errs.Also(errorIfStepResultReferencedInField(e.ConfigMapRef.LocalObjectReference.Name, "envFrom.configMapRef"))
}
if e.SecretRef != nil {
errs = errs.Also(errorIfStepResultReferenceinField(e.SecretRef.LocalObjectReference.Name, "envFrom.secretRef"))
errs = errs.Also(errorIfStepResultReferencedInField(e.SecretRef.LocalObjectReference.Name, "envFrom.secretRef"))
}
}
for _, v := range s.VolumeMounts {
errs = errs.Also(errorIfStepResultReferenceinField(v.Name, "volumeMounts.name"))
errs = errs.Also(errorIfStepResultReferenceinField(v.MountPath, "volumeMounts.mountPath"))
errs = errs.Also(errorIfStepResultReferenceinField(v.SubPath, "volumeMounts.subPath"))
errs = errs.Also(errorIfStepResultReferencedInField(v.Name, "volumeMounts.name"))
errs = errs.Also(errorIfStepResultReferencedInField(v.MountPath, "volumeMounts.mountPath"))
errs = errs.Also(errorIfStepResultReferencedInField(v.SubPath, "volumeMounts.subPath"))
}
for _, v := range s.VolumeDevices {
errs = errs.Also(errorIfStepResultReferenceinField(v.Name, "volumeDevices.name"))
errs = errs.Also(errorIfStepResultReferenceinField(v.DevicePath, "volumeDevices.devicePath"))
errs = errs.Also(errorIfStepResultReferencedInField(v.Name, "volumeDevices.name"))
errs = errs.Also(errorIfStepResultReferencedInField(v.DevicePath, "volumeDevices.devicePath"))
}
return errs
}

func errorIfStepResultReferenceinField(value, fieldName string) (errs *apis.FieldError) {
func errorIfStepResultReferencedInField(value, fieldName string) (errs *apis.FieldError) {
matches := resultref.StepResultRegex.FindAllStringSubmatch(value, -1)
if len(matches) > 0 {
errs = errs.Also(&apis.FieldError{
Expand Down Expand Up @@ -369,7 +369,7 @@ func errorIfStepArtifactReferencedInField(value, fieldName string) (errs *apis.F
return errs
}

func validateSidecar(errs *apis.FieldError, sc Sidecar) *apis.FieldError {
func (sc *Sidecar) Validate(ctx context.Context) (errs *apis.FieldError) {
if sc.Name == pipeline.ReservedResultsSidecarName {
errs = errs.Also(&apis.FieldError{
Message: fmt.Sprintf("Invalid: cannot use reserved sidecar name %v ", sc.Name),
Expand Down
78 changes: 78 additions & 0 deletions pkg/apis/pipeline/v1/container_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package v1_test

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
cfgtesting "github.com/tektoncd/pipeline/pkg/apis/config/testing"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/test/diff"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -167,3 +170,78 @@ func TestRef_Invalid(t *testing.T) {
})
}
}

func TestSidecarValidate(t *testing.T) {
tests := []struct {
name string
sidecar v1.Sidecar
}{{
name: "valid sidecar",
sidecar: v1.Sidecar{
Name: "my-sidecar",
Image: "my-image",
},
}}

for _, sct := range tests {
t.Run(sct.name, func(t *testing.T) {
err := sct.sidecar.Validate(context.Background())
if err != nil {
t.Errorf("Sidecar.Validate() returned error for valid Sidecar: %v", err)
}
})
}
}

func TestSidecarValidateError(t *testing.T) {
tests := []struct {
name string
sidecar v1.Sidecar
expectedError apis.FieldError
}{{
name: "cannot use reserved sidecar name",
sidecar: v1.Sidecar{
Name: "tekton-log-results",
Image: "my-image",
},
expectedError: apis.FieldError{
Message: fmt.Sprintf("Invalid: cannot use reserved sidecar name %v ", pipeline.ReservedResultsSidecarName),
Paths: []string{"name"},
},
}, {
name: "missing image",
sidecar: v1.Sidecar{
Name: "tekton-invalid-side-car",
},
expectedError: apis.FieldError{
Message: "missing field(s)",
Paths: []string{"image"},
},
}, {
name: "invalid command with script",
sidecar: v1.Sidecar{
Name: "tekton-invalid-side-car",
Image: "ubuntu",
Command: []string{"command foo"},
Script: `
#!/usr/bin/env bash
echo foo`,
},
expectedError: apis.FieldError{
Message: "script cannot be used with command",
Paths: []string{"script"},
},
}}

for _, sct := range tests {
t.Run(sct.name, func(t *testing.T) {
err := sct.sidecar.Validate(context.Background())
if err == nil {
t.Fatalf("Expected an error, got nothing for %v", sct.sidecar)
}
if d := cmp.Diff(sct.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("Sidecar.Validate() errors diff %s", diff.PrintWantGot(d))
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/v1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ type TaskList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []Task `json:"items"`
}

// SidecarList is a list of Sidecars
type SidecarList []Sidecar
8 changes: 4 additions & 4 deletions pkg/apis/pipeline/v1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (ts *TaskSpec) Validate(ctx context.Context) (errs *apis.FieldError) {
}

errs = errs.Also(validateSteps(ctx, mergedSteps).ViaField("steps"))
errs = errs.Also(validateSidecars(ts.Sidecars).ViaField("sidecars"))
errs = errs.Also(SidecarList(ts.Sidecars).Validate(ctx).ViaField("sidecars"))
errs = errs.Also(ValidateParameterTypes(ctx, ts.Params).ViaField("params"))
errs = errs.Also(ValidateParameterVariables(ctx, ts.Steps, ts.Params))
errs = errs.Also(validateTaskContextVariables(ctx, ts.Steps))
Expand Down Expand Up @@ -248,9 +248,9 @@ func ValidateStepResultsVariables(ctx context.Context, results []StepResult, scr
return errs
}

func validateSidecars(sidecars []Sidecar) (errs *apis.FieldError) {
for _, sc := range sidecars {
errs = validateSidecar(errs, sc)
func (l SidecarList) Validate(ctx context.Context) (errs *apis.FieldError) {
for _, sc := range l {
errs = errs.Also(sc.Validate(ctx))
}
return errs
}
Expand Down
85 changes: 0 additions & 85 deletions pkg/apis/pipeline/v1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/config"
cfgtesting "github.com/tektoncd/pipeline/pkg/apis/config/testing"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/test/diff"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -1976,90 +1975,6 @@ func TestTaskSpecValidateErrorWithArtifactsRef(t *testing.T) {
})
}
}
func TestTaskSpecValidateErrorSidecars(t *testing.T) {
tests := []struct {
name string
sidecars []v1.Sidecar
expectedError apis.FieldError
}{{
name: "missing image",
sidecars: []v1.Sidecar{{
Name: "tekton-invalid-side-car",
}},
expectedError: apis.FieldError{
Message: "missing field(s)",
Paths: []string{"sidecars.image"},
},
}, {
name: "invalid command with script",
sidecars: []v1.Sidecar{{
Name: "tekton-invalid-side-car",
Image: "ubuntu",
Command: []string{"command foo"},
Script: `
#!/usr/bin/env bash
echo foo`,
}},
expectedError: apis.FieldError{
Message: "script cannot be used with command",
Paths: []string{"sidecars.script"},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := &v1.TaskSpec{
Steps: []v1.Step{{
Name: "does-not-matter",
Image: "does-not-matter",
}},
Sidecars: tt.sidecars,
}
err := ts.Validate(context.Background())
if err == nil {
t.Fatalf("Expected an error, got nothing for %v", ts)
}
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d))
}
})
}
}

func TestTaskSpecValidateErrorSidecarName(t *testing.T) {
tests := []struct {
name string
sidecars []v1.Sidecar
expectedError apis.FieldError
}{{
name: "cannot use reserved sidecar name",
sidecars: []v1.Sidecar{{
Name: "tekton-log-results",
Image: "my-image",
}},
expectedError: apis.FieldError{
Message: fmt.Sprintf("Invalid: cannot use reserved sidecar name %v ", pipeline.ReservedResultsSidecarName),
Paths: []string{"sidecars.name"},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := &v1.TaskSpec{
Steps: []v1.Step{{
Name: "does-not-matter",
Image: "does-not-matter",
}},
Sidecars: tt.sidecars,
}
err := ts.Validate(context.Background())
if err == nil {
t.Fatalf("Expected an error, got nothing for %v", ts)
}
if d := cmp.Diff(tt.expectedError.Error(), err.Error(), cmpopts.IgnoreUnexported(apis.FieldError{})); d != "" {
t.Errorf("TaskSpec.Validate() errors diff %s", diff.PrintWantGot(d))
}
})
}
}

func TestStepAndSidecarWorkspaces(t *testing.T) {
type fields struct {
Expand Down
22 changes: 22 additions & 0 deletions pkg/apis/pipeline/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading