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.
pull/6/head
Yifan Gu 2016-03-21 14:28:57 -07:00
parent d814d973ff
commit d4dc037bf7
7 changed files with 51 additions and 11 deletions

View File

@ -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.

View File

@ -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{}

View File

@ -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 {

View File

@ -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
}

View File

@ -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")

View File

@ -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
}

View File

@ -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)