diff --git a/pkg/apis/riotkit.org/v1alpha1/podfilesystemsync.go b/pkg/apis/riotkit.org/v1alpha1/podfilesystemsync.go index 9915c7d..e257a59 100644 --- a/pkg/apis/riotkit.org/v1alpha1/podfilesystemsync.go +++ b/pkg/apis/riotkit.org/v1alpha1/podfilesystemsync.go @@ -109,14 +109,23 @@ type InitContainerPlacementSpec struct { Placement ContainerPlacement `json:"placement,omitempty"` } +func (ic *InitContainerPlacementSpec) GetPlacement() ContainerPlacement { + if ic.Placement == "" { + return "last" + } + return ic.Placement +} + func (ic *InitContainerPlacementSpec) Validate() error { - if (ic.Placement == "before" || ic.Placement == "after") && ic.ContainerReference == "" { + p := ic.GetPlacement() + + if (p == "before" || p == "after") && ic.ContainerReference == "" { return errors.Errorf("Cannot place container as '%s' to unknown container, when containerReference is not specified. Specify .spec.initContainerPlacement.containerReference", ic.Placement) } - if (ic.Placement == "last" || ic.Placement == "first") && ic.ContainerReference != "" { - return errors.Errorf("Cannot specify .spec.initContainerPlacement.containerReference together with '%v'", ic.Placement) + if (p == "last" || p == "first") && ic.ContainerReference != "" { + return errors.Errorf("Cannot specify .spec.initContainerPlacement.containerReference together with '%v'", ic.GetPlacement()) } - if ic.Placement != "last" && ic.Placement != "first" && ic.Placement != "before" && ic.Placement != "after" { + if p != "last" && p != "first" && p != "before" && p != "after" { return errors.Errorf(".spec.initContainerPlacement.Placement is not one of: last, first, before, after") } return nil diff --git a/pkg/server/mutation/containers.go b/pkg/server/mutation/containers.go index a7516e4..1f07e6f 100644 --- a/pkg/server/mutation/containers.go +++ b/pkg/server/mutation/containers.go @@ -178,21 +178,21 @@ func reorderInitContainer(containers []corev1.Container, containerName string, p copied = append(copied, container) } - if placement.Placement == "first" && foundAtIndex != 0 { + if placement.GetPlacement() == "first" && foundAtIndex != 0 { return append([]corev1.Container{ourContainer}, copied...), nil - } else if placement.Placement == "last" && foundAtIndex+1 != len(containers) { + } else if placement.GetPlacement() == "last" && foundAtIndex+1 != len(containers) { return append(copied, ourContainer), nil - } else if placement.Placement == "before" || placement.Placement == "after" { + } else if placement.GetPlacement() == "before" || placement.GetPlacement() == "after" { var final []corev1.Container for _, container := range copied { - if container.Name == placement.ContainerReference && placement.Placement == "before" { + if container.Name == placement.ContainerReference && placement.GetPlacement() == "before" { final = append(final, ourContainer) } final = append(final, container) - if container.Name == placement.ContainerReference && placement.Placement == "after" { + if container.Name == placement.ContainerReference && placement.GetPlacement() == "after" { final = append(final, ourContainer) } }