Skip to content

Commit eb53518

Browse files
authored
Merge pull request #17 from dolittle/feature/k8s-api-mock
Kubernetes Mock API
2 parents ba0fd2c + c16c4bd commit eb53518

30 files changed

+2393
-4
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"**/.DS_Store": true
88
},
99
"editor.codeActionsOnSave": {
10-
"source.fixAll.eslint": true
10+
"source.fixAll.eslint": false
1111
},
1212
"cSpell.words": [
1313
"Deduplication",

Environment/nginx.conf

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ http {
2121
proxy_set_header Tenant-ID 508c1745-5f2a-4b4c-b7a5-2fbb1484346d;
2222
}
2323

24+
location /api/v1 {
25+
proxy_pass http://host.docker.internal:3001;
26+
proxy_set_header Host localhost:9000;
27+
proxy_set_header X-Forwarded-Proto $scheme;
28+
proxy_set_header Tenant-ID 508c1745-5f2a-4b4c-b7a5-2fbb1484346d;
29+
}
30+
2431
location /api/k8s {
2532
proxy_pass http://host.docker.internal:3001;
2633
proxy_set_header Host localhost:9000;

Source/Applications/Backend/applications/ApplicationQueries.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { ILogger } from '@shared/backend/logging';
66
import { injectable } from 'tsyringe';
77
import { Application, ApplicationModel } from './Application';
88

9+
import { NamespaceList } from '@shared/backend/k8s/NamespaceList';
10+
11+
import { Guid } from '@dolittle/rudiments';
12+
import fetch from 'node-fetch';
13+
914
@injectable()
1015
@Resolver(Application)
1116
export class ApplicationQueries {
@@ -14,6 +19,18 @@ export class ApplicationQueries {
1419

1520
@Query(returns => [Application])
1621
async allApplications() {
17-
return ApplicationModel.find();
22+
23+
const response = await fetch('http://localhost:3001/api/v1/namespaces');
24+
const namespaces = await response.json() as NamespaceList;
25+
26+
const applications = namespaces.items.map(_ => {
27+
const guid = _.metadata.name.replace('application-', '');
28+
const application = new Application();
29+
application._id = Guid.parse(guid);
30+
application.name = _.metadata.labels.application || 'unknown';
31+
return application;
32+
});
33+
34+
return applications;
1835
}
1936
}

Source/K8sMock/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import { PartitionedFilterResult } from '@dolittle/sdk.events.filtering';
2424
dolittleRuntimePort: 50055,
2525
graphQLSchema: schema,
2626
defaultDatabaseName: 'k8s',
27+
defaultEventStoreDatabaseName: 'event_store_k8smock',
2728
expressCallback: _ => {
2829
_.use(
29-
'/api/k8s/swagger',
30+
'/api/swagger',
3031
swaggerUi.serve,
3132
swaggerUi.setup(swagger)
3233
);

Source/K8sMock/k8s-swagger.json

+1
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Controller, Get, Route, Tags } from 'tsoa';
5+
import { NamespaceList } from '@shared/backend/k8s/NamespaceList';
6+
7+
const namespaces = require('./namespaces.json') as NamespaceList;
8+
9+
@Route('api/v1/namespaces')
10+
@Tags('core_v1')
11+
export class NamespacesController extends Controller {
12+
13+
@Get()
14+
async getNamespaces(): Promise<NamespaceList> {
15+
return namespaces;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"apiVersion": "v1",
3+
"items": [
4+
{
5+
"metadata": {
6+
"name": "Portal",
7+
"creationTimestamp": "2020-12-07T17:00:28Z",
8+
"labels": {
9+
"app": "Something"
10+
}
11+
},
12+
"spec": {
13+
"containers": [
14+
{
15+
"name": "Portal",
16+
"image": "dolittle/studio/portal:1.0.0",
17+
"args": []
18+
}
19+
]
20+
}
21+
}
22+
],
23+
"status": {
24+
"phase": "Running",
25+
"startTime": "2020-12-07T17:00:28Z",
26+
"containerStatuses": [
27+
{
28+
"containerID": "docker://9656265f3d8207d2ec8ce5962f3d25b34f784a9466c268492f06766f03665881",
29+
"name": "Portal",
30+
"image": "dolittle/studio/portal:1.0.0",
31+
"imageID": "docker-pullable://docker/desktop-vpnkit-controller@sha256:6800d69751e483710a0949fbd01c459934a18ede9d227166def0af44f3a46970",
32+
"ready": true,
33+
"started": true,
34+
"state": {
35+
"running": {
36+
"startedAt": "2020-12-07T17:06:07Z"
37+
}
38+
},
39+
"lastState": {}
40+
}
41+
]
42+
}
43+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"apiVersion": "v1",
3+
"kind": "NamespaceList",
4+
"items": [
5+
{
6+
"metadata": {
7+
"name": "application-fe7736bb-57fc-4166-bb91-6954f4dd4eb7",
8+
"labels": {
9+
"tenant": "Dolittle",
10+
"application": "Studio"
11+
}
12+
}
13+
}
14+
]
15+
}

Source/K8sMock/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"dev": "concurrently \"nodemon -x tsoa spec-and-routes\" \"nodemon index.ts\"",
1010
"debug": "nodemon --inspect -e ts --exec node -r ts-node/register index.ts",
11+
"k8s-api": "docker run --rm -p 80:8080 -e SWAGGER_JSON=/k8s-swagger.json -v $(pwd)/k8s-swagger.json:/k8s-swagger.json swaggerapi/swagger-ui",
1112
"start": "ts-node index.ts",
1213
"build": "tsc -b --clean && tsc -b"
1314
},

Source/K8sMock/pods/PodsController.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { Controller, Get, Route, Tags } from 'tsoa';
5+
import { PodList } from '@shared/backend/k8s/PodList';
6+
7+
const pods = require('./pods.json') as PodList;
8+
9+
@Route('api/v1/namespaces')
10+
@Tags('core_v1')
11+
export class PodsController extends Controller {
12+
13+
@Get('{namespace}/pods')
14+
async getPods(): Promise<PodList> {
15+
return pods;
16+
}
17+
}

0 commit comments

Comments
 (0)