Skip to content

Commit 44376bd

Browse files
authored
chore(annotations): add support for kubernetes ns annotation (#71)
* chore(annotations): add helpers and kubernetes namespace Signed-off-by: Jonas Beck <dev@jonasbeck.dk> * chore(content): make use of new annotation helpers Signed-off-by: Jonas Beck <dev@jonasbeck.dk> * chore(changeset): add changeset Signed-off-by: Jonas Beck <dev@jonasbeck.dk> * test(annotations): remove invalid test case Signed-off-by: Jonas Beck <dev@jonasbeck.dk> --------- Signed-off-by: Jonas Beck <dev@jonasbeck.dk>
1 parent ee217ed commit 44376bd

File tree

6 files changed

+398
-35
lines changed

6 files changed

+398
-35
lines changed

.changeset/little-otters-bathe.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@kyverno/backstage-plugin-policy-reporter-common': minor
3+
'@kyverno/backstage-plugin-policy-reporter': minor
4+
---
5+
6+
Add support for the `backstage.io/kubernetes-namespace` annotation as an alternative for the `kyverno.io/namespace`. This is useful for anyone already using the Kubernetes plugin. The kyverno namespaced annotation will always be the first priority.
7+
8+
Add `isPolicyReporterAvailable` helper function to validate if PolicyReporter is configured for an entity.

plugins/policy-reporter-common/src/annotations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export const KYVERNO_KIND_ANNOTATION = 'kyverno.io/kind';
77
// The namespace of the resource to display policies
88
export const KYVERNO_NAMESPACE_ANNOTATION = 'kyverno.io/namespace';
99

10+
// The namespace of the resource to display policies
11+
export const KUBERNETES_NAMESPACE_ANNOTATION =
12+
'backstage.io/kubernetes-namespace';
13+
1014
// The endpoint for the Policy Reporter API
1115
// EXAMPLE: https://kyverno.io/policy-reporter/api/
1216
export const KYVERNO_ENDPOINT_ANNOTATION = 'kyverno.io/endpoint';

plugins/policy-reporter/src/components/EntityCustomPoliciesContent/EntityCustomPoliciesContent.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ import { Grid } from '@material-ui/core';
1212
import { PolicyReportsTable } from '../PolicyReportsTable';
1313
import { SelectEnvironment } from '../SelectEnvironment';
1414
import {
15-
containsRequiredAnnotations,
16-
annotationsRequired,
15+
getKinds,
16+
getNamespaces,
17+
getResourceName,
18+
isPolicyReporterAvailable,
1719
} from '../../utils/annotations';
1820
import { MissingEnvironmentsEmptyState } from '../MissingEnvironmentsEmptyState';
1921
import { useEntityEnvironment } from '../../hooks/useEntityEnvironment';
20-
import {
21-
KYVERNO_KIND_ANNOTATION,
22-
KYVERNO_NAMESPACE_ANNOTATION,
23-
KYVERNO_RESOURCE_NAME_ANNOTATION,
24-
} from '@kyverno/backstage-plugin-policy-reporter-common';
22+
import { KYVERNO_RESOURCE_NAME_ANNOTATION } from '@kyverno/backstage-plugin-policy-reporter-common';
2523

2624
type EntityCustomPoliciesContentProps = {
2725
annotationsDocumentationUrl?: string;
@@ -49,8 +47,7 @@ export const EntityCustomPoliciesContent = ({
4947
const { entity } = useEntity();
5048
const annotations = entity.metadata.annotations;
5149

52-
// Boolean variable to validate that entity have required annotations
53-
const annotationsState: boolean = containsRequiredAnnotations(annotations);
50+
const annotationsState = isPolicyReporterAvailable(entity);
5451

5552
const {
5653
environments,
@@ -65,7 +62,7 @@ export const EntityCustomPoliciesContent = ({
6562
<PageContent>
6663
<MissingAnnotationEmptyState
6764
readMoreUrl={annotationsDocumentationUrl}
68-
annotation={annotationsRequired}
65+
annotation={KYVERNO_RESOURCE_NAME_ANNOTATION}
6966
/>
7067
</PageContent>
7168
);
@@ -81,9 +78,9 @@ export const EntityCustomPoliciesContent = ({
8178
</PageContent>
8279
);
8380

84-
const namespaces = annotations![KYVERNO_NAMESPACE_ANNOTATION].split(',');
85-
const kinds = annotations![KYVERNO_KIND_ANNOTATION].split(',');
86-
const resourceName = annotations![KYVERNO_RESOURCE_NAME_ANNOTATION];
81+
const namespaces = getNamespaces(annotations);
82+
const kinds = getKinds(annotations);
83+
const resourceName = getResourceName(annotations);
8784

8885
return (
8986
<PageContent>

plugins/policy-reporter/src/components/EntityKyvernoPoliciesContent/EntityKyvernoPoliciesContent.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ import { Grid } from '@material-ui/core';
1212
import { PolicyReportsTable } from '../PolicyReportsTable';
1313
import { SelectEnvironment } from '../SelectEnvironment';
1414
import {
15-
containsRequiredAnnotations,
16-
annotationsRequired,
15+
getKinds,
16+
getNamespaces,
17+
getResourceName,
18+
isPolicyReporterAvailable,
1719
} from '../../utils/annotations';
1820
import { MissingEnvironmentsEmptyState } from '../MissingEnvironmentsEmptyState';
1921
import { useEntityEnvironment } from '../../hooks/useEntityEnvironment';
20-
import {
21-
KYVERNO_KIND_ANNOTATION,
22-
KYVERNO_NAMESPACE_ANNOTATION,
23-
KYVERNO_RESOURCE_NAME_ANNOTATION,
24-
} from '@kyverno/backstage-plugin-policy-reporter-common';
22+
import { KYVERNO_RESOURCE_NAME_ANNOTATION } from '@kyverno/backstage-plugin-policy-reporter-common';
2523

2624
type KyvernoPoliciesContentProps = {
2725
annotationsDocumentationUrl?: string;
@@ -46,7 +44,7 @@ export const EntityKyvernoPoliciesContent = ({
4644
const annotations = entity.metadata.annotations;
4745

4846
// Boolean variable to validate that entity have required annotations
49-
const annotationsState: boolean = containsRequiredAnnotations(annotations);
47+
const annotationsState = isPolicyReporterAvailable(entity);
5048

5149
const {
5250
environments,
@@ -61,7 +59,7 @@ export const EntityKyvernoPoliciesContent = ({
6159
<PageContent>
6260
<MissingAnnotationEmptyState
6361
readMoreUrl={annotationsDocumentationUrl}
64-
annotation={annotationsRequired}
62+
annotation={KYVERNO_RESOURCE_NAME_ANNOTATION}
6563
/>
6664
</PageContent>
6765
);
@@ -77,9 +75,9 @@ export const EntityKyvernoPoliciesContent = ({
7775
</PageContent>
7876
);
7977

80-
const namespaces = annotations![KYVERNO_NAMESPACE_ANNOTATION].split(',');
81-
const kinds = annotations![KYVERNO_KIND_ANNOTATION].split(',');
82-
const resourceName = annotations![KYVERNO_RESOURCE_NAME_ANNOTATION];
78+
const namespaces = getNamespaces(annotations);
79+
const kinds = getKinds(annotations);
80+
const resourceName = getResourceName(annotations);
8381

8482
return (
8583
<PageContent>

0 commit comments

Comments
 (0)