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

Improve log handling #16

Merged
merged 5 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/kubernetes/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
IonRow,
IonTitle,
IonToolbar,
isPlatform,
} from '@ionic/react';
import { V1Container, V1ContainerPort, V1ContainerState, V1ContainerStatus, V1EnvVarSource } from '@kubernetes/client-node'
import { close } from 'ionicons/icons';
Expand Down Expand Up @@ -93,11 +94,12 @@ const Container: React.FunctionComponent<IContainerProps> = ({ container, logs,
<IonLabel>
<h2>{container.name}</h2>
</IonLabel>
{!isPlatform('hybrid') && logs && name && namespace ? <Logs activator="button" name={name} namespace={namespace} container={container.name} /> : null}
</IonItem>

{logs && name && namespace ? (
{isPlatform('hybrid') && logs && name && namespace ? (
<IonItemOptions side="end">
<Logs name={name} namespace={namespace} container={container.name} />
<Logs activator="item-option" name={name} namespace={namespace} container={container.name} />
</IonItemOptions>
) : null}
</IonItemSliding>
Expand Down
75 changes: 61 additions & 14 deletions src/components/kubernetes/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,68 @@ import {
IonContent,
IonHeader,
IonIcon,
IonItem,
IonItemOption,
IonLabel,
IonList,
IonModal,
IonPopover,
IonProgressBar,
IonTitle,
IonToolbar,
} from '@ionic/react';
import { close, list, refresh } from 'ionicons/icons';
import React, {useContext, useEffect, useState} from 'react';
import { close, list, more } from 'ionicons/icons';
import React, { useContext, useEffect, useState } from 'react';

import { AppContext } from '../../context';
import { IContext } from '../../declarations';
import { IContext, TActivator } from '../../declarations';
import Editor from '../misc/Editor';

const TAIL_LINES = 1000;

interface ILogsProps {
activator: TActivator;
name: string;
namespace: string;
container: string;
}

const Logs: React.FunctionComponent<ILogsProps> = ({ name, namespace, container }) => {
const Logs: React.FunctionComponent<ILogsProps> = ({ activator, name, namespace, container }) => {
const context = useContext<IContext>(AppContext);

const [showModal, setShowModal] = useState<boolean>(false);
const [showLoading, setShowLoading] = useState<boolean>(false);
const [error, setError] = useState<string>('');
const [logs, setLogs] = useState<string>('');
const [showPopover, setShowPopover] = useState<boolean>(false);
const [popoverEvent, setPopoverEvent] = useState();

useEffect(() => {
if (showModal) {
(async() => {
setLogs('');
await load();
await load(false, TAIL_LINES);
})();
}

return () => {};
}, [showModal]); /* eslint-disable-line */

const load = async () => {
const load = async (previous: boolean, tailLines: number) => {
setShowLoading(true);

try {
const data: any = await context.request('GET', `/api/v1/namespaces/${namespace}/pods/${name}/log?container=${container}`, '');
let parameters = `container=${container}`;

if (previous) {
parameters = `${parameters}&previous=true`;
}

if (tailLines !== 0) {
parameters = `${parameters}&tailLines=${tailLines}`;
}

const data: any = await context.request('GET', `/api/v1/namespaces/${namespace}/pods/${name}/log?${parameters}`, '');
setLogs(data);
} catch (err) {
setError(err);
Expand All @@ -60,23 +79,51 @@ const Logs: React.FunctionComponent<ILogsProps> = ({ name, namespace, container
<React.Fragment>
{error !== '' ? <IonAlert isOpen={error !== ''} onDidDismiss={() => setError('')} header="Could not load logs" message={error} buttons={['OK']} /> : null}

<IonItemOption color="primary" onClick={() => setShowModal(true)}>
<IonIcon slot="start" icon={list} />
Logs
</IonItemOption>
{activator === 'item-option' ? (
<IonItemOption color="primary" onClick={() => setShowModal(true)}>
<IonIcon slot="start" icon={list} />
Logs
</IonItemOption>
) : null}

{activator === 'button' ? (
<IonButton fill="outline" slot="end" onClick={(e) => { e.stopPropagation(); setShowModal(true); }}>
<IonIcon slot="start" icon={list} />
Logs
</IonButton>
) : null}

<IonModal isOpen={showModal} onDidDismiss={() => setShowModal(false)}>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonButton onClick={() => setShowModal(false)}>
<IonButton onClick={(e) => { e.stopPropagation(); setShowModal(false); }}>
<IonIcon slot="icon-only" icon={close} />
</IonButton>
</IonButtons>
<IonTitle>{container}</IonTitle>
<IonButtons slot="primary" onClick={() => load()}>
<IonIcon slot="icon-only" icon={refresh} />
<IonButtons slot="primary">
<IonButton onClick={(e) => { e.stopPropagation(); e.persist(); setPopoverEvent(e); setShowPopover(true); }}>
<IonIcon slot="icon-only" icon={more} />
</IonButton>
</IonButtons>

<IonPopover isOpen={showPopover} event={popoverEvent} onDidDismiss={() => setShowPopover(false)}>
<IonList>
<IonItem button={true} detail={false} onClick={(e) => { e.stopPropagation(); setShowPopover(false); load(false, TAIL_LINES); }}>
<IonLabel>{`Last ${TAIL_LINES} Log Lines`}</IonLabel>
</IonItem>
<IonItem button={true} detail={false} onClick={(e) => { e.stopPropagation(); setShowPopover(false); load(false, 0); }}>
<IonLabel>All Log Lines</IonLabel>
</IonItem>
<IonItem button={true} detail={false} onClick={(e) => { e.stopPropagation(); setShowPopover(false); load(true, TAIL_LINES); }}>
<IonLabel>{`Previous Last ${TAIL_LINES} Log Lines`}</IonLabel>
</IonItem>
<IonItem button={true} detail={false} onClick={(e) => { e.stopPropagation(); setShowPopover(false); load(true, 0); }}>
<IonLabel>All Previous Log Lines</IonLabel>
</IonItem>
</IonList>
</IonPopover>
</IonToolbar>
</IonHeader>
<IonContent>
Expand Down
4 changes: 2 additions & 2 deletions src/components/kubernetes/NamespacePopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ const NamespacePopover: React.FunctionComponent = () => {
<IonPopover isOpen={showPopover} event={popoverEvent} onDidDismiss={() => setShowPopover(false)}>
{namespaces ? (
<IonList>
<IonItem onClick={() => setAllNamespaces()}>
<IonItem button={true} detail={false} onClick={() => setAllNamespaces()}>
{context.clusters && context.cluster && context.clusters.hasOwnProperty(context.cluster) && context.clusters[context.cluster].namespace === '' ? <IonIcon slot="end" color="primary" icon={checkmark} /> : null}
<IonLabel>All Namespaces</IonLabel>
</IonItem>

{namespaces.items.map((namespace, index) => {
return (
<IonItem key={index} onClick={() => setNamespace(namespace)}>
<IonItem key={index} button={true} detail={false} onClick={() => setNamespace(namespace)}>
{namespace.metadata && context.clusters && context.cluster && context.clusters.hasOwnProperty(context.cluster) && context.clusters[context.cluster].namespace === namespace.metadata.name ? <IonIcon slot="end" color="primary" icon={checkmark} /> : null}
<IonLabel>{namespace.metadata ? namespace.metadata.name : ''}</IonLabel>
</IonItem>
Expand Down
2 changes: 1 addition & 1 deletion src/components/misc/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const Editor: React.FunctionComponent<IEditorProps> = ({ onChange, readOnly, mod
<React.Fragment>
{showScrollToBottomButton ? (
<div className="editor-scroll-to-bottom-button">
<IonButton size="small" onClick={() => scrollToBottom()}>Scroll to Bottom</IonButton>
<IonButton size="small" onClick={(e) => { e.stopPropagation(); scrollToBottom(); }}>Scroll to Bottom</IonButton>
</div>
) : null}

Expand Down
2 changes: 1 addition & 1 deletion src/theme/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ th {
.editor-scroll-to-bottom-button {
position: absolute;
z-index: 999;
bottom: 10px;
bottom: 20px;
width: 100%;
text-align: center;
}
Expand Down