Skip to content

Commit

Permalink
Error popups on trying to delete a project on sandbox due to user lim…
Browse files Browse the repository at this point in the history
…itation #3643

Fixes: #3643

Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny authored and datho7561 committed Nov 30, 2023
1 parent 80658d1 commit 38bc97b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@
},
{
"command": "openshift.project.delete",
"when": "view == openshiftProjectExplorer && viewItem == openshift.project",
"when": "view == openshiftProjectExplorer && viewItem =~ /openshift\\.project.*\\.canDelete*/",
"group": "p3"
},
{
Expand Down Expand Up @@ -1713,7 +1713,7 @@
},
{
"command": "openshift.project.set",
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem == openshift.project) && canCreateNamespace",
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem =~ /openshift\\.project.*/) && canCreateNamespace",
"group": "inline"
},
{
Expand Down
24 changes: 16 additions & 8 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
return OpenShiftExplorer.instance;
}

private static generateOpenshiftProjectContextValue(namespace: string): Thenable<string> {
const contextValue = `openshift.project.${namespace}`;
return Oc.Instance.canDeleteNamespace(namespace)
.then(result => (result ? `${contextValue}.canDelete` : contextValue));
}

// eslint-disable-next-line class-methods-use-this
getTreeItem(element: ExplorerItem): TreeItem | Thenable<TreeItem> {

Expand Down Expand Up @@ -162,12 +168,15 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
// otherwise it is a KubernetesObject instance
if ('kind' in element) {
if (element.kind === 'project') {
return {
contextValue: 'openshift.project',
label: element.metadata.name,
collapsibleState: TreeItemCollapsibleState.Collapsed,
iconPath: path.resolve(__dirname, '../../images/context/project-node.png')
}
return OpenShiftExplorer.generateOpenshiftProjectContextValue(element.metadata.name)
.then(namespace => {
return {
contextValue: namespace,
label: element.metadata.name,
collapsibleState: TreeItemCollapsibleState.Collapsed,
iconPath: path.resolve(__dirname, '../../images/context/project-node.png')
}
});
} else if (element.kind === 'helm') {
return {
contextValue: 'openshift.helm.repos',
Expand Down Expand Up @@ -205,8 +214,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
result = [this.kubeContext];
if (this.kubeContext) {
const config = getKubeConfigFiles();
const canCreateNamespace = await Oc.Instance.canCreateNamespace();
void commands.executeCommand('setContext', 'canCreateNamespace', canCreateNamespace);
void commands.executeCommand('setContext', 'canCreateNamespace', await Oc.Instance.canCreateNamespace());
result.unshift({ label: process.env.KUBECONFIG ? 'Custom KubeConfig' : 'Default KubeConfig', description: config.join(':') })
}
} catch (err) {
Expand Down
24 changes: 24 additions & 0 deletions src/oc/ocWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ export class Oc {
return false;
}

/**
* Returns true if the current user is authorized to delete a namespace on the cluster, and false otherwise.
*
* @param namespace the namespace to be deleted (defaults to the current namespace if none is provided)
* @returns true if the current user is authorized to delete namespace on the cluster, and false otherwise
*/
public async canDeleteNamespace(namespace?: string): Promise<boolean> {
try {
const args: CommandOption[] = [];
if (namespace) {
args.push(new CommandOption('--namespace', namespace));
}
const result = await CliChannel.getInstance().executeTool(
new CommandText('oc', 'auth can-i delete projects', args)
);
if (result.stdout === 'yes') {
return true;
}
} catch {
//ignore
}
return false;
}

/**
* Returns true if the current user is authorized to get a kubernates objects on the cluster, and false otherwise.
*
Expand Down

0 comments on commit 38bc97b

Please sign in to comment.