Skip to content

Commit

Permalink
Merge pull request #7185 from yifan-gu/interface
Browse files Browse the repository at this point in the history
kubelet/container: Move Prober/HandlerRunner interface to container/help...
  • Loading branch information
vmarmol committed Apr 22, 2015
2 parents 97302af + f590134 commit a0cc7c2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
32 changes: 32 additions & 0 deletions pkg/kubelet/container/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package container

import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
)

// HandlerRunner runs a lifecycle handler for a container.
type HandlerRunner interface {
Run(containerID string, pod *api.Pod, container *api.Container, handler *api.Handler) error
}

// Prober checks the healthiness of a container.
type Prober interface {
Probe(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error)
}
7 changes: 2 additions & 5 deletions pkg/kubelet/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@ import (
"strconv"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/dockertools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)

type HandlerRunner interface {
Run(containerID string, pod *api.Pod, container *api.Container, handler *api.Handler) error
}

type handlerRunner struct {
httpGetter httpGetter
commandRunner dockertools.ContainerCommandRunner
containerManager *dockertools.DockerManager
}

// TODO(yifan): Merge commandRunner and containerManager once containerManager implements the ContainerCommandRunner interface.
func NewHandlerRunner(httpGetter httpGetter, commandRunner dockertools.ContainerCommandRunner, containerManager *dockertools.DockerManager) *handlerRunner {
func newHandlerRunner(httpGetter httpGetter, commandRunner dockertools.ContainerCommandRunner, containerManager *dockertools.DockerManager) kubecontainer.HandlerRunner {
return &handlerRunner{
httpGetter: httpGetter,
commandRunner: commandRunner,
Expand Down
8 changes: 4 additions & 4 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func NewMainKubelet(
}

klet.podManager = newBasicPodManager(klet.kubeClient)
klet.prober = NewProber(klet.runner, klet.readinessManager, klet.containerRefManager, klet.recorder)
klet.handlerRunner = NewHandlerRunner(klet.httpClient, klet.runner, klet.containerManager)
klet.prober = newProber(klet.runner, klet.readinessManager, klet.containerRefManager, klet.recorder)
klet.handlerRunner = newHandlerRunner(klet.httpClient, klet.runner, klet.containerManager)

runtimeCache, err := kubecontainer.NewRuntimeCache(containerManager)
if err != nil {
Expand Down Expand Up @@ -317,10 +317,10 @@ type Kubelet struct {
networkPlugin network.NetworkPlugin

// Healthy check prober.
prober *Prober
prober kubecontainer.Prober

// Container lifecycle handler runner.
handlerRunner HandlerRunner
handlerRunner kubecontainer.HandlerRunner

// Container readiness state manager.
readinessManager *kubecontainer.ReadinessManager
Expand Down
12 changes: 6 additions & 6 deletions pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func newTestKubelet(t *testing.T) *TestKubelet {
},
fakeRecorder)
kubelet.containerManager.Puller = &dockertools.FakeDockerPuller{}
kubelet.prober = NewProber(nil, kubelet.readinessManager, kubelet.containerRefManager, kubelet.recorder)
kubelet.handlerRunner = NewHandlerRunner(&fakeHTTP{}, &fakeContainerCommandRunner{}, kubelet.containerManager)
kubelet.prober = newProber(nil, kubelet.readinessManager, kubelet.containerRefManager, kubelet.recorder)
kubelet.handlerRunner = newHandlerRunner(&fakeHTTP{}, &fakeContainerCommandRunner{}, kubelet.containerManager)

return &TestKubelet{kubelet, fakeDocker, mockCadvisor, fakeKubeClient, waitGroup, fakeMirrorClient}
}
Expand Down Expand Up @@ -768,7 +768,7 @@ func TestSyncPodsWithPodInfraCreatesContainerCallsHandler(t *testing.T) {
waitGroup := testKubelet.waitGroup
fakeHttp := fakeHTTP{}
kubelet.httpClient = &fakeHttp
kubelet.handlerRunner = NewHandlerRunner(kubelet.httpClient, &fakeContainerCommandRunner{}, kubelet.containerManager)
kubelet.handlerRunner = newHandlerRunner(kubelet.httpClient, &fakeContainerCommandRunner{}, kubelet.containerManager)
pods := []*api.Pod{
{
ObjectMeta: api.ObjectMeta{
Expand Down Expand Up @@ -1690,7 +1690,7 @@ func TestRunHandlerExec(t *testing.T) {
kubelet := testKubelet.kubelet
fakeDocker := testKubelet.fakeDocker
kubelet.runner = &fakeCommandRunner
kubelet.handlerRunner = NewHandlerRunner(&fakeHTTP{}, kubelet.runner, kubelet.containerManager)
kubelet.handlerRunner = newHandlerRunner(&fakeHTTP{}, kubelet.runner, kubelet.containerManager)

containerID := "abc1234"
podName := "podFoo"
Expand Down Expand Up @@ -1745,7 +1745,7 @@ func TestRunHandlerHttp(t *testing.T) {
testKubelet := newTestKubelet(t)
kubelet := testKubelet.kubelet
kubelet.httpClient = &fakeHttp
kubelet.handlerRunner = NewHandlerRunner(kubelet.httpClient, &fakeContainerCommandRunner{}, kubelet.containerManager)
kubelet.handlerRunner = newHandlerRunner(kubelet.httpClient, &fakeContainerCommandRunner{}, kubelet.containerManager)

containerID := "abc1234"
podName := "podFoo"
Expand Down Expand Up @@ -1813,7 +1813,7 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
kubelet.httpClient = &fakeHTTP{
err: fmt.Errorf("test error"),
}
kubelet.handlerRunner = NewHandlerRunner(kubelet.httpClient, &fakeContainerCommandRunner{}, kubelet.containerManager)
kubelet.handlerRunner = newHandlerRunner(kubelet.httpClient, &fakeContainerCommandRunner{}, kubelet.containerManager)

pods := []*api.Pod{
{
Expand Down
23 changes: 11 additions & 12 deletions pkg/kubelet/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ import (

const maxProbeRetries = 3

// Prober helps to check the liveness/readiness of a container.
// TODO(yifan): Replace the concrete type with interface later.
type Prober struct {
// prober helps to check the liveness/readiness of a container.
type prober struct {
exec execprobe.ExecProber
http httprobe.HTTPProber
tcp tcprobe.TCPProber
Expand All @@ -52,13 +51,13 @@ type Prober struct {

// NewProber creates a Prober, it takes a command runner and
// several container info managers.
func NewProber(
func newProber(
runner dockertools.ContainerCommandRunner,
readinessManager *kubecontainer.ReadinessManager,
refManager *kubecontainer.RefManager,
recorder record.EventRecorder) *Prober {
recorder record.EventRecorder) kubecontainer.Prober {

return &Prober{
return &prober{
exec: execprobe.New(),
http: httprobe.New(),
tcp: tcprobe.New(),
Expand All @@ -73,7 +72,7 @@ func NewProber(
// Probe checks the liveness/readiness of the given container.
// If the container's liveness probe is unsuccessful, set readiness to false.
// If liveness is successful, do a readiness check and set readiness accordingly.
func (pb *Prober) Probe(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error) {
func (pb *prober) Probe(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error) {
// Probe liveness.
live, err := pb.probeLiveness(pod, status, container, containerID, createdAt)
if err != nil {
Expand Down Expand Up @@ -113,7 +112,7 @@ func (pb *Prober) Probe(pod *api.Pod, status api.PodStatus, container api.Contai

// probeLiveness probes the liveness of a container.
// If the initalDelay since container creation on liveness probe has not passed the probe will return probe.Success.
func (pb *Prober) probeLiveness(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error) {
func (pb *prober) probeLiveness(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error) {
p := container.LivenessProbe
if p == nil {
return probe.Success, nil
Expand All @@ -126,7 +125,7 @@ func (pb *Prober) probeLiveness(pod *api.Pod, status api.PodStatus, container ap

// probeReadiness probes the readiness of a container.
// If the initial delay on the readiness probe has not passed the probe will return probe.Failure.
func (pb *Prober) probeReadiness(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error) {
func (pb *prober) probeReadiness(pod *api.Pod, status api.PodStatus, container api.Container, containerID string, createdAt int64) (probe.Result, error) {
p := container.ReadinessProbe
if p == nil {
return probe.Success, nil
Expand All @@ -139,7 +138,7 @@ func (pb *Prober) probeReadiness(pod *api.Pod, status api.PodStatus, container a

// runProbeWithRetries tries to probe the container in a finite loop, it returns the last result
// if it never succeeds.
func (pb *Prober) runProbeWithRetries(p *api.Probe, pod *api.Pod, status api.PodStatus, container api.Container, containerID string, retires int) (probe.Result, error) {
func (pb *prober) runProbeWithRetries(p *api.Probe, pod *api.Pod, status api.PodStatus, container api.Container, containerID string, retires int) (probe.Result, error) {
var err error
var result probe.Result
for i := 0; i < retires; i++ {
Expand All @@ -151,7 +150,7 @@ func (pb *Prober) runProbeWithRetries(p *api.Probe, pod *api.Pod, status api.Pod
return result, err
}

func (pb *Prober) runProbe(p *api.Probe, pod *api.Pod, status api.PodStatus, container api.Container, containerID string) (probe.Result, error) {
func (pb *prober) runProbe(p *api.Probe, pod *api.Pod, status api.PodStatus, container api.Container, containerID string) (probe.Result, error) {
timeout := time.Duration(p.TimeoutSeconds) * time.Second
if p.Exec != nil {
glog.V(4).Infof("Exec-Probe Pod: %v, Container: %v", pod, container)
Expand Down Expand Up @@ -228,7 +227,7 @@ type execInContainer struct {
run func() ([]byte, error)
}

func (p *Prober) newExecInContainer(pod *api.Pod, container api.Container, containerID string) exec.Cmd {
func (p *prober) newExecInContainer(pod *api.Pod, container api.Container, containerID string) exec.Cmd {
return execInContainer{func() ([]byte, error) {
return p.runner.RunInContainer(containerID, container.LivenessProbe.Exec.Command)
}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func makeTestKubelet(result probe.Result, err error) *Kubelet {
containerRefManager: kubecontainer.NewRefManager(),
}

kl.prober = &Prober{
kl.prober = &prober{
exec: fakeExecProber{
result: result,
err: err,
Expand Down

0 comments on commit a0cc7c2

Please sign in to comment.