|
| 1 | +import { Divider, Alert, Table, Spin } from "antd"; |
| 2 | +import React, { useCallback, useEffect, useState } from "react"; |
| 3 | +import { mapResponseError } from "../../utils/api/errors"; |
| 4 | +import { useResourceListActions } from "./ResourceList/ResourceListActionsContext"; |
| 5 | +import ReactAce from "react-ace"; |
| 6 | +import YAML from "yaml"; |
| 7 | + |
| 8 | +interface Props { |
| 9 | + namespace: string; |
| 10 | + name: string; |
| 11 | +} |
| 12 | + |
| 13 | +interface NetworkPolicyData { |
| 14 | + pods: []; |
| 15 | + ingress: {}; |
| 16 | + egress: {}; |
| 17 | +} |
| 18 | + |
| 19 | +const NetworkPolicy = ({ namespace, name }: Props) => { |
| 20 | + const [loading, setLoading] = useState(true); |
| 21 | + const { fetchResource } = useResourceListActions(); |
| 22 | + |
| 23 | + const [networkPolicy, setNetworkPolicy] = useState<NetworkPolicyData>({ |
| 24 | + pods: [], |
| 25 | + ingress: {}, |
| 26 | + egress: {}, |
| 27 | + }); |
| 28 | + |
| 29 | + const [error, setError] = useState({ |
| 30 | + message: "", |
| 31 | + description: "", |
| 32 | + }); |
| 33 | + |
| 34 | + const fetchNetworkPolicy = useCallback(() => { |
| 35 | + fetchResource("networking.k8s.io", "v1", "NetworkPolicy", namespace, name)() |
| 36 | + .then((res) => { |
| 37 | + setNetworkPolicy(res); |
| 38 | + setLoading(false); |
| 39 | + }) |
| 40 | + .catch((error) => { |
| 41 | + setError(mapResponseError(error)); |
| 42 | + setLoading(false); |
| 43 | + }); |
| 44 | + }, [name, fetchResource]); |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + fetchNetworkPolicy(); |
| 48 | + |
| 49 | + const interval = setInterval(() => fetchNetworkPolicy(), 15000); |
| 50 | + return () => { |
| 51 | + clearInterval(interval); |
| 52 | + }; |
| 53 | + }, [fetchNetworkPolicy]); |
| 54 | + |
| 55 | + const editorHeight = (lines: number) => { |
| 56 | + if (lines > 20) { |
| 57 | + return "320px"; |
| 58 | + } else { |
| 59 | + return `${lines * 16}px`; |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + const stringifyRulesToYaml = (obj: any) => { |
| 64 | + try { |
| 65 | + return YAML.stringify(obj); |
| 66 | + } catch (error) { |
| 67 | + console.error("YAML stringify error:", error); |
| 68 | + return `YAML stringify error: ${error}`; |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + if (loading) return <Spin size="large" style={{ marginTop: "20px" }} />; |
| 73 | + |
| 74 | + return ( |
| 75 | + <div> |
| 76 | + {error.message.length !== 0 && ( |
| 77 | + <Alert |
| 78 | + message={error.message} |
| 79 | + description={error.description} |
| 80 | + type="error" |
| 81 | + closable |
| 82 | + afterClose={() => { |
| 83 | + setError({ |
| 84 | + message: "", |
| 85 | + description: "", |
| 86 | + }); |
| 87 | + }} |
| 88 | + style={{ marginBottom: "20px" }} |
| 89 | + /> |
| 90 | + )} |
| 91 | + <Divider |
| 92 | + style={{ fontSize: "120%" }} |
| 93 | + orientationMargin="0" |
| 94 | + orientation={"left"} |
| 95 | + > |
| 96 | + Ingress |
| 97 | + </Divider> |
| 98 | + <ReactAce |
| 99 | + style={{ |
| 100 | + width: "100%", |
| 101 | + height: editorHeight( |
| 102 | + stringifyRulesToYaml(networkPolicy.ingress).split("\n").length, |
| 103 | + ), |
| 104 | + }} |
| 105 | + mode={"sass"} |
| 106 | + value={stringifyRulesToYaml(networkPolicy.ingress)} |
| 107 | + readOnly={true} |
| 108 | + /> |
| 109 | + <Divider |
| 110 | + style={{ fontSize: "120%" }} |
| 111 | + orientationMargin="0" |
| 112 | + orientation={"left"} |
| 113 | + > |
| 114 | + Egress |
| 115 | + </Divider> |
| 116 | + <ReactAce |
| 117 | + style={{ |
| 118 | + width: "100%", |
| 119 | + height: editorHeight( |
| 120 | + stringifyRulesToYaml(networkPolicy.egress).split("\n").length, |
| 121 | + ), |
| 122 | + }} |
| 123 | + mode={"sass"} |
| 124 | + value={stringifyRulesToYaml(networkPolicy.egress)} |
| 125 | + readOnly={true} |
| 126 | + /> |
| 127 | + <Divider |
| 128 | + style={{ fontSize: "120%" }} |
| 129 | + orientationMargin="0" |
| 130 | + orientation={"left"} |
| 131 | + > |
| 132 | + Target pods |
| 133 | + </Divider> |
| 134 | + <Table dataSource={networkPolicy.pods}> |
| 135 | + <Table.Column title={"Name"} dataIndex="name" /> |
| 136 | + <Table.Column title={"Namespace"} dataIndex="namespace" /> |
| 137 | + </Table> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +export default NetworkPolicy; |
0 commit comments