diff --git a/plugins/kubernetes-cluster/src/components/KubernetesClusterContent/KubernetesClusterContent.tsx b/plugins/kubernetes-cluster/src/components/KubernetesClusterContent/KubernetesClusterContent.tsx index d005a6a2aa..d47e28d294 100644 --- a/plugins/kubernetes-cluster/src/components/KubernetesClusterContent/KubernetesClusterContent.tsx +++ b/plugins/kubernetes-cluster/src/components/KubernetesClusterContent/KubernetesClusterContent.tsx @@ -24,8 +24,8 @@ import { useKubernetesClusterError, } from '../KubernetesClusterErrorContext/KubernetesClusterErrorContext'; import { WarningPanel } from '@backstage/core-components'; -import { useKubernetesClustersPermission } from '../../hooks/useKubernetesClustersPermission'; import { kubernetesClustersPermission } from '@backstage/plugin-kubernetes-common'; +import { RequirePermission } from '@backstage/plugin-permission-react'; const ContentGrid = () => { const { error } = useKubernetesClusterError(); @@ -59,17 +59,20 @@ const ContentGrid = () => { * @public */ export const KubernetesClusterContent = () => { - const hasKubernetesClustersPermission = useKubernetesClustersPermission(); - - return !hasKubernetesClustersPermission ? ( - - ) : ( - - - + return ( + + } + > + + + + ); }; diff --git a/plugins/kubernetes-cluster/src/hooks/useKubernetesClustersPermission.ts b/plugins/kubernetes-cluster/src/hooks/useKubernetesClustersPermission.ts deleted file mode 100644 index 4b71d381f6..0000000000 --- a/plugins/kubernetes-cluster/src/hooks/useKubernetesClustersPermission.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2025 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { kubernetesClustersPermission } from '@backstage/plugin-kubernetes-common'; -import { usePermission } from '@backstage/plugin-permission-react'; - -export const useKubernetesClustersPermission = () => { - const kubernetesClustersPermissionResult = usePermission({ - permission: kubernetesClustersPermission, - }); - - return kubernetesClustersPermissionResult.allowed; -}; diff --git a/plugins/kubernetes/src/KubernetesContent.tsx b/plugins/kubernetes/src/KubernetesContent.tsx index f9bde67f62..633d6b2a5a 100644 --- a/plugins/kubernetes/src/KubernetesContent.tsx +++ b/plugins/kubernetes/src/KubernetesContent.tsx @@ -28,18 +28,14 @@ import { import { DetectedError, detectErrors, - kubernetesClustersPermission, - kubernetesResourcesPermission, } from '@backstage/plugin-kubernetes-common'; import { Content, EmptyState, Page, Progress, - WarningPanel, } from '@backstage/core-components'; -import { useKubernetesClustersPermission } from './hooks/useKubernetesClustersPermission'; -import { useKubernetesResourcesPermission } from './hooks/useKubernetesResourcesPermission'; +import { RequireKubernetesPermissions } from './RequireKubernetesPermissions'; type KubernetesContentProps = { entity: Entity; @@ -51,27 +47,11 @@ export const KubernetesContent = ({ entity, refreshIntervalMs, }: KubernetesContentProps) => { - const hasKubernetesClustersPermission = useKubernetesClustersPermission(); - const hasKubernetesResourcesPermission = useKubernetesResourcesPermission(); const { kubernetesObjects, error } = useKubernetesObjects( entity, refreshIntervalMs, ); - if (!hasKubernetesClustersPermission || !hasKubernetesResourcesPermission) { - return ( - - - - - - ); - } - const clusters = kubernetesObjects?.items.map(item => item.cluster) ?? []; const clustersWithErrors = @@ -83,89 +63,93 @@ export const KubernetesContent = ({ : new Map(); return ( - - - - {kubernetesObjects === undefined && error === undefined && ( - - )} + + + + + {kubernetesObjects === undefined && error === undefined && ( + + )} - {/* errors retrieved from the kubernetes clusters */} - {clustersWithErrors.length > 0 && ( - - - + {/* errors retrieved from the kubernetes clusters */} + {clustersWithErrors.length > 0 && ( + + + + - - )} + )} - {/* other errors */} - {error !== undefined && ( - - - + {/* other errors */} + {error !== undefined && ( + + + + - - )} + )} - {kubernetesObjects && ( - - - - - - Your Clusters - - - {kubernetesObjects?.items.length <= 0 && ( - - - - - - )} - {kubernetesObjects?.items.length > 0 && - kubernetesObjects?.items.map((item, i) => { - const podsWithErrors = new Set( - detectedErrors - .get(item.cluster.name) - ?.filter(de => de.sourceRef.kind === 'Pod') - .map(de => de.sourceRef.name), - ); - - return ( - - + + + + + Your Clusters + + + {kubernetesObjects?.items.length <= 0 && ( + + + - ); - })} + + )} + {kubernetesObjects?.items.length > 0 && + kubernetesObjects?.items.map((item, i) => { + const podsWithErrors = new Set( + detectedErrors + .get(item.cluster.name) + ?.filter(de => de.sourceRef.kind === 'Pod') + .map(de => de.sourceRef.name), + ); + + return ( + + + + ); + })} + - - )} - - - + )} + + + + ); }; diff --git a/plugins/kubernetes/src/RequireKubernetesPermissions.tsx b/plugins/kubernetes/src/RequireKubernetesPermissions.tsx new file mode 100644 index 0000000000..36f802b127 --- /dev/null +++ b/plugins/kubernetes/src/RequireKubernetesPermissions.tsx @@ -0,0 +1,63 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import React, { ReactNode } from 'react'; +import { + kubernetesClustersPermission, + kubernetesResourcesPermission, +} from '@backstage/plugin-kubernetes-common'; +import { usePermission } from '@backstage/plugin-permission-react'; +import { Content, Page, WarningPanel } from '@backstage/core-components'; + +export type RequireKubernetesPermissionProps = { + children: ReactNode; +}; + +export function RequireKubernetesPermissions( + props: RequireKubernetesPermissionProps, +): JSX.Element | null { + const kubernetesClustersPermissionResult = usePermission({ + permission: kubernetesClustersPermission, + }); + const kubernetesResourcesPermissionResult = usePermission({ + permission: kubernetesResourcesPermission, + }); + + if ( + kubernetesClustersPermissionResult.loading || + kubernetesResourcesPermissionResult.loading + ) { + return null; + } + + if ( + kubernetesClustersPermissionResult.allowed && + kubernetesResourcesPermissionResult.allowed + ) { + return <>{props.children}; + } + + return ( + + + + + + ); +} diff --git a/plugins/kubernetes/src/hooks/useKubernetesClustersPermission.ts b/plugins/kubernetes/src/hooks/useKubernetesClustersPermission.ts deleted file mode 100644 index 4b71d381f6..0000000000 --- a/plugins/kubernetes/src/hooks/useKubernetesClustersPermission.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2025 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { kubernetesClustersPermission } from '@backstage/plugin-kubernetes-common'; -import { usePermission } from '@backstage/plugin-permission-react'; - -export const useKubernetesClustersPermission = () => { - const kubernetesClustersPermissionResult = usePermission({ - permission: kubernetesClustersPermission, - }); - - return kubernetesClustersPermissionResult.allowed; -}; diff --git a/plugins/kubernetes/src/hooks/useKubernetesResourcesPermission.ts b/plugins/kubernetes/src/hooks/useKubernetesResourcesPermission.ts deleted file mode 100644 index d5d730f39b..0000000000 --- a/plugins/kubernetes/src/hooks/useKubernetesResourcesPermission.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2025 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { kubernetesResourcesPermission } from '@backstage/plugin-kubernetes-common'; -import { usePermission } from '@backstage/plugin-permission-react'; - -export const useKubernetesResourcesPermission = () => { - const kubernetesResourcesPermissionResult = usePermission({ - permission: kubernetesResourcesPermission, - }); - - return kubernetesResourcesPermissionResult.allowed; -};