mirror of
https://github.com/gethomepage/homepage.git
synced 2026-01-10 18:52:07 +08:00
Enhancement: add kubernetes support to portainer widget (#5414)
Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
@@ -6,15 +6,64 @@ import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
export default function Component({ service }) {
|
||||
const { widget } = service;
|
||||
|
||||
const { data: containersData, error: containersError } = useWidgetAPI(widget, "docker/containers/json", {
|
||||
all: 1,
|
||||
});
|
||||
if (!widget.fields) {
|
||||
widget.fields = widget.kubernetes ? ["applications", "services", "namespaces"] : ["running", "stopped", "total"];
|
||||
}
|
||||
|
||||
const { data: containersCount, error: containersError } = useWidgetAPI(
|
||||
widget,
|
||||
widget.kubernetes ? "" : "docker/containers",
|
||||
{
|
||||
all: 1,
|
||||
},
|
||||
);
|
||||
|
||||
const { data: applicationsCount, error: applicationsError } = useWidgetAPI(
|
||||
widget,
|
||||
widget.kubernetes ? "kubernetes/applications" : "",
|
||||
);
|
||||
|
||||
const { data: servicesCount, error: servicesError } = useWidgetAPI(
|
||||
widget,
|
||||
widget.kubernetes ? "kubernetes/services" : "",
|
||||
);
|
||||
|
||||
const { data: namespacesCount, error: namespacesError } = useWidgetAPI(
|
||||
widget,
|
||||
widget.kubernetes ? "kubernetes/namespaces" : "",
|
||||
);
|
||||
|
||||
if (widget.kubernetes) {
|
||||
// count can be an error object
|
||||
const error = applicationsError ?? servicesError ?? namespacesError ?? applicationsCount;
|
||||
if (error) {
|
||||
return <Container service={service} error={error} />;
|
||||
}
|
||||
|
||||
if (applicationsCount == undefined || servicesCount == undefined || namespacesCount == undefined) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="portainer.applications" />
|
||||
<Block label="portainer.services" />
|
||||
<Block label="portainer.namespaces" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="portainer.applications" value={applicationsCount ?? 0} />
|
||||
<Block label="portainer.services" value={servicesCount ?? 0} />
|
||||
<Block label="portainer.namespaces" value={namespacesCount ?? 0} />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
if (containersError) {
|
||||
return <Container service={service} error={containersError} />;
|
||||
}
|
||||
|
||||
if (!containersData) {
|
||||
if (!containersCount) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="portainer.running" />
|
||||
@@ -24,14 +73,14 @@ export default function Component({ service }) {
|
||||
);
|
||||
}
|
||||
|
||||
if (containersData.error || containersData.message) {
|
||||
if (containersCount.error || containersCount.message) {
|
||||
// containersData can be itself an error object e.g. if environment fails
|
||||
return <Container service={service} error={containersData?.error ?? containersData} />;
|
||||
return <Container service={service} error={containersCount?.error ?? containersCount} />;
|
||||
}
|
||||
|
||||
const running = containersData.filter((c) => c.State === "running").length;
|
||||
const stopped = containersData.filter((c) => c.State === "exited").length;
|
||||
const total = containersData.length;
|
||||
const running = containersCount.filter((c) => c.State === "running").length;
|
||||
const stopped = containersCount.filter((c) => c.State === "exited").length;
|
||||
const total = containersCount.length;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
|
||||
Reference in New Issue
Block a user