Skip to content

Commit

Permalink
Split the Kubelet flag options and struct
Browse files Browse the repository at this point in the history
Reduces the size of the app/server.go file and ensures that the flags
and their defaults are clearly separated.
  • Loading branch information
smarterclayton committed Jan 7, 2016
1 parent b1e4831 commit 791d160
Show file tree
Hide file tree
Showing 21 changed files with 410 additions and 358 deletions.
7 changes: 4 additions & 3 deletions cmd/hyperkube/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package main

import (
kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
"k8s.io/kubernetes/cmd/kubelet/app"
"k8s.io/kubernetes/cmd/kubelet/app/options"
)

// NewKubelet creates a new hyperkube Server object that includes the
// description and flags.
func NewKubelet() *Server {
s := kubeletapp.NewKubeletServer()
s := options.NewKubeletServer()
hks := Server{
SimpleUsage: "kubelet",
Long: `The kubelet binary is responsible for maintaining a set of containers on a
Expand All @@ -33,7 +34,7 @@ func NewKubelet() *Server {
configuration data, with the running set of containers by starting or stopping
Docker containers.`,
Run: func(_ *Server, _ []string) error {
return s.Run(nil)
return app.Run(s, nil)
},
}
s.AddFlags(hks.Flags())
Expand Down
280 changes: 280 additions & 0 deletions cmd/kubelet/app/options/options.go

Large diffs are not rendered by default.

304 changes: 28 additions & 276 deletions cmd/kubelet/app/server.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions cmd/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
"os"
"runtime"

kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
"k8s.io/kubernetes/cmd/kubelet/app"
"k8s.io/kubernetes/cmd/kubelet/app/options"
"k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/version/verflag"

Expand All @@ -34,7 +35,7 @@ import (

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
s := kubeletapp.NewKubeletServer()
s := options.NewKubeletServer()
s.AddFlags(pflag.CommandLine)

util.InitFlags()
Expand All @@ -43,7 +44,7 @@ func main() {

verflag.PrintAndExitIfRequested()

if err := s.Run(nil); err != nil {
if err := app.Run(s, nil); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand Down
13 changes: 7 additions & 6 deletions contrib/mesos/pkg/executor/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
bindings "github.com/mesos/mesos-go/executor"
"github.com/spf13/pflag"
kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
"k8s.io/kubernetes/cmd/kubelet/app/options"
"k8s.io/kubernetes/contrib/mesos/pkg/executor"
"k8s.io/kubernetes/contrib/mesos/pkg/executor/config"
"k8s.io/kubernetes/contrib/mesos/pkg/hyperkube"
Expand All @@ -42,7 +43,7 @@ import (
)

type KubeletExecutorServer struct {
*kubeletapp.KubeletServer
*options.KubeletServer
SuicideTimeout time.Duration
LaunchGracePeriod time.Duration

Expand All @@ -54,7 +55,7 @@ type KubeletExecutorServer struct {

func NewKubeletExecutorServer() *KubeletExecutorServer {
k := &KubeletExecutorServer{
KubeletServer: kubeletapp.NewKubeletServer(),
KubeletServer: options.NewKubeletServer(),
SuicideTimeout: config.DefaultSuicideTimeout,
LaunchGracePeriod: config.DefaultLaunchGracePeriod,
kletReady: make(chan struct{}),
Expand Down Expand Up @@ -156,7 +157,7 @@ func (s *KubeletExecutorServer) runKubelet(
}
}()

kcfg, err := s.UnsecuredKubeletConfig()
kcfg, err := kubeletapp.UnsecuredKubeletConfig(s.KubeletServer)
if err != nil {
return err
}
Expand Down Expand Up @@ -186,7 +187,7 @@ func (s *KubeletExecutorServer) runKubelet(
kcfg.KubeClient = apiclient

// taken from KubeletServer#Run(*KubeletConfig)
eventClientConfig, err := s.CreateAPIServerClientConfig()
eventClientConfig, err := kubeletapp.CreateAPIServerClientConfig(s.KubeletServer)
if err != nil {
return err
}
Expand Down Expand Up @@ -242,7 +243,7 @@ func (s *KubeletExecutorServer) runKubelet(
// initialize the cloud provider. We explicitly wouldn't want
// that because then every kubelet instance would query the master
// state.json which does not scale.
err = s.KubeletServer.Run(kcfg)
err = kubeletapp.Run(s.KubeletServer, kcfg)

return
}
Expand All @@ -263,7 +264,7 @@ func (s *KubeletExecutorServer) Run(hks hyperkube.Interface, _ []string) error {

// create apiserver client
var apiclient *client.Client
clientConfig, err := s.CreateAPIServerClientConfig()
clientConfig, err := kubeletapp.CreateAPIServerClientConfig(s.KubeletServer)
if err == nil {
apiclient, err = client.New(clientConfig)
}
Expand Down
3 changes: 2 additions & 1 deletion contrib/mesos/pkg/minion/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"syscall"

kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
exservice "k8s.io/kubernetes/contrib/mesos/pkg/executor/service"
"k8s.io/kubernetes/contrib/mesos/pkg/hyperkube"
"k8s.io/kubernetes/contrib/mesos/pkg/minion/config"
Expand Down Expand Up @@ -250,7 +251,7 @@ func (ms *MinionServer) Run(hks hyperkube.Interface, _ []string) error {
}

// create apiserver client
clientConfig, err := ms.KubeletExecutorServer.CreateAPIServerClientConfig()
clientConfig, err := kubeletapp.CreateAPIServerClientConfig(ms.KubeletExecutorServer.KubeletServer)
if err != nil {
// required for k8sm since we need to send api.Binding information
// back to the apiserver
Expand Down
4 changes: 2 additions & 2 deletions contrib/mesos/pkg/node/statusupdater.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"time"

log "github.com/golang/glog"
kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
"k8s.io/kubernetes/cmd/kubelet/app/options"
"k8s.io/kubernetes/contrib/mesos/pkg/runtime"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
Expand All @@ -47,7 +47,7 @@ type StatusUpdater struct {
}

func NewStatusUpdater(client *client.Client, relistPeriod time.Duration, nowFunc func() time.Time) *StatusUpdater {
kubecfg := kubeletapp.NewKubeletServer() // only create to get the config, this is without side-effects
kubecfg := options.NewKubeletServer() // only create to get the config, this is without side-effects
return &StatusUpdater{
client: client,
relistPeriod: relistPeriod,
Expand Down
8 changes: 4 additions & 4 deletions contrib/mesos/pkg/node/statusupdater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"time"

"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/cmd/kube-controller-manager/app"
kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app"
"k8s.io/kubernetes/cmd/kubelet/app/options"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
Expand All @@ -46,8 +46,8 @@ func Test_nodeWithUpdatedStatus(t *testing.T) {
}
}

cm := app.NewCMServer()
kubecfg := kubeletapp.NewKubeletServer()
cm := cmapp.NewCMServer()
kubecfg := options.NewKubeletServer()
assert.True(t, kubecfg.NodeStatusUpdateFrequency*3 < cm.NodeMonitorGracePeriod) // sanity check for defaults

n := testNode(0, api.ConditionTrue, "KubeletReady")
Expand Down
10 changes: 10 additions & 0 deletions pkg/kubelet/container/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ func (c *ContainerID) UnmarshalJSON(data []byte) error {
return c.ParseString(string(data))
}

// DockerID is an ID of docker container. It is a type to make it clear when we're working with docker container Ids
type DockerID string

func (id DockerID) ContainerID() ContainerID {
return ContainerID{
Type: "docker",
ID: string(id),
}
}

type ContainerState string

const (
Expand Down
3 changes: 1 addition & 2 deletions pkg/kubelet/dockertools/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
)

// This file contains helper functions to convert docker API types to runtime
Expand Down Expand Up @@ -55,7 +54,7 @@ func toRuntimeContainer(c *docker.APIContainers) (*kubecontainer.Container, erro
}

return &kubecontainer.Container{
ID: kubetypes.DockerID(c.ID).ContainerID(),
ID: kubecontainer.DockerID(c.ID).ContainerID(),
Name: dockerName.ContainerName,
Image: c.Image,
Hash: hash,
Expand Down
7 changes: 3 additions & 4 deletions pkg/kubelet/dockertools/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ import (
)

const (
PodInfraContainerName = leaky.PodInfraContainerName
DockerPrefix = "docker://"
PodInfraContainerImage = "gcr.io/google_containers/pause:2.0"
LogSuffix = "log"
PodInfraContainerName = leaky.PodInfraContainerName
DockerPrefix = "docker://"
LogSuffix = "log"
)

const (
Expand Down
20 changes: 10 additions & 10 deletions pkg/kubelet/dockertools/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestExecSupportNotExists(t *testing.T) {

func TestDockerContainerCommand(t *testing.T) {
runner := &DockerManager{}
containerID := kubetypes.DockerID("1234").ContainerID()
containerID := kubecontainer.DockerID("1234").ContainerID()
command := []string{"ls"}
cmd, _ := runner.getRunInContainerCommand(containerID, command)
if cmd.Dir != "/var/lib/docker/execdriver/native/"+containerID.ID {
Expand Down Expand Up @@ -578,13 +578,13 @@ func TestFindContainersByPod(t *testing.T) {
Namespace: "ns",
Containers: []*kubecontainer.Container{
{
ID: kubetypes.DockerID("foobar").ContainerID(),
ID: kubecontainer.DockerID("foobar").ContainerID(),
Name: "foobar",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
},
{
ID: kubetypes.DockerID("baz").ContainerID(),
ID: kubecontainer.DockerID("baz").ContainerID(),
Name: "baz",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
Expand All @@ -597,7 +597,7 @@ func TestFindContainersByPod(t *testing.T) {
Namespace: "ns",
Containers: []*kubecontainer.Container{
{
ID: kubetypes.DockerID("barbar").ContainerID(),
ID: kubecontainer.DockerID("barbar").ContainerID(),
Name: "barbar",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
Expand Down Expand Up @@ -639,19 +639,19 @@ func TestFindContainersByPod(t *testing.T) {
Namespace: "ns",
Containers: []*kubecontainer.Container{
{
ID: kubetypes.DockerID("foobar").ContainerID(),
ID: kubecontainer.DockerID("foobar").ContainerID(),
Name: "foobar",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
},
{
ID: kubetypes.DockerID("barfoo").ContainerID(),
ID: kubecontainer.DockerID("barfoo").ContainerID(),
Name: "barfoo",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
},
{
ID: kubetypes.DockerID("baz").ContainerID(),
ID: kubecontainer.DockerID("baz").ContainerID(),
Name: "baz",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
Expand All @@ -664,7 +664,7 @@ func TestFindContainersByPod(t *testing.T) {
Namespace: "ns",
Containers: []*kubecontainer.Container{
{
ID: kubetypes.DockerID("barbar").ContainerID(),
ID: kubecontainer.DockerID("barbar").ContainerID(),
Name: "barbar",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
Expand All @@ -677,7 +677,7 @@ func TestFindContainersByPod(t *testing.T) {
Namespace: "ns",
Containers: []*kubecontainer.Container{
{
ID: kubetypes.DockerID("bazbaz").ContainerID(),
ID: kubecontainer.DockerID("bazbaz").ContainerID(),
Name: "bazbaz",
Hash: 0x1234,
State: kubecontainer.ContainerStateUnknown,
Expand All @@ -696,7 +696,7 @@ func TestFindContainersByPod(t *testing.T) {
fakeClient := &FakeDockerClient{}
np, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
// image back-off is set to nil, this test shouldnt pull images
containerManager := NewFakeDockerManager(fakeClient, &record.FakeRecorder{}, nil, nil, &cadvisorapi.MachineInfo{}, PodInfraContainerImage, 0, 0, "", kubecontainer.FakeOS{}, np, nil, nil, nil)
containerManager := NewFakeDockerManager(fakeClient, &record.FakeRecorder{}, nil, nil, &cadvisorapi.MachineInfo{}, kubetypes.PodInfraContainerImage, 0, 0, "", kubecontainer.FakeOS{}, np, nil, nil, nil)
for i, test := range tests {
fakeClient.ContainerList = test.containerList
fakeClient.ExitedContainerList = test.exitedContainerList
Expand Down
Loading

0 comments on commit 791d160

Please sign in to comment.