Skip to content

Commit 4aeb6e2

Browse files
committed
Error popups on trying to delete a project on sandbox due to user limitation #3643
Fixes: #3643 Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
1 parent b859e8d commit 4aeb6e2

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@
15921592
},
15931593
{
15941594
"command": "openshift.project.delete",
1595-
"when": "view == openshiftProjectExplorer && viewItem == openshift.project",
1595+
"when": "view == openshiftProjectExplorer && viewItem =~ /openshift\\.project.*\\.canDelete*/",
15961596
"group": "p3"
15971597
},
15981598
{
@@ -1713,7 +1713,7 @@
17131713
},
17141714
{
17151715
"command": "openshift.project.set",
1716-
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem == openshift.project) && canCreateNamespace",
1716+
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem =~ /openshift\\.project.*/) && canCreateNamespace",
17171717
"group": "inline"
17181718
},
17191719
{

src/explorer.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,16 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
109109
return OpenShiftExplorer.instance;
110110
}
111111

112+
private static async generateOpenshiftProjectContextValue(namespace: string): Promise<string> {
113+
let contextValue = `openshift.project.${namespace}`;
114+
if (await Oc.Instance.canDeleteNamespace(namespace)) {
115+
contextValue += `${contextValue}.canDelete`;
116+
}
117+
return contextValue;
118+
}
119+
112120
// eslint-disable-next-line class-methods-use-this
113-
getTreeItem(element: ExplorerItem): TreeItem | Thenable<TreeItem> {
121+
async getTreeItem(element: ExplorerItem): Promise<TreeItem | Thenable<TreeItem>> {
114122

115123
if ('command' in element) {
116124
return element;
@@ -163,7 +171,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
163171
if ('kind' in element) {
164172
if (element.kind === 'project') {
165173
return {
166-
contextValue: 'openshift.project',
174+
contextValue: await OpenShiftExplorer.generateOpenshiftProjectContextValue(element.metadata.name),
167175
label: element.metadata.name,
168176
collapsibleState: TreeItemCollapsibleState.Collapsed,
169177
iconPath: path.resolve(__dirname, '../../images/context/project-node.png')
@@ -205,8 +213,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
205213
result = [this.kubeContext];
206214
if (this.kubeContext) {
207215
const config = getKubeConfigFiles();
208-
const canCreateNamespace = await Oc.Instance.canCreateNamespace();
209-
void commands.executeCommand('setContext', 'canCreateNamespace', canCreateNamespace);
216+
void commands.executeCommand('setContext', 'canCreateNamespace', await Oc.Instance.canCreateNamespace());
210217
result.unshift({ label: process.env.KUBECONFIG ? 'Custom KubeConfig' : 'Default KubeConfig', description: config.join(':') })
211218
}
212219
} catch (err) {

src/oc/ocWrapper.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ export class Oc {
194194
return false;
195195
}
196196

197+
/**
198+
* Returns true if the current user is authorized to delete a namespace on the cluster, and false otherwise.
199+
*
200+
* @param namespace the namespace to be deleted (defaults to the current namespace if none is provided)
201+
* @returns true if the current user is authorized to delete namespace on the cluster, and false otherwise
202+
*/
203+
public async canDeleteNamespace(namespace?: string): Promise<boolean> {
204+
try {
205+
const args: CommandOption[] = [];
206+
if (namespace) {
207+
args.push(new CommandOption('--namespace', namespace));
208+
}
209+
const result = await CliChannel.getInstance().executeTool(
210+
new CommandText('oc', 'auth can-i delete projects', args)
211+
);
212+
if (result.stdout === 'yes') {
213+
return true;
214+
}
215+
} catch {
216+
//ignore
217+
}
218+
return false;
219+
}
220+
197221
/**
198222
* Returns true if the current user is authorized to get a kubernates objects on the cluster, and false otherwise.
199223
*

0 commit comments

Comments
 (0)