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 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 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
610bad8
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Jan 8, 2025
7d295bc
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Jan 15, 2025
d2c94ae
Introduce a separated reusable component for object logs
Sheikh-Abubaker Feb 9, 2025
045989a
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops int…
Sheikh-Abubaker Feb 9, 2025
c3cd8a0
Remove unused imports
Sheikh-Abubaker Feb 9, 2025
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 @@ -95,6 +96,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
2 changes: 1 addition & 1 deletion cyclops-ui/src/components/k8s-resources/Deployment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { Col, Divider, Row, Alert, Spin } from "antd";
import { mapResponseError } from "../../utils/api/errors";
import PodTable from "./common/PodTable/PodTable";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import ClusterRole from "../ClusterRole";
import ConfigMap from "../ConfigMap";
import PersistentVolumeClaim from "../PersistentVolumeClaim";
import Secret from "../Secret";
import ObjectLogsButton from "../common/ObjectLogsButton";
import {
CaretRightOutlined,
CheckCircleTwoTone,
Expand Down Expand Up @@ -589,6 +590,16 @@ const ResourceList = ({
/>
</Col>
)}
{(resource.kind === "Deployment" ||
resource.kind === "StatefulSet") && (
<Col style={{ float: "right" }}>
<ObjectLogsButton
name={resource.name}
namespace={resource.namespace}
workload={getWorkload(resourceRef)}
/>
</Col>
)}
</Row>
{resourceDetails}
</Collapse.Panel>,
Expand Down
4 changes: 2 additions & 2 deletions cyclops-ui/src/components/k8s-resources/StatefulSet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";
import { Col, Divider, Row, Alert, Spin } from "antd";
import { mapResponseError } from "../../utils/api/errors";
import PodTable from "./common/PodTable/PodTable";
Expand All @@ -15,11 +15,11 @@ const StatefulSet = ({ name, namespace, workload }: Props) => {
const { fetchResource, streamingDisabled } = useResourceListActions();

const [loading, setLoading] = useState(true);

const [statefulSet, setStatefulSet] = useState({
status: "",
pods: [],
});

const [error, setError] = useState({
message: "",
description: "",
Expand Down
274 changes: 274 additions & 0 deletions cyclops-ui/src/components/k8s-resources/common/ObjectLogsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
import { useState, useRef } from "react";
import { Col, Divider, Alert, TabsProps, Button, Tabs, Modal } from "antd";
import { logStream } from "../../../utils/api/sse/logs";
import { mapResponseError } from "../../../utils/api/errors";
import ReactAce from "react-ace/lib/ace";
import { DownloadOutlined, ReadOutlined } from "@ant-design/icons";

import { useResourceListActions } from "../ResourceList/ResourceListActionsContext";

interface Props {
name: string;
namespace: string;
workload: any;
}

const ObjectLogsButton = ({ 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" }} />
View Logs
</Button>
<Modal
title="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>
</>
);
};

export default ObjectLogsButton;