1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/cluster-detectors/cluster-id-detector.injectable.ts
Sebastian Malton 8e65a0acd6
Improve the injectability of cluster metadata detection (#6910)
* Improve the injectability of cluster metadata detection

- Remove unnecessary and complex base class

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove dead code

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove dead code

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-01-19 16:34:31 +02:00

44 lines
1.3 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { clusterMetadataDetectorInjectionToken } from "./token";
import { createHash } from "crypto";
import { ClusterMetadataKey } from "../../common/cluster-types";
import { getInjectable } from "@ogre-tools/injectable";
import k8SRequestInjectable from "../k8s-request.injectable";
import type { Cluster } from "../../common/cluster/cluster";
const clusterIdDetectorFactoryInjectable = getInjectable({
id: "cluster-id-detector-factory",
instantiate: (di) => {
const k8sRequest = di.inject(k8SRequestInjectable);
const getDefaultNamespaceId = async (cluster: Cluster) => {
const { metadata } = await k8sRequest(cluster, "/api/v1/namespaces/default") as { metadata: { uid: string }};
return metadata.uid;
};
return {
key: ClusterMetadataKey.CLUSTER_ID,
detect: async (cluster) => {
let id: string;
try {
id = await getDefaultNamespaceId(cluster);
} catch(_) {
id = cluster.apiUrl;
}
const value = createHash("sha256").update(id).digest("hex");
return { value, accuracy: 100 };
},
};
},
injectionToken: clusterMetadataDetectorInjectionToken,
});
export default clusterIdDetectorFactoryInjectable;