Skip to content

Commit

Permalink
Add Name to PodOptions (#1695)
Browse files Browse the repository at this point in the history
* add name to pod options

* add unit test

* remove unnecessary else

* address review comments
  • Loading branch information
kale-amruta committed Oct 27, 2022
1 parent 7139cc9 commit 1625355
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type PodOptions struct {
Annotations map[string]string
Command []string
ContainerName string
Name string
GenerateName string
Image string
Labels map[string]string
Expand Down Expand Up @@ -129,6 +130,11 @@ func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions)
Spec: patchedSpecs,
}

// Override `GenerateName` if `Name` option is provided
if opts.Name != "" {
pod.Name = opts.Name
}

// Override default container name if applicable
if opts.ContainerName != "" {
pod.Spec.Containers[0].Name = opts.ContainerName
Expand Down
15 changes: 15 additions & 0 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ func (s *PodSuite) TestPod(c *C) {
"run": "pod",
},
},
{
Namespace: s.namespace,
Name: "testpod",
GenerateName: "test-",
Image: consts.LatestKanisterToolsImage,
ContainerName: "test-container",
Command: []string{"sh", "-c", "tail -f /dev/null"},
Labels: map[string]string{
"run": "pod",
},
},
}

for _, po := range podOptions {
Expand All @@ -188,6 +199,10 @@ func (s *PodSuite) TestPod(c *C) {
c.Check(pod.ObjectMeta.Annotations, DeepEquals, po.Annotations)
}

if po.Name != "" {
c.Assert(pod.ObjectMeta.Name, Equals, po.Name)
}

c.Check(len(pod.ObjectMeta.Labels), Equals, len(po.Labels)+1)
c.Check(pod.ObjectMeta.Labels[consts.LabelKeyCreatedBy], Equals, consts.LabelValueKanister)
for key, value := range po.Labels {
Expand Down

0 comments on commit 1625355

Please sign in to comment.