Skip to content

Commit 19f802a

Browse files
Merge pull request #15758 from openshift-cherrypick-robot/cherry-pick-15745-to-release-4.20
[release-4.20] OCPBUGS-65900: Fix unnecessary rerenders with pod-connect
2 parents 71efa9b + ad2cf8a commit 19f802a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

frontend/public/components/pod-connect.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,18 @@ export const PodConnect: React.FCC<PodConnectProps> = ({
6969

7070
const containers = useMemo(() => _.keyBy(_.get(obj, 'spec.containers', []), 'name'), [obj]);
7171

72+
const podName = useMemo(() => obj.metadata?.name || '', [obj?.metadata?.name]);
73+
const namespace = useMemo(() => obj.metadata?.namespace || 'default', [obj?.metadata?.namespace]);
74+
// We are being more specific with the dependency array here to avoid additional rerenders when other fields in obj changes
75+
// eslint-disable-next-line react-hooks/exhaustive-deps
76+
const isWindows = useMemo(() => isWindowsPod(obj), [obj?.spec?.tolerations]);
77+
7278
const connect = useCallback(() => {
73-
const {
74-
metadata: { name, namespace },
75-
} = obj;
7679
const usedClient = isOpenShift ? 'oc' : 'kubectl';
77-
const command = isWindowsPod(obj) ? ['cmd'] : ['sh', '-i', '-c', 'TERM=xterm sh'];
80+
const command = isWindows ? ['cmd'] : ['sh', '-i', '-c', 'TERM=xterm sh'];
7881
const params = {
7982
ns: namespace,
80-
name,
83+
name: podName,
8184
path: attach ? ATTACH_COMMAND : EXEC_COMMAND,
8285
queryParams: {
8386
stdout: '1',
@@ -100,7 +103,7 @@ export const PodConnect: React.FCC<PodConnectProps> = ({
100103
const subprotocols = impersonate.subprotocols.concat('base64.channel.k8s.io');
101104

102105
let previous = '';
103-
wsRef.current = new WSFactory(`${name}-terminal`, {
106+
wsRef.current = new WSFactory(`${podName}-terminal`, {
104107
host: 'auto',
105108
reconnect: true,
106109
path: resourceURL(PodModel, params),
@@ -141,7 +144,7 @@ export const PodConnect: React.FCC<PodConnectProps> = ({
141144
})
142145
// eslint-disable-next-line no-console
143146
.onerror((evt: any) => console.error(`WS error?! ${evt}`));
144-
}, [obj, attach, activeContainer, t, isOpenShift]);
147+
}, [podName, namespace, isWindows, attach, activeContainer, t, isOpenShift]);
145148

146149
// Connect on mount and when dependencies change
147150
useEffect(() => {

0 commit comments

Comments
 (0)