Skip to content

Commit 3d65214

Browse files
vrubezhnydatho7561
authored andcommitted
Creating project into sandbox has user limitation #3336
Fixes: #3336 Signed-off-by: Victor Rubezhny <[email protected]>
1 parent 5e54f54 commit 3d65214

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@
15211521
},
15221522
{
15231523
"command": "openshift.project.create",
1524-
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext",
1524+
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext && canCreateNamespace",
15251525
"group": "c1"
15261526
},
15271527
{
@@ -1667,7 +1667,7 @@
16671667
},
16681668
{
16691669
"command": "openshift.project.set",
1670-
"when": "view == openshiftProjectExplorer && viewItem == openshift.k8sContext || viewItem == openshift.project",
1670+
"when": "view == openshiftProjectExplorer && (viewItem == openshift.k8sContext || viewItem == openshift.project) && canCreateNamespace",
16711671
"group": "inline"
16721672
},
16731673
{

src/explorer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ export class OpenShiftExplorer implements TreeDataProvider<ExplorerItem>, Dispos
179179
result = [this.kubeContext];
180180
if (this.kubeContext) {
181181
const config = getKubeConfigFiles();
182+
const canCreateNamespace = await Oc.Instance.canCreateNamespace();
183+
void commands.executeCommand('setContext', 'canCreateNamespace', canCreateNamespace);
182184
result.unshift({label: process.env.KUBECONFIG ? 'Custom KubeConfig' : 'Default KubeConfig', description: config.join(':')})
183185
}
184186
} catch (err) {

src/oc/ocWrapper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ export class Oc {
175175
return false;
176176
}
177177

178+
/**
179+
* Returns true if the current user is authorized to create a namespace on the cluster, and false otherwise.
180+
*
181+
* @returns true if the current user is authorized to create namespace on the cluster, and false otherwise
182+
*/
183+
public async canCreateNamespace(): Promise<boolean> {
184+
try {
185+
const result = await CliChannel.getInstance().executeTool(
186+
new CommandText('oc', 'auth can-i create projectrequests'),
187+
);
188+
if (result.stdout === 'yes') {
189+
return true;
190+
}
191+
} catch {
192+
//ignore
193+
}
194+
return false;
195+
}
196+
178197
/**
179198
* Deletes all deployments in the current namespace that have a label "component" with a value `componentName`.
180199
*

test/integration/ocWrapper.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ suite('./oc/ocWrapper.ts', function () {
6363
}
6464
});
6565

66+
test('canCreateNamespace()', async function () {
67+
const canCreateNamespace1 = await Oc.Instance.canCreateNamespace();
68+
expect(canCreateNamespace1).to.exist;
69+
expect(canCreateNamespace1).to.equal(true);
70+
if (isOpenShift) {
71+
await Oc.Instance.logout();
72+
const canCreateNamespace2 = await Oc.Instance.canCreateNamespace();
73+
expect(canCreateNamespace2).to.exist;
74+
expect(canCreateNamespace2).to.equal(false);
75+
await Oc.Instance.loginWithUsernamePassword(clusterUrl, username, password);
76+
}
77+
});
78+
6679
suite('create, list, and delete kubernetes objects', function () {
6780
const serviceName = 'my-test-service';
6881
const projectName = 'my-test-service-project2';

0 commit comments

Comments
 (0)