Skip to content

Commit

Permalink
Add function GetPodObjectFromPodOptions to fetch pod object before ru…
Browse files Browse the repository at this point in the history
…nning CreatePod (#1722)

* Populate pod object using podOptions in a function separate from CreatePod

* Remove string return type that passes namespace to CreatePod function
  • Loading branch information
shlokc9 committed Nov 9, 2022
1 parent 10c6724 commit 13d446a
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/kube/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ type PodOptions struct {
OwnerReferences []metav1.OwnerReference
}

// 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) {
func GetPodObjectFromPodOptions(cli kubernetes.Interface, opts *PodOptions) (*v1.Pod, error) {
// If Namespace is not specified, use the controller Namespace.
cns, err := GetControllerNamespace()
if err != nil {
Expand Down Expand Up @@ -156,9 +155,21 @@ func CreatePod(ctx context.Context, cli kubernetes.Interface, opts *PodOptions)
pod.ObjectMeta.Labels[key] = value
}

pod, err = cli.CoreV1().Pods(ns).Create(ctx, pod, metav1.CreateOptions{})
pod.Namespace = ns

return pod, nil
}

// 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)
if err != nil {
return nil, errors.Wrapf(err, "Failed to get pod from podOptions. Namespace: %s, NameFmt: %s", pod.Namespace, opts.GenerateName)
}

pod, err = cli.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Failed to create pod. Namespace: %s, NameFmt: %s", ns, opts.GenerateName)
return nil, errors.Wrapf(err, "Failed to create pod. Namespace: %s, NameFmt: %s", pod.Namespace, opts.GenerateName)
}
return pod, nil
}
Expand Down

0 comments on commit 13d446a

Please sign in to comment.