-
Notifications
You must be signed in to change notification settings - Fork 613
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
Bug 1838658: Use new proxy to connect to cloudshell in terminal #5428
Merged
openshift-merge-robot
merged 7 commits into
openshift:master
from
abhinandan13jan:cloud-shell-terminal
May 22, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e6684c3
fix(cloud-shell): Use CloudShell exec and resizable terminal
abhinandan13jan 2678c42
Fix terminal resize, tab layout and some cleanup (#2)
rohitkrai03 235ca8e
track3 - check workspace namespace before returning CloudShellExec
invalid-email-address 5bb04a1
Convert to FC, fix tab layout and other cleanups (#3)
rohitkrai03 b6de504
TerminalLoadingBox and related Tests
invalid-email-address 34b8402
Fixes based on review comments (#4)
rohitkrai03 1b47f51
track5
invalid-email-address File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
frontend/packages/console-app/src/components/cloud-shell/CloudShellExec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import * as React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Base64 } from 'js-base64'; | ||
import { LoadError } from '@console/internal/components/utils'; | ||
import { connectToFlags, WithFlagsProps } from '@console/internal/reducers/features'; | ||
import { impersonateStateToProps } from '@console/internal/reducers/ui'; | ||
import { FLAGS } from '@console/shared'; | ||
import { WSFactory } from '@console/internal/module/ws-factory'; | ||
import { resourceURL } from '@console/internal/module/k8s'; | ||
import { PodModel } from '@console/internal/models'; | ||
import Terminal, { ImperativeTerminalType } from './Terminal'; | ||
import TerminalLoadingBox from './TerminalLoadingBox'; | ||
|
||
// pod exec WS protocol is FD prefixed, base64 encoded data (sometimes json stringified) | ||
|
||
// Channel 0 is STDIN, 1 is STDOUT, 2 is STDERR (if TTY is not requested), and 3 is a special error channel - 4 is C&C | ||
// The server only reads from STDIN, writes to the other three. | ||
// see also: https://github.com/kubernetes/kubernetes/pull/13885 | ||
|
||
type Props = { | ||
container: string; | ||
podname: string; | ||
namespace: string; | ||
shcommand?: string[]; | ||
}; | ||
|
||
type StateProps = { | ||
impersonate?: { | ||
subprotocols: string[]; | ||
}; | ||
}; | ||
|
||
type CloudShellExecProps = Props & StateProps & WithFlagsProps; | ||
|
||
const NO_SH = | ||
'starting container process caused "exec: \\"sh\\": executable file not found in $PATH"'; | ||
|
||
const CloudShellExec: React.FC<CloudShellExecProps> = ({ | ||
container, | ||
podname, | ||
namespace, | ||
shcommand, | ||
flags, | ||
impersonate, | ||
}) => { | ||
const [wsOpen, setWsOpen] = React.useState<boolean>(false); | ||
const [wsError, setWsError] = React.useState<string>(); | ||
const ws = React.useRef<WSFactory>(); | ||
const terminal = React.useRef<ImperativeTerminalType>(); | ||
|
||
const onData = React.useCallback((data: string): void => { | ||
ws.current && ws.current.send(`0${Base64.encode(data)}`); | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
let unmounted: boolean; | ||
const usedClient = flags[FLAGS.OPENSHIFT] ? 'oc' : 'kubectl'; | ||
const cmd = shcommand || ['sh', '-i', '-c', 'TERM=xterm sh']; | ||
const subprotocols = (impersonate?.subprotocols || []).concat('base64.channel.k8s.io'); | ||
|
||
const urlOpts = { | ||
ns: namespace, | ||
name: podname, | ||
path: 'exec', | ||
queryParams: { | ||
stdout: '1', | ||
stdin: '1', | ||
stderr: '1', | ||
tty: '1', | ||
container, | ||
command: cmd.map((c) => encodeURIComponent(c)).join('&command='), | ||
}, | ||
}; | ||
|
||
const path = resourceURL(PodModel, urlOpts); | ||
const wsOpts = { | ||
host: 'auto', | ||
reconnect: true, | ||
jsonParse: false, | ||
path, | ||
subprotocols, | ||
}; | ||
|
||
const websocket: WSFactory = new WSFactory(`${podname}-terminal`, wsOpts); | ||
let previous; | ||
|
||
websocket | ||
.onmessage((msg) => { | ||
const currentTerminal = terminal.current; | ||
// error channel | ||
if (msg[0] === '3') { | ||
if (previous.includes(NO_SH)) { | ||
const errMsg = `This container doesn't have a /bin/sh shell. Try specifying your command in a terminal with:\r\n\r\n ${usedClient} -n ${namespace} exec ${podname} -ti <command>`; | ||
currentTerminal && currentTerminal.reset(); | ||
currentTerminal && currentTerminal.onConnectionClosed(errMsg); | ||
websocket.destroy(); | ||
previous = ''; | ||
return; | ||
} | ||
} | ||
const data = Base64.decode(msg.slice(1)); | ||
currentTerminal && currentTerminal.onDataReceived(data); | ||
previous = data; | ||
}) | ||
.onopen(() => { | ||
const currentTerminal = terminal.current; | ||
currentTerminal && currentTerminal.reset(); | ||
previous = ''; | ||
if (!unmounted) setWsOpen(true); | ||
}) | ||
.onclose((evt) => { | ||
if (!evt || evt.wasClean === true) { | ||
return; | ||
} | ||
const currentTerminal = terminal.current; | ||
const error = evt.reason || 'The terminal connection has closed.'; | ||
currentTerminal && currentTerminal.onConnectionClosed(error); | ||
websocket.destroy(); | ||
if (!unmounted) setWsError(error); | ||
}) // eslint-disable-next-line no-console | ||
.onerror((evt) => console.error(`WS error?! ${evt}`)); | ||
|
||
if (ws.current !== websocket) { | ||
ws.current && ws.current.destroy(); | ||
ws.current = websocket; | ||
const currentTerminal = terminal.current; | ||
currentTerminal && currentTerminal.onConnectionClosed(`connecting to ${container}`); | ||
} | ||
|
||
return () => { | ||
unmounted = true; | ||
websocket.destroy(); | ||
}; | ||
}, [container, flags, impersonate, namespace, podname, shcommand]); | ||
|
||
if (wsError) { | ||
return <LoadError message={wsError} label="OpenShift command line terminal" canRetry={false} />; | ||
} | ||
|
||
if (wsOpen) { | ||
return <Terminal onData={onData} ref={terminal} />; | ||
} | ||
|
||
return <TerminalLoadingBox />; | ||
}; | ||
|
||
export default connect<StateProps>(impersonateStateToProps)( | ||
connectToFlags<CloudShellExecProps & WithFlagsProps>(FLAGS.OPENSHIFT)(CloudShellExec), | ||
); |
21 changes: 12 additions & 9 deletions
21
frontend/packages/console-app/src/components/cloud-shell/CloudShellTab.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
frontend/packages/console-app/src/components/cloud-shell/CloudShellTab.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import * as React from 'react'; | ||
import { InlineTechPreviewBadge } from '@console/shared'; | ||
import CloudShellTerminal from './CloudShellTerminal'; | ||
import './CloudShellTab.scss'; | ||
import { InlineTechPreviewBadge } from '@console/shared'; | ||
|
||
const CloudShellTab: React.FC = () => ( | ||
<div className="co-cloud-shell-tab"> | ||
<> | ||
<div className="co-cloud-shell-tab__header"> | ||
<div className="co-cloud-shell-tab__header-text">OpenShift command line terminal</div> | ||
<InlineTechPreviewBadge /> | ||
</div> | ||
<CloudShellTerminal /> | ||
</div> | ||
<div className="co-cloud-shell-tab__body"> | ||
<CloudShellTerminal /> | ||
</div> | ||
</> | ||
); | ||
|
||
export default CloudShellTab; |
7 changes: 7 additions & 0 deletions
7
frontend/packages/console-app/src/components/cloud-shell/CloudShellTerminal.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.co-cloudshell-terminal { | ||
&__container { | ||
background-color: #000; | ||
color: var(--pf-global--Color--light-100); | ||
height: 100%; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
frontend/packages/console-app/src/components/cloud-shell/CloudShellTerminalFrame.scss
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
frontend/packages/console-app/src/components/cloud-shell/CloudShellTerminalFrame.tsx
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
frontend/packages/console-app/src/components/cloud-shell/Terminal.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.co-terminal { | ||
height: 100%; | ||
|
||
> .terminal { | ||
height: 100%; | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these props affect the web socket but none of them will cause the web socket to reconnect if they change.
We may not get into this use case for the current implementation however we should be creating reactive components that update according to their incoming props.