De-share the Handler struct in core API (#105979)
* De-share the Handler struct in core API An upcoming PR adds a handler that only applies on one of these paths. Having fields that don't work seems bad. This never should have been shared. Lifecycle hooks are like a "write" while probes are more like a "read". HTTPGet and TCPSocket don't really make sense as lifecycle hooks (but I can't take that back). When we add gRPC, it is EXPLICITLY a health check (defined by gRPC) not an arbitrary RPC - so a probe makes sense but a hook does not. In the future I can also see adding lifecycle hooks that don't make sense as probes. E.g. 'sleep' is a common lifecycle request. The only option is `exec`, which requires having a sleep binary in your image. * Run update scripts
This commit is contained in:
@@ -39,7 +39,7 @@ import (
|
||||
|
||||
// HandlerRunner runs a lifecycle handler for a container.
|
||||
type HandlerRunner interface {
|
||||
Run(containerID ContainerID, pod *v1.Pod, container *v1.Container, handler *v1.Handler) (string, error)
|
||||
Run(containerID ContainerID, pod *v1.Pod, container *v1.Container, handler *v1.LifecycleHandler) (string, error)
|
||||
}
|
||||
|
||||
// RuntimeHelper wraps kubelet to make container runtime
|
||||
|
||||
@@ -255,7 +255,7 @@ func TestLifeCycleHook(t *testing.T) {
|
||||
},
|
||||
}
|
||||
cmdPostStart := &v1.Lifecycle{
|
||||
PostStart: &v1.Handler{
|
||||
PostStart: &v1.LifecycleHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"PostStartCMD"},
|
||||
},
|
||||
@@ -263,7 +263,7 @@ func TestLifeCycleHook(t *testing.T) {
|
||||
}
|
||||
|
||||
httpLifeCycle := &v1.Lifecycle{
|
||||
PreStop: &v1.Handler{
|
||||
PreStop: &v1.LifecycleHandler{
|
||||
HTTPGet: &v1.HTTPGetAction{
|
||||
Host: "testHost.com",
|
||||
Path: "/GracefulExit",
|
||||
@@ -272,7 +272,7 @@ func TestLifeCycleHook(t *testing.T) {
|
||||
}
|
||||
|
||||
cmdLifeCycle := &v1.Lifecycle{
|
||||
PreStop: &v1.Handler{
|
||||
PreStop: &v1.LifecycleHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"PreStopCMD"},
|
||||
},
|
||||
|
||||
@@ -67,7 +67,7 @@ type annotatedContainerInfo struct {
|
||||
PodTerminationGracePeriod *int64
|
||||
TerminationMessagePath string
|
||||
TerminationMessagePolicy v1.TerminationMessagePolicy
|
||||
PreStopHandler *v1.Handler
|
||||
PreStopHandler *v1.LifecycleHandler
|
||||
ContainerPorts []v1.ContainerPort
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ func getContainerInfoFromAnnotations(annotations map[string]string) *annotatedCo
|
||||
klog.ErrorS(err, "Unable to get label value from annotations", "label", podTerminationGracePeriodLabel, "annotations", annotations)
|
||||
}
|
||||
|
||||
preStopHandler := &v1.Handler{}
|
||||
preStopHandler := &v1.LifecycleHandler{}
|
||||
if found, err := getJSONObjectFromLabel(annotations, containerPreStopHandlerLabel, preStopHandler); err != nil {
|
||||
klog.ErrorS(err, "Unable to get label value from annotations", "label", containerPreStopHandlerLabel, "annotations", annotations)
|
||||
} else if found {
|
||||
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
@@ -31,7 +31,7 @@ func TestContainerLabels(t *testing.T) {
|
||||
terminationGracePeriod := int64(10)
|
||||
lifecycle := &v1.Lifecycle{
|
||||
// Left PostStart as nil
|
||||
PreStop: &v1.Handler{
|
||||
PreStop: &v1.LifecycleHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"action1", "action2"},
|
||||
},
|
||||
@@ -100,7 +100,7 @@ func TestContainerAnnotations(t *testing.T) {
|
||||
}
|
||||
lifecycle := &v1.Lifecycle{
|
||||
// Left PostStart as nil
|
||||
PreStop: &v1.Handler{
|
||||
PreStop: &v1.LifecycleHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"action1", "action2"},
|
||||
},
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/klog/v2"
|
||||
@@ -56,7 +56,7 @@ func NewHandlerRunner(httpGetter kubetypes.HTTPGetter, commandRunner kubecontain
|
||||
}
|
||||
}
|
||||
|
||||
func (hr *handlerRunner) Run(containerID kubecontainer.ContainerID, pod *v1.Pod, container *v1.Container, handler *v1.Handler) (string, error) {
|
||||
func (hr *handlerRunner) Run(containerID kubecontainer.ContainerID, pod *v1.Pod, container *v1.Container, handler *v1.LifecycleHandler) (string, error) {
|
||||
switch {
|
||||
case handler.Exec != nil:
|
||||
var msg string
|
||||
@@ -105,7 +105,7 @@ func resolvePort(portReference intstr.IntOrString, container *v1.Container) (int
|
||||
return -1, fmt.Errorf("couldn't find port: %v in %v", portReference, container)
|
||||
}
|
||||
|
||||
func (hr *handlerRunner) runHTTPHandler(pod *v1.Pod, container *v1.Container, handler *v1.Handler) (string, error) {
|
||||
func (hr *handlerRunner) runHTTPHandler(pod *v1.Pod, container *v1.Container, handler *v1.LifecycleHandler) (string, error) {
|
||||
host := handler.HTTPGet.Host
|
||||
if len(host) == 0 {
|
||||
status, err := hr.containerManager.GetPodStatus(pod.UID, pod.Name, pod.Namespace)
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
||||
"k8s.io/kubernetes/pkg/kubelet/util/format"
|
||||
@@ -99,7 +99,7 @@ func TestRunHandlerExec(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &v1.Lifecycle{
|
||||
PostStart: &v1.Handler{
|
||||
PostStart: &v1.LifecycleHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"ls", "-a"},
|
||||
},
|
||||
@@ -142,7 +142,7 @@ func TestRunHandlerHttp(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &v1.Lifecycle{
|
||||
PostStart: &v1.Handler{
|
||||
PostStart: &v1.LifecycleHandler{
|
||||
HTTPGet: &v1.HTTPGetAction{
|
||||
Host: "foo",
|
||||
Port: intstr.FromInt(8080),
|
||||
@@ -175,7 +175,7 @@ func TestRunHandlerNil(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &v1.Lifecycle{
|
||||
PostStart: &v1.Handler{},
|
||||
PostStart: &v1.LifecycleHandler{},
|
||||
},
|
||||
}
|
||||
pod := v1.Pod{}
|
||||
@@ -200,7 +200,7 @@ func TestRunHandlerExecFailure(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &v1.Lifecycle{
|
||||
PostStart: &v1.Handler{
|
||||
PostStart: &v1.LifecycleHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: command,
|
||||
},
|
||||
@@ -234,7 +234,7 @@ func TestRunHandlerHttpFailure(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &v1.Lifecycle{
|
||||
PostStart: &v1.Handler{
|
||||
PostStart: &v1.LifecycleHandler{
|
||||
HTTPGet: &v1.HTTPGetAction{
|
||||
Host: "foo",
|
||||
Port: intstr.FromInt(8080),
|
||||
|
||||
@@ -75,7 +75,7 @@ func getTestPod() *v1.Pod {
|
||||
|
||||
func setTestProbe(pod *v1.Pod, probeType probeType, probeSpec v1.Probe) {
|
||||
// All tests rely on the fake exec prober.
|
||||
probeSpec.Handler = v1.Handler{
|
||||
probeSpec.ProbeHandler = v1.ProbeHandler{
|
||||
Exec: &v1.ExecAction{},
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ func init() {
|
||||
}
|
||||
|
||||
var defaultProbe = &v1.Probe{
|
||||
Handler: v1.Handler{
|
||||
ProbeHandler: v1.ProbeHandler{
|
||||
Exec: &v1.ExecAction{},
|
||||
},
|
||||
TimeoutSeconds: 1,
|
||||
|
||||
@@ -100,7 +100,7 @@ func TestGetURLParts(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Ports: []v1.ContainerPort{{Name: "found", ContainerPort: 93}},
|
||||
LivenessProbe: &v1.Probe{
|
||||
Handler: v1.Handler{
|
||||
ProbeHandler: v1.ProbeHandler{
|
||||
HTTPGet: test.probe,
|
||||
},
|
||||
},
|
||||
@@ -153,7 +153,7 @@ func TestGetTCPAddrParts(t *testing.T) {
|
||||
container := v1.Container{
|
||||
Ports: []v1.ContainerPort{{Name: "found", ContainerPort: 93}},
|
||||
LivenessProbe: &v1.Probe{
|
||||
Handler: v1.Handler{
|
||||
ProbeHandler: v1.ProbeHandler{
|
||||
TCPSocket: test.probe,
|
||||
},
|
||||
},
|
||||
@@ -204,7 +204,7 @@ func TestProbe(t *testing.T) {
|
||||
containerID := kubecontainer.ContainerID{Type: "test", ID: "foobar"}
|
||||
|
||||
execProbe := &v1.Probe{
|
||||
Handler: v1.Handler{
|
||||
ProbeHandler: v1.ProbeHandler{
|
||||
Exec: &v1.ExecAction{},
|
||||
},
|
||||
}
|
||||
@@ -255,7 +255,7 @@ func TestProbe(t *testing.T) {
|
||||
},
|
||||
{ // Probe arguments are passed through
|
||||
probe: &v1.Probe{
|
||||
Handler: v1.Handler{
|
||||
ProbeHandler: v1.ProbeHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"/bin/bash", "-c", "some script"},
|
||||
},
|
||||
@@ -267,7 +267,7 @@ func TestProbe(t *testing.T) {
|
||||
},
|
||||
{ // Probe arguments are passed through
|
||||
probe: &v1.Probe{
|
||||
Handler: v1.Handler{
|
||||
ProbeHandler: v1.ProbeHandler{
|
||||
Exec: &v1.ExecAction{
|
||||
Command: []string{"/bin/bash", "-c", "some $(A) $(B)"},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user