Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: config: allow init containers if not decorated #15432

Closed
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: 19 additions & 11 deletions prow/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ func validateJobBase(v JobBase, jobType prowapi.ProwJobType, podNamespace string
if err := validateAgent(v, podNamespace); err != nil {
return err
}
if err := validatePodSpec(jobType, v.Spec); err != nil {
if err := validatePodSpec(jobType, v.Decorate, v.Spec); err != nil {
return err
}
if err := ValidatePipelineRunSpec(jobType, v.ExtraRefs, v.PipelineRunSpec); err != nil {
Expand Down Expand Up @@ -1644,28 +1644,36 @@ func ValidatePipelineRunSpec(jobType prowapi.ProwJobType, extraRefs []prowapi.Re
return nil
}

func validatePodSpec(jobType prowapi.ProwJobType, spec *v1.PodSpec) error {
func validatePodSpec(jobType prowapi.ProwJobType, decorated bool, spec *v1.PodSpec) error {
if spec == nil {
return nil
}

if len(spec.InitContainers) != 0 {
return errors.New("pod spec may not use init containers")
if len(spec.InitContainers) != 0 && decorated {
return errors.New("pod spec may not use init containers when decorate is true")
}

if n := len(spec.Containers); n != 1 {
return fmt.Errorf("pod spec must specify exactly 1 container, found: %d", n)
if n := len(spec.Containers); n != 1 && decorated {
return fmt.Errorf("pod spec must specify exactly 1 container when decorate is true, found: %d", n)
} else if n == 0 {
return fmt.Errorf("pod spec must specify at leaset 1 container, found: %d", n)
}

for _, env := range spec.Containers[0].Env {
for _, prowEnv := range downwardapi.EnvForType(jobType) {
if env.Name == prowEnv {
// TODO(fejta): consider allowing this
return fmt.Errorf("env %s is reserved", env.Name)
for _, container := range spec.Containers {
for _, env := range container.Env {
for _, prowEnv := range downwardapi.EnvForType(jobType) {
if env.Name == prowEnv {
// TODO(fejta): consider allowing this
return fmt.Errorf("env %s is reserved", env.Name)
}
}
}
}

if !decorated {
return nil
}

for _, mount := range spec.Containers[0].VolumeMounts {
for _, prowMount := range decorate.VolumeMounts() {
if mount.Name == prowMount {
Expand Down
52 changes: 39 additions & 13 deletions prow/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,12 @@ func TestValidatePodSpec(t *testing.T) {
postEnv := sets.NewString(downwardapi.EnvForType(prowapi.PostsubmitJob)...)
preEnv := sets.NewString(downwardapi.EnvForType(prowapi.PresubmitJob)...)
cases := []struct {
name string
jobType prowapi.ProwJobType
spec func(s *v1.PodSpec)
noSpec bool
pass bool
name string
decorate bool
jobType prowapi.ProwJobType
spec func(s *v1.PodSpec)
noSpec bool
pass bool
}{
{
name: "allow nil spec",
Expand All @@ -558,25 +559,45 @@ func TestValidatePodSpec(t *testing.T) {
pass: true,
},
{
name: "reject init containers",
name: "reject init containers",
decorate: true,
spec: func(s *v1.PodSpec) {
s.InitContainers = []v1.Container{
{},
}
},
},
{
name: "allow init containers if not decorated",
decorate: false,
spec: func(s *v1.PodSpec) {
s.InitContainers = []v1.Container{
{},
}
},
pass: true,
},
{
name: "reject 0 containers",
spec: func(s *v1.PodSpec) {
s.Containers = nil
},
},
{
name: "reject 2 containers",
name: "reject 2 containers",
decorate: true,
spec: func(s *v1.PodSpec) {
s.Containers = append(s.Containers, v1.Container{})
},
},
{
name: "allow 2 containers if not decorated",
decorate: false,
spec: func(s *v1.PodSpec) {
s.Containers = append(s.Containers, v1.Container{})
},
pass: true,
},
{
name: "reject reserved presubmit env",
jobType: prowapi.PresubmitJob,
Expand Down Expand Up @@ -620,7 +641,8 @@ func TestValidatePodSpec(t *testing.T) {
},
},
{
name: "reject reserved mount name",
name: "reject reserved mount name",
decorate: true,
spec: func(s *v1.PodSpec) {
s.Containers[0].VolumeMounts = append(s.Containers[0].VolumeMounts, v1.VolumeMount{
Name: decorate.VolumeMounts()[0],
Expand All @@ -629,7 +651,8 @@ func TestValidatePodSpec(t *testing.T) {
},
},
{
name: "reject reserved mount path",
name: "reject reserved mount path",
decorate: true,
spec: func(s *v1.PodSpec) {
s.Containers[0].VolumeMounts = append(s.Containers[0].VolumeMounts, v1.VolumeMount{
Name: "fun",
Expand All @@ -638,7 +661,8 @@ func TestValidatePodSpec(t *testing.T) {
},
},
{
name: "reject conflicting mount paths (decorate in user)",
name: "reject conflicting mount paths (decorate in user)",
decorate: true,
spec: func(s *v1.PodSpec) {
s.Containers[0].VolumeMounts = append(s.Containers[0].VolumeMounts, v1.VolumeMount{
Name: "foo",
Expand All @@ -647,7 +671,8 @@ func TestValidatePodSpec(t *testing.T) {
},
},
{
name: "reject conflicting mount paths (user in decorate)",
name: "reject conflicting mount paths (user in decorate)",
decorate: true,
spec: func(s *v1.PodSpec) {
s.Containers[0].VolumeMounts = append(s.Containers[0].VolumeMounts, v1.VolumeMount{
Name: "foo",
Expand All @@ -656,7 +681,8 @@ func TestValidatePodSpec(t *testing.T) {
},
},
{
name: "reject reserved volume",
name: "reject reserved volume",
decorate: true,
spec: func(s *v1.PodSpec) {
s.Volumes = append(s.Volumes, v1.Volume{Name: decorate.VolumeMounts()[0]})
},
Expand All @@ -681,7 +707,7 @@ func TestValidatePodSpec(t *testing.T) {
} else if tc.spec != nil {
tc.spec(current)
}
switch err := validatePodSpec(jt, current); {
switch err := validatePodSpec(jt, tc.decorate, current); {
case err == nil && !tc.pass:
t.Error("validation failed to raise an error")
case err != nil && tc.pass:
Expand Down