Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Define route, endpoint and service for kubernetes #289

Merged
merged 15 commits into from
Apr 29, 2021
Merged
2 changes: 1 addition & 1 deletion packages/teleport/src/Kubes/Kubes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import React from 'react';
import { Box, Indicator } from 'design';
import { Danger } from 'design/Alert';
import { Attempt } from 'shared/hooks/useAttemptNext';
import KubeList from 'teleport/components/KubeList';
import KubeList from 'teleport/Kubes/KubeList';
import { Kube } from 'teleport/services/kube';
import {
FeatureBox,
Expand Down
10 changes: 10 additions & 0 deletions packages/teleport/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const cfg = {
loginErrorCallback: '/web/msg/error/login/callback',
userInvite: '/web/invite/:tokenId',
userReset: '/web/reset/:tokenId',
kubernetes: '/web/cluster/:clusterId/kubernetes',
// whitelist sso handlers
oidcHandler: '/v1/webapi/oidc/*',
samlHandler: '/v1/webapi/saml/*',
Expand Down Expand Up @@ -100,6 +101,7 @@ const cfg = {
ttyWsAddr:
'wss://:fqdm/v1/webapi/sites/:clusterId/connect?access_token=:token&params=:params',
terminalSessionPath: '/v1/webapi/sites/:clusterId/sessions/:sid?',
kubernetesPath: '/v1/webapi/sites/:clusterId/kubernetes',

usersPath: '/v1/webapi/users',
usersDelete: '/v1/webapi/users/:username',
Expand Down Expand Up @@ -236,6 +238,10 @@ const cfg = {
return cfg.baseUrl + generatePath(route, { tokenId });
},

getKubernetesRoute(clusterId: string) {
return generatePath(cfg.routes.kubernetes, { clusterId });
},

getUsersUrl() {
return cfg.api.usersPath;
},
Expand Down Expand Up @@ -282,6 +288,10 @@ const cfg = {
return generatePath(cfg.api.rolesPath, { name });
},

getKubernetesUrl(clusterId: string) {
return generatePath(cfg.api.kubernetesPath, { clusterId });
},

init(backendConfig = {}) {
merge(this, backendConfig);
},
Expand Down
3 changes: 3 additions & 0 deletions packages/teleport/src/services/kube/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import KubeService from './kube';

export * from './types';
export default KubeService;
60 changes: 60 additions & 0 deletions packages/teleport/src/services/kube/kube.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2021 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import KubeService from './kube';
import api from 'teleport/services/api';

kimlisa marked this conversation as resolved.
Show resolved Hide resolved
test('correct processed fetch response formatting', async () => {
jest.spyOn(api, 'get').mockResolvedValue(mockApiResponse);

const kubeService = new KubeService();
const response = await kubeService.fetchKubernetes('clusterId');

expect(response).toEqual([
{
name: 'tele.logicoma.dev-prod',
tags: ['kernal: 4.15.0-51-generic', 'env: prod'],
},
]);
});

test('handling of null fetch response', async () => {
jest.spyOn(api, 'get').mockResolvedValue(null);

const kubeService = new KubeService();
const response = await kubeService.fetchKubernetes('clusterId');

expect(response).toEqual([]);
});

test('handling of null labels', async () => {
jest.spyOn(api, 'get').mockResolvedValue([{ name: 'test', labels: null }]);

const kubeService = new KubeService();
const response = await kubeService.fetchKubernetes('clusterId');

expect(response).toEqual([{ name: 'test', tags: [] }]);
});

const mockApiResponse = [
{
name: 'tele.logicoma.dev-prod',
labels: [
{ name: 'kernal', value: '4.15.0-51-generic' },
{ name: 'env', value: 'prod' },
],
},
];
30 changes: 30 additions & 0 deletions packages/teleport/src/services/kube/kube.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2021 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { map } from 'lodash';
import api from 'teleport/services/api';
import cfg from 'teleport/config';
import makeKube from './makeKube';

class KubeService {
fetchKubernetes(clusterId) {
return api
.get(cfg.getKubernetesUrl(clusterId))
.then(json => map(json, makeKube));
}
}

export default KubeService;
27 changes: 27 additions & 0 deletions packages/teleport/src/services/kube/makeKube.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2021 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Kube } from './types';

export default function makeKube(json): Kube {
const { name } = json;
const labels = json.labels || [];

return {
name,
tags: labels.map(label => `${label.name}: ${label.value}`),
};
}
2 changes: 2 additions & 0 deletions packages/teleport/src/teleportContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import sshService from './services/ssh';
import ResourceService from './services/resources';
import userService from './services/user';
import appService from './services/apps';
import KubeService from './services/kube';

class TeleportContext implements types.Context {
// stores
Expand All @@ -41,6 +42,7 @@ class TeleportContext implements types.Context {
resourceService = new ResourceService();
userService = userService;
appService = appService;
kubeService = new KubeService();

isEnterprise = cfg.isEnterprise;

Expand Down