Skip to content

Commit

Permalink
fix: getWorkspaceConditions method
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <[email protected]>
  • Loading branch information
olexii4 committed Sep 23, 2024
1 parent 14c0dd3 commit 83c505c
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 139 deletions.
16 changes: 4 additions & 12 deletions devfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ components:
endpoints:
- exposure: public
name: local-server
protocol: http
protocol: https
targetPort: 8080
path: /
- exposure: public
name: bundle-analyzer
path: /
protocol: http
protocol: https
targetPort: 8888
env:
- name: KUBEDOCK_ENABLED
Expand All @@ -31,7 +31,7 @@ commands:
label: "[UD] install dependencies"
component: tools
workingDir: ${PROJECT_SOURCE}
commandLine: "yarn install --non-interactive"
commandLine: "yarn install"
group:
kind: build
isDefault: true
Expand All @@ -40,7 +40,7 @@ commands:
label: "[UD] build"
component: tools
workingDir: ${PROJECT_SOURCE}
commandLine: "yarn install --non-interactive && yarn build"
commandLine: "yarn install && yarn build"
group:
kind: build
- id: watchfrontend
Expand Down Expand Up @@ -115,11 +115,3 @@ commands:
commandLine: "yarn --cwd packages/dashboard-backend build --env bundleAnalyzer=true"
group:
kind: build
- id: install-requirements
exec:
component: tools
commandLine: "npm i [email protected] -g"
label: Install [email protected]
events:
postStart:
- "install-requirements"
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Red Hat, Inc. - initial API and implementation
*/

import { getStartWorkspaceConditions } from '@/components/WorkspaceProgress/utils';
import { getWorkspaceConditions } from '@/components/WorkspaceProgress/utils';
import { DevWorkspaceBuilder } from '@/store/__mocks__/devWorkspaceBuilder';

describe('WorkspaceProgress utils', () => {
Expand All @@ -20,13 +20,13 @@ describe('WorkspaceProgress utils', () => {
jest.clearAllMocks();
});

describe('getStartWorkspaceCondition', () => {
describe('getWorkspaceCondition', () => {
it('should return an empty array as a default value', () => {
const devWorkspace = new DevWorkspaceBuilder().build();

expect(devWorkspace.status?.conditions).toBeUndefined();

const conditions = getStartWorkspaceConditions(devWorkspace);
const conditions = getWorkspaceConditions(devWorkspace);

expect(conditions).toEqual([]);
});
Expand All @@ -43,19 +43,15 @@ describe('WorkspaceProgress utils', () => {
};
const devWorkspace = new DevWorkspaceBuilder().withStatus(status).build();

const conditions = getStartWorkspaceConditions(devWorkspace);
const conditions = getWorkspaceConditions(devWorkspace);

expect(conditions).toEqual(status.conditions);
});
});
it('should filter conditions that are not related to the workspace start', () => {
it('should return default value if one of conditions has special type that is shown when workspace start failed', () => {
const status = {
phase: 'Starting',
conditions: [
{
message: 'DevWorkspace is starting',
status: 'True',
type: 'Started',
},
{
message: 'Resolved plugins and parents from DevWorkspace',
status: 'True',
Expand All @@ -81,39 +77,19 @@ describe('WorkspaceProgress utils', () => {
status: 'False',
type: 'DeploymentReady',
},
{
message:
'Error creating DevWorkspace deployment: Container tools has state ImagePullBackOff',
reason: 'InfrastructureFailure',
status: 'True',
type: 'FailedStart',
},
],
};
const devWorkspace = new DevWorkspaceBuilder().withStatus(status).build();

const conditions = getStartWorkspaceConditions(devWorkspace);
const conditions = getWorkspaceConditions(devWorkspace);

expect(conditions).not.toEqual(status.conditions);
expect(conditions).toEqual([
{
message: 'DevWorkspace is starting',
status: 'True',
type: 'Started',
},
{
message: 'Resolved plugins and parents from DevWorkspace',
status: 'True',
type: 'DevWorkspaceResolved',
},
{
message: 'Storage ready',
status: 'True',
type: 'StorageReady',
},
{
message: 'Networking ready',
status: 'True',
type: 'RoutingReady',
},
{
message: 'DevWorkspace serviceaccount ready',
status: 'True',
type: 'ServiceAccountReady',
},
]);
expect(conditions).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import StartingStepStartWorkspace from '@/components/WorkspaceProgress/StartingS
import StartingStepWorkspaceConditions from '@/components/WorkspaceProgress/StartingSteps/WorkspaceConditions';
import {
ConditionType,
getStartWorkspaceConditions,
getWorkspaceConditions,
isWorkspaceStatusCondition,
} from '@/components/WorkspaceProgress/utils';
import WorkspaceProgressWizard, {
Expand Down Expand Up @@ -188,7 +188,7 @@ class Progress extends React.Component<Props, State> {
workspace.status === DevWorkspaceStatus.FAILING ||
workspace.status === DevWorkspaceStatus.FAILED)
) {
const conditions = getStartWorkspaceConditions(workspace.ref);
const conditions = getWorkspaceConditions(workspace.ref);

const lastScore = this.scoreConditions(this.state.conditions);
const score = this.scoreConditions(conditions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { V1alpha2DevWorkspaceStatusConditions } from '@devfile/api';

import devfileApi from '@/services/devfileApi';
import { DevWorkspaceStatus } from '@/services/helpers/types';

export type ConditionType = V1alpha2DevWorkspaceStatusConditions & {
status: 'True' | 'False' | 'Unknown';
Expand All @@ -39,18 +40,23 @@ export function isConditionReady(
);
}

export function getStartWorkspaceConditions(
export function getWorkspaceConditions(
workspace: devfileApi.DevWorkspace,
): V1alpha2DevWorkspaceStatusConditions[] {
if (!workspace.status?.conditions || workspace.status.conditions.length === 0) {
return [];
}

const conditions = [...workspace.status.conditions];
// remove all conditions that are not related to the workspace start
for (let i = conditions.length; i > 0; i--) {
if (conditions[i - 1].type === 'ServiceAccountReady') {
conditions.length = i;
break;

// if the target workspace has status starting
if (workspace.status.phase === DevWorkspaceStatus.STARTING) {
// if one of conditions has special type that is shown when workspace start failed
const failedStart = conditions.find(condition => condition.type === 'FailedStart');
// this is a special condition that is shown when workspace start failed
if (failedStart !== undefined) {
// it means that all the conditions are from the previous start
return [];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ class WorkspaceActionsProvider extends React.Component<Props, State> {
break;
}
case WorkspaceAction.START_DEBUG_AND_OPEN_LOGS: {
// todo: open a new tab with the DEBUG_WORKSPACE_START parameter instead
await this.props.startWorkspace(workspace, {
'debug-workspace-start': true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,75 +450,6 @@ describe('DevWorkspace store, actions', () => {
expect(actions).toStrictEqual(expectedActions);
});

it('should remove all workspace conditions in the store before start a DevWorkspace', async () => {
(checkRunningWorkspacesLimit as jest.Mock).mockImplementation(() => undefined);
(isRunningDevWorkspacesClusterLimitExceeded as jest.Mock).mockReturnValue(
Promise.resolve(true),
);

// set a condition with an error message to the devWorkspace
devWorkspace.status = {
devworkspaceId: '1234',
conditions: [
{
type: 'Stopped',
status: 'False',
reason: 'LimitReached',
message: 'Workspace stopped due to error.',
},
],
};

const store = storeBuilder.withDevWorkspaces({ workspaces: [devWorkspace] }).build();

await store.dispatch(testStore.actionCreators.startWorkspace(devWorkspace));

const actions = store.getActions();

const expectedDevWorkspaceWithEmptyConditions = Object.assign({}, devWorkspace, {
status: {
devworkspaceId: '1234',
conditions: [],
},
});

const expectedActions: Array<
| testStore.KnownAction
| testDevWorkspaceClusterStore.KnownAction
| ServerConfigStore.KnownAction
> = [
{
type: testStore.Type.UPDATE_DEVWORKSPACE,
workspace: expectedDevWorkspaceWithEmptyConditions,
},
{
type: testDevWorkspaceClusterStore.Type.REQUEST_DEVWORKSPACES_CLUSTER,
check: AUTHORIZED,
},
{
type: testDevWorkspaceClusterStore.Type.RECEIVED_DEVWORKSPACES_CLUSTER,
isRunningDevWorkspacesClusterLimitExceeded: true,
},
{
type: testStore.Type.REQUEST_DEVWORKSPACE,
check: AUTHORIZED,
},
{
type: 'REQUEST_DW_SERVER_CONFIG',
},
{
config: {} as api.IServerConfig,
type: 'RECEIVE_DW_SERVER_CONFIG',
},
{
type: testStore.Type.UPDATE_DEVWORKSPACE,
workspace: devWorkspace,
},
];

expect(actions).toStrictEqual(expectedActions);
});

it('should create REQUEST_DEVWORKSPACE and RECEIVE_DEVWORKSPACE_ERROR when failed to start a DevWorkspace', async () => {
(checkRunningWorkspacesLimit as jest.Mock).mockImplementation(() => {
throw new Error('Limit reached.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,6 @@ export const actionCreators: ActionCreators = {
console.warn(`Workspace ${_workspace.metadata.name} already started`);
return;
}
if (workspace.status?.conditions && workspace.status?.conditions?.length > 0) {
workspace.status.conditions = [];
dispatch({
type: Type.UPDATE_DEVWORKSPACE,
workspace,
});
}
try {
await OAuthService.refreshTokenIfProjectExists(workspace);
} catch (e: unknown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class DevWorkspaceBuilder {

withStatus(status: {
conditions?: Array<V1alpha2DevWorkspaceStatusConditions>;
phase?: keyof typeof DevWorkspaceStatus;
phase?: keyof typeof DevWorkspaceStatus | string;
devworkspaceId?: string;
mainUrl?: string;
message?: string;
Expand All @@ -115,7 +115,11 @@ export class DevWorkspaceBuilder {
this.workspace.status.conditions = Object.assign([], status.conditions);
}
if (status.phase) {
this.workspace.status.phase = DevWorkspaceStatus[status.phase];
if (DevWorkspaceStatus[status.phase]) {
this.workspace.status.phase = DevWorkspaceStatus[status.phase];
} else {
this.workspace.status.phase = status.phase;
}
}
if (status.devworkspaceId) {
this.workspace.status.devworkspaceId = status.devworkspaceId;
Expand Down
2 changes: 1 addition & 1 deletion run/local-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if [ ! -d $DASHBOARD_FRONTEND/lib/public/dashboard/devfile-registry ]; then
-i "packages/devfile-registry/air-gap/index.json"

if [ -s "$DASHBOARD_FRONTEND/lib/public/dashboard/devfile-registry/air-gap/index.json" ]; then
sed -i 's|CHE_DASHBOARD_INTERNAL_URL|http://localhost:8080|g' "$DASHBOARD_FRONTEND/lib/public/dashboard/devfile-registry/air-gap/index.json"
scripts/sed_in_place 's|CHE_DASHBOARD_INTERNAL_URL|http://localhost:8080|g' "$DASHBOARD_FRONTEND/lib/public/dashboard/devfile-registry/air-gap/index.json"
fi
fi

Expand Down
8 changes: 8 additions & 0 deletions scripts/container_tool.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#!/bin/bash
#
# Copyright (c) 2021-2024 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#

# Function to check if a command is available
command_exists() {
Expand Down
23 changes: 23 additions & 0 deletions scripts/sed_in_place.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#
# Copyright (c) 2021-2024 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#

set -e

SHORT_UNAME=$(uname -s)
if [ "$(uname)" == "Darwin" ]; then
sed -i '' "$@"
elif [ "${SHORT_UNAME:0:5}" == "Linux" ]; then
sed -i "$@"
else
echo "Unknown OS"
exit 1
fi

exit 0

0 comments on commit 83c505c

Please sign in to comment.