From 5f58196529ab0e14b212893f3934b553e28dbc6a Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 28 Nov 2022 11:13:25 -0500 Subject: [PATCH] Fix core resources not showing up Signed-off-by: Sebastian Malton --- src/common/cluster/cluster.ts | 24 +++-------------- ...t-namespace-list-permissions.injectable.ts | 13 ++++++---- .../config-maps-route.injectable.ts | 5 ++-- ...zontal-pod-autoscalers-route.injectable.ts | 4 +-- .../config/leases/leases-route.injectable.ts | 1 + .../limit-ranges-route.injectable.ts | 1 + .../resource-quotas-route.injectable.ts | 1 + .../secrets/secrets-route.injectable.ts | 1 + .../cluster/events/events-route.injectable.ts | 1 + .../namespaces/namespaces-route.injectable.ts | 1 + .../endpoints/endpoints-route.injectable.ts | 1 + .../services/services-route.injectable.ts | 1 + .../cluster/nodes/nodes-route.injectable.ts | 1 + .../cluster-overview-route.injectable.ts | 1 + ...rsistent-volume-claims-route.injectable.ts | 1 + .../persistent-volumes-route.injectable.ts | 1 + .../service-accounts-route.injectable.ts | 1 + .../workloads/pods/pods-route.injectable.ts | 1 + src/common/rbac.ts | 26 +++++++++++++------ .../edit-namespace-from-new-tab.test.tsx | 1 + ...espace-from-previously-opened-tab.test.tsx | 1 + .../visibility-of-sidebar-items.test.tsx | 2 ++ .../cluster/workload-overview.test.tsx | 1 + .../pods-workload.injectable.ts | 1 + src/renderer/initializers/workload-events.tsx | 5 ++-- 25 files changed, 58 insertions(+), 39 deletions(-) diff --git a/src/common/cluster/cluster.ts b/src/common/cluster/cluster.ts index cdc442782b..303ee89361 100644 --- a/src/common/cluster/cluster.ts +++ b/src/common/cluster/cluster.ts @@ -175,15 +175,6 @@ export class Cluster implements ClusterModel { private readonly knownResources = observable.array(); - private readonly knownNamespacedResources = computed(() => ( - this.knownResources - .filter(r => r.namespaced === true) - )); - private readonly knownClusterscopedResources = computed(() => ( - this.knownResources - .filter(r => r.namespaced === false) - )); - // The formatting of this is `group.name` or `name` (if in core) private readonly allowedResources = observable.set(); @@ -677,21 +668,14 @@ export class Cluster implements ClusterModel { } try { - const canListClusterScopedResource = await requestNamespaceListPermissions(""); const apiLimit = plimit(5); // 5 concurrent api requests - const canListNamespacedResourceCheckers = await Promise.all(( + const canListResourceCheckers = await Promise.all(( this.allowedNamespaces.map(namespace => apiLimit(() => requestNamespaceListPermissions(namespace))) )); - const canListNamespacedResource: CanListResource = (resource) => canListNamespacedResourceCheckers.some(fn => fn(resource)); + const canListNamespacedResource: CanListResource = (resource) => canListResourceCheckers.some(fn => fn(resource)); - const allowedClusterScopedResources = this.knownClusterscopedResources - .get() - .filter(canListClusterScopedResource); - const allowedNamespaceScopedResources = this.knownNamespacedResources - .get() - .filter(canListNamespacedResource); - - return [...allowedClusterScopedResources, ...allowedNamespaceScopedResources] + return this.knownResources + .filter(canListNamespacedResource) .map(formatKubeApiResource); } catch (error) { return []; diff --git a/src/common/cluster/request-namespace-list-permissions.injectable.ts b/src/common/cluster/request-namespace-list-permissions.injectable.ts index 845152cda3..62d2477e42 100644 --- a/src/common/cluster/request-namespace-list-permissions.injectable.ts +++ b/src/common/cluster/request-namespace-list-permissions.injectable.ts @@ -47,11 +47,14 @@ const requestNamespaceListPermissionsForInjectable = getInjectable({ const { resourceRules } = status; return (resource) => { - const resourceRule = resourceRules.find(rule => { - console.log(rule); - void resource; + const resourceRule = resourceRules.find(({ + apiGroups = [], + resources = [], + }) => { + const isAboutRelevantApiGroup = apiGroups.includes("*") || apiGroups.includes(resource.group); + const isAboutResource = resources.includes("*") || resources.includes(resource.apiName); - return true; + return isAboutRelevantApiGroup && isAboutResource; }); if (!resourceRule) { @@ -63,7 +66,7 @@ const requestNamespaceListPermissionsForInjectable = getInjectable({ return verbs.includes("*") || verbs.includes("list"); }; } catch (error) { - logger.error(`[AUTHORIZATION-NAMESPACE-REVIEW]: failed to create subject rules review: ${error}`, { namespace }); + logger.error(`[AUTHORIZATION-NAMESPACE-REVIEW]: failed to create subject rules review`, { namespace, error }); return () => true; } diff --git a/src/common/front-end-routing/routes/cluster/config/config-maps/config-maps-route.injectable.ts b/src/common/front-end-routing/routes/cluster/config/config-maps/config-maps-route.injectable.ts index 743c827cd6..6ea03fff08 100644 --- a/src/common/front-end-routing/routes/cluster/config/config-maps/config-maps-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/config/config-maps/config-maps-route.injectable.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import shouldShowResourceInjectable from "../../../../../../renderer/cluster-frame-context/should-show-resource.injectable"; +import { shouldShowResourceInjectionToken } from "../../../../../cluster-store/allowed-resources-injection-token"; import { frontEndRouteInjectionToken } from "../../../../front-end-route-injection-token"; const configMapsRouteInjectable = getInjectable({ @@ -11,8 +11,9 @@ const configMapsRouteInjectable = getInjectable({ instantiate: (di) => ({ path: "/configmaps", clusterFrame: true, - isEnabled: di.inject(shouldShowResourceInjectable, { + isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "configmaps", + group: "v1", }), }), injectionToken: frontEndRouteInjectionToken, diff --git a/src/common/front-end-routing/routes/cluster/config/horizontal-pod-autoscalers/horizontal-pod-autoscalers-route.injectable.ts b/src/common/front-end-routing/routes/cluster/config/horizontal-pod-autoscalers/horizontal-pod-autoscalers-route.injectable.ts index 95384dc2c4..00002620ee 100644 --- a/src/common/front-end-routing/routes/cluster/config/horizontal-pod-autoscalers/horizontal-pod-autoscalers-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/config/horizontal-pod-autoscalers/horizontal-pod-autoscalers-route.injectable.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import shouldShowResourceInjectable from "../../../../../../renderer/cluster-frame-context/should-show-resource.injectable"; +import { shouldShowResourceInjectionToken } from "../../../../../cluster-store/allowed-resources-injection-token"; import { frontEndRouteInjectionToken } from "../../../../front-end-route-injection-token"; const horizontalPodAutoscalersRouteInjectable = getInjectable({ @@ -12,7 +12,7 @@ const horizontalPodAutoscalersRouteInjectable = getInjectable({ instantiate: (di) => ({ path: "/hpa", clusterFrame: true, - isEnabled: di.inject(shouldShowResourceInjectable, { + isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "horizontalpodautoscalers", group: "autoscaling", }), diff --git a/src/common/front-end-routing/routes/cluster/config/leases/leases-route.injectable.ts b/src/common/front-end-routing/routes/cluster/config/leases/leases-route.injectable.ts index 4dd1a03692..ea4eb2ae59 100644 --- a/src/common/front-end-routing/routes/cluster/config/leases/leases-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/config/leases/leases-route.injectable.ts @@ -14,6 +14,7 @@ const leasesRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "leases", + group: "coordination.k8s.io", }), }), diff --git a/src/common/front-end-routing/routes/cluster/config/limit-ranges/limit-ranges-route.injectable.ts b/src/common/front-end-routing/routes/cluster/config/limit-ranges/limit-ranges-route.injectable.ts index 8d537d8048..8623f3520e 100644 --- a/src/common/front-end-routing/routes/cluster/config/limit-ranges/limit-ranges-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/config/limit-ranges/limit-ranges-route.injectable.ts @@ -14,6 +14,7 @@ const limitRangesRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "limitranges", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/config/resource-quotas/resource-quotas-route.injectable.ts b/src/common/front-end-routing/routes/cluster/config/resource-quotas/resource-quotas-route.injectable.ts index 3c2b400f9a..209f77e19a 100644 --- a/src/common/front-end-routing/routes/cluster/config/resource-quotas/resource-quotas-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/config/resource-quotas/resource-quotas-route.injectable.ts @@ -14,6 +14,7 @@ const resourceQuotasRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "resourcequotas", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/config/secrets/secrets-route.injectable.ts b/src/common/front-end-routing/routes/cluster/config/secrets/secrets-route.injectable.ts index 74cd6dfd5f..079ddcbf83 100644 --- a/src/common/front-end-routing/routes/cluster/config/secrets/secrets-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/config/secrets/secrets-route.injectable.ts @@ -14,6 +14,7 @@ const secretsRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "secrets", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/events/events-route.injectable.ts b/src/common/front-end-routing/routes/cluster/events/events-route.injectable.ts index d548ad9a7e..b3df358ad8 100644 --- a/src/common/front-end-routing/routes/cluster/events/events-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/events/events-route.injectable.ts @@ -14,6 +14,7 @@ const eventsRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "events", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/namespaces/namespaces-route.injectable.ts b/src/common/front-end-routing/routes/cluster/namespaces/namespaces-route.injectable.ts index 68243c0bfb..2aa6c23efe 100644 --- a/src/common/front-end-routing/routes/cluster/namespaces/namespaces-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/namespaces/namespaces-route.injectable.ts @@ -14,6 +14,7 @@ const namespacesRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "namespaces", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/network/endpoints/endpoints-route.injectable.ts b/src/common/front-end-routing/routes/cluster/network/endpoints/endpoints-route.injectable.ts index bdabc44bbf..c88ec04714 100644 --- a/src/common/front-end-routing/routes/cluster/network/endpoints/endpoints-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/network/endpoints/endpoints-route.injectable.ts @@ -14,6 +14,7 @@ const endpointsRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "endpoints", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/network/services/services-route.injectable.ts b/src/common/front-end-routing/routes/cluster/network/services/services-route.injectable.ts index fda8830076..53300ee241 100644 --- a/src/common/front-end-routing/routes/cluster/network/services/services-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/network/services/services-route.injectable.ts @@ -14,6 +14,7 @@ const servicesRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "services", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/nodes/nodes-route.injectable.ts b/src/common/front-end-routing/routes/cluster/nodes/nodes-route.injectable.ts index 96990236f5..81323843d5 100644 --- a/src/common/front-end-routing/routes/cluster/nodes/nodes-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/nodes/nodes-route.injectable.ts @@ -14,6 +14,7 @@ const nodesRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "nodes", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/overview/cluster-overview-route.injectable.ts b/src/common/front-end-routing/routes/cluster/overview/cluster-overview-route.injectable.ts index 54af37d666..8315fd7773 100644 --- a/src/common/front-end-routing/routes/cluster/overview/cluster-overview-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/overview/cluster-overview-route.injectable.ts @@ -14,6 +14,7 @@ const clusterOverviewRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "nodes", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/storage/persistent-volume-claims/persistent-volume-claims-route.injectable.ts b/src/common/front-end-routing/routes/cluster/storage/persistent-volume-claims/persistent-volume-claims-route.injectable.ts index fc063947cd..1b96933136 100644 --- a/src/common/front-end-routing/routes/cluster/storage/persistent-volume-claims/persistent-volume-claims-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/storage/persistent-volume-claims/persistent-volume-claims-route.injectable.ts @@ -14,6 +14,7 @@ const persistentVolumeClaimsRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "persistentvolumeclaims", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/storage/persistent-volumes/persistent-volumes-route.injectable.ts b/src/common/front-end-routing/routes/cluster/storage/persistent-volumes/persistent-volumes-route.injectable.ts index 46aeebd9d0..52f95b32c6 100644 --- a/src/common/front-end-routing/routes/cluster/storage/persistent-volumes/persistent-volumes-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/storage/persistent-volumes/persistent-volumes-route.injectable.ts @@ -14,6 +14,7 @@ const persistentVolumesRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "persistentvolumes", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/user-management/service-accounts/service-accounts-route.injectable.ts b/src/common/front-end-routing/routes/cluster/user-management/service-accounts/service-accounts-route.injectable.ts index 8b3c3ff5d3..3bf6c1ec00 100644 --- a/src/common/front-end-routing/routes/cluster/user-management/service-accounts/service-accounts-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/user-management/service-accounts/service-accounts-route.injectable.ts @@ -14,6 +14,7 @@ const serviceAccountsRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "serviceaccounts", + group: "v1", }), }), diff --git a/src/common/front-end-routing/routes/cluster/workloads/pods/pods-route.injectable.ts b/src/common/front-end-routing/routes/cluster/workloads/pods/pods-route.injectable.ts index e524bb6739..577f1c1a91 100644 --- a/src/common/front-end-routing/routes/cluster/workloads/pods/pods-route.injectable.ts +++ b/src/common/front-end-routing/routes/cluster/workloads/pods/pods-route.injectable.ts @@ -14,6 +14,7 @@ const podsRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "pods", + group: "v1", }), }), diff --git a/src/common/rbac.ts b/src/common/rbac.ts index 5e385b51bd..99e564a377 100644 --- a/src/common/rbac.ts +++ b/src/common/rbac.ts @@ -13,25 +13,21 @@ export type KubeResource = export interface KubeApiResource { kind: string; - group?: string; + group: string; apiName: string; namespaced: boolean; } export interface KubeApiResourceDescriptor { apiName: string; - group?: string; + group: string; } -export const formatKubeApiResource = (res: KubeApiResourceDescriptor) => ( - res.group - ? `${res.group}/${res.apiName}` - : res.apiName -); +export const formatKubeApiResource = (res: KubeApiResourceDescriptor) => `${res.group}/${res.apiName}`; export interface KubeApiResourceData { kind: string; // resource type (e.g. "Namespace") - group?: string; // api-group, if empty then "core" + group: string; // api-group, if empty then "core" namespaced: boolean; } @@ -48,6 +44,7 @@ export const apiResourceRecord: Record = { }, configmaps: { kind: "ConfigMap", + group: "v1", namespaced: true, }, cronjobs: { @@ -72,10 +69,12 @@ export const apiResourceRecord: Record = { }, endpoints: { kind: "Endpoint", + group: "v1", namespaced: true, }, events: { kind: "Event", + group: "v1", namespaced: true, }, horizontalpodautoscalers: { @@ -95,14 +94,17 @@ export const apiResourceRecord: Record = { }, namespaces: { kind: "Namespace", + group: "v1", namespaced: false, }, limitranges: { kind: "LimitRange", + group: "v1", namespaced: true, }, leases: { kind: "Lease", + group: "v1", namespaced: true, }, networkpolicies: { @@ -112,18 +114,22 @@ export const apiResourceRecord: Record = { }, nodes: { kind: "Node", + group: "v1", namespaced: false, }, persistentvolumes: { kind: "PersistentVolume", + group: "v1", namespaced: false, }, persistentvolumeclaims: { kind: "PersistentVolumeClaim", + group: "v1", namespaced: true, }, pods: { kind: "Pod", + group: "v1", namespaced: true, }, poddisruptionbudgets: { @@ -148,6 +154,7 @@ export const apiResourceRecord: Record = { }, resourcequotas: { kind: "ResourceQuota", + group: "v1", namespaced: true, }, replicasets: { @@ -167,14 +174,17 @@ export const apiResourceRecord: Record = { }, secrets: { kind: "Secret", + group: "v1", namespaced: true, }, serviceaccounts: { kind: "ServiceAccount", + group: "v1", namespaced: true, }, services: { kind: "Service", + group: "v1", namespaced: true, }, statefulsets: { diff --git a/src/features/cluster/namespaces/edit-namespace-from-new-tab.test.tsx b/src/features/cluster/namespaces/edit-namespace-from-new-tab.test.tsx index 18ad33e3d5..f37ada1736 100644 --- a/src/features/cluster/namespaces/edit-namespace-from-new-tab.test.tsx +++ b/src/features/cluster/namespaces/edit-namespace-from-new-tab.test.tsx @@ -100,6 +100,7 @@ describe("cluster/namespaces - edit namespace from new tab", () => { builder.allowKubeResource({ apiName: "namespaces", + group: "v1", }); }); diff --git a/src/features/cluster/namespaces/edit-namespace-from-previously-opened-tab.test.tsx b/src/features/cluster/namespaces/edit-namespace-from-previously-opened-tab.test.tsx index 444d9ddf21..4cf63ee353 100644 --- a/src/features/cluster/namespaces/edit-namespace-from-previously-opened-tab.test.tsx +++ b/src/features/cluster/namespaces/edit-namespace-from-previously-opened-tab.test.tsx @@ -44,6 +44,7 @@ describe("cluster/namespaces - edit namespaces from previously opened tab", () = builder.allowKubeResource({ apiName: "namespaces", + group: "v1", }); }); diff --git a/src/features/cluster/visibility-of-sidebar-items.test.tsx b/src/features/cluster/visibility-of-sidebar-items.test.tsx index 09d206f7f7..2eb42b7fdf 100644 --- a/src/features/cluster/visibility-of-sidebar-items.test.tsx +++ b/src/features/cluster/visibility-of-sidebar-items.test.tsx @@ -52,6 +52,7 @@ describe("cluster - visibility of sidebar items", () => { beforeEach(() => { builder.allowKubeResource({ apiName: "namespaces", + group: "v1", }); }); @@ -76,6 +77,7 @@ const testRouteInjectable = getInjectable({ clusterFrame: true, isEnabled: di.inject(shouldShowResourceInjectionToken, { apiName: "namespaces", + group: "v1", }), }), diff --git a/src/features/cluster/workload-overview.test.tsx b/src/features/cluster/workload-overview.test.tsx index 66d07fd2e1..205c837b47 100644 --- a/src/features/cluster/workload-overview.test.tsx +++ b/src/features/cluster/workload-overview.test.tsx @@ -15,6 +15,7 @@ describe("workload overview", () => { applicationBuilder = getApplicationBuilder().setEnvironmentToClusterFrame(); applicationBuilder.allowKubeResource({ apiName: "pods", + group: "v1", }); rendered = await applicationBuilder.render(); }); diff --git a/src/renderer/components/+workloads-overview/workloads/implementations/pods-workload.injectable.ts b/src/renderer/components/+workloads-overview/workloads/implementations/pods-workload.injectable.ts index 644fccaf08..452f7903aa 100644 --- a/src/renderer/components/+workloads-overview/workloads/implementations/pods-workload.injectable.ts +++ b/src/renderer/components/+workloads-overview/workloads/implementations/pods-workload.injectable.ts @@ -21,6 +21,7 @@ const podsWorkloadInjectable = getInjectable({ return { resource: { apiName: "pods", + group: "v1", }, open: navigate, diff --git a/src/renderer/initializers/workload-events.tsx b/src/renderer/initializers/workload-events.tsx index 426bb875c1..735b4e2c74 100644 --- a/src/renderer/initializers/workload-events.tsx +++ b/src/renderer/initializers/workload-events.tsx @@ -7,7 +7,7 @@ import { withInjectables } from "@ogre-tools/injectable-react"; import type { IComputedValue } from "mobx"; import { observer } from "mobx-react"; import React from "react"; -import shouldShowResourceInjectable from "../cluster-frame-context/should-show-resource.injectable"; +import { shouldShowResourceInjectionToken } from "../../common/cluster-store/allowed-resources-injection-token"; import { Events } from "../components/+events/events"; export interface WorkloadEventsProps {} @@ -32,8 +32,9 @@ const NonInjectedWorkloadEvents = observer(({ workloadEventsAreAllowed }: Depend export const WorkloadEvents = withInjectables(NonInjectedWorkloadEvents, { getProps: (di, props) => ({ - workloadEventsAreAllowed: di.inject(shouldShowResourceInjectable, { + workloadEventsAreAllowed: di.inject(shouldShowResourceInjectionToken, { apiName: "events", + group: "v1", }), ...props, }),