-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌐 support networking policies in ui (#765)
* support networking policies in ui * add deps
- Loading branch information
1 parent
a0ef6f9
commit 96452dd
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
cyclops-ui/src/components/k8s-resources/NetworkPolicy.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { Divider, Alert, Table, Spin } from "antd"; | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
import { mapResponseError } from "../../utils/api/errors"; | ||
import { useResourceListActions } from "./ResourceList/ResourceListActionsContext"; | ||
import ReactAce from "react-ace"; | ||
import YAML from "yaml"; | ||
|
||
interface Props { | ||
namespace: string; | ||
name: string; | ||
} | ||
|
||
interface NetworkPolicyData { | ||
pods: []; | ||
ingress: {}; | ||
egress: {}; | ||
} | ||
|
||
const NetworkPolicy = ({ namespace, name }: Props) => { | ||
const [loading, setLoading] = useState(true); | ||
const { fetchResource } = useResourceListActions(); | ||
|
||
const [networkPolicy, setNetworkPolicy] = useState<NetworkPolicyData>({ | ||
pods: [], | ||
ingress: {}, | ||
egress: {}, | ||
}); | ||
|
||
const [error, setError] = useState({ | ||
message: "", | ||
description: "", | ||
}); | ||
|
||
const fetchNetworkPolicy = useCallback(() => { | ||
fetchResource("networking.k8s.io", "v1", "NetworkPolicy", namespace, name)() | ||
.then((res) => { | ||
setNetworkPolicy(res); | ||
setLoading(false); | ||
}) | ||
.catch((error) => { | ||
setError(mapResponseError(error)); | ||
setLoading(false); | ||
}); | ||
}, [name, namespace, fetchResource]); | ||
|
||
useEffect(() => { | ||
fetchNetworkPolicy(); | ||
|
||
const interval = setInterval(() => fetchNetworkPolicy(), 15000); | ||
return () => { | ||
clearInterval(interval); | ||
}; | ||
}, [fetchNetworkPolicy]); | ||
|
||
const editorHeight = (lines: number) => { | ||
if (lines > 20) { | ||
return "320px"; | ||
} else { | ||
return `${lines * 16}px`; | ||
} | ||
}; | ||
|
||
const stringifyRulesToYaml = (obj: any) => { | ||
try { | ||
return YAML.stringify(obj); | ||
} catch (error) { | ||
console.error("YAML stringify error:", error); | ||
return `YAML stringify error: ${error}`; | ||
} | ||
}; | ||
|
||
if (loading) return <Spin size="large" style={{ marginTop: "20px" }} />; | ||
|
||
return ( | ||
<div> | ||
{error.message.length !== 0 && ( | ||
<Alert | ||
message={error.message} | ||
description={error.description} | ||
type="error" | ||
closable | ||
afterClose={() => { | ||
setError({ | ||
message: "", | ||
description: "", | ||
}); | ||
}} | ||
style={{ marginBottom: "20px" }} | ||
/> | ||
)} | ||
<Divider | ||
style={{ fontSize: "120%" }} | ||
orientationMargin="0" | ||
orientation={"left"} | ||
> | ||
Ingress | ||
</Divider> | ||
<ReactAce | ||
style={{ | ||
width: "100%", | ||
height: editorHeight( | ||
stringifyRulesToYaml(networkPolicy.ingress).split("\n").length, | ||
), | ||
}} | ||
mode={"sass"} | ||
value={stringifyRulesToYaml(networkPolicy.ingress)} | ||
readOnly={true} | ||
/> | ||
<Divider | ||
style={{ fontSize: "120%" }} | ||
orientationMargin="0" | ||
orientation={"left"} | ||
> | ||
Egress | ||
</Divider> | ||
<ReactAce | ||
style={{ | ||
width: "100%", | ||
height: editorHeight( | ||
stringifyRulesToYaml(networkPolicy.egress).split("\n").length, | ||
), | ||
}} | ||
mode={"sass"} | ||
value={stringifyRulesToYaml(networkPolicy.egress)} | ||
readOnly={true} | ||
/> | ||
<Divider | ||
style={{ fontSize: "120%" }} | ||
orientationMargin="0" | ||
orientation={"left"} | ||
> | ||
Target pods | ||
</Divider> | ||
<Table dataSource={networkPolicy.pods}> | ||
<Table.Column title={"Name"} dataIndex="name" /> | ||
<Table.Column title={"Namespace"} dataIndex="namespace" /> | ||
</Table> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NetworkPolicy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters