Skip to content

Commit e5ace19

Browse files
Merge pull request #16117 from alexlarsson/container-terminal-helper
Add and use libpod/Container.Terminal() helper
2 parents 619366d + 1038f06 commit e5ace19

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

libpod/container.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,14 @@ func (c *Container) WorkingDir() string {
680680
return "/"
681681
}
682682

683+
// Terminal returns true if the container has a terminal
684+
func (c *Container) Terminal() bool {
685+
if c.config.Spec != nil && c.config.Spec.Process != nil {
686+
return c.config.Spec.Process.Terminal
687+
}
688+
return false
689+
}
690+
683691
// State Accessors
684692
// Require locking
685693

libpod/container_api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (c *Container) Attach(streams *define.AttachStreams, keys string, resize <-
274274
// Send a SIGWINCH after attach succeeds so that most programs will
275275
// redraw the screen for the new attach session.
276276
attachRdy := make(chan bool, 1)
277-
if c.config.Spec.Process != nil && c.config.Spec.Process.Terminal {
277+
if c.Terminal() {
278278
go func() {
279279
<-attachRdy
280280
if err := c.ociRuntime.KillContainer(c, uint(signal.SIGWINCH), false); err != nil {

libpod/kube.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ func containerToV1Container(ctx context.Context, c *Container) (v1.Container, []
698698
// container.EnvFromSource =
699699
kubeContainer.SecurityContext = kubeSec
700700
kubeContainer.StdinOnce = false
701-
kubeContainer.TTY = c.config.Spec.Process.Terminal
701+
kubeContainer.TTY = c.Terminal()
702702

703703
if c.config.Spec.Linux != nil &&
704704
c.config.Spec.Linux.Resources != nil {

libpod/oci_conmon_common.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,7 @@ func socketCloseWrite(conn *net.UnixConn) error {
493493
// Returns any errors that occurred, and whether the connection was successfully
494494
// hijacked before that error occurred.
495495
func (r *ConmonOCIRuntime) HTTPAttach(ctr *Container, req *http.Request, w http.ResponseWriter, streams *HTTPAttachStreams, detachKeys *string, cancel <-chan bool, hijackDone chan<- bool, streamAttach, streamLogs bool) (deferredErr error) {
496-
isTerminal := false
497-
if ctr.config.Spec.Process != nil {
498-
isTerminal = ctr.config.Spec.Process.Terminal
499-
}
496+
isTerminal := ctr.Terminal()
500497

501498
if streams != nil {
502499
if !streams.Stdin && !streams.Stdout && !streams.Stderr {
@@ -1038,7 +1035,7 @@ func (r *ConmonOCIRuntime) createOCIContainer(ctr *Container, restoreOptions *Co
10381035
args = append(args, fmt.Sprintf("--sdnotify-socket=%s", ctr.config.SdNotifySocket))
10391036
}
10401037

1041-
if ctr.config.Spec.Process.Terminal {
1038+
if ctr.Terminal() {
10421039
args = append(args, "-t")
10431040
} else if ctr.config.Stdin {
10441041
args = append(args, "-i")
@@ -1135,7 +1132,7 @@ func (r *ConmonOCIRuntime) createOCIContainer(ctr *Container, restoreOptions *Co
11351132
cmd.Stdin = os.Stdin
11361133
cmd.Stdout = os.Stdout
11371134
cmd.Stderr = os.Stderr
1138-
if ctr.config.Spec.Process.Terminal {
1135+
if ctr.Terminal() {
11391136
cmd.Stderr = &stderrBuf
11401137
}
11411138

pkg/domain/infra/abi/containers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,10 +1690,8 @@ func (ic *ContainerEngine) ContainerClone(ctx context.Context, ctrCloneOpts enti
16901690
return nil, err
16911691
}
16921692

1693-
conf := c.Config()
1694-
if conf.Spec != nil && conf.Spec.Process != nil && conf.Spec.Process.Terminal { // if we do not pass term, running ctrs exit
1695-
spec.Terminal = true
1696-
}
1693+
// if we do not pass term, running ctrs exit
1694+
spec.Terminal = c.Terminal()
16971695

16981696
// Print warnings
16991697
if len(out) > 0 {

pkg/domain/infra/abi/terminal/terminal_common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func StartAttachCtr(ctx context.Context, ctr *libpod.Container, stdout, stderr,
4949

5050
// Check if we are attached to a terminal. If we are, generate resize
5151
// events, and set the terminal to raw mode
52-
if haveTerminal && ctr.Spec().Process.Terminal {
52+
53+
if haveTerminal && ctr.Terminal() {
5354
cancel, oldTermState, err := handleTerminalAttach(ctx, resize)
5455
if err != nil {
5556
return err

0 commit comments

Comments
 (0)