Skip to content

Commit

Permalink
Add lifecycle param to PodOptions struct (#1890)
Browse files Browse the repository at this point in the history
* Add lifecycle param to PodOptions struct

* Linter fix
  • Loading branch information
TimonOmsk authored Feb 8, 2023
1 parent f658f6f commit 6950681
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type PodOptions struct {
RestartPolicy v1.RestartPolicy
OwnerReferences []metav1.OwnerReference
EnvironmentVariables []v1.EnvVar
Lifecycle *v1.Lifecycle
}

func GetPodObjectFromPodOptions(cli kubernetes.Interface, opts *PodOptions) (*v1.Pod, error) {
Expand Down Expand Up @@ -178,6 +179,10 @@ func GetPodObjectFromPodOptions(cli kubernetes.Interface, opts *PodOptions) (*v1
pod.Spec.Containers[0].SecurityContext = opts.ContainerSecurityContext
}

if opts.Lifecycle != nil {
pod.Spec.Containers[0].Lifecycle = opts.Lifecycle
}

for key, value := range opts.Labels {
pod.ObjectMeta.Labels[key] = value
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/kube/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,25 @@ func (s *PodSuite) TestSetPodSecurityContextOverridesPodOverride(c *C) {
c.Assert(*pod.Spec.SecurityContext.RunAsUser, DeepEquals, uidAndGidExpected)
c.Assert(*pod.Spec.SecurityContext.RunAsGroup, DeepEquals, uidAndGidExpected)
}

func (s *PodSuite) TestSetLifecycleHook(c *C) {
lch := &v1.Lifecycle{
PostStart: &v1.LifecycleHandler{
Exec: &v1.ExecAction{
Command: []string{"/bin/bash", "-c", "echo 1"},
},
},
}

po := &PodOptions{
Namespace: s.namespace,
GenerateName: "test-",
Image: consts.LatestKanisterToolsImage,
Command: []string{"sh", "-c", "tail -f /dev/null"},
Lifecycle: lch,
}

pod, err := CreatePod(context.Background(), s.cli, po)
c.Assert(err, IsNil)
c.Assert(pod.Spec.Containers[0].Lifecycle, DeepEquals, lch)
}

0 comments on commit 6950681

Please sign in to comment.