1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Simplify function signature to not solve quirks of caller

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-02-03 10:19:08 +02:00
parent a399e06aa4
commit 7d76753175
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
4 changed files with 18 additions and 27 deletions

View File

@ -3,35 +3,19 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import type { IComputedValue } from "mobx";
import allowedResourcesInjectable from "../cluster-store/allowed-resources.injectable"; import allowedResourcesInjectable from "../cluster-store/allowed-resources.injectable";
import type { KubeResource } from "../rbac"; import type { KubeResource } from "../rbac";
export type IsAllowedResource = (...resources: KubeResource[]) => boolean; export type IsAllowedResource = (resource: KubeResource) => boolean;
interface Dependencies {
allowedResources: IComputedValue<Set<string>>;
}
const isAllowedResource = ({ allowedResources }: Dependencies) => (
(...resource) => {
const resources = resource.flat(2);
if (resources.length === 0) {
// Fix for the fact that JS's Array.every method is not the same as ∀
return true;
}
const allowed = allowedResources.get();
return resources.every(resource => allowed.has(resource));
}
) as IsAllowedResource;
// TODO: This injectable obscures MobX de-referencing. Make it more apparent in usage.
const isAllowedResourceInjectable = getInjectable({ const isAllowedResourceInjectable = getInjectable({
instantiate: (di) => isAllowedResource({ instantiate: (di) => {
allowedResources: di.inject(allowedResourcesInjectable), const allowedResources = di.inject(allowedResourcesInjectable);
}),
return (resource: KubeResource) => allowedResources.get().has(resource);
},
lifecycle: lifecycleEnum.singleton, lifecycle: lifecycleEnum.singleton,
}); });

View File

@ -5,11 +5,14 @@
import type { KubeResource } from "../../common/rbac"; import type { KubeResource } from "../../common/rbac";
import isAllowedResourceInjectable from "../../common/utils/is-allowed-resource.injectable"; import isAllowedResourceInjectable from "../../common/utils/is-allowed-resource.injectable";
import { asLegacyGlobalFunctionForExtensionApi } from "../as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api"; import { asLegacyGlobalFunctionForExtensionApi } from "../as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api";
import { castArray } from "lodash/fp";
export function isAllowedResource(resource: KubeResource | KubeResource[]) { export function isAllowedResource(resource: KubeResource | KubeResource[]) {
const _isAllowedResource = asLegacyGlobalFunctionForExtensionApi(isAllowedResourceInjectable); const _isAllowedResource = asLegacyGlobalFunctionForExtensionApi(isAllowedResourceInjectable);
return _isAllowedResource(...[resource].flat()); const resources = castArray(resource);
return resources.every(x => _isAllowedResource(x));
} }
export { ResourceStack } from "../../common/k8s/resource-stack"; export { ResourceStack } from "../../common/k8s/resource-stack";

View File

@ -9,10 +9,11 @@ import type { KubeObject } from "../../../common/k8s-api/kube-object";
import { workloadURL } from "../../../common/routes"; import { workloadURL } from "../../../common/routes";
import { ResourceNames } from "../../utils/rbac"; import { ResourceNames } from "../../utils/rbac";
import type { NamespaceStore } from "../+namespaces/namespace-store/namespace.store"; import type { NamespaceStore } from "../+namespaces/namespace-store/namespace.store";
import type { IsAllowedResource } from "../../../common/utils/is-allowed-resource.injectable";
interface Dependencies { interface Dependencies {
workloadStores: Map<KubeResource, KubeObjectStore<KubeObject>>; workloadStores: Map<KubeResource, KubeObjectStore<KubeObject>>;
isAllowedResource: (resource: KubeResource) => boolean; isAllowedResource: IsAllowedResource;
namespaceStore: NamespaceStore; namespaceStore: NamespaceStore;
} }

View File

@ -50,6 +50,7 @@ import type { History } from "history";
import type { IsAllowedResource } from "../../../common/utils/is-allowed-resource.injectable"; import type { IsAllowedResource } from "../../../common/utils/is-allowed-resource.injectable";
import isAllowedResourceInjectable from "../../../common/utils/is-allowed-resource.injectable"; import isAllowedResourceInjectable from "../../../common/utils/is-allowed-resource.injectable";
import { HelmRoute } from "../../components/+helm/route"; import { HelmRoute } from "../../components/+helm/route";
import type { KubeResource } from "../../../common/rbac";
interface Dependencies { interface Dependencies {
history: History; history: History;
@ -78,7 +79,9 @@ class NonInjectedClusterFrame extends React.Component<Dependencies> {
} }
@computed get startUrl() { @computed get startUrl() {
return this.props.isAllowedResource("events", "nodes", "pods") const resources : KubeResource[] = ["events", "nodes", "pods"];
return resources.every(x => this.props.isAllowedResource(x))
? routes.clusterURL() ? routes.clusterURL()
: routes.workloadsURL(); : routes.workloadsURL();
} }