1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Allow to configure default namespace used in terminal (#3706)

* Allow to configure default namespace used in terminal

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Fix lint issues

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala 2021-09-01 12:50:11 +03:00 committed by GitHub
parent 1f7cdcdb01
commit 584d45c5e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 16 deletions

View File

@ -95,6 +95,7 @@ export interface ClusterPreferences extends ClusterPrometheusPreferences {
hiddenMetrics?: string[]; hiddenMetrics?: string[];
nodeShellImage?: string; nodeShellImage?: string;
imagePullSecret?: string; imagePullSecret?: string;
defaultNamespace?: string;
} }
/** /**

View File

@ -216,6 +216,18 @@ export class Cluster implements ClusterModel, ClusterState {
return toJS({ prometheus, prometheusProvider }); return toJS({ prometheus, prometheusProvider });
} }
/**
* defaultNamespace preference
*
* @computed
* @internal
*/
@computed get defaultNamespace(): string {
const { defaultNamespace } = this.preferences;
return defaultNamespace;
}
constructor(model: ClusterModel) { constructor(model: ClusterModel) {
makeObservable(this); makeObservable(this);
this.id = model.id; this.id = model.id;
@ -298,10 +310,25 @@ export class Cluster implements ClusterModel, ClusterState {
clearInterval(refreshTimer); clearInterval(refreshTimer);
clearInterval(refreshMetadataTimer); clearInterval(refreshMetadataTimer);
}, },
reaction(() => this.defaultNamespace, () => this.recreateProxyKubeconfig()),
); );
} }
} }
/**
* @internal
*/
async recreateProxyKubeconfig() {
logger.info("Recreate proxy kubeconfig");
try {
this.kubeconfigManager.clear();
} catch {
// do nothing
}
this.getProxyKubeconfig();
}
/** /**
* internal * internal
*/ */

View File

@ -56,6 +56,15 @@ export class KubeconfigManager {
return this.tempFile; return this.tempFile;
} }
async clear() {
if (!this.tempFile) {
return;
}
logger.info(`Deleting temporary kubeconfig: ${this.tempFile}`);
await fs.unlink(this.tempFile);
}
async unlink() { async unlink() {
if (!this.tempFile) { if (!this.tempFile) {
return; return;
@ -106,7 +115,7 @@ export class KubeconfigManager {
user: "proxy", user: "proxy",
name: contextName, name: contextName,
cluster: contextName, cluster: contextName,
namespace: kubeConfig.getContextObject(contextName).namespace, namespace: cluster.defaultNamespace || kubeConfig.getContextObject(contextName).namespace,
} }
] ]
}; };

View File

@ -33,43 +33,76 @@ interface Props {
@observer @observer
export class ClusterHomeDirSetting extends React.Component<Props> { export class ClusterHomeDirSetting extends React.Component<Props> {
@observable directory = ""; @observable directory = "";
@observable defaultNamespace = "";
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
makeObservable(this); makeObservable(this);
} }
componentDidMount() { async componentDidMount() {
const kubeconfig = await this.props.cluster.getKubeconfig();
const defaultNamespace = this.props.cluster.preferences?.defaultNamespace || kubeconfig.getContextObject(this.props.cluster.contextName).namespace;
disposeOnUnmount(this, disposeOnUnmount(this,
autorun(() => { autorun(() => {
this.directory = this.props.cluster.preferences.terminalCWD || ""; this.directory = this.props.cluster.preferences.terminalCWD || "";
this.defaultNamespace = defaultNamespace || "";
}) })
); );
} }
save = () => { saveCWD = () => {
this.props.cluster.preferences.terminalCWD = this.directory; this.props.cluster.preferences.terminalCWD = this.directory;
}; };
onChange = (value: string) => { onChangeTerminalCWD = (value: string) => {
this.directory = value; this.directory = value;
}; };
saveDefaultNamespace = () => {
if (this.defaultNamespace) {
this.props.cluster.preferences.defaultNamespace = this.defaultNamespace;
} else {
this.props.cluster.preferences.defaultNamespace = undefined;
}
};
onChangeDefaultNamespace = (value: string) => {
this.defaultNamespace = value;
};
render() { render() {
return ( return (
<> <>
<SubTitle title="Working Directory"/> <section>
<Input <SubTitle title="Working Directory"/>
theme="round-black" <Input
value={this.directory} theme="round-black"
onChange={this.onChange} value={this.directory}
onBlur={this.save} onChange={this.onChangeTerminalCWD}
placeholder="$HOME" onBlur={this.saveCWD}
/> placeholder="$HOME"
<small className="hint"> />
An explicit start path where the terminal will be launched,{" "} <small className="hint">
this is used as the current working directory (cwd) for the shell process. An explicit start path where the terminal will be launched,{" "}
</small> this is used as the current working directory (cwd) for the shell process.
</small>
</section>
<section>
<SubTitle title="Default Namespace"/>
<Input
theme="round-black"
value={this.defaultNamespace}
onChange={this.onChangeDefaultNamespace}
onBlur={this.saveDefaultNamespace}
placeholder={this.defaultNamespace}
/>
<small className="hint">
Default namespace used for kubectl.
</small>
</section>
</> </>
); );
} }