Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,7 @@
},
{
"command": "openshift.project.create",
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext",
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext && canCreateNamespace",
"group": "c1"
},
{
Expand Down Expand Up @@ -1667,7 +1667,7 @@
},
{
"command": "openshift.project.set",
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext || viewItem == openshift.project",
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem == openshift.project) && canCreateNamespace",
"group": "inline"
},
{
Expand Down
2 changes: 2 additions & 0 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ 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);
result.unshift({label: process.env.KUBECONFIG ? 'Custom KubeConfig' : 'Default KubeConfig', description: config.join(':')})
}
} catch (err) {
Expand Down
19 changes: 19 additions & 0 deletions src/oc/ocWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ export class Oc {
return false;
}

/**
* Returns true if the current user is authorized to create a namespace on the cluster, and false otherwise.
*
* @returns true if the current user is authorized to create namespace on the cluster, and false otherwise
*/
public async canCreateNamespace(): Promise<boolean> {
try {
const result = await CliChannel.getInstance().executeTool(
new CommandText('oc', 'auth can-i create projectrequests'),
);
if (result.stdout === 'yes') {
return true;
}
} catch {
//ignore
}
return false;
}

/**
* Deletes all deployments in the current namespace that have a label "component" with a value `componentName`.
*
Expand Down
13 changes: 13 additions & 0 deletions test/integration/ocWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ suite('./oc/ocWrapper.ts', function () {
}
});

test('canCreateNamespace()', async function () {
const canCreateNamespace1 = await Oc.Instance.canCreateNamespace();
expect(canCreateNamespace1).to.exist;
expect(canCreateNamespace1).to.equal(true);
if (isOpenShift) {
await Oc.Instance.logout();
const canCreateNamespace2 = await Oc.Instance.canCreateNamespace();
expect(canCreateNamespace2).to.exist;
expect(canCreateNamespace2).to.equal(false);
await Oc.Instance.loginWithUsernamePassword(clusterUrl, username, password);
}
});

suite('create, list, and delete kubernetes objects', function () {
const serviceName = 'my-test-service';
const projectName = 'my-test-service-project2';
Expand Down