From d4dc037bf7501e44647742f18d60fbd85c1b4560 Mon Sep 17 00:00:00 2001 From: Yifan Gu Date: Mon, 21 Mar 2016 14:28:57 -0700 Subject: [PATCH] rkt: Add '--hostname' support for rkt. Add GeneratePodHostNameAndDomain() to RuntimeHelper to get the hostname of the pod from kubelet. Also update the logging flag to change the journal match from _HOSTNAME to _MACHINE_ID. --- pkg/kubelet/container/helpers.go | 1 + pkg/kubelet/dockertools/manager_test.go | 5 ++++ pkg/kubelet/kubelet.go | 5 ++-- pkg/kubelet/rkt/fake_rkt_interface_test.go | 6 ++++ pkg/kubelet/rkt/log.go | 7 +++-- pkg/kubelet/rkt/rkt.go | 4 +++ pkg/kubelet/rkt/rkt_test.go | 34 ++++++++++++++++++---- 7 files changed, 51 insertions(+), 11 deletions(-) diff --git a/pkg/kubelet/container/helpers.go b/pkg/kubelet/container/helpers.go index abada5d5ef..b62232fc12 100644 --- a/pkg/kubelet/container/helpers.go +++ b/pkg/kubelet/container/helpers.go @@ -41,6 +41,7 @@ type HandlerRunner interface { type RuntimeHelper interface { GenerateRunContainerOptions(pod *api.Pod, container *api.Container, podIP string) (*RunContainerOptions, error) GetClusterDNS(pod *api.Pod) (dnsServers []string, dnsSearches []string, err error) + GeneratePodHostNameAndDomain(pod *api.Pod) (hostname string, hostDomain string) } // ShouldContainerBeRestarted checks whether a container needs to be restarted. diff --git a/pkg/kubelet/dockertools/manager_test.go b/pkg/kubelet/dockertools/manager_test.go index 9afe6a9538..6d897503a2 100644 --- a/pkg/kubelet/dockertools/manager_test.go +++ b/pkg/kubelet/dockertools/manager_test.go @@ -84,6 +84,11 @@ func (f *fakeRuntimeHelper) GetClusterDNS(pod *api.Pod) ([]string, []string, err return nil, nil, fmt.Errorf("not implemented") } +// This is not used by docker runtime. +func (f *fakeRuntimeHelper) GeneratePodHostNameAndDomain(pod *api.Pod) (string, string) { + return "", "" +} + func newTestDockerManagerWithHTTPClientWithVersion(fakeHTTPClient *fakeHTTP, version, apiVersion string) (*DockerManager, *FakeDockerClient) { fakeDocker := NewFakeDockerClientWithVersion(version, apiVersion) fakeRecorder := &record.FakeRecorder{} diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 925b5ac982..a6695f4767 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -1334,9 +1334,10 @@ func makePortMappings(container *api.Container) (ports []kubecontainer.PortMappi return } -func generatePodHostNameAndDomain(pod *api.Pod, clusterDomain string) (string, string) { +func (kl *Kubelet) GeneratePodHostNameAndDomain(pod *api.Pod) (string, string) { // TODO(vmarmol): Handle better. // Cap hostname at 63 chars (specification is 64bytes which is 63 chars and the null terminating char). + clusterDomain := kl.clusterDomain const hostnameMaxLen = 63 podAnnotations := pod.Annotations if podAnnotations == nil { @@ -1366,7 +1367,7 @@ func generatePodHostNameAndDomain(pod *api.Pod, clusterDomain string) (string, s func (kl *Kubelet) GenerateRunContainerOptions(pod *api.Pod, container *api.Container, podIP string) (*kubecontainer.RunContainerOptions, error) { var err error opts := &kubecontainer.RunContainerOptions{CgroupParent: kl.cgroupRoot} - hostname, hostDomainName := generatePodHostNameAndDomain(pod, kl.clusterDomain) + hostname, hostDomainName := kl.GeneratePodHostNameAndDomain(pod) opts.Hostname = hostname vol, ok := kl.volumeManager.GetVolumes(pod.UID) if !ok { diff --git a/pkg/kubelet/rkt/fake_rkt_interface_test.go b/pkg/kubelet/rkt/fake_rkt_interface_test.go index ef029e2874..d2bbf1b094 100644 --- a/pkg/kubelet/rkt/fake_rkt_interface_test.go +++ b/pkg/kubelet/rkt/fake_rkt_interface_test.go @@ -150,6 +150,8 @@ func (f *fakeSystemd) Reload() error { type fakeRuntimeHelper struct { dnsServers []string dnsSearches []string + hostName string + hostDomain string err error } @@ -160,3 +162,7 @@ func (f *fakeRuntimeHelper) GenerateRunContainerOptions(pod *api.Pod, container func (f *fakeRuntimeHelper) GetClusterDNS(pod *api.Pod) ([]string, []string, error) { return f.dnsServers, f.dnsSearches, f.err } + +func (f *fakeRuntimeHelper) GeneratePodHostNameAndDomain(pod *api.Pod) (string, string) { + return f.hostName, f.hostDomain +} diff --git a/pkg/kubelet/rkt/log.go b/pkg/kubelet/rkt/log.go index 254758a22c..cad61bce4d 100644 --- a/pkg/kubelet/rkt/log.go +++ b/pkg/kubelet/rkt/log.go @@ -23,6 +23,7 @@ import ( "io" "os/exec" "strconv" + "strings" "sync" "time" @@ -100,7 +101,8 @@ func pipeLog(wg *sync.WaitGroup, logOptions *api.PodLogOptions, r io.ReadCloser, // stream the log. Set |follow| to false and specify the number of lines (e.g. // "100" or "all") to tail the log. // -// In rkt runtime's implementation, per container log is get via 'journalctl -m _HOSTNAME=[rkt-$UUID] -u [APP_NAME]'. +// In rkt runtime's implementation, per container log is get via 'journalctl -m _MACHINE_ID=[MACHINE_ID] -u [APP_NAME]'. +// Where MACHINE_ID is the rkt id without dash. // See https://github.com/coreos/rkt/blob/master/Documentation/commands.md#logging for more details. // // TODO(yifan): If the rkt is using lkvm as the stage1 image, then this function will fail. @@ -110,8 +112,7 @@ func (r *Runtime) GetContainerLogs(pod *api.Pod, containerID kubecontainer.Conta return err } - cmd := exec.Command("journalctl", "-m", fmt.Sprintf("_HOSTNAME=rkt-%s", id.uuid), "-u", id.appName, "-a") - + cmd := exec.Command("journalctl", "-m", fmt.Sprintf("_MACHINE_ID=%s", strings.Replace(id.uuid, "-", "", -1)), "-u", id.appName, "-a") // Get the json structured logs. cmd.Args = append(cmd.Args, "-o", "json") diff --git a/pkg/kubelet/rkt/rkt.go b/pkg/kubelet/rkt/rkt.go index e8d3a4eafc..70c35cee2a 100644 --- a/pkg/kubelet/rkt/rkt.go +++ b/pkg/kubelet/rkt/rkt.go @@ -760,6 +760,10 @@ func (r *Runtime) generateRunCommand(pod *api.Pod, uuid string) (string, error) if len(dnsServers) > 0 || len(dnsSearches) > 0 { runPrepared = append(runPrepared, fmt.Sprintf("--dns-opt=%s", defaultDNSOption)) } + + // TODO(yifan): host domain is not being used. + hostname, _ := r.runtimeHelper.GeneratePodHostNameAndDomain(pod) + runPrepared = append(runPrepared, fmt.Sprintf("--hostname=%s", hostname)) runPrepared = append(runPrepared, uuid) return strings.Join(runPrepared, " "), nil } diff --git a/pkg/kubelet/rkt/rkt_test.go b/pkg/kubelet/rkt/rkt_test.go index dd8ed73e0d..bad8d5ea36 100644 --- a/pkg/kubelet/rkt/rkt_test.go +++ b/pkg/kubelet/rkt/rkt_test.go @@ -1037,6 +1037,7 @@ func TestGenerateRunCommand(t *testing.T) { dnsServers []string dnsSearches []string + hostName string err error expect string @@ -1044,26 +1045,38 @@ func TestGenerateRunCommand(t *testing.T) { // Case #0, returns error. { &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "pod-name-foo", + }, Spec: api.PodSpec{}, }, "rkt-uuid-foo", []string{}, []string{}, + "", fmt.Errorf("failed to get cluster dns"), "", }, // Case #1, returns no dns, with private-net. { - &api.Pod{}, + &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "pod-name-foo", + }, + }, "rkt-uuid-foo", []string{}, []string{}, + "pod-hostname-foo", nil, - "/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=rkt.kubernetes.io rkt-uuid-foo", + "/bin/rkt/rkt --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=rkt.kubernetes.io --hostname=pod-hostname-foo rkt-uuid-foo", }, // Case #2, returns no dns, with host-net. { &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "pod-name-foo", + }, Spec: api.PodSpec{ SecurityContext: &api.PodSecurityContext{ HostNetwork: true, @@ -1073,12 +1086,16 @@ func TestGenerateRunCommand(t *testing.T) { "rkt-uuid-foo", []string{}, []string{}, + "pod-hostname-foo", nil, - "/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=host rkt-uuid-foo", + "/bin/rkt/rkt --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=host --hostname=pod-hostname-foo rkt-uuid-foo", }, // Case #3, returns dns, dns searches, with private-net. { &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "pod-name-foo", + }, Spec: api.PodSpec{ SecurityContext: &api.PodSecurityContext{ HostNetwork: false, @@ -1088,12 +1105,16 @@ func TestGenerateRunCommand(t *testing.T) { "rkt-uuid-foo", []string{"127.0.0.1"}, []string{"."}, + "pod-hostname-foo", nil, - "/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=rkt.kubernetes.io --dns=127.0.0.1 --dns-search=. --dns-opt=ndots:5 rkt-uuid-foo", + "/bin/rkt/rkt --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=rkt.kubernetes.io --dns=127.0.0.1 --dns-search=. --dns-opt=ndots:5 --hostname=pod-hostname-foo rkt-uuid-foo", }, // Case #4, returns dns, dns searches, with host-network. { &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "pod-name-foo", + }, Spec: api.PodSpec{ SecurityContext: &api.PodSecurityContext{ HostNetwork: true, @@ -1103,8 +1124,9 @@ func TestGenerateRunCommand(t *testing.T) { "rkt-uuid-foo", []string{"127.0.0.1"}, []string{"."}, + "pod-hostname-foo", nil, - "/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=host --dns=127.0.0.1 --dns-search=. --dns-opt=ndots:5 rkt-uuid-foo", + "/bin/rkt/rkt --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=host --dns=127.0.0.1 --dns-search=. --dns-opt=ndots:5 --hostname=pod-hostname-foo rkt-uuid-foo", }, } @@ -1120,7 +1142,7 @@ func TestGenerateRunCommand(t *testing.T) { for i, tt := range tests { testCaseHint := fmt.Sprintf("test case #%d", i) - rkt.runtimeHelper = &fakeRuntimeHelper{tt.dnsServers, tt.dnsSearches, tt.err} + rkt.runtimeHelper = &fakeRuntimeHelper{tt.dnsServers, tt.dnsSearches, tt.hostName, "", tt.err} result, err := rkt.generateRunCommand(tt.pod, tt.uuid) assert.Equal(t, tt.err, err, testCaseHint)