Skip to content
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
27 changes: 27 additions & 0 deletions libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/sirupsen/logrus"
"go.podman.io/common/libnetwork/types"
"go.podman.io/common/pkg/config"
"go.podman.io/image/v5/manifest"
)

// GenerateForKube takes a slice of libpod containers and generates
Expand Down Expand Up @@ -1073,9 +1074,35 @@ func containerToV1Container(ctx context.Context, c *Container, getService bool)
}
dns.Options = dnsOptions
}
if hc := c.config.HealthCheckConfig; hc != nil {
kubeContainer.LivenessProbe = healthConfigToProbe(hc)
}

return kubeContainer, kubeVolumes, &dns, annotations, nil
}

// healthConfigToProbe converts a container's Schema2HealthConfig into a
// Kubernetes Probe for use as a LivenessProbe in generated kube YAML.
func healthConfigToProbe(hc *manifest.Schema2HealthConfig) *v1.Probe {
if hc == nil || len(hc.Test) == 0 {
return nil
}
// Test[0] is the type: NONE, CMD, or CMD-SHELL. NONE means disabled.
if hc.Test[0] == define.HealthConfigTestNone {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine this with the first conditional

return nil
}
probe := &v1.Probe{
Handler: v1.Handler{
Exec: &v1.ExecAction{Command: hc.Test[1:]},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be handling CMD and CMD-SHELL separately here? In CMD-SHELL we want to wrap this with a shell

},
InitialDelaySeconds: int32(hc.StartPeriod.Seconds()),
TimeoutSeconds: int32(hc.Timeout.Seconds()),
PeriodSeconds: int32(hc.Interval.Seconds()),
FailureThreshold: int32(hc.Retries),
}
return probe
}

// portMappingToContainerPort takes a portmapping and converts
// it to a v1.ContainerPort format for kube output
func portMappingToContainerPort(portMappings []types.PortMapping, getService bool) ([]v1.ContainerPort, error) {
Expand Down
36 changes: 35 additions & 1 deletion test/e2e/generate_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2001,4 +2001,38 @@ EXPOSE 2004-2005/tcp`, CITEST_IMAGE)
kube.WaitWithDefaultTimeout()
Expect(kube).Should(ExitWithError(125, "k8s DaemonSets can only have restartPolicy set to Always"))
})
})

It("on container with healthcheck exports LivenessProbe", func() {
ctrName := "test-hc-ctr"
session := podmanTest.Podman([]string{
"create", "--name", ctrName,
"--health-cmd", "CMD /bin/true",
"--health-interval", "10s",
"--health-timeout", "5s",
"--health-retries", "3",
"--health-start-period", "2s",
CITEST_IMAGE, "top",
})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())

kube := podmanTest.Podman([]string{"kube", "generate", ctrName})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(ExitCleanly())

pod := new(v1.Pod)
err := yaml.Unmarshal(kube.Out.Contents(), pod)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Spec.Containers).To(HaveLen(1))

probe := pod.Spec.Containers[0].LivenessProbe
Expect(probe).ToNot(BeNil(), "LivenessProbe should be set when container has a healthcheck")
Expect(probe.Exec).ToNot(BeNil())
Expect(probe.Exec.Command).To(ContainElement("/bin/true"))
Expect(probe.PeriodSeconds).To(Equal(int32(10)))
Expect(probe.TimeoutSeconds).To(Equal(int32(5)))
Expect(probe.FailureThreshold).To(Equal(int32(3)))
Expect(probe.InitialDelaySeconds).To(Equal(int32(2)))
})

})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing ending newline