Skip to content

Commit

Permalink
kubeadm: make kube-apiserver's liveness probe match its bindport.
Browse files Browse the repository at this point in the history
It had previously been hardcoded, so if you used --apiserver-bind-port
to override the default port (6443), then the health check for the pod
would quickly fail and kubelet would continuously kill the apiserver.
  • Loading branch information
pipejakob committed Mar 7, 2017
1 parent 20c13c1 commit fe81169
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/kubeadm/app/master/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ go_test(
deps = [
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
"//vendor:k8s.io/apimachinery/pkg/util/intstr",
"//vendor:k8s.io/apimachinery/pkg/util/yaml",
"//vendor:k8s.io/client-go/pkg/api/v1",
],
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/master/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
Image: images.GetCoreImage(images.KubeAPIServerImage, cfg, kubeadmapi.GlobalEnvParams.HyperkubeImage),
Command: getAPIServerCommand(cfg, false),
VolumeMounts: volumeMounts,
LivenessProbe: componentProbe(6443, "/healthz", api.URISchemeHTTPS),
LivenessProbe: componentProbe(int(cfg.API.BindPort), "/healthz", api.URISchemeHTTPS),
Resources: componentResources("250m"),
Env: getProxyEnvVars(),
}, volumes...),
Expand Down
55 changes: 53 additions & 2 deletions cmd/kubeadm/app/master/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/yaml"
api "k8s.io/client-go/pkg/api/v1"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
Expand All @@ -45,13 +46,23 @@ func TestWriteStaticPodManifests(t *testing.T) {
defer func() { kubeadmapi.GlobalEnvParams = oldEnv }()

var tests = []struct {
cfg *kubeadmapi.MasterConfiguration
expected bool
cfg *kubeadmapi.MasterConfiguration
expected bool
expectedAPIProbePort int32
}{
{
cfg: &kubeadmapi.MasterConfiguration{},
expected: true,
},
{
cfg: &kubeadmapi.MasterConfiguration{
API: kubeadmapi.API{
BindPort: 443,
},
},
expected: true,
expectedAPIProbePort: 443,
},
}
for _, rt := range tests {
actual := WriteStaticPodManifests(rt.cfg)
Expand All @@ -61,6 +72,46 @@ func TestWriteStaticPodManifests(t *testing.T) {
rt.expected,
(actual == nil),
)
continue
}

if rt.expectedAPIProbePort != 0 {
manifest, err := os.Open(fmt.Sprintf("%s/manifests/kube-apiserver.yaml", kubeadmapi.GlobalEnvParams.KubernetesDir))
if err != nil {
t.Error("WriteStaticPodManifests: error opening manifests/kube-apiserver.yaml")
continue
}

var pod api.Pod
d := yaml.NewYAMLOrJSONDecoder(manifest, 4096)
if err := d.Decode(&pod); err != nil {
t.Error("WriteStaticPodManifests: error decoding manifests/kube-apiserver.yaml into Pod")
continue
}

// Lots of individual checks as we traverse pointers so we don't panic dereferencing a nil on failure
containers := pod.Spec.Containers
if containers == nil || len(containers) == 0 {
t.Error("WriteStaticPodManifests: wrote an apiserver manifest without any containers")
continue
}

probe := containers[0].LivenessProbe
if probe == nil {
t.Error("WriteStaticPodManifests: wrote an apiserver manifest without a liveness probe")
continue
}

httpGET := probe.Handler.HTTPGet
if httpGET == nil {
t.Error("WriteStaticPodManifests: wrote an apiserver manifest without an HTTP liveness probe")
continue
}

port := httpGET.Port.IntVal
if rt.expectedAPIProbePort != port {
t.Errorf("WriteStaticPodManifests: apiserver pod liveness probe port was: %v, wanted %v", port, rt.expectedAPIProbePort)
}
}
}
}
Expand Down

0 comments on commit fe81169

Please sign in to comment.