Skip to content

Commit

Permalink
Add a utility to get container name from podoptions
Browse files Browse the repository at this point in the history
This commit adds a utility in `pod.go` to get us container name from podoptions
so that we can use this while creating the Pod and while getting the logs from
pod and we would get the correct container name that was while creating the pod
.
In the next PRs this utility will be consumed by rest of the code.
  • Loading branch information
viveksinghggits committed Sep 8, 2023
1 parent 0234692 commit d3da99f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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)
}

0 comments on commit d3da99f

Please sign in to comment.