Skip to content

Commit

Permalink
fix: various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akurinnoy committed Sep 8, 2021
1 parent b315296 commit 9efeb04
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 60 deletions.
9 changes: 6 additions & 3 deletions packages/dashboard-backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import 'reflect-metadata';
import fastify, { FastifyRequest } from 'fastify';
import fastify from 'fastify';
import args from 'args';
import { registerStaticServer } from './static';
import { registerDevworkspaceWebsocketWatcher } from './api/devworkspaceWebsocketWatcher';
Expand All @@ -21,6 +21,7 @@ import { registerTemplateApi } from './api/templateApi';
import { registerCheServerApiProxy } from './cheServerApiProxy';
import { registerCors } from './cors';
import { registerSwagger } from './swagger';
import { HttpError } from '@kubernetes/client-node';

const CHE_HOST = process.env.CHE_HOST as string;

Expand Down Expand Up @@ -48,8 +49,10 @@ server.addContentTypeParser(
try {
const json = JSON.parse(body as string);
done(null, json);
} catch (err) {
done(new Error('Bad Request'), undefined);
} catch (e) {
const error = e as HttpError;
error.statusCode = 400;
done(error, undefined);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Red Hat, Inc. - initial API and implementation
*/

import { getMessage } from '@eclipse-che/common/lib/helpers/errors';
import { IDevWorkspaceCallbacks } from '../devworkspace-client';
import { DwClientProvider } from './kubeclient/dwClientProvider';

Expand Down Expand Up @@ -50,7 +51,7 @@ class DevWorkspaceWatcher {
}
this.unsubscribeFunction = unsubscribeFunction;
} catch (error) {
this.callbacks.onError((error as any).toString());
this.callbacks.onError(getMessage(error));
throw error;
}
}
Expand Down
25 changes: 0 additions & 25 deletions packages/dashboard-backend/src/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,3 @@ export function getSchema(additionalParams: restParams.ISchemaParams): { schema:

return {schema};
}

export function getErrorMessage(error: unknown): string {
if (!error) {
return '';
}
if (isError(error) && error.message) {
return error.message;
}
if (isAxiosResponse(error)) {
if (error.data.message && typeof error.data.message === 'string') {
return error.data.message;
} else {
return JSON.stringify(error.data);
}
}
return JSON.stringify(error);
}

function isError(error: unknown): error is Error {
return (error as Error).message !== undefined;
}

function isAxiosResponse(response: unknown): response is AxiosResponse {
return (response as AxiosResponse).status !== undefined && (response as AxiosResponse).data !== undefined;
}
28 changes: 12 additions & 16 deletions packages/dashboard-backend/src/services/kubeclient/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Red Hat, Inc. - initial API and implementation
*/

import { getMessage } from '@eclipse-che/common/lib/helpers/errors';
import * as k8s from '@kubernetes/client-node';

const projectApiGroup = 'project.openshift.io';
Expand All @@ -18,24 +19,19 @@ export async function isOpenShift(apisApi: k8s.ApisApi): Promise<boolean> {
try {
return findApi(apisApi, projectApiGroup);
} catch (e) {
console.log('Can\'t evaluate target platform: ', e);
return e as boolean;
throw new Error(`Can't evaluate target platform: ${getMessage(e)}`);
}
}

async function findApi(apisApi: k8s.ApisApi, apiName: string, version?: string): Promise<boolean> {
try {
const resp = await apisApi.getAPIVersions();
const groups = resp.body.groups;
const filtered =
groups.some((apiGroup: k8s.V1APIGroup) => {
if (version) {
return apiGroup.name === apiName && apiGroup.versions.filter(versionGroup => versionGroup.version === version).length > 0;
}
return apiGroup.name === apiName;
});
return filtered;
} catch (e) {
return false;
}
const resp = await apisApi.getAPIVersions();
const groups = resp.body.groups;
const filtered =
groups.some((apiGroup: k8s.V1APIGroup) => {
if (version) {
return apiGroup.name === apiName && apiGroup.versions.filter(versionGroup => versionGroup.version === version).length > 0;
}
return apiGroup.name === apiName;
});
return filtered;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import axios from 'axios';
import { getErrorMessage } from '../helpers';
import { getMessage } from '@eclipse-che/common/lib/helpers/errors';
import { isCheServerApiProxyRequired } from '../../index';
import * as https from 'https';
import { URL } from 'url';
Expand Down Expand Up @@ -50,7 +50,7 @@ export async function validateToken(keycloakToken: string): Promise<void> {
throw {
statusCode,
error: statusText,
message: `Failed to validate token: ${getErrorMessage(e)}`
message: `Failed to validate token: ${getMessage(e)}`
};
}
}
Expand Down Expand Up @@ -81,7 +81,7 @@ async function evaluateKeycloakEndpointUrl(): Promise<URL> {
throw {
statusCode,
error: statusText,
message: `Failed to fetch keycloak settings: ${getErrorMessage(e)}`
message: `Failed to fetch keycloak settings: ${getMessage(e)}`
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
import { V1alpha2DevWorkspaceTemplateMetadata } from '@devfile/api';

export type DevWorkspaceTemplateMetadata = V1alpha2DevWorkspaceTemplateMetadata
& Required<Pick<V1alpha2DevWorkspaceTemplateMetadata, 'annotations' | 'labels' | 'name' | 'namespace'>>;
& Required<Pick<V1alpha2DevWorkspaceTemplateMetadata, 'annotations' | 'name' | 'namespace'>>;
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export function isDevWorkspaceTemplate(template: unknown): template is devfileAp

export function isDevWorkspaceTemplateMetadata(metadata: unknown): metadata is devfileApi.DevWorkspaceTemplateMetadata {
return metadata !== undefined
&& (metadata as devfileApi.DevWorkspaceTemplateMetadata).labels !== undefined
&& (metadata as devfileApi.DevWorkspaceTemplateMetadata).name !== undefined
&& (metadata as devfileApi.DevWorkspaceTemplateMetadata).namespace !== undefined;
}
5 changes: 3 additions & 2 deletions packages/dashboard-frontend/src/services/keycloak/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ export class KeycloakSetupService {
return settings;
} catch (e) {
if (common.helpers.errors.isAxiosError(e) && e.response?.status === 404) {
// not found, which mean Che Server is configured to be run without keycloak.
return;
}

let errorMessage = 'Cannot get Keycloak settings';
if (common.helpers.errors.isAxiosError(e) && e.response?.status) {
errorMessage += `: ${e.response.status} ${e.response.statusText}`;
if (common.helpers.errors.isAxiosError(e)) {
errorMessage += ': ' + common.helpers.errors.getMessage(e);
} else {
errorMessage += '. Response is not available, please check the Network tab of Developer tools.';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import { getId, getStatus } from './helper';
import devfileApi, { isDevfileV2, isDevWorkspace } from '../devfileApi';
import { devWorkspaceKind } from '../devfileApi/devWorkspace';

const ROUTING_CLASS = 'che';

export interface Workspace {
readonly ref: che.Workspace | devfileApi.DevWorkspace;

Expand Down Expand Up @@ -239,15 +237,18 @@ export class WorkspaceAdapter<T extends che.Workspace | devfileApi.DevWorkspace>
if (isCheWorkspace(this.workspace)) {
this.workspace.devfile = devfile as che.WorkspaceDevfile;
} else {
const workspace = this.workspace as devfileApi.DevWorkspace;
const converted = devfileToDevWorkspace(
devfile as devfileApi.Devfile,
ROUTING_CLASS,
this.status === DevWorkspaceStatus.RUNNING
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
workspace.spec.routingClass!,
workspace.spec.started
);
if (isDevWorkspace(converted)) {
(this.workspace as devfileApi.DevWorkspace) = converted;
} else {
console.error(`WorkspaceAdapter: the received devworkspace either has wrong "kind" (not ${devWorkspaceKind}) or lacks some of mandatory : `, converted);
console.error(`WorkspaceAdapter: the received devworkspace either has wrong "kind" (not ${devWorkspaceKind}) or lacks some of mandatory fields: `, converted);
throw new Error('Unexpected error happened. Please check the Console tab of Developer tools.');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export function devfileToDevWorkspace(devfile: devfileApi.Devfile, routingClass:
name: devfile.metadata.name,
namespace: devfile.metadata.namespace,
annotations: devWorkspaceAnnotations,
labels: {},
uid: '',
},
spec: {
started,
Expand All @@ -34,7 +36,7 @@ export function devfileToDevWorkspace(devfile: devfileApi.Devfile, routingClass:
components: []
}
}
} as devfileApi.DevWorkspaceLike as devfileApi.DevWorkspace;
} as devfileApi.DevWorkspace;
if (devfile.projects) {
template.spec.template.projects = devfile.projects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import { DevWorkspaceStatus } from '../../../services/helpers/types';
import { createObject } from '../../helpers';
import { DevWorkspaceClient, DEVWORKSPACE_NEXT_START_ANNOTATION, IStatusUpdate } from '../../../services/workspace-client/devworkspace/devWorkspaceClient';
import { CheWorkspaceClient } from '../../../services/workspace-client/cheworkspace/cheWorkspaceClient';
import devfileApi from '../../../services/devfileApi';
import devfileApi, { isDevWorkspace } from '../../../services/devfileApi';
import { deleteLogs, mergeLogs } from '../logs';
import { getDefer, IDeferred } from '../../../services/helpers/deferred';
import { DisposableCollection } from '../../../services/helpers/disposable';
import { selectDwPluginsList } from '../../Plugins/devWorkspacePlugins/selectors';
import { getId } from '../../../services/workspace-adapter/helper';
import { devWorkspaceKind } from '../../../services/devfileApi/devWorkspace';

const cheWorkspaceClient = container.get(CheWorkspaceClient);
const devWorkspaceClient = container.get(DevWorkspaceClient);
Expand Down Expand Up @@ -205,7 +206,13 @@ export const actionCreators: ActionCreators = {
// If the workspace has DEVWORKSPACE_NEXT_START_ANNOTATION then update the devworkspace with the DEVWORKSPACE_NEXT_START_ANNOTATION annotation value and then start the devworkspace
const state = getState();
const plugins = selectDwPluginsList(state);
const storedDevWorkspace = JSON.parse(workspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION]) as devfileApi.DevWorkspace;

const storedDevWorkspace = JSON.parse(workspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION]) as unknown;
if (!isDevWorkspace(storedDevWorkspace)) {
console.error(`The stored devworkspace either has wrong "kind" (not ${devWorkspaceKind}) or lacks some of mandatory fields: `, storedDevWorkspace);
throw new Error('Unexpected error happened. Please check the Console tab of Developer tools.');
}

delete workspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION];
workspace.spec.template = storedDevWorkspace.spec.template;
workspace.spec.started = true;
Expand Down

0 comments on commit 9efeb04

Please sign in to comment.