-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to devfile metadata via ConfigMap (#912)
* feat: Allow to devfile metadata via ConfigMap Signed-off-by: Anatolii Bazko <[email protected]>
- Loading branch information
Showing
15 changed files
with
300 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...hboard-backend/src/devworkspaceClient/services/__tests__/gettingStartedSamplesApi.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2018-2023 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 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as mockClient from '@kubernetes/client-node'; | ||
import { GettingStartedSamplesApiService } from '../gettingStartedSamplesApi'; | ||
import { api } from '@eclipse-che/common'; | ||
|
||
describe('Getting Started Samples API Service', () => { | ||
const env = process.env; | ||
let gettingStartedSample: GettingStartedSamplesApiService; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { | ||
CHECLUSTER_CR_NAMESPACE: 'eclipse-che', | ||
}; | ||
|
||
const { KubeConfig } = mockClient; | ||
const kubeConfig = new KubeConfig(); | ||
|
||
kubeConfig.makeApiClient = jest.fn().mockImplementation(() => { | ||
return { | ||
listNamespacedConfigMap: () => { | ||
return Promise.resolve({ | ||
body: { items: [{ data: { mySample: JSON.stringify(getGettingStartedSample()) } }] }, | ||
}); | ||
}, | ||
}; | ||
}); | ||
|
||
gettingStartedSample = new GettingStartedSamplesApiService(kubeConfig); | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = env; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('fetching metadata', async () => { | ||
const res = await gettingStartedSample.list(); | ||
expect(res).toEqual([getGettingStartedSample()]); | ||
}); | ||
}); | ||
|
||
function getGettingStartedSample(): api.IGettingStartedSample { | ||
return { | ||
displayName: 'Eclipse Che Dashboard', | ||
description: 'Specifies development environment needed to develop the Eclipse Che Dashboard.', | ||
tags: ['Eclipse Che', 'Dashboard'], | ||
url: 'https://github.com/che-incubator/quarkus-api-example/', | ||
icon: { | ||
base64data: 'base64-encoded-data', | ||
mediatype: 'image/png', | ||
}, | ||
}; | ||
} |
79 changes: 79 additions & 0 deletions
79
packages/dashboard-backend/src/devworkspaceClient/services/gettingStartedSamplesApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2018-2023 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 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { IGettingStartedSampleApi } from '../types'; | ||
import * as k8s from '@kubernetes/client-node'; | ||
import { api } from '@eclipse-che/common'; | ||
import { CoreV1API, prepareCoreV1API } from './helpers/prepareCoreV1API'; | ||
import { createError } from './helpers/createError'; | ||
import { getIcon } from './helpers/getSampleIcon'; | ||
import http from 'http'; | ||
import { V1ConfigMapList } from '@kubernetes/client-node/dist/gen/model/v1ConfigMapList'; | ||
|
||
const API_ERROR_LABEL = 'CORE_V1_API_ERROR'; | ||
const DEVFILE_METADATA_LABEL_SELECTOR = | ||
'app.kubernetes.io/component=getting-started-samples,app.kubernetes.io/part-of=che.eclipse.org'; | ||
|
||
export class GettingStartedSamplesApiService implements IGettingStartedSampleApi { | ||
private readonly coreV1API: CoreV1API; | ||
constructor(kubeConfig: k8s.KubeConfig) { | ||
this.coreV1API = prepareCoreV1API(kubeConfig); | ||
} | ||
|
||
private get env(): { NAMESPACE?: string } { | ||
return { | ||
NAMESPACE: process.env.CHECLUSTER_CR_NAMESPACE, | ||
}; | ||
} | ||
|
||
async list(): Promise<Array<api.IGettingStartedSample>> { | ||
if (!this.env.NAMESPACE) { | ||
console.warn('Mandatory environment variables are not defined: $CHECLUSTER_CR_NAMESPACE'); | ||
return []; | ||
} | ||
|
||
let response: { response: http.IncomingMessage; body: V1ConfigMapList }; | ||
try { | ||
response = await this.coreV1API.listNamespacedConfigMap( | ||
this.env.NAMESPACE, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
DEVFILE_METADATA_LABEL_SELECTOR, | ||
); | ||
} catch (error) { | ||
const additionalMessage = 'Unable to list getting started samples ConfigMap'; | ||
throw createError(error, API_ERROR_LABEL, additionalMessage); | ||
} | ||
|
||
const samples: api.IGettingStartedSample[] = []; | ||
|
||
for (const cm of response.body.items) { | ||
if (cm.data) { | ||
for (const key in cm.data) { | ||
try { | ||
const sample = JSON.parse(cm.data[key]); | ||
Array.isArray(sample) ? samples.push(...sample) : samples.push(sample); | ||
} catch (error) { | ||
console.error(`Failed to parse getting started samples: ${error}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Ensure icon for each sample | ||
samples.forEach(sample => (sample.icon = getIcon(sample))); | ||
|
||
return samples; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/dashboard-backend/src/devworkspaceClient/services/helpers/getSampleIcon.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2018-2023 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 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { api } from '@eclipse-che/common'; | ||
|
||
const DEFAULT_ICON = { | ||
base64data: | ||
'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgaGVpZ2h0PSI1ZW0iIHdpZHRoPSI1ZW0iIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj4KICA8ZyBmaWxsPSIjNmE2ZTczIj4KICA8cGF0aAogICAgICBkPSJNNDg4LjYgMjUwLjJMMzkyIDIxNFYxMDUuNWMwLTE1LTkuMy0yOC40LTIzLjQtMzMuN2wtMTAwLTM3LjVjLTguMS0zLjEtMTcuMS0zLjEtMjUuMyAwbC0xMDAgMzcuNWMtMTQuMSA1LjMtMjMuNCAxOC43LTIzLjQgMzMuN1YyMTRsLTk2LjYgMzYuMkM5LjMgMjU1LjUgMCAyNjguOSAwIDI4My45VjM5NGMwIDEzLjYgNy43IDI2LjEgMTkuOSAzMi4ybDEwMCA1MGMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAzLjktNTIgMTAzLjkgNTJjMTAuMSA1LjEgMjIuMSA1LjEgMzIuMiAwbDEwMC01MGMxMi4yLTYuMSAxOS45LTE4LjYgMTkuOS0zMi4yVjI4My45YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43ek0zNTggMjE0LjhsLTg1IDMxLjl2LTY4LjJsODUtMzd2NzMuM3pNMTU0IDEwNC4xbDEwMi0zOC4yIDEwMiAzOC4ydi42bC0xMDIgNDEuNC0xMDItNDEuNHYtLjZ6bTg0IDI5MS4xbC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnptMjQwIDExMmwtODUgNDIuNXYtNzkuMWw4NS0zOC44djc1LjR6bTAtMTEybC0xMDIgNDEuNC0xMDItNDEuNHYtLjZsMTAyLTM4LjIgMTAyIDM4LjJ2LjZ6Ij48L3BhdGg+CiAgPC9nPgo8L3N2Zz4K', | ||
mediatype: 'image/svg+xml', | ||
}; | ||
|
||
export function getIcon(sample: api.IGettingStartedSample) { | ||
if (!sample?.icon) { | ||
return DEFAULT_ICON; | ||
} | ||
return sample.icon; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/dashboard-backend/src/routes/api/gettingStartedSample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2018-2023 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 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { FastifyInstance } from 'fastify'; | ||
import { baseApiPath } from '../../constants/config'; | ||
import { getSchema } from '../../services/helpers'; | ||
import { getDevWorkspaceClient } from './helpers/getDevWorkspaceClient'; | ||
import { getServiceAccountToken } from './helpers/getServiceAccountToken'; | ||
|
||
const tags = ['Getting Started Samples']; | ||
|
||
export function registerGettingStartedSamplesRoutes(instance: FastifyInstance) { | ||
instance.register(async server => { | ||
server.get(`${baseApiPath}/getting-started-sample`, getSchema({ tags }), async () => { | ||
const token = getServiceAccountToken(); | ||
const { gettingStartedSampleApi } = getDevWorkspaceClient(token); | ||
return gettingStartedSampleApi.list(); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.