Skip to content

Commit ae989d7

Browse files
authored
Add support for --container flag (#9703)
Add support for --container flag, which sets an explicit container name for exec operations. Defaults to `controller`. Signed-off-by: Jacob Henner <[email protected]>
1 parent 97a1a6d commit ae989d7

File tree

10 files changed

+47
-31
lines changed

10 files changed

+47
-31
lines changed

cmd/plugin/commands/backends/backends.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
// CreateCommand creates and returns this cobra subcommand
3232
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
33-
var pod, deployment, selector *string
33+
var pod, deployment, selector, container *string
3434
cmd := &cobra.Command{
3535
Use: "backends",
3636
Short: "Inspect the dynamic backend information of an ingress-nginx instance",
@@ -47,22 +47,23 @@ func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
4747
return fmt.Errorf("--list and --backend cannot both be specified")
4848
}
4949

50-
util.PrintError(backends(flags, *pod, *deployment, *selector, backend, onlyList))
50+
util.PrintError(backends(flags, *pod, *deployment, *selector, *container, backend, onlyList))
5151
return nil
5252
},
5353
}
5454

5555
pod = util.AddPodFlag(cmd)
5656
deployment = util.AddDeploymentFlag(cmd)
5757
selector = util.AddSelectorFlag(cmd)
58+
container = util.AddContainerFlag(cmd)
5859

5960
cmd.Flags().String("backend", "", "Output only the information for the given backend")
6061
cmd.Flags().Bool("list", false, "Output a newline-separated list of backend names")
6162

6263
return cmd
6364
}
6465

65-
func backends(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, backend string, onlyList bool) error {
66+
func backends(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, container string, backend string, onlyList bool) error {
6667
var command []string
6768
if onlyList {
6869
command = []string{"/dbg", "backends", "list"}
@@ -77,7 +78,7 @@ func backends(flags *genericclioptions.ConfigFlags, podName string, deployment s
7778
return err
7879
}
7980

80-
out, err := kubectl.PodExecString(flags, &pod, command)
81+
out, err := kubectl.PodExecString(flags, &pod, container, command)
8182
if err != nil {
8283
return err
8384
}

cmd/plugin/commands/certs/certs.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
// CreateCommand creates and returns this cobra subcommand
3232
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
33-
var pod, deployment, selector *string
33+
var pod, deployment, selector, container *string
3434
cmd := &cobra.Command{
3535
Use: "certs",
3636
Short: "Output the certificate data stored in an ingress-nginx pod",
@@ -40,7 +40,7 @@ func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
4040
return err
4141
}
4242

43-
util.PrintError(certs(flags, *pod, *deployment, *selector, host))
43+
util.PrintError(certs(flags, *pod, *deployment, *selector, *container, host))
4444
return nil
4545
},
4646
}
@@ -50,19 +50,20 @@ func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
5050
pod = util.AddPodFlag(cmd)
5151
deployment = util.AddDeploymentFlag(cmd)
5252
selector = util.AddSelectorFlag(cmd)
53+
container = util.AddContainerFlag(cmd)
5354

5455
return cmd
5556
}
5657

57-
func certs(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, host string) error {
58+
func certs(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, container string, host string) error {
5859
command := []string{"/dbg", "certs", "get", host}
5960

6061
pod, err := request.ChoosePod(flags, podName, deployment, selector)
6162
if err != nil {
6263
return err
6364
}
6465

65-
out, err := kubectl.PodExecString(flags, &pod, command)
66+
out, err := kubectl.PodExecString(flags, &pod, container, command)
6667
if err != nil {
6768
return err
6869
}

cmd/plugin/commands/conf/conf.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
// CreateCommand creates and returns this cobra subcommand
3434
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
35-
var pod, deployment, selector *string
35+
var pod, deployment, selector, container *string
3636
cmd := &cobra.Command{
3737
Use: "conf",
3838
Short: "Inspect the generated nginx.conf",
@@ -42,25 +42,26 @@ func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
4242
return err
4343
}
4444

45-
util.PrintError(conf(flags, host, *pod, *deployment, *selector))
45+
util.PrintError(conf(flags, host, *pod, *deployment, *selector, *container))
4646
return nil
4747
},
4848
}
4949
cmd.Flags().String("host", "", "Print just the server block with this hostname")
5050
pod = util.AddPodFlag(cmd)
5151
deployment = util.AddDeploymentFlag(cmd)
5252
selector = util.AddSelectorFlag(cmd)
53+
container = util.AddContainerFlag(cmd)
5354

5455
return cmd
5556
}
5657

57-
func conf(flags *genericclioptions.ConfigFlags, host string, podName string, deployment string, selector string) error {
58+
func conf(flags *genericclioptions.ConfigFlags, host string, podName string, deployment string, selector string, container string) error {
5859
pod, err := request.ChoosePod(flags, podName, deployment, selector)
5960
if err != nil {
6061
return err
6162
}
6263

63-
nginxConf, err := kubectl.PodExecString(flags, &pod, []string{"/dbg", "conf"})
64+
nginxConf, err := kubectl.PodExecString(flags, &pod, container, []string{"/dbg", "conf"})
6465
if err != nil {
6566
return err
6667
}

cmd/plugin/commands/exec/exec.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,21 @@ import (
2929
// CreateCommand creates and returns this cobra subcommand
3030
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
3131
opts := execFlags{}
32-
var pod, deployment, selector *string
32+
var pod, deployment, selector, container *string
3333

3434
cmd := &cobra.Command{
3535
Use: "exec",
3636
Short: "Execute a command inside an ingress-nginx pod",
3737
RunE: func(cmd *cobra.Command, args []string) error {
38-
util.PrintError(exec(flags, *pod, *deployment, *selector, args, opts))
38+
util.PrintError(exec(flags, *pod, *deployment, *selector, *container, args, opts))
3939
return nil
4040
},
4141
}
4242
pod = util.AddPodFlag(cmd)
4343
deployment = util.AddDeploymentFlag(cmd)
4444
selector = util.AddSelectorFlag(cmd)
45+
container = util.AddContainerFlag(cmd)
46+
4547
cmd.Flags().BoolVarP(&opts.TTY, "tty", "t", false, "Stdin is a TTY")
4648
cmd.Flags().BoolVarP(&opts.Stdin, "stdin", "i", false, "Pass stdin to the container")
4749

@@ -53,7 +55,7 @@ type execFlags struct {
5355
Stdin bool
5456
}
5557

56-
func exec(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, cmd []string, opts execFlags) error {
58+
func exec(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, container string, cmd []string, opts execFlags) error {
5759
pod, err := request.ChoosePod(flags, podName, deployment, selector)
5860
if err != nil {
5961
return err
@@ -67,7 +69,7 @@ func exec(flags *genericclioptions.ConfigFlags, podName string, deployment strin
6769
args = append(args, "-i")
6870
}
6971

70-
args = append(args, []string{"-n", pod.Namespace, pod.Name, "--"}...)
72+
args = append(args, []string{"-n", pod.Namespace, "-c", container, pod.Name, "--"}...)
7173
args = append(args, cmd...)
7274
return kubectl.Exec(flags, args)
7375
}

cmd/plugin/commands/general/general.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,30 @@ import (
3030

3131
// CreateCommand creates and returns this cobra subcommand
3232
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
33-
var pod, deployment, selector *string
33+
var pod, deployment, selector, container *string
3434
cmd := &cobra.Command{
3535
Use: "general",
3636
Short: "Inspect the other dynamic ingress-nginx information",
3737
RunE: func(cmd *cobra.Command, args []string) error {
38-
util.PrintError(general(flags, *pod, *deployment, *selector))
38+
util.PrintError(general(flags, *pod, *deployment, *selector, *container))
3939
return nil
4040
},
4141
}
4242
pod = util.AddPodFlag(cmd)
4343
deployment = util.AddDeploymentFlag(cmd)
4444
selector = util.AddSelectorFlag(cmd)
45+
container = util.AddContainerFlag(cmd)
4546

4647
return cmd
4748
}
4849

49-
func general(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string) error {
50+
func general(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, container string) error {
5051
pod, err := request.ChoosePod(flags, podName, deployment, selector)
5152
if err != nil {
5253
return err
5354
}
5455

55-
out, err := kubectl.PodExecString(flags, &pod, []string{"/dbg", "general"})
56+
out, err := kubectl.PodExecString(flags, &pod, container, []string{"/dbg", "general"})
5657
if err != nil {
5758
return err
5859
}

cmd/plugin/commands/logs/logs.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ import (
3131
// CreateCommand creates and returns this cobra subcommand
3232
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
3333
o := logsFlags{}
34-
var pod, deployment, selector *string
34+
var pod, deployment, selector, container *string
3535

3636
cmd := &cobra.Command{
3737
Use: "logs",
3838
Short: "Get the kubernetes logs for an ingress-nginx pod",
3939
RunE: func(cmd *cobra.Command, args []string) error {
40-
util.PrintError(logs(flags, *pod, *deployment, *selector, o))
40+
util.PrintError(logs(flags, *pod, *deployment, *selector, *container, o))
4141
return nil
4242
},
4343
}
4444
pod = util.AddPodFlag(cmd)
4545
deployment = util.AddDeploymentFlag(cmd)
4646
selector = util.AddSelectorFlag(cmd)
47+
container = util.AddContainerFlag(cmd)
4748

4849
cmd.Flags().BoolVarP(&o.Follow, "follow", "f", o.Follow, "Specify if the logs should be streamed.")
4950
cmd.Flags().BoolVar(&o.Timestamps, "timestamps", o.Timestamps, "Include timestamps on each line in the log output")
@@ -94,13 +95,13 @@ func (o *logsFlags) toStrings() []string {
9495
return r
9596
}
9697

97-
func logs(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, opts logsFlags) error {
98+
func logs(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, container string, opts logsFlags) error {
9899
pod, err := request.ChoosePod(flags, podName, deployment, selector)
99100
if err != nil {
100101
return err
101102
}
102103

103-
cmd := []string{"logs", "-n", pod.Namespace, pod.Name}
104+
cmd := []string{"logs", "-n", pod.Namespace, "-c", container, pod.Name}
104105
cmd = append(cmd, opts.toStrings()...)
105106
return kubectl.Exec(flags, cmd)
106107
}

cmd/plugin/commands/ssh/ssh.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,28 @@ import (
2828

2929
// CreateCommand creates and returns this cobra subcommand
3030
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
31-
var pod, deployment, selector *string
31+
var pod, deployment, selector, container *string
3232
cmd := &cobra.Command{
3333
Use: "ssh",
3434
Short: "ssh into a running ingress-nginx pod",
3535
RunE: func(cmd *cobra.Command, args []string) error {
36-
util.PrintError(ssh(flags, *pod, *deployment, *selector))
36+
util.PrintError(ssh(flags, *pod, *deployment, *selector, *container))
3737
return nil
3838
},
3939
}
4040
pod = util.AddPodFlag(cmd)
4141
deployment = util.AddDeploymentFlag(cmd)
4242
selector = util.AddSelectorFlag(cmd)
43+
container = util.AddContainerFlag(cmd)
4344

4445
return cmd
4546
}
4647

47-
func ssh(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string) error {
48+
func ssh(flags *genericclioptions.ConfigFlags, podName string, deployment string, selector string, container string) error {
4849
pod, err := request.ChoosePod(flags, podName, deployment, selector)
4950
if err != nil {
5051
return err
5152
}
5253

53-
return kubectl.Exec(flags, []string{"exec", "-it", "-n", pod.Namespace, pod.Name, "--", "/bin/bash"})
54+
return kubectl.Exec(flags, []string{"exec", "-it", "-n", pod.Namespace, "-c", container, pod.Name, "--", "/bin/bash"})
5455
}

cmd/plugin/kubectl/kubectl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131

3232
// PodExecString takes a pod and a command, uses kubectl exec to run the command in the pod
3333
// and returns stdout as a string
34-
func PodExecString(flags *genericclioptions.ConfigFlags, pod *apiv1.Pod, args []string) (string, error) {
35-
args = append([]string{"exec", "-n", pod.Namespace, pod.Name}, args...)
34+
func PodExecString(flags *genericclioptions.ConfigFlags, pod *apiv1.Pod, container string, args []string) (string, error) {
35+
args = append([]string{"exec", "-n", pod.Namespace, "-c", container, pod.Name}, args...)
3636
return ExecToString(flags, args)
3737
}
3838

cmd/plugin/util/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
const (
3232
DefaultIngressDeploymentName = "ingress-nginx-controller"
3333
DefaultIngressServiceName = "ingress-nginx-controller"
34+
DefaultIngressContainerName = "controller"
3435
)
3536

3637
// IssuePrefix is the github url that we can append an issue number to to link to it
@@ -127,6 +128,13 @@ func AddSelectorFlag(cmd *cobra.Command) *string {
127128
return &v
128129
}
129130

131+
// AddContainerFlag adds a --container flag to a cobra command
132+
func AddContainerFlag(cmd *cobra.Command) *string {
133+
v := ""
134+
cmd.Flags().StringVar(&v, "container", DefaultIngressContainerName, "The name of the ingress-nginx controller container")
135+
return &v
136+
}
137+
130138
// GetNamespace takes a set of kubectl flag values and returns the namespace we should be operating in
131139
func GetNamespace(flags *genericclioptions.ConfigFlags) string {
132140
namespace, _, err := flags.ToRawKubeConfigLoader().Namespace()

docs/kubectl-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Use "ingress-nginx [command] --help" for more information about a command.
6868
## Common Flags
6969

7070
- Every subcommand supports the basic `kubectl` configuration flags like `--namespace`, `--context`, `--client-key` and so on.
71-
- Subcommands that act on a particular `ingress-nginx` pod (`backends`, `certs`, `conf`, `exec`, `general`, `logs`, `ssh`), support the `--deployment <deployment>` and `--pod <pod>` flags to select either a pod from a deployment with the given name, or a pod with the given name. The `--deployment` flag defaults to `ingress-nginx-controller`.
71+
- Subcommands that act on a particular `ingress-nginx` pod (`backends`, `certs`, `conf`, `exec`, `general`, `logs`, `ssh`), support the `--deployment <deployment>`, `--pod <pod>`, and `--container <container>` flags to select either a pod from a deployment with the given name, or a pod with the given name (and the given container name). The `--deployment` flag defaults to `ingress-nginx-controller`, and the `--container` flag defaults to `controller`.
7272
- Subcommands that inspect resources (`ingresses`, `lint`) support the `--all-namespaces` flag, which causes them to inspect resources in every namespace.
7373

7474
## Subcommands

0 commit comments

Comments
 (0)