Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/cyclops-ui: Add support to fetch Deployment/StatefulSet logs #660

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
bde5ad2
Add button to fetch Deployment/StatefulSet logs
Sheikh-Abubaker Oct 28, 2024
0e9b491
Revert style to marginBottom and remove container parameter from onLo…
Sheikh-Abubaker Oct 28, 2024
47a9c93
Add handler functions for Deployment/Sts logs and point axios.get to …
Sheikh-Abubaker Oct 28, 2024
00c9c05
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Oct 28, 2024
db3068c
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Oct 29, 2024
f1f62bb
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Oct 31, 2024
e78b3df
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Nov 2, 2024
becd137
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Nov 5, 2024
7d3ac86
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Nov 9, 2024
a6f7731
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Nov 13, 2024
bb91f0c
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Nov 25, 2024
3cc3d6d
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Nov 27, 2024
63d85ec
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Dec 1, 2024
7a8fbaf
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Dec 6, 2024
ffff91c
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Dec 7, 2024
4df08a4
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Dec 12, 2024
7cd922b
Resolve Conflicts
Sheikh-Abubaker Dec 14, 2024
0b19391
Resolve Conflicts
Sheikh-Abubaker Dec 14, 2024
6579a29
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cyclops-ctrl/internal/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package handler

import (
"net/http"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/controller/sse"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/integrations/helm"
"github.com/gin-gonic/gin"
"net/http"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/controller"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/internal/prometheus"
Expand Down Expand Up @@ -96,6 +97,8 @@ func (h *Handler) Start() error {
h.router.GET("/resources/pods/:namespace/:name/:container/logs", modulesController.GetLogs)
h.router.GET("/resources/pods/:namespace/:name/:container/logs/stream", sse.HeadersMiddleware(), modulesController.GetLogsStream)
h.router.GET("/resources/pods/:namespace/:name/:container/logs/download", modulesController.DownloadLogs)
h.router.GET("/resources/deployments/:namespace/:deployment/:container/logs", modulesController.GetDeploymentLogs)
h.router.GET("/resources/statefulsets/:namespace/:name/:container/logs", modulesController.GetStatefulSetsLogs)

h.router.GET("/manifest", modulesController.GetManifest)
h.router.GET("/resources", modulesController.GetResource)
Expand Down
277 changes: 273 additions & 4 deletions cyclops-ui/src/components/k8s-resources/Deployment.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import React, { useCallback, useEffect, useState } from "react";
import { Col, Divider, Row, Alert, Spin } from "antd";
import { useCallback, useEffect, useState, useRef } from "react";
import {
Col,
Divider,
Row,
Alert,
Spin,
TabsProps,
Button,
Tabs,
Modal,
} from "antd";
import { mapResponseError } from "../../utils/api/errors";
import PodTable from "./common/PodTable/PodTable";
import { isStreamingEnabled } from "../../utils/api/common";
import { logStream } from "../../utils/api/sse/logs";
import ReactAce from "react-ace/lib/ace";
import { DownloadOutlined, ReadOutlined } from "@ant-design/icons";
import { useResourceListActions } from "./ResourceList/ResourceListActionsContext";

interface Props {
Expand All @@ -11,7 +24,7 @@ interface Props {
workload: any;
}

const Deployment = ({ name, namespace, workload }: Props) => {
export const Deployment = ({ name, namespace, workload }: Props) => {
const { fetchResource, streamingDisabled } = useResourceListActions();

const [loading, setLoading] = useState(true);
Expand Down Expand Up @@ -107,4 +120,260 @@ const Deployment = ({ name, namespace, workload }: Props) => {
);
};

export default Deployment;
export const DeploymentLogsButton = ({ name, namespace, workload }: Props) => {
const { streamingDisabled, getPodLogs, downloadPodLogs, streamPodLogs } =
useResourceListActions();
const [logs, setLogs] = useState<string[]>([]);
const [logsModal, setLogsModal] = useState({
on: false,
containers: [],
initContainers: [],
});

const logsSignalControllerRef = useRef<AbortController | null>(null);

const [error, setError] = useState({
message: "",
description: "",
});

const handleCancelLogs = () => {
setLogsModal({
on: false,
containers: [],
initContainers: [],
});
setLogs([]);

// send the abort signal
if (logsSignalControllerRef.current !== null) {
logsSignalControllerRef.current.abort();
}
};

const getTabItems = () => {
let items: TabsProps["items"] = [];

let container: any;

if (logsModal.containers !== null) {
for (container of logsModal.containers) {
items.push({
key: container.name,
label: container.name,
children: (
<Col>
{downloadPodLogs ? (
<div>
<Button
type="primary"
icon={<DownloadOutlined />}
onClick={downloadLogs(container.name)}
disabled={logs.length === 0}
>
Download
</Button>
<Divider
style={{ marginTop: "16px", marginBottom: "16px" }}
/>
</div>
) : (
<></>
)}
<ReactAce
style={{ width: "100%" }}
mode={"sass"}
value={
logs.length === 0 ? "No logs available" : logs.join("\n")
}
readOnly={true}
/>
</Col>
),
});
}
}

if (logsModal.initContainers !== null) {
for (container of logsModal.initContainers) {
items.push({
key: container.name,
label: "(init container) " + container.name,
children: (
<Col>
{downloadPodLogs ? (
<div>
<Button
type="primary"
icon={<DownloadOutlined />}
onClick={downloadLogs(container.name)}
disabled={logs.length === 0}
>
Download
</Button>
<Divider
style={{ marginTop: "16px", marginBottom: "16px" }}
/>
</div>
) : (
<></>
)}
<ReactAce
style={{ width: "100%" }}
mode={"sass"}
value={
logs.length === 0 ? "No logs available" : logs.join("\n")
}
readOnly={true}
/>
</Col>
),
});
}
}

return items;
};

const onLogsTabsChange = () => {
const controller = new AbortController();
if (logsSignalControllerRef.current !== null) {
logsSignalControllerRef.current.abort();
}
logsSignalControllerRef.current = controller; // store the controller to be able to abort the request
setLogs(() => []);

if (!streamingDisabled) {
logStream(
namespace,
name,
workload.pods[0].containers[0].name,
(log, isReset = false) => {
if (isReset) {
setLogs(() => []);
} else {
setLogs((prevLogs) => {
return [...prevLogs, log];
});
}
},
(err, isReset = false) => {
if (isReset) {
setError({
message: "",
description: "",
});
} else {
setError(mapResponseError(err));
}
},
controller,
streamPodLogs,
);
} else {
getPodLogs(namespace, name, workload.pods[0].containers[0].name)
.then((res) => {
if (res) {
setLogs(res);
} else {
setLogs(() => []);
}
})
.catch((error) => {
setError(mapResponseError(error));
});
}
};

const downloadLogs = (container: string) => {
return () => downloadPodLogs(namespace, workload.pods[0].name, container);
};

return (
<>
<Button
style={{ width: "100%" }}
onClick={function () {
if (!streamingDisabled) {
const controller = new AbortController();
logsSignalControllerRef.current = controller; // store the controller to be able to abort the request

logStream(
namespace,
workload.pods[0].name,
workload.pods[0].containers[0].name,
(log, isReset = false) => {
if (isReset) {
setLogs(() => []);
} else {
setLogs((prevLogs) => {
return [...prevLogs, log];
});
}
},
(err, isReset = false) => {
if (isReset) {
setError({
message: "",
description: "",
});
} else {
setError(mapResponseError(err));
}
},
controller,
streamPodLogs,
);
} else {
getPodLogs(namespace, name, workload.pods[0].containers[0].name)
.then((res) => {
if (res) {
setLogs(res);
} else {
setLogs(() => []);
}
})
.catch((error) => {
setError(mapResponseError(error));
});
}

setLogsModal({
on: true,
containers: workload.pods[0].containers,
initContainers: workload.pods[0].initContainers,
});
}}
>
<ReadOutlined style={{ paddingRight: "5px" }} />
Deployment Logs
</Button>
<Modal
title="Deployment Logs"
open={logsModal.on}
onOk={handleCancelLogs}
onCancel={handleCancelLogs}
cancelButtonProps={{ style: { display: "none" } }}
style={{ zIndex: 100 }}
width={"80%"}
>
{error.message.length !== 0 && (
<Alert
message={error.message}
description={error.description}
type="error"
closable
afterClose={() => {
setError({
message: "",
description: "",
});
}}
style={{ marginBottom: "20px" }}
/>
)}
<Tabs items={getTabItems()} onChange={onLogsTabsChange} />
</Modal>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import {
ResourceRef,
resourceRefKey,
} from "../../../utils/resourceRef";
import Deployment from "../Deployment";
import { Deployment, DeploymentLogsButton } from "../Deployment";
import CronJob from "../CronJob";
import Job from "../Job";
import DaemonSet from "../DaemonSet";
import StatefulSet from "../StatefulSet";
import { StatefulSet, StatefulSetLogsButton } from "../StatefulSet";
import Pod from "../Pod";
import Service from "../Service";
import ClusterRole from "../ClusterRole";
Expand Down Expand Up @@ -574,6 +574,24 @@ const ResourceList = ({
/>
</Col>
)}
{resource.kind === "Deployment" && (
<Col style={{ float: "right" }}>
<DeploymentLogsButton
name={resource.name}
namespace={resource.namespace}
workload={getWorkload(resourceRef)}
/>
</Col>
)}
{resource.kind === "StatefulSet" && (
<Col style={{ float: "right" }}>
<StatefulSetLogsButton
name={resource.name}
namespace={resource.namespace}
workload={getWorkload(resourceRef)}
/>
</Col>
)}
</Row>
{resourceDetails}
</Collapse.Panel>,
Expand Down
Loading
Loading