Skip to content

Commit

Permalink
Merge pull request #17 from dolittle/feature/k8s-api-mock
Browse files Browse the repository at this point in the history
Kubernetes Mock API
  • Loading branch information
pavsaund authored Dec 8, 2020
2 parents 19a2bd6 + 6b6a2bb commit 88f0645
Show file tree
Hide file tree
Showing 30 changed files with 2,393 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"**/.DS_Store": true
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": false
},
"cSpell.words": [
"Deduplication",
Expand Down
7 changes: 7 additions & 0 deletions Environment/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ http {
proxy_set_header Tenant-ID 508c1745-5f2a-4b4c-b7a5-2fbb1484346d;
}

location /api/v1 {
proxy_pass http://host.docker.internal:3001;
proxy_set_header Host localhost:9000;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Tenant-ID 508c1745-5f2a-4b4c-b7a5-2fbb1484346d;
}

location /api/k8s {
proxy_pass http://host.docker.internal:3001;
proxy_set_header Host localhost:9000;
Expand Down
19 changes: 18 additions & 1 deletion Source/Applications/Backend/applications/ApplicationQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { ILogger } from '@shared/backend/logging';
import { injectable } from 'tsyringe';
import { Application, ApplicationModel } from './Application';

import { NamespaceList } from '@shared/backend/k8s/NamespaceList';

import { Guid } from '@dolittle/rudiments';
import fetch from 'node-fetch';

@injectable()
@Resolver(Application)
export class ApplicationQueries {
Expand All @@ -14,6 +19,18 @@ export class ApplicationQueries {

@Query(returns => [Application])
async allApplications() {
return ApplicationModel.find();

const response = await fetch('http://localhost:3001/api/v1/namespaces');
const namespaces = await response.json() as NamespaceList;

const applications = namespaces.items.map(_ => {
const guid = _.metadata.name.replace('application-', '');
const application = new Application();
application._id = Guid.parse(guid);
application.name = _.metadata.labels.application || 'unknown';
return application;
});

return applications;
}
}
3 changes: 2 additions & 1 deletion Source/K8sMock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import { PartitionedFilterResult } from '@dolittle/sdk.events.filtering';
dolittleRuntimePort: 50055,
graphQLSchema: schema,
defaultDatabaseName: 'k8s',
defaultEventStoreDatabaseName: 'event_store_k8smock',
expressCallback: _ => {
_.use(
'/api/k8s/swagger',
'/api/swagger',
swaggerUi.serve,
swaggerUi.setup(swagger)
);
Expand Down
1 change: 1 addition & 0 deletions Source/K8sMock/k8s-swagger.json

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Source/K8sMock/namespaces/NamespacesController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import { Controller, Get, Route, Tags } from 'tsoa';
import { NamespaceList } from '@shared/backend/k8s/NamespaceList';

const namespaces = require('./namespaces.json') as NamespaceList;

@Route('api/v1/namespaces')
@Tags('core_v1')
export class NamespacesController extends Controller {

@Get()
async getNamespaces(): Promise<NamespaceList> {
return namespaces;
}
}
43 changes: 43 additions & 0 deletions Source/K8sMock/namespaces/namespaces-full-sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"apiVersion": "v1",
"items": [
{
"metadata": {
"name": "Portal",
"creationTimestamp": "2020-12-07T17:00:28Z",
"labels": {
"app": "Something"
}
},
"spec": {
"containers": [
{
"name": "Portal",
"image": "dolittle/studio/portal:1.0.0",
"args": []
}
]
}
}
],
"status": {
"phase": "Running",
"startTime": "2020-12-07T17:00:28Z",
"containerStatuses": [
{
"containerID": "docker://9656265f3d8207d2ec8ce5962f3d25b34f784a9466c268492f06766f03665881",
"name": "Portal",
"image": "dolittle/studio/portal:1.0.0",
"imageID": "docker-pullable://docker/desktop-vpnkit-controller@sha256:6800d69751e483710a0949fbd01c459934a18ede9d227166def0af44f3a46970",
"ready": true,
"started": true,
"state": {
"running": {
"startedAt": "2020-12-07T17:06:07Z"
}
},
"lastState": {}
}
]
}
}
15 changes: 15 additions & 0 deletions Source/K8sMock/namespaces/namespaces.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"apiVersion": "v1",
"kind": "NamespaceList",
"items": [
{
"metadata": {
"name": "application-fe7736bb-57fc-4166-bb91-6954f4dd4eb7",
"labels": {
"tenant": "Dolittle",
"application": "Studio"
}
}
}
]
}
1 change: 1 addition & 0 deletions Source/K8sMock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"dev": "concurrently \"nodemon -x tsoa spec-and-routes\" \"nodemon index.ts\"",
"debug": "nodemon --inspect -e ts --exec node -r ts-node/register index.ts",
"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",
"start": "ts-node index.ts",
"build": "tsc -b --clean && tsc -b"
},
Expand Down
17 changes: 17 additions & 0 deletions Source/K8sMock/pods/PodsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import { Controller, Get, Route, Tags } from 'tsoa';
import { PodList } from '@shared/backend/k8s/PodList';

const pods = require('./pods.json') as PodList;

@Route('api/v1/namespaces')
@Tags('core_v1')
export class PodsController extends Controller {

@Get('{namespace}/pods')
async getPods(): Promise<PodList> {
return pods;
}
}
Loading

0 comments on commit 88f0645

Please sign in to comment.