Skip to content
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 1881953: Generate new namespace with empty node selector for node… #6767

Merged
merged 1 commit into from Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

This file was deleted.

100 changes: 47 additions & 53 deletions frontend/packages/console-app/src/components/nodes/NodeTerminal.tsx
Expand Up @@ -5,16 +5,12 @@ import {
FirehoseResource,
FirehoseResult,
LoadingBox,
NsDropdown,
} from '@console/internal/components/utils';
import { NodeKind, PodKind } from '@console/internal/module/k8s';
import { getRandomChars } from '@console/shared/src/utils';
import { PodExecLoader } from '../../../../../public/components/pod';
import { ImageStreamTagModel, PodModel } from '../../../../../public/models';
import { ImageStreamTagModel, NamespaceModel, PodModel } from '../../../../../public/models';
import { k8sCreate, k8sGet, k8sKillByName } from '../../../../../public/module/k8s';

import './NodeTerminal.scss';

type NodeTerminalErrorProps = {
error: React.ReactNode;
};
Expand All @@ -23,11 +19,6 @@ type NodeTerminalInnerProps = {
obj?: FirehoseResult<PodKind>;
};

type NodeTerminalContentsProps = {
obj: NodeKind;
namespace: string;
};

type NodeTerminalProps = {
obj: NodeKind;
};
Expand Down Expand Up @@ -128,40 +119,63 @@ const NodeTerminalInner: React.FC<NodeTerminalInnerProps> = ({ obj }) => {
}
};

const NodeTerminalContents: React.FC<NodeTerminalContentsProps> = ({ obj: node, namespace }) => {
const NodeTerminal: React.FC<NodeTerminalProps> = ({ obj: node }) => {
const [resources, setResources] = React.useState<FirehoseResource[]>([]);
const [errorMessage, setErrorMessage] = React.useState('');
const nodeName = node.metadata.name;
React.useEffect(() => {
const name = `${nodeName}-debug-${getRandomChars()}`;
getDebugPod(name, namespace, nodeName)
.then((debugPod: PodKind) => k8sCreate(PodModel, debugPod))
.then(() => {
setResources([
{
isList: false,
kind: 'Pod',
name,
namespace,
prop: 'obj',
},
]);
})
.catch((e) => setErrorMessage(e.message));
const deletePod = async () => {
let namespace;
const name = `${nodeName}-debug`;
const deleteNamespace = async (ns) => {
try {
await k8sKillByName(PodModel, name, namespace);
await k8sKillByName(NamespaceModel, ns);
} catch (e) {
// eslint-disable-next-line no-console
console.warn('Could not delete node terminal debug pod.', e);
console.warn('Could not delete node terminal debug namespace.', e);
}
};
const createDebugPod = async () => {
try {
namespace = await k8sCreate(NamespaceModel, {
metadata: {
generateName: 'openshift-debug-node-',
labels: {
'openshift.io/run-level': '0',
},
annotations: {
'openshift.io/node-selector': '',
},
},
});
const podToCreate = await getDebugPod(name, namespace.metadata.name, nodeName);
// wait for the namespace to be ready
await new Promise((resolve) => setTimeout(resolve, 1000));
const debugPod = await k8sCreate(PodModel, podToCreate);
if (debugPod) {
setResources([
{
isList: false,
kind: 'Pod',
name,
namespace: namespace.metadata.name,
prop: 'obj',
},
]);
}
} catch (e) {
setErrorMessage(e.message);
if (namespace) {
deleteNamespace(namespace.metadata.name);
}
}
};
window.addEventListener('beforeunload', deletePod);
createDebugPod();
window.addEventListener('beforeunload', deleteNamespace);
return () => {
deletePod();
window.removeEventListener('beforeunload', deletePod);
deleteNamespace(namespace.metadata.name);
window.removeEventListener('beforeunload', deleteNamespace);
};
}, [nodeName, namespace]);
}, [nodeName]);

return errorMessage ? (
<NodeTerminalError error={errorMessage} />
Expand All @@ -172,24 +186,4 @@ const NodeTerminalContents: React.FC<NodeTerminalContentsProps> = ({ obj: node,
);
};

const NodeTerminal: React.FC<NodeTerminalProps> = ({ obj: node }) => {
const [targetNamespace, setTargetNamespace] = React.useState<string>(null);

return targetNamespace ? (
<NodeTerminalContents obj={node} namespace={targetNamespace} />
) : (
<div className="co-m-pane__body">
<div className="form-group co-node-terminal-namespace">
<label className="control-label" htmlFor="dropdown-selectbox">
Namespace for the Terminal Debug Pod
</label>
<NsDropdown
id="dropdown-selectbox"
onChange={(selectedKey: string) => setTargetNamespace(selectedKey)}
/>
</div>
</div>
);
};

export default NodeTerminal;