-
Notifications
You must be signed in to change notification settings - Fork 779
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
Sheikh-Abubaker
wants to merge
25
commits into
cyclops-ui:main
Choose a base branch
from
Sheikh-Abubaker:log-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bde5ad2
Add button to fetch Deployment/StatefulSet logs
Sheikh-Abubaker 0e9b491
Revert style to marginBottom and remove container parameter from onLoβ¦
Sheikh-Abubaker 47a9c93
Add handler functions for Deployment/Sts logs and point axios.get to β¦
Sheikh-Abubaker 00c9c05
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker db3068c
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker f1f62bb
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker e78b3df
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker becd137
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 7d3ac86
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker a6f7731
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker bb91f0c
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 3cc3d6d
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 63d85ec
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 7a8fbaf
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker ffff91c
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 4df08a4
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 7cd922b
Resolve Conflicts
Sheikh-Abubaker 0b19391
Resolve Conflicts
Sheikh-Abubaker 6579a29
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 610bad8
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker 7d295bc
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker d2c94ae
Introduce a separated reusable component for object logs
Sheikh-Abubaker 045989a
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker c3cd8a0
Remove unused imports
Sheikh-Abubaker 440b559
Merge branch 'main' of https://github.com/Sheikh-Abubaker/cyclops intβ¦
Sheikh-Abubaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
274 changes: 274 additions & 0 deletions
274
cyclops-ui/src/components/k8s-resources/common/ObjectLogsButton.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,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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't fetch logs only from the first pod. We should implement a new backend endpoint that would aggregate all the logs from all the pods of a Deployment and show them all instead. You could then implement fetch logs here by deployment name and container name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so you mean we need a new handler function for it ?