mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
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.
This commit is contained in:
parent
d814d973ff
commit
d4dc037bf7
@ -41,6 +41,7 @@ type HandlerRunner interface {
|
|||||||
type RuntimeHelper interface {
|
type RuntimeHelper interface {
|
||||||
GenerateRunContainerOptions(pod *api.Pod, container *api.Container, podIP string) (*RunContainerOptions, error)
|
GenerateRunContainerOptions(pod *api.Pod, container *api.Container, podIP string) (*RunContainerOptions, error)
|
||||||
GetClusterDNS(pod *api.Pod) (dnsServers []string, dnsSearches []string, err 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.
|
// ShouldContainerBeRestarted checks whether a container needs to be restarted.
|
||||||
|
@ -84,6 +84,11 @@ func (f *fakeRuntimeHelper) GetClusterDNS(pod *api.Pod) ([]string, []string, err
|
|||||||
return nil, nil, fmt.Errorf("not implemented")
|
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) {
|
func newTestDockerManagerWithHTTPClientWithVersion(fakeHTTPClient *fakeHTTP, version, apiVersion string) (*DockerManager, *FakeDockerClient) {
|
||||||
fakeDocker := NewFakeDockerClientWithVersion(version, apiVersion)
|
fakeDocker := NewFakeDockerClientWithVersion(version, apiVersion)
|
||||||
fakeRecorder := &record.FakeRecorder{}
|
fakeRecorder := &record.FakeRecorder{}
|
||||||
|
@ -1334,9 +1334,10 @@ func makePortMappings(container *api.Container) (ports []kubecontainer.PortMappi
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func generatePodHostNameAndDomain(pod *api.Pod, clusterDomain string) (string, string) {
|
func (kl *Kubelet) GeneratePodHostNameAndDomain(pod *api.Pod) (string, string) {
|
||||||
// TODO(vmarmol): Handle better.
|
// TODO(vmarmol): Handle better.
|
||||||
// Cap hostname at 63 chars (specification is 64bytes which is 63 chars and the null terminating char).
|
// Cap hostname at 63 chars (specification is 64bytes which is 63 chars and the null terminating char).
|
||||||
|
clusterDomain := kl.clusterDomain
|
||||||
const hostnameMaxLen = 63
|
const hostnameMaxLen = 63
|
||||||
podAnnotations := pod.Annotations
|
podAnnotations := pod.Annotations
|
||||||
if podAnnotations == nil {
|
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) {
|
func (kl *Kubelet) GenerateRunContainerOptions(pod *api.Pod, container *api.Container, podIP string) (*kubecontainer.RunContainerOptions, error) {
|
||||||
var err error
|
var err error
|
||||||
opts := &kubecontainer.RunContainerOptions{CgroupParent: kl.cgroupRoot}
|
opts := &kubecontainer.RunContainerOptions{CgroupParent: kl.cgroupRoot}
|
||||||
hostname, hostDomainName := generatePodHostNameAndDomain(pod, kl.clusterDomain)
|
hostname, hostDomainName := kl.GeneratePodHostNameAndDomain(pod)
|
||||||
opts.Hostname = hostname
|
opts.Hostname = hostname
|
||||||
vol, ok := kl.volumeManager.GetVolumes(pod.UID)
|
vol, ok := kl.volumeManager.GetVolumes(pod.UID)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -150,6 +150,8 @@ func (f *fakeSystemd) Reload() error {
|
|||||||
type fakeRuntimeHelper struct {
|
type fakeRuntimeHelper struct {
|
||||||
dnsServers []string
|
dnsServers []string
|
||||||
dnsSearches []string
|
dnsSearches []string
|
||||||
|
hostName string
|
||||||
|
hostDomain string
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,3 +162,7 @@ func (f *fakeRuntimeHelper) GenerateRunContainerOptions(pod *api.Pod, container
|
|||||||
func (f *fakeRuntimeHelper) GetClusterDNS(pod *api.Pod) ([]string, []string, error) {
|
func (f *fakeRuntimeHelper) GetClusterDNS(pod *api.Pod) ([]string, []string, error) {
|
||||||
return f.dnsServers, f.dnsSearches, f.err
|
return f.dnsServers, f.dnsSearches, f.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *fakeRuntimeHelper) GeneratePodHostNameAndDomain(pod *api.Pod) (string, string) {
|
||||||
|
return f.hostName, f.hostDomain
|
||||||
|
}
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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.
|
// stream the log. Set |follow| to false and specify the number of lines (e.g.
|
||||||
// "100" or "all") to tail the log.
|
// "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.
|
// 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.
|
// 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
|
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.
|
// Get the json structured logs.
|
||||||
cmd.Args = append(cmd.Args, "-o", "json")
|
cmd.Args = append(cmd.Args, "-o", "json")
|
||||||
|
|
||||||
|
@ -760,6 +760,10 @@ func (r *Runtime) generateRunCommand(pod *api.Pod, uuid string) (string, error)
|
|||||||
if len(dnsServers) > 0 || len(dnsSearches) > 0 {
|
if len(dnsServers) > 0 || len(dnsSearches) > 0 {
|
||||||
runPrepared = append(runPrepared, fmt.Sprintf("--dns-opt=%s", defaultDNSOption))
|
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)
|
runPrepared = append(runPrepared, uuid)
|
||||||
return strings.Join(runPrepared, " "), nil
|
return strings.Join(runPrepared, " "), nil
|
||||||
}
|
}
|
||||||
|
@ -1037,6 +1037,7 @@ func TestGenerateRunCommand(t *testing.T) {
|
|||||||
|
|
||||||
dnsServers []string
|
dnsServers []string
|
||||||
dnsSearches []string
|
dnsSearches []string
|
||||||
|
hostName string
|
||||||
err error
|
err error
|
||||||
|
|
||||||
expect string
|
expect string
|
||||||
@ -1044,26 +1045,38 @@ func TestGenerateRunCommand(t *testing.T) {
|
|||||||
// Case #0, returns error.
|
// Case #0, returns error.
|
||||||
{
|
{
|
||||||
&api.Pod{
|
&api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "pod-name-foo",
|
||||||
|
},
|
||||||
Spec: api.PodSpec{},
|
Spec: api.PodSpec{},
|
||||||
},
|
},
|
||||||
"rkt-uuid-foo",
|
"rkt-uuid-foo",
|
||||||
[]string{},
|
[]string{},
|
||||||
[]string{},
|
[]string{},
|
||||||
|
"",
|
||||||
fmt.Errorf("failed to get cluster dns"),
|
fmt.Errorf("failed to get cluster dns"),
|
||||||
"",
|
"",
|
||||||
},
|
},
|
||||||
// Case #1, returns no dns, with private-net.
|
// Case #1, returns no dns, with private-net.
|
||||||
{
|
{
|
||||||
&api.Pod{},
|
&api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "pod-name-foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
"rkt-uuid-foo",
|
"rkt-uuid-foo",
|
||||||
[]string{},
|
[]string{},
|
||||||
[]string{},
|
[]string{},
|
||||||
|
"pod-hostname-foo",
|
||||||
nil,
|
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.
|
// Case #2, returns no dns, with host-net.
|
||||||
{
|
{
|
||||||
&api.Pod{
|
&api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "pod-name-foo",
|
||||||
|
},
|
||||||
Spec: api.PodSpec{
|
Spec: api.PodSpec{
|
||||||
SecurityContext: &api.PodSecurityContext{
|
SecurityContext: &api.PodSecurityContext{
|
||||||
HostNetwork: true,
|
HostNetwork: true,
|
||||||
@ -1073,12 +1086,16 @@ func TestGenerateRunCommand(t *testing.T) {
|
|||||||
"rkt-uuid-foo",
|
"rkt-uuid-foo",
|
||||||
[]string{},
|
[]string{},
|
||||||
[]string{},
|
[]string{},
|
||||||
|
"pod-hostname-foo",
|
||||||
nil,
|
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.
|
// Case #3, returns dns, dns searches, with private-net.
|
||||||
{
|
{
|
||||||
&api.Pod{
|
&api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "pod-name-foo",
|
||||||
|
},
|
||||||
Spec: api.PodSpec{
|
Spec: api.PodSpec{
|
||||||
SecurityContext: &api.PodSecurityContext{
|
SecurityContext: &api.PodSecurityContext{
|
||||||
HostNetwork: false,
|
HostNetwork: false,
|
||||||
@ -1088,12 +1105,16 @@ func TestGenerateRunCommand(t *testing.T) {
|
|||||||
"rkt-uuid-foo",
|
"rkt-uuid-foo",
|
||||||
[]string{"127.0.0.1"},
|
[]string{"127.0.0.1"},
|
||||||
[]string{"."},
|
[]string{"."},
|
||||||
|
"pod-hostname-foo",
|
||||||
nil,
|
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.
|
// Case #4, returns dns, dns searches, with host-network.
|
||||||
{
|
{
|
||||||
&api.Pod{
|
&api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "pod-name-foo",
|
||||||
|
},
|
||||||
Spec: api.PodSpec{
|
Spec: api.PodSpec{
|
||||||
SecurityContext: &api.PodSecurityContext{
|
SecurityContext: &api.PodSecurityContext{
|
||||||
HostNetwork: true,
|
HostNetwork: true,
|
||||||
@ -1103,8 +1124,9 @@ func TestGenerateRunCommand(t *testing.T) {
|
|||||||
"rkt-uuid-foo",
|
"rkt-uuid-foo",
|
||||||
[]string{"127.0.0.1"},
|
[]string{"127.0.0.1"},
|
||||||
[]string{"."},
|
[]string{"."},
|
||||||
|
"pod-hostname-foo",
|
||||||
nil,
|
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 {
|
for i, tt := range tests {
|
||||||
testCaseHint := fmt.Sprintf("test case #%d", i)
|
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)
|
result, err := rkt.generateRunCommand(tt.pod, tt.uuid)
|
||||||
assert.Equal(t, tt.err, err, testCaseHint)
|
assert.Equal(t, tt.err, err, testCaseHint)
|
||||||
|
Loading…
Reference in New Issue
Block a user