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

[Don't Review] Add a utility to get container name from podoptions #2319

Closed
wants to merge 1 commit into from
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
11 changes: 11 additions & 0 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ func GetPodObjectFromPodOptions(cli kubernetes.Interface, opts *PodOptions) (*v1
return pod, nil
}

// ContainerNameFromPodOptsOrDefault returns the container name if it's set in
// the passed `podOptions` value. If not, it's returns the default container
// name. This should be used whenever we create pods for Kanister functions.
func ContainerNameFromPodOptsOrDefault(po *PodOptions) string {
if po.ContainerName != "" {
return po.ContainerName
}

return defaultContainerName
}

// CreatePod creates a pod with a single container based on the specified image
func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions) (*v1.Pod, error) {
pod, err := GetPodObjectFromPodOptions(cli, opts)
Expand Down
24 changes: 24 additions & 0 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,27 @@ func (s *PodSuite) TestSetLifecycleHook(c *C) {
c.Assert(err, IsNil)
c.Assert(pod.Spec.Containers[0].Lifecycle, DeepEquals, lch)
}

func (s *PodControllerTestSuite) TestContainerNameFromPodOptsOrDefault(c *C) {
for _, tc := range []struct {
podOptsContainerName string
expectedContainerName string
}{
{
podOptsContainerName: "conone",
expectedContainerName: "conone",
},
{
podOptsContainerName: "",
expectedContainerName: defaultContainerName,
},
} {
name := ContainerNameFromPodOptsOrDefault(&PodOptions{
ContainerName: tc.podOptsContainerName,
})
c.Assert(name, Equals, tc.expectedContainerName)
}

name := ContainerNameFromPodOptsOrDefault(&PodOptions{})
c.Assert(name, Equals, defaultContainerName)
}