Skip to content

Commit 6632648

Browse files
committed
UI selectors
1 parent 29272b1 commit 6632648

File tree

2 files changed

+102
-40
lines changed

2 files changed

+102
-40
lines changed

frontend/src/components/adminnetworkpolicy/Details.tsx

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
*/
1616

1717
import Box from '@mui/material/Box';
18+
import Tooltip from '@mui/material/Tooltip';
1819
import Typography from '@mui/material/Typography';
1920
import { useTranslation } from 'react-i18next';
2021
import { useParams } from 'react-router';
21-
import { matchExpressionSimplifier, matchLabelsSimplifier } from '../../lib/k8s';
22+
import { matchLabelsSimplifier } from '../../lib/k8s';
2223
import AdminNetworkPolicy, {
2324
AdminNetworkPolicyEgressRule,
2425
AdminNetworkPolicyIngressRule,
2526
AdminNetworkPolicyPort,
2627
} from '../../lib/k8s/adminnetworkpolicy';
27-
import { LabelSelector } from '../../lib/k8s/cluster';
2828
import NameValueTable from '../common/NameValueTable';
2929
import { DetailsGrid, MetadataDictGrid } from '../common/Resource';
3030
import { metadataStyles } from '../common/Resource';
3131
import SectionBox from '../common/SectionBox';
32+
import { KubeIcon } from '../resourceMap/kubeIcon/KubeIcon';
3233

3334
export default function AdminNetworkPolicyDetails(props: {
3435
name?: string;
@@ -39,33 +40,6 @@ export default function AdminNetworkPolicyDetails(props: {
3940
const { name = params.name, subject = params.subject, cluster } = props;
4041
const { t } = useTranslation(['glossary', 'translation']);
4142

42-
function prepareMatchLabelsAndExpressions(
43-
type: 'Pods' | 'Namespaces',
44-
matchLabels: LabelSelector['matchLabels'],
45-
matchExpressions: LabelSelector['matchExpressions']
46-
) {
47-
const matchLabelsSimplified = matchLabelsSimplifier(matchLabels) || [];
48-
const matchExpressionsSimplified = matchExpressionSimplifier(matchExpressions) || [];
49-
if (matchLabels === undefined && matchExpressions === undefined) {
50-
return <>{type}: All</>;
51-
}
52-
53-
return (
54-
<>
55-
{matchLabelsSimplified.map(label => (
56-
<Typography sx={metadataStyles} display="inline">
57-
{label}
58-
</Typography>
59-
))}
60-
{matchExpressionsSimplified.map(expression => (
61-
<Typography sx={metadataStyles} display="inline">
62-
{expression}
63-
</Typography>
64-
))}
65-
</>
66-
);
67-
}
68-
6943
function SubjectSelector(props: { AdminNetworkPolicy: AdminNetworkPolicy }) {
7044
const { AdminNetworkPolicy } = props;
7145
// this is an exception which should never happen, but we handle it gracefully
@@ -77,18 +51,79 @@ export default function AdminNetworkPolicyDetails(props: {
7751
}
7852

7953
if (AdminNetworkPolicy.jsonData?.spec?.subject?.pods !== undefined) {
80-
return prepareMatchLabelsAndExpressions(
81-
'Pods',
82-
AdminNetworkPolicy.jsonData?.spec?.subject?.pods?.podSelector?.matchLabels,
83-
AdminNetworkPolicy.jsonData?.spec?.subject?.pods?.podSelector?.matchExpressions
54+
return (
55+
<>
56+
<KubeIcon kind="Namespace" width="30px" height="30px" />
57+
<Tooltip title="namespaceSelector">
58+
{(() => {
59+
const labels = matchLabelsSimplifier(
60+
AdminNetworkPolicy.jsonData?.spec?.subject?.pods?.namespaceSelector?.matchLabels
61+
);
62+
return Array.isArray(labels) ? (
63+
<>
64+
{labels.map((label: string) => (
65+
<Typography sx={metadataStyles} display="inline" key={label}>
66+
{label}
67+
</Typography>
68+
))}
69+
</>
70+
) : (
71+
<Typography sx={metadataStyles} display="inline">
72+
All Namespaces
73+
</Typography>
74+
);
75+
})()}
76+
</Tooltip>
77+
<KubeIcon kind="Pod" width="30px" height="30px" />
78+
<Tooltip title="podSelector">
79+
{(() => {
80+
const labels = matchLabelsSimplifier(
81+
AdminNetworkPolicy.jsonData?.spec?.subject?.pods?.podSelector?.matchLabels
82+
);
83+
return Array.isArray(labels) ? (
84+
<>
85+
{labels.map((label: string) => (
86+
<Typography sx={metadataStyles} display="inline" key={label}>
87+
{label}
88+
</Typography>
89+
))}
90+
</>
91+
) : (
92+
<Typography sx={metadataStyles} display="inline">
93+
All Pods
94+
</Typography>
95+
);
96+
})()}
97+
</Tooltip>
98+
</>
8499
);
85100
}
86101

87102
if (AdminNetworkPolicy.jsonData?.spec?.subject?.namespaces !== undefined) {
88-
return prepareMatchLabelsAndExpressions(
89-
'Namespaces',
90-
AdminNetworkPolicy.jsonData?.spec?.subject?.namespaces?.podSelector?.matchLabels,
91-
AdminNetworkPolicy.jsonData?.spec?.subject?.namespaces?.podSelector?.matchExpressions
103+
return (
104+
<>
105+
<Typography sx={metadataStyles} display="inline">
106+
{(() => {
107+
const labels = matchLabelsSimplifier(
108+
AdminNetworkPolicy.jsonData?.spec?.subject?.namespaces?.namespaceSelector
109+
?.matchLabels
110+
);
111+
return Array.isArray(labels) ? (
112+
<>
113+
{labels.map((label: string) => (
114+
<Typography sx={metadataStyles} display="inline" key={label}>
115+
{label}
116+
</Typography>
117+
))}
118+
</>
119+
) : (
120+
<Typography sx={metadataStyles} display="inline">
121+
All Pods
122+
</Typography>
123+
);
124+
})()}
125+
</Typography>
126+
</>
92127
);
93128
}
94129
}

frontend/src/components/adminnetworkpolicy/List.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { Tooltip, Typography } from '@mui/material';
1718
import { useTranslation } from 'react-i18next';
19+
import { matchLabelsSimplifier } from '../../lib/k8s';
1820
import AdminNetworkPolicy from '../../lib/k8s/adminnetworkpolicy';
21+
import { metadataStyles } from '../common/Resource';
1922
import ResourceListView from '../common/Resource/ResourceListView';
23+
import { KubeIcon } from '../resourceMap/kubeIcon/KubeIcon';
2024

2125
export default function AdminNetworkPolicyList() {
2226
const { t } = useTranslation(['glossary']);
@@ -50,12 +54,35 @@ export default function AdminNetworkPolicyList() {
5054
if (!subject) return 'N/A';
5155

5256
if (subject.namespaces) {
53-
const nsKeys = Object.keys(subject.namespaces);
54-
return `Namespaces: ${nsKeys.length > 0 ? nsKeys.join(', ') : 'All'}`;
57+
// const nsKeys = Object.keys(subject.namespaces);
58+
return (
59+
<>
60+
<Typography sx={metadataStyles} display="inline">
61+
<span style={{ display: 'flex', alignItems: 'center', gap: '4px' }}></span>
62+
<KubeIcon kind="Namespace" width="20px" height="20px" />
63+
</Typography>
64+
</>
65+
);
66+
// return `Namespaces: ${nsKeys.length > 0 ? nsKeys.join(', ') : 'All'}`;
5567
}
5668

5769
if (subject.pods) {
58-
return `Pods: ${JSON.stringify(subject.pods)}`;
70+
return (
71+
<>
72+
<Typography sx={metadataStyles} display="inline">
73+
<span style={{ display: 'flex', alignItems: 'center', gap: '4px' }}>
74+
<KubeIcon kind="Namespace" width="30px" height="30px" />
75+
<Tooltip title="namespaceSelector">
76+
{matchLabelsSimplifier(subject.pods.namespaceSelector.matchLabels)}
77+
</Tooltip>
78+
<KubeIcon kind="Pod" width="30px" height="30px" />
79+
<Tooltip title="podSelector">
80+
{matchLabelsSimplifier(subject.pods.podSelector.matchLabels)}
81+
</Tooltip>
82+
</span>
83+
</Typography>
84+
</>
85+
);
5986
}
6087

6188
return 'Unknown Subject Format';

0 commit comments

Comments
 (0)