Skip to content

Commit 18b4adb

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 18b4adb

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
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: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
109109
return OpenShiftExplorer.instance;
110110
}
111111

112+
private static generateOpenshiftProjectContextValue(namespace: string): Thenable<string> {
113+
const contextValue = `openshift.project.${namespace}`;
114+
return Oc.Instance.canDeleteNamespace(namespace)
115+
.then(result => (result ? `${contextValue}.canDelete` : contextValue));
116+
}
117+
112118
// eslint-disable-next-line class-methods-use-this
113119
getTreeItem(element: ExplorerItem): TreeItem | Thenable<TreeItem> {
114120

@@ -162,12 +168,15 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
162168
// otherwise it is a KubernetesObject instance
163169
if ('kind' in element) {
164170
if (element.kind === 'project') {
165-
return {
166-
contextValue: 'openshift.project',
167-
label: element.metadata.name,
168-
collapsibleState: TreeItemCollapsibleState.Collapsed,
169-
iconPath: path.resolve(__dirname, '../../images/context/project-node.png')
170-
}
171+
return OpenShiftExplorer.generateOpenshiftProjectContextValue(element.metadata.name)
172+
.then(namespace => {
173+
return {
174+
contextValue: namespace,
175+
label: element.metadata.name,
176+
collapsibleState: TreeItemCollapsibleState.Collapsed,
177+
iconPath: path.resolve(__dirname, '../../images/context/project-node.png')
178+
}
179+
});
171180
} else if (element.kind === 'helm') {
172181
return {
173182
contextValue: 'openshift.helm.repos',
@@ -205,8 +214,7 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
205214
result = [this.kubeContext];
206215
if (this.kubeContext) {
207216
const config = getKubeConfigFiles();
208-
const canCreateNamespace = await Oc.Instance.canCreateNamespace();
209-
void commands.executeCommand('setContext', 'canCreateNamespace', canCreateNamespace);
217+
void commands.executeCommand('setContext', 'canCreateNamespace', await Oc.Instance.canCreateNamespace());
210218
result.unshift({ label: process.env.KUBECONFIG ? 'Custom KubeConfig' : 'Default KubeConfig', description: config.join(':') })
211219
}
212220
} 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)