Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
fix: Make sure the placement is set to default
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya Baya committed Jul 12, 2022
1 parent fc16cee commit 5d32b23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions pkg/apis/riotkit.org/v1alpha1/podfilesystemsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions pkg/server/mutation/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 5d32b23

Please sign in to comment.