mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to using injectable to setup route for getting service account
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
ef92a98755
commit
c41d10de02
@ -10,8 +10,8 @@ import type httpProxy from "http-proxy";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { readFile } from "fs-extra";
|
import { readFile } from "fs-extra";
|
||||||
import type { Cluster } from "../common/cluster/cluster";
|
import type { Cluster } from "../common/cluster/cluster";
|
||||||
import { apiPrefix, appName, publicPath } from "../common/vars";
|
import { appName, publicPath } from "../common/vars";
|
||||||
import { KubeconfigRoute, VersionRoute } from "./routes";
|
import { VersionRoute } from "./routes";
|
||||||
import logger from "./logger";
|
import logger from "./logger";
|
||||||
|
|
||||||
export interface RouterRequestOpts {
|
export interface RouterRequestOpts {
|
||||||
@ -170,7 +170,6 @@ export class Router {
|
|||||||
|
|
||||||
|
|
||||||
this.router.add({ method: "get", path: "/version" }, VersionRoute.getVersion);
|
this.router.add({ method: "get", path: "/version" }, VersionRoute.getVersion);
|
||||||
this.router.add({ method: "get", path: `${apiPrefix}/kubeconfig/service-account/{namespace}/{account}` }, KubeconfigRoute.routeServiceAccountRoute);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,5 +3,4 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export * from "./kubeconfig-route";
|
|
||||||
export * from "./version-route";
|
export * from "./version-route";
|
||||||
|
|||||||
@ -1,62 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import type { LensApiRequest } from "../router";
|
|
||||||
import { respondJson } from "../utils/http-responses";
|
|
||||||
import type { Cluster } from "../../common/cluster/cluster";
|
|
||||||
import { CoreV1Api, V1Secret } from "@kubernetes/client-node";
|
|
||||||
|
|
||||||
function generateKubeConfig(username: string, secret: V1Secret, cluster: Cluster) {
|
|
||||||
const tokenData = Buffer.from(secret.data["token"], "base64");
|
|
||||||
|
|
||||||
return {
|
|
||||||
"apiVersion": "v1",
|
|
||||||
"kind": "Config",
|
|
||||||
"clusters": [
|
|
||||||
{
|
|
||||||
"name": cluster.contextName,
|
|
||||||
"cluster": {
|
|
||||||
"server": cluster.apiUrl,
|
|
||||||
"certificate-authority-data": secret.data["ca.crt"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"users": [
|
|
||||||
{
|
|
||||||
"name": username,
|
|
||||||
"user": {
|
|
||||||
"token": tokenData.toString("utf8"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"contexts": [
|
|
||||||
{
|
|
||||||
"name": cluster.contextName,
|
|
||||||
"context": {
|
|
||||||
"user": username,
|
|
||||||
"cluster": cluster.contextName,
|
|
||||||
"namespace": secret.metadata.namespace,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"current-context": cluster.contextName,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class KubeconfigRoute {
|
|
||||||
static async routeServiceAccountRoute(request: LensApiRequest) {
|
|
||||||
const { params, response, cluster } = request;
|
|
||||||
const client = (await cluster.getProxyKubeconfig()).makeApiClient(CoreV1Api);
|
|
||||||
const secretList = await client.listNamespacedSecret(params.namespace);
|
|
||||||
const secret = secretList.body.items.find(secret => {
|
|
||||||
const { annotations } = secret.metadata;
|
|
||||||
|
|
||||||
return annotations && annotations["kubernetes.io/service-account.name"] == params.account;
|
|
||||||
});
|
|
||||||
const data = generateKubeConfig(params.account, secret, cluster);
|
|
||||||
|
|
||||||
respondJson(response, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { apiPrefix } from "../../../common/vars";
|
||||||
|
import type { LensApiRequest } from "../../router";
|
||||||
|
import { respondJson } from "../../utils/http-responses";
|
||||||
|
import { routeInjectionToken } from "../../router/router.injectable";
|
||||||
|
import type { Cluster } from "../../../common/cluster/cluster";
|
||||||
|
import { CoreV1Api, V1Secret } from "@kubernetes/client-node";
|
||||||
|
|
||||||
|
|
||||||
|
const getServiceAccountRouteInjectable = getInjectable({
|
||||||
|
id: "get-service-account-route",
|
||||||
|
|
||||||
|
instantiate: () => ({
|
||||||
|
method: "get",
|
||||||
|
path: `${apiPrefix}/kubeconfig/service-account/{namespace}/{account}`,
|
||||||
|
|
||||||
|
handler: async (request: LensApiRequest) => {
|
||||||
|
const { params, response, cluster } = request;
|
||||||
|
const client = (await cluster.getProxyKubeconfig()).makeApiClient(CoreV1Api);
|
||||||
|
const secretList = await client.listNamespacedSecret(params.namespace);
|
||||||
|
|
||||||
|
const secret = secretList.body.items.find(secret => {
|
||||||
|
const { annotations } = secret.metadata;
|
||||||
|
|
||||||
|
return annotations && annotations["kubernetes.io/service-account.name"] == params.account;
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = generateKubeConfig(params.account, secret, cluster);
|
||||||
|
|
||||||
|
respondJson(response, data);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
|
||||||
|
injectionToken: routeInjectionToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default getServiceAccountRouteInjectable;
|
||||||
|
|
||||||
|
function generateKubeConfig(username: string, secret: V1Secret, cluster: Cluster) {
|
||||||
|
const tokenData = Buffer.from(secret.data["token"], "base64");
|
||||||
|
|
||||||
|
return {
|
||||||
|
"apiVersion": "v1",
|
||||||
|
"kind": "Config",
|
||||||
|
"clusters": [
|
||||||
|
{
|
||||||
|
"name": cluster.contextName,
|
||||||
|
"cluster": {
|
||||||
|
"server": cluster.apiUrl,
|
||||||
|
"certificate-authority-data": secret.data["ca.crt"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"users": [
|
||||||
|
{
|
||||||
|
"name": username,
|
||||||
|
"user": {
|
||||||
|
"token": tokenData.toString("utf8"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"contexts": [
|
||||||
|
{
|
||||||
|
"name": cluster.contextName,
|
||||||
|
"context": {
|
||||||
|
"user": username,
|
||||||
|
"cluster": cluster.contextName,
|
||||||
|
"namespace": secret.metadata.namespace,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"current-context": cluster.contextName,
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user