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[];
nodeShellImage?: string;
imagePullSecret?: string;
defaultNamespace?: string;
}
/**

View File

@ -216,6 +216,18 @@ export class Cluster implements ClusterModel, ClusterState {
return toJS({ prometheus, prometheusProvider });
}
/**
* defaultNamespace preference
*
* @computed
* @internal
*/
@computed get defaultNamespace(): string {
const { defaultNamespace } = this.preferences;
return defaultNamespace;
}
constructor(model: ClusterModel) {
makeObservable(this);
this.id = model.id;
@ -298,10 +310,25 @@ export class Cluster implements ClusterModel, ClusterState {
clearInterval(refreshTimer);
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
*/

View File

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

View File

@ -33,43 +33,76 @@ interface Props {
@observer
export class ClusterHomeDirSetting extends React.Component<Props> {
@observable directory = "";
@observable defaultNamespace = "";
constructor(props: Props) {
super(props);
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,
autorun(() => {
this.directory = this.props.cluster.preferences.terminalCWD || "";
this.defaultNamespace = defaultNamespace || "";
})
);
}
save = () => {
saveCWD = () => {
this.props.cluster.preferences.terminalCWD = this.directory;
};
onChange = (value: string) => {
onChangeTerminalCWD = (value: string) => {
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() {
return (
<>
<SubTitle title="Working Directory"/>
<Input
theme="round-black"
value={this.directory}
onChange={this.onChange}
onBlur={this.save}
placeholder="$HOME"
/>
<small className="hint">
An explicit start path where the terminal will be launched,{" "}
this is used as the current working directory (cwd) for the shell process.
</small>
<section>
<SubTitle title="Working Directory"/>
<Input
theme="round-black"
value={this.directory}
onChange={this.onChangeTerminalCWD}
onBlur={this.saveCWD}
placeholder="$HOME"
/>
<small className="hint">
An explicit start path where the terminal will be launched,{" "}
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>
</>
);
}