mirror of
https://github.com/containers/podman.git
synced 2025-06-24 11:28:24 +08:00
Merge pull request #7496 from zhangguanzhang/play-kube-handle-hostAliases
handle play kube with pod.spec.hostAliases
This commit is contained in:
@ -144,6 +144,16 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
|
|||||||
podOptions = append(podOptions, libpod.WithPodHostNetwork())
|
podOptions = append(podOptions, libpod.WithPodHostNetwork())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if podYAML.Spec.HostAliases != nil {
|
||||||
|
hosts := make([]string, 0, len(podYAML.Spec.HostAliases))
|
||||||
|
for _, hostAlias := range podYAML.Spec.HostAliases {
|
||||||
|
for _, host := range hostAlias.Hostnames {
|
||||||
|
hosts = append(hosts, host+":"+hostAlias.IP)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
podOptions = append(podOptions, libpod.WithPodHosts(hosts))
|
||||||
|
}
|
||||||
|
|
||||||
nsOptions, err := generate.GetNamespaceOptions(strings.Split(createconfig.DefaultKernelNamespaces, ","))
|
nsOptions, err := generate.GetNamespaceOptions(strings.Split(createconfig.DefaultKernelNamespaces, ","))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -267,6 +267,16 @@ func (c *CreateConfig) getContainerCreateOptions(runtime *libpod.Runtime, pod *l
|
|||||||
options = append(options, runtime.WithPod(pod))
|
options = append(options, runtime.WithPod(pod))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle some spec from the InfraContainer when it's a pod
|
||||||
|
if pod != nil && pod.HasInfraContainer() {
|
||||||
|
InfraCtr, err := pod.InfraContainer()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// handle the pod.spec.hostAliases
|
||||||
|
options = append(options, libpod.WithHosts(InfraCtr.HostsAdd()))
|
||||||
|
}
|
||||||
|
|
||||||
if len(mounts) != 0 || len(namedVolumes) != 0 {
|
if len(mounts) != 0 || len(namedVolumes) != 0 {
|
||||||
destinations := []string{}
|
destinations := []string{}
|
||||||
|
|
||||||
|
@ -42,6 +42,14 @@ metadata:
|
|||||||
|
|
||||||
spec:
|
spec:
|
||||||
hostname: {{ .Hostname }}
|
hostname: {{ .Hostname }}
|
||||||
|
hostAliases:
|
||||||
|
{{ range .HostAliases }}
|
||||||
|
- hostnames:
|
||||||
|
{{ range .HostName }}
|
||||||
|
- {{ . }}
|
||||||
|
{{ end }}
|
||||||
|
ip: {{ .IP }}
|
||||||
|
{{ end }}
|
||||||
containers:
|
containers:
|
||||||
{{ with .Ctrs }}
|
{{ with .Ctrs }}
|
||||||
{{ range . }}
|
{{ range . }}
|
||||||
@ -249,16 +257,22 @@ func generateDeploymentKubeYaml(deployment *Deployment, fileName string) error {
|
|||||||
type Pod struct {
|
type Pod struct {
|
||||||
Name string
|
Name string
|
||||||
Hostname string
|
Hostname string
|
||||||
|
HostAliases []HostAlias
|
||||||
Ctrs []*Ctr
|
Ctrs []*Ctr
|
||||||
Volumes []*Volume
|
Volumes []*Volume
|
||||||
Annotations map[string]string
|
Annotations map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HostAlias struct {
|
||||||
|
IP string
|
||||||
|
HostName []string
|
||||||
|
}
|
||||||
|
|
||||||
// getPod takes a list of podOptions and returns a pod with sane defaults
|
// getPod takes a list of podOptions and returns a pod with sane defaults
|
||||||
// and the configured options
|
// and the configured options
|
||||||
// if no containers are added, it will add the default container
|
// if no containers are added, it will add the default container
|
||||||
func getPod(options ...podOption) *Pod {
|
func getPod(options ...podOption) *Pod {
|
||||||
p := Pod{defaultPodName, "", make([]*Ctr, 0), make([]*Volume, 0), make(map[string]string)}
|
p := Pod{defaultPodName, "", nil, make([]*Ctr, 0), make([]*Volume, 0), make(map[string]string)}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(&p)
|
option(&p)
|
||||||
}
|
}
|
||||||
@ -276,6 +290,15 @@ func withHostname(h string) podOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withHostAliases(ip string, host []string) podOption {
|
||||||
|
return func(pod *Pod) {
|
||||||
|
pod.HostAliases = append(pod.HostAliases, HostAlias{
|
||||||
|
IP: ip,
|
||||||
|
HostName: host,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func withCtr(c *Ctr) podOption {
|
func withCtr(c *Ctr) podOption {
|
||||||
return func(pod *Pod) {
|
return func(pod *Pod) {
|
||||||
pod.Ctrs = append(pod.Ctrs, c)
|
pod.Ctrs = append(pod.Ctrs, c)
|
||||||
@ -597,6 +620,30 @@ var _ = Describe("Podman generate kube", func() {
|
|||||||
Expect(inspect.OutputToString()).To(Equal(hostname))
|
Expect(inspect.OutputToString()).To(Equal(hostname))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman play kube test HostAliases", func() {
|
||||||
|
pod := getPod(withHostAliases("192.168.1.2", []string{
|
||||||
|
"test1.podman.io",
|
||||||
|
"test2.podman.io",
|
||||||
|
}),
|
||||||
|
withHostAliases("192.168.1.3", []string{
|
||||||
|
"test3.podman.io",
|
||||||
|
"test4.podman.io",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
err := generatePodKubeYaml(pod, kubeYaml)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
|
||||||
|
kube.WaitWithDefaultTimeout()
|
||||||
|
Expect(kube.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
inspect := podmanTest.Podman([]string{"inspect", getCtrNameInPod(pod), "--format", "{{ .HostConfig.ExtraHosts }}"})
|
||||||
|
inspect.WaitWithDefaultTimeout()
|
||||||
|
Expect(inspect.ExitCode()).To(Equal(0))
|
||||||
|
Expect(inspect.OutputToString()).
|
||||||
|
To(Equal("[test1.podman.io:192.168.1.2 test2.podman.io:192.168.1.2 test3.podman.io:192.168.1.3 test4.podman.io:192.168.1.3]"))
|
||||||
|
})
|
||||||
|
|
||||||
It("podman play kube cap add", func() {
|
It("podman play kube cap add", func() {
|
||||||
capAdd := "CAP_SYS_ADMIN"
|
capAdd := "CAP_SYS_ADMIN"
|
||||||
ctr := getCtr(withCapAdd([]string{capAdd}), withCmd([]string{"cat", "/proc/self/status"}), withArg(nil))
|
ctr := getCtr(withCapAdd([]string{capAdd}), withCmd([]string{"cat", "/proc/self/status"}), withArg(nil))
|
||||||
|
Reference in New Issue
Block a user