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 --publish-all flag for kube play #18557

Closed
Closed
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
4 changes: 4 additions & 0 deletions cmd/podman/kube/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func playFlags(cmd *cobra.Command) {
flags.StringSliceVar(&playOptions.PublishPorts, publishPortsFlagName, []string{}, "Publish a container's port, or a range of ports, to the host")
_ = cmd.RegisterFlagCompletionFunc(publishPortsFlagName, completion.AutocompleteNone)

publishAllFlagName := "publish-all"
flags.BoolVar(&playOptions.PublishAll, publishAllFlagName, false, "Publish all containerPorts from the YAML file without a matching hostPort")
_ = cmd.RegisterFlagCompletionFunc(publishAllFlagName, completion.AutocompleteNone)

waitFlagName := "wait"
flags.BoolVarP(&playOptions.Wait, waitFlagName, "w", false, "Clean up all objects created when a SIGTERM is received or pods exit")

Expand Down
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-kube-play.1.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ Define or override a port definition in the YAML file.
The lists of ports in the YAML file and the command line are merged. Matching is done by using the **containerPort** field.
If **containerPort** exists in both the YAML file and the option, the latter takes precedence.

#### **--publish-all**

Allow container port publication without specifying a `hostPort` pair

#### **--quiet**, **-q**

Suppress output information when pulling images
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
StaticIPs []string `schema:"staticIPs"`
StaticMACs []string `schema:"staticMACs"`
NoHosts bool `schema:"noHosts"`
PublishAll bool `schema:"publishAll"`
PublishPorts []string `schema:"publishPorts"`
Wait bool `schema:"wait"`
ServiceContainer bool `schema:"serviceContainer"`
Expand Down Expand Up @@ -100,6 +101,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
PublishPorts: query.PublishPorts,
Wait: query.Wait,
ServiceContainer: query.ServiceContainer,
PublishAll: query.PublishAll,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this to work remotely, we also need to touch pkg/bindings. Those are the go-bindings podman-remote uses. In case the client does not set the publish-all parameter, we should query contianers.conf here and use the configured defaults on the server side.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is mostly generated code I might need some guidance.

The PlayOptions in pkg/binding/kube/types.go needs to be modified right?

After that we need to check if the flag is checked in the PlayWithBody func in the pkg/binding/kube/kube.go if I'm not mistaken.

Where and how should I run the generate command?

Currently I'm not sure how and where should I query the containers.conf may I require some aditional information about where should we do that exactly?

}
if _, found := r.URL.Query()["tlsVerify"]; found {
options.SkipTLSVerify = types.NewOptionalBool(!query.TLSVerify)
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type PlayKubeOptions struct {
Force bool
// PublishPorts - configure how to expose ports configured inside the K8S YAML file
PublishPorts []string
// PublishAll - expose all container ports without a host pair
PublishAll bool
// Wait - indicates whether to return after having created the pods
Wait bool
}
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/entities/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type PodCreateOptions struct {
Userns specgen.Namespace `json:"-"`
Volume []string `json:"volume,omitempty"`
VolumesFrom []string `json:"volumes_from,omitempty"`
PublishAll bool ``
SecurityOpt []string `json:"security_opt,omitempty"`
Sysctl []string `json:"sysctl,omitempty"`
}
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
Infra: true,
Net: &entities.NetOptions{NoHosts: options.NoHosts},
ExitPolicy: string(config.PodExitPolicyStop),
PublishAll: options.PublishAll,
}
podOpt, err = kube.ToPodOpt(ctx, podName, podOpt, podYAML)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ToPodOpt(ctx context.Context, podName string, p entities.PodCreateOptions,
}
p.Net.AddHosts = hosts
}
podPorts := getPodPorts(podYAML.Spec.Containers)
podPorts := getPodPorts(podYAML.Spec.Containers, p.PublishAll)
p.Net.PublishPorts = podPorts

if dnsConfig := podYAML.Spec.DNSConfig; dnsConfig != nil {
Expand Down Expand Up @@ -1044,15 +1044,17 @@ func getContainerResources(container v1.Container) (v1.ResourceRequirements, err

// getPodPorts converts a slice of kube container descriptions to an
// array of portmapping
func getPodPorts(containers []v1.Container) []types.PortMapping {
func getPodPorts(containers []v1.Container, publishAll bool) []types.PortMapping {
var infraPorts []types.PortMapping
for _, container := range containers {
for _, p := range container.Ports {
if p.HostPort != 0 && p.ContainerPort == 0 {
p.ContainerPort = p.HostPort
}
if p.HostPort == 0 && p.ContainerPort != 0 {
p.HostPort = p.ContainerPort
if publishAll {
p.HostPort = p.ContainerPort
}
}
if p.Protocol == "" {
p.Protocol = "tcp"
Expand Down