Skip to content

Commit

Permalink
Update --exec capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
stepro committed May 31, 2020
1 parent 7605997 commit db9d211
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,13 @@ These flags customize how the command is run.
Flag | Default | Description
---- | ------- | -----------
`-x, --exec` | `false` | execute command in an existing pod
`-k, --prekill` | `[]` | kill existing processes prior to an exec
`-i, --stdin` | `false` | connect standard input to the container
`-t, --tty` | `false` | allocate a pseudo-TTY in the container

When using the `-x, --exec` flag, build, configuration and session flags are ignored with the exception of the `-c, --inherit` flag which is used to help identify the target container. Additionally, this flag cannot be combined with the `-d, --detach` or `--delete` flags.
When using the `-x, --exec` flag, build, configuration and session flags are ignored with the exception of the `-c, --inherit` flag which is used to help identify the target container, and the `-p, --forward` flag. Additionally, this flag cannot be combined with the `-d, --detach` or `--delete` flags.

The `-k, --prekill` flag can be used with the `-x, --exec` flag to pre-kill existing processes by name that may be running in the container. This requires the `pkill` command in the container, and it sends a SIGKILL to all processes matching the specified flag values.

### Detached pod flags

Expand Down
58 changes: 42 additions & 16 deletions cli/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ var flags struct {
listen []string
}
command struct {
exec bool
stdin bool
tty bool
exec bool
prekill []string
stdin bool
tty bool
}
detach bool
delete bool
Expand Down Expand Up @@ -184,6 +185,8 @@ func init() {
// Command flags
cmd.Flags().BoolVarP(&flags.command.exec,
"exec", "x", false, "execute command in an existing container")
cmd.Flags().StringArrayVarP(&flags.command.prekill,
"prekill", "k", nil, "kill existing processes prior to an exec")
cmd.Flags().BoolVarP(&flags.command.stdin,
"stdin", "i", false, "connect standard input to the container")
cmd.Flags().BoolVarP(&flags.command.tty,
Expand Down Expand Up @@ -240,6 +243,24 @@ func init() {
})
}

func forwardPorts(k *kubectl.CLI, pod string) (func(), error) {
var hasForwardedPorts bool
portsForwarded := make(chan bool)
portForwardEnded := make(chan error)
stop := k.StartLines(append([]string{"port-forward", pod}, flags.session.ports...), func(line string) {
if !hasForwardedPorts && strings.HasPrefix(line, "Forwarding from 127.0.0.1:") {
hasForwardedPorts = true
portsForwarded <- true
}
}, portForwardEnded)
select {
case err := <-portForwardEnded:
return nil, err
case <-portsForwarded:
return stop, nil
}
}

func run(cmd *cobra.Command, args []string) error {
var k = kubectl.NewCLI(
flags.kubectl.path,
Expand Down Expand Up @@ -270,6 +291,9 @@ func run(cmd *cobra.Command, args []string) error {
return errors.New("Cannot combine sync, forward or listen flags with detach flag")
}
}
if !flags.command.exec && len(flags.command.prekill) > 0 {
return errors.New("Can only use prekill flag with exec flag")
}
if flags.command.exec && flags.detach {
return errors.New("Cannot combine exec and detach flags")
}
Expand Down Expand Up @@ -306,6 +330,18 @@ func run(cmd *cobra.Command, args []string) error {
container = nameContainer[1]
}
execArgs = append(execArgs, container)
if len(flags.command.prekill) > 0 {
killArgs := append(execArgs, "--", "pkill", "-9")
killArgs = append(killArgs, flags.command.prekill...)
k.Run(killArgs...)
}
if len(flags.session.ports) > 0 {
stop, err := forwardPorts(k, "kudo-"+hash)
if err != nil {
return err
}
defer stop()
}
if flags.command.stdin {
execArgs = append(execArgs, "--stdin")
}
Expand Down Expand Up @@ -419,22 +455,12 @@ func run(cmd *cobra.Command, args []string) error {

if len(flags.session.ports) > 0 {
op := out.Start("Forwarding ports")
var hasForwardedPorts bool
portsForwarded := make(chan bool)
portForwardEnded := make(chan error)
stop := k.StartLines(append([]string{"port-forward", p.Pod}, flags.session.ports...), func(line string) {
if !hasForwardedPorts && strings.HasPrefix(line, "Forwarding from 127.0.0.1:") {
hasForwardedPorts = true
portsForwarded <- true
}
}, portForwardEnded)
select {
case err := <-portForwardEnded:
stop, err := forwardPorts(k, p.Pod)
if err != nil {
op.Failed()
return err
case <-portsForwarded:
op.Done()
}
op.Done()
defer stop()
}

Expand Down

0 comments on commit db9d211

Please sign in to comment.