Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pods created by play kube to a default network #16029

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/markdown/podman-kube-play.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Note: When joining multiple networks you should use the **--network name:mac=\<m

@@option network

The *host* network mode should be configured in the YAML file.
When no network option is specified and *host* network mode is not configured in the YAML file, a new network stack is created and pods are attached to it making possible pod to pod communication.

@@option no-hosts

Expand Down
21 changes: 21 additions & 0 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import (
// container-specific sd-notify modes.
const sdNotifyAnnotation = "io.containers.sdnotify"

// default network created/used by kube
const kubeDefaultNetwork = "podman-default-kube-network"

// createServiceContainer creates a container that can later on
// be associated with the pods of a K8s yaml. It will be started along with
// the first pod.
Expand Down Expand Up @@ -114,6 +117,19 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
report := &entities.PlayKubeReport{}
validKinds := 0

// when no network options are specified, create a common network for all the pods
if len(options.Networks) == 0 {
_, err := ic.NetworkCreate(
ctx, nettypes.Network{
Name: kubeDefaultNetwork,
DNSEnabled: true,
},
)
if err != nil && !errors.Is(err, nettypes.ErrNetworkExists) {
return nil, err
}
}

// read yaml document
content, err := io.ReadAll(body)
if err != nil {
Expand Down Expand Up @@ -338,6 +354,11 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
return nil, err
}

// add kube default network if no network is explicitly added
if podOpt.Net.Network.NSMode != "host" && len(options.Networks) == 0 {
options.Networks = []string{kubeDefaultNetwork}
}

if len(options.Networks) > 0 {
ns, networks, netOpts, err := specgen.ParseNetworkFlag(options.Networks)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,21 @@ MemoryReservation: {{ .HostConfig.MemoryReservation }}`})
Expect(logs.OutputToString()).To(Equal(netns))
})

It("podman play kube test with kube default network", func() {
pod := getPod()
err := generateKubeYaml("pod", pod, kubeYaml)
Expect(err).To(BeNil())

kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout()
Expect(kube).Should(Exit(0))

inspect := podmanTest.Podman([]string{"inspect", pod.Name, "--format", "{{ .InfraConfig.Networks }}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
Expect(inspect.OutputToString()).To(Equal("[podman-default-kube-network]"))
})

It("podman play kube persistentVolumeClaim", func() {
volName := "myvol"
volDevice := "tmpfs"
Expand Down