Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When turned to using service account for a Sandbox a wrong warning me… #3879

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
9 changes: 7 additions & 2 deletions src/openshift/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

export interface QuickPickItemExt extends QuickPickItem {
name: string,
cluster: string
cluster: string,
user: string,
namespace: string
}

export class Cluster extends OpenShiftItem {
Expand Down Expand Up @@ -186,6 +188,8 @@
.map((ctx) => ({
name: `${ctx.name}`,
cluster: `${ctx.cluster}`,
user: `${ctx.user}`,
namespace: `${ctx.namespace}`,
label: `${ctx.label}`,
description: `on ${ctx.cluster}`,
detail: `User: ${ctx.user}`,
Expand Down Expand Up @@ -223,8 +227,9 @@
if (await LoginUtil.Instance.requireLogin(clusterURL)) {
const status = await Cluster.login(choice.name, true);
if (status) {
const newKcu = new KubeConfigUtils(); // Can be updated after login

Check warning on line 230 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L230

Added line #L230 was not covered by tests
if (Cluster.isSandboxCluster(clusterURL)
&& !k8sConfig.equalsToCurrentContext(choice.name)) {
&& !newKcu.equalsToCurrentContext(choice.name, choice.cluster, choice.namespace, choice.user)) {

Check warning on line 232 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L232

Added line #L232 was not covered by tests
await window.showWarningMessage(
'The cluster appears to be a OpenShift Dev Sandbox cluster, \
but the required project doesn\'t appear to be existing. \
Expand Down Expand Up @@ -840,19 +845,19 @@
} else {
ocToken = userToken;
}
return Progress.execFunctionWithProgress(`Login to the cluster: ${clusterURL}`, async () => {
try {
await Oc.Instance.loginWithToken(clusterURL, ocToken);

Check warning on line 850 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L848-L850

Added lines #L848 - L850 were not covered by tests
if (Cluster.isOpenShiftSandbox(clusterURL)) {
const YES = 'Yes';
const result = await window.showInformationMessage('OpenShift Sandbox logs you out after 15 minutes. Would you like to switch to a service account to prevent this?', YES, 'No');

Check warning on line 853 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L852-L853

Added lines #L852 - L853 were not covered by tests
if (result === YES) {
await Cluster.installPipelineUserContext();

Check warning on line 855 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L855

Added line #L855 was not covered by tests
}
}
return Cluster.loginMessage(clusterURL);

Check warning on line 858 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L858

Added line #L858 was not covered by tests
} catch (error) {
throw new VsCommandError(

Check warning on line 860 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L860

Added line #L860 was not covered by tests
`Failed to login to cluster '${clusterURL}' with '${Filters.filterToken(
error.message,
)}'!`,
Expand Down Expand Up @@ -886,14 +891,14 @@
return Cluster.tokenLogin(apiEndpointUrl, true, clipboard);
}

static async installPipelineUserContext(): Promise<void> {
const kcu = new KubeConfigUtils();
const kcFiles = getKubeConfigFiles();

Check warning on line 896 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L894-L896

Added lines #L894 - L896 were not covered by tests
if (kcFiles.length === 0) {
throw new Error('Could not locate Kube Config when trying to replace OpenShift Sandbox token with a longer-lived token');

Check warning on line 898 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L898

Added line #L898 was not covered by tests
}
const kcPath = kcFiles[0];
const kcActual = YAML.load((await fs.readFile(kcPath)).toString('utf-8')) as {

Check warning on line 901 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L900-L901

Added lines #L900 - L901 were not covered by tests
users: { name: string; user: { token: string } }[];
contexts: {
context: { cluster: string; user: string; namespace: string };
Expand All @@ -903,20 +908,20 @@
clusters: object[];
};

const currentCtx = kcu.getCurrentContext();
const currentCtxObj = kcActual.contexts.find(ctx => ctx.name === currentCtx);
const sandboxUser = currentCtxObj.context.user;
const sandboxUserObj = kcActual.users.find(user => user.name === sandboxUser);

Check warning on line 914 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L911-L914

Added lines #L911 - L914 were not covered by tests

const secrets = await Oc.Instance.getKubernetesObjects('Secret');
const pipelineTokenSecret = secrets.find((secret) => secret.metadata.name.startsWith('pipeline-token')) as any;
const pipelineToken = Buffer.from(pipelineTokenSecret.data.token, 'base64').toString();

Check warning on line 918 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L916-L918

Added lines #L916 - L918 were not covered by tests

sandboxUserObj.user = {

Check warning on line 920 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L920

Added line #L920 was not covered by tests
token: pipelineToken
}

await fs.writeFile(kcPath, YAML.dump(kcActual, { lineWidth: Number.POSITIVE_INFINITY }));

Check warning on line 924 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L924

Added line #L924 was not covered by tests
}

static async loginUsingClipboardInfo(dashboardUrl: string): Promise<string | null> {
Expand All @@ -941,8 +946,8 @@
return `Successfully logged in to '${clusterURL}'`;
}

static isOpenShiftSandbox(url :string): boolean {
const asUrl = new URL(url);
return asUrl.hostname.endsWith('openshiftapps.com');

Check warning on line 951 in src/openshift/cluster.ts

View check run for this annotation

Codecov / codecov/patch

src/openshift/cluster.ts#L949-L951

Added lines #L949 - L951 were not covered by tests
}
}
21 changes: 6 additions & 15 deletions src/util/kubeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,15 @@
return undefined;
}

public equalContexts(c1:string, c2:string): boolean {
if (c1 === c2) return true;
const context1 = this.findContext(c1);
const context2 = this.findContext(c2);
if (context1 === context2) return true; // Both are undefibed or reference the same object
if (context1 === undefined && context2 !== undefined) return false;
if (context1 === undefined && context2 !== undefined) return false;
if (context1.cluster !== context2.cluster) return false;
if (context1.namespace !== context2.namespace) return false;
if (context1.user !== context2.user) return false;
return true;
}

public equalsToCurrentContext(contextName:string): boolean {
public equalsToCurrentContext(contextName:string, cluster: string, namespace: string, user: string): boolean {

Check warning on line 131 in src/util/kubeUtils.ts

View check run for this annotation

Codecov / codecov/patch

src/util/kubeUtils.ts#L131

Added line #L131 was not covered by tests
const currentContext = this.findContext(this.currentContext);
if (!currentContext) return false;

return this.equalContexts(currentContext.name, contextName);
if (currentContext.name !== contextName) return false;
if (currentContext.cluster !== cluster) return false;
if (currentContext.namespace !== namespace) return false;
if (currentContext.user !== user) return false;
return true;

Check warning on line 139 in src/util/kubeUtils.ts

View check run for this annotation

Codecov / codecov/patch

src/util/kubeUtils.ts#L139

Added line #L139 was not covered by tests
}
}

Expand Down
Loading