mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Move Cluster.apiUrl to seperate injectable (#7354)
* Refactor out Cluster.apiUrl to direct reading - Should help prevent using stale data - Removes some uses of synchronous FS operations Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Rename helper function to better communicate intent Signed-off-by: Sebastian Malton <sebastian@malton.name> * Improve prometheus handler tests to override less things Signed-off-by: Sebastian Malton <sebastian@malton.name> --------- Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
18d660ea77
commit
fe2fd4c1fd
53 files changed
+472
-640
No files matched your search
+40
-57
@@ -5,12 +5,11 @@
|
||||
|
||||
import { apiPrefix } from "../../../common/vars";
|
||||
import { getRouteInjectable } from "../../router/router.injectable";
|
||||
import type { Cluster } from "../../../common/cluster/cluster";
|
||||
import type { V1Secret } from "@kubernetes/client-node";
|
||||
import { CoreV1Api } from "@kubernetes/client-node";
|
||||
import { clusterRoute } from "../../router/route";
|
||||
import { dump } from "js-yaml";
|
||||
import * as yaml from "js-yaml";
|
||||
import loadProxyKubeconfigInjectable from "../../cluster/load-proxy-kubeconfig.injectable";
|
||||
import clusterApiUrlInjectable from "../../../features/cluster/connections/main/api-url.injectable";
|
||||
|
||||
const getServiceAccountRouteInjectable = getRouteInjectable({
|
||||
id: "get-service-account-route",
|
||||
@@ -25,73 +24,57 @@ const getServiceAccountRouteInjectable = getRouteInjectable({
|
||||
const secretList = await client.listNamespacedSecret(params.namespace);
|
||||
|
||||
const secret = secretList.body.items.find(secret => {
|
||||
const { annotations } = secret.metadata ?? {};
|
||||
const { annotations = {}} = secret.metadata ?? {};
|
||||
|
||||
return annotations?.["kubernetes.io/service-account.name"] === params.account;
|
||||
return annotations["kubernetes.io/service-account.name"] === params.account;
|
||||
});
|
||||
|
||||
if (!secret) {
|
||||
if (!secret || !secret.data || !secret.metadata) {
|
||||
return {
|
||||
error: "No secret found",
|
||||
statusCode: 404,
|
||||
};
|
||||
}
|
||||
|
||||
const kubeconfig = generateKubeConfig(params.account, secret, cluster);
|
||||
|
||||
if (!kubeconfig) {
|
||||
return {
|
||||
error: "No secret found",
|
||||
statusCode: 404,
|
||||
};
|
||||
}
|
||||
const { token, "ca.crt": caCrt } = secret.data;
|
||||
const apiUrl = (await di.inject(clusterApiUrlInjectable, cluster)()).toString();
|
||||
const contextName = cluster.contextName.get();
|
||||
|
||||
return {
|
||||
response: kubeconfig,
|
||||
response: yaml.dump({
|
||||
apiVersion: "v1",
|
||||
kind: "Config",
|
||||
clusters: [
|
||||
{
|
||||
name: contextName,
|
||||
cluster: {
|
||||
server: apiUrl,
|
||||
"certificate-authority-data": caCrt,
|
||||
},
|
||||
},
|
||||
],
|
||||
users: [
|
||||
{
|
||||
name: params.account,
|
||||
user: {
|
||||
token: Buffer.from(token, "base64").toString("utf8"),
|
||||
},
|
||||
},
|
||||
],
|
||||
contexts: [
|
||||
{
|
||||
name: `${contextName}-${params.account}`,
|
||||
context: {
|
||||
user: params.account,
|
||||
cluster: contextName,
|
||||
namespace: secret.metadata.namespace,
|
||||
},
|
||||
},
|
||||
],
|
||||
"current-context": contextName,
|
||||
}),
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
export default getServiceAccountRouteInjectable;
|
||||
|
||||
function generateKubeConfig(username: string, secret: V1Secret, cluster: Cluster): string | undefined {
|
||||
if (!secret.data || !secret.metadata) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { token, "ca.crt": caCrt } = secret.data;
|
||||
const tokenData = Buffer.from(token, "base64");
|
||||
|
||||
return dump({
|
||||
"apiVersion": "v1",
|
||||
"kind": "Config",
|
||||
"clusters": [
|
||||
{
|
||||
"name": cluster.contextName.get(),
|
||||
"cluster": {
|
||||
"server": cluster.apiUrl.get(),
|
||||
"certificate-authority-data": caCrt,
|
||||
},
|
||||
},
|
||||
],
|
||||
"users": [
|
||||
{
|
||||
"name": username,
|
||||
"user": {
|
||||
"token": tokenData.toString("utf8"),
|
||||
},
|
||||
},
|
||||
],
|
||||
"contexts": [
|
||||
{
|
||||
"name": [cluster.contextName.get(), username].join("-"),
|
||||
"context": {
|
||||
"user": username,
|
||||
"cluster": cluster.contextName.get(),
|
||||
"namespace": secret.metadata.namespace,
|
||||
},
|
||||
},
|
||||
],
|
||||
"current-context": cluster.contextName.get(),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user