Skip to content

Commit

Permalink
Fetching pipelinerun either from k8s or from tekton results
Browse files Browse the repository at this point in the history
  • Loading branch information
lprabhu committed Nov 8, 2023
1 parent bde3ee8 commit 3c42594
Show file tree
Hide file tree
Showing 6 changed files with 534 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export type MatchLabels = {
export type Selector = {
matchLabels?: MatchLabels;
matchExpressions?: MatchExpression[];
filterByName?: string;
};

type K8sVerb =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as React from 'react';
import { useK8sWatchResource } from '@console/dynamic-plugin-sdk/src/utils/k8s/hooks/useK8sWatchResource';
import { DetailsPage, DetailsPageProps } from '@console/internal/components/factory';
import { KebabAction, navFactory, viewYamlComponent } from '@console/internal/components/utils';
import { FirehoseResult, K8sResourceKind, referenceForModel } from '@console/internal/module/k8s';
import { PipelineRunModel } from '../../models';
import { PipelineRunKind } from '../../types';
import { usePipelineTechPreviewBadge } from '../../utils/hooks';
import { getPipelineRunKebabActions } from '../../utils/pipeline-actions';
import { pipelineRunStatus } from '../../utils/pipeline-filter-reducer';
Expand All @@ -11,11 +15,14 @@ import { PipelineRunDetails } from './detail-page-tabs/PipelineRunDetails';
import { PipelineRunLogsWithActiveTask } from './detail-page-tabs/PipelineRunLogs';
import TaskRuns from './detail-page-tabs/TaskRuns';
import PipelineRunEvents from './events/PipelineRunEvents';
import { useTRPipelineRuns } from './hooks/useTektonResults';
import PipelineRunParametersForm from './PipelineRunParametersForm';
import { useMenuActionsWithUserAnnotation } from './triggered-by';

const PipelineRunDetailsPage: React.FC<DetailsPageProps> = (props) => {
const { kindObj, match } = props;
const { kindObj, match, namespace, name } = props;
const [data, setData] = React.useState<FirehoseResult<K8sResourceKind> | PipelineRunKind>();
const [loaded, setLoaded] = React.useState(false);
const operatorVersion = usePipelineOperatorVersion(props.namespace);
const [taskRuns] = useTaskRuns(props.namespace);
const menuActions: KebabAction[] = useMenuActionsWithUserAnnotation(
Expand All @@ -24,9 +31,29 @@ const PipelineRunDetailsPage: React.FC<DetailsPageProps> = (props) => {
const breadcrumbsFor = useDevPipelinesBreadcrumbsFor(kindObj, match);
const badge = usePipelineTechPreviewBadge(props.namespace);

const [pipelineRun, pipelineRunLoaded] = useK8sWatchResource<FirehoseResult<K8sResourceKind>>({
kind: referenceForModel(PipelineRunModel),
namespace,
name,
isList: false,
});
const [TRPlrs, TRPlrsLoaded] = useTRPipelineRuns(namespace, {
selector: { filterByName: name },
});

React.useEffect(() => {
setData(pipelineRun || TRPlrs[0]);
setLoaded(pipelineRunLoaded || TRPlrsLoaded);
}, [TRPlrs, pipelineRun, pipelineRunLoaded, TRPlrsLoaded]);

return (
<DetailsPage
{...props}
obj={{
data,
loaded,
loadError: undefined,
}}
badge={badge}
menuActions={menuActions}
getResourceStatus={pipelineRunStatus}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { pipelineRunFilterReducer } from '../../../utils/pipeline-filter-reducer
import { TektonResourceLabel } from '../../pipelines/const';
import { ColoredStatusIcon } from '../../pipelines/detail-page-tabs/pipeline-details/StatusIcon';
import { useTaskRuns } from '../../taskruns/useTaskRuns';
import { useTRTaskRuns } from '../hooks/useTektonResults';
import { ErrorDetailsWithStaticLog } from '../logs/log-snippet-types';
import { getDownloadAllLogsCallback } from '../logs/logs-utils';
import LogsWrapperComponent from '../logs/LogsWrapperComponent';
Expand Down Expand Up @@ -208,11 +209,22 @@ export const PipelineRunLogsWithActiveTask: React.FC<PipelineRunLogsWithActiveTa
obj,
params,
}) => {
const [data, setData] = React.useState<TaskRunKind[]>();
const [loaded, setLoaded] = React.useState(false);
const activeTask = _.get(params, 'match.params.name');
const [taskRuns, taskRunsLoaded] = useTaskRuns(obj?.metadata?.namespace, obj?.metadata?.name);
return (
taskRunsLoaded && <PipelineRunLogs obj={obj} activeTask={activeTask} taskRuns={taskRuns} />
);
const [resultTaskRuns, resultTaskRunsLoaded] = useTRTaskRuns(obj?.metadata?.namespace, {
selector: {
matchLabels: {
[TektonResourceLabel.pipelinerun]: obj?.metadata?.name,
},
},
});
React.useEffect(() => {
setData(taskRuns || resultTaskRuns);
setLoaded(taskRunsLoaded || resultTaskRunsLoaded);
}, [taskRuns, resultTaskRuns, taskRunsLoaded, resultTaskRunsLoaded]);
return loaded && <PipelineRunLogs obj={obj} activeTask={activeTask} taskRuns={data} />;
};

export default PipelineRunLogs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import * as React from 'react';
import { K8sResourceCommon } from '@console/dynamic-plugin-sdk/src';
import { PipelineRunKind, TaskRunKind } from '../../../types';
import {
getPipelineRuns,
TektonResultsOptions,
getTaskRuns,
RecordsList,
// getTaskRunLog,
} from '../utils/tekton-results';

export type GetNextPage = () => void | undefined;

const useTRRuns = <Kind extends K8sResourceCommon>(
getRuns: (
namespace: string,
options?: TektonResultsOptions,
nextPageToken?: string,
cacheKey?: string,
) => Promise<[Kind[], RecordsList, boolean?]>,
namespace: string,
options?: TektonResultsOptions,
cacheKey?: string,
): [Kind[], boolean, unknown, GetNextPage] => {
const [nextPageToken, setNextPageToken] = React.useState<string>(null);
const [localCacheKey, setLocalCacheKey] = React.useState(cacheKey);

if (cacheKey !== localCacheKey) {
// force update local cache key
setLocalCacheKey(cacheKey);
}

const [result, setResult] = React.useState<[Kind[], boolean, unknown, GetNextPage]>([
[],
false,
undefined,
undefined,
]);

// reset token if namespace or options change
React.useEffect(() => {
setNextPageToken(null);
}, [namespace, options, cacheKey]);

// eslint-disable-next-line consistent-return
React.useEffect(() => {
let disposed = false;
if (namespace) {
(async () => {
try {
const tkPipelineRuns = await getRuns(namespace, options, nextPageToken, localCacheKey);
if (!disposed) {
const token = tkPipelineRuns[1].nextPageToken;
const callInflight = !!tkPipelineRuns?.[2];
const loaded = !callInflight;
if (!callInflight) {
setResult((cur) => [
nextPageToken ? [...cur[0], ...tkPipelineRuns[0]] : tkPipelineRuns[0],
loaded,
undefined,
token
? (() => {
// ensure we can only call this once
let executed = false;
return () => {
if (!disposed && !executed) {
executed = true;
// trigger the update
setNextPageToken(token);
return true;
}
return false;
};
})()
: undefined,
]);
}
}
} catch (e) {
if (!disposed) {
if (nextPageToken) {
setResult((cur) => [cur[0], cur[1], e, undefined]);
} else {
setResult([[], false, e, undefined]);
}
}
}
})();
return () => {
disposed = true;
};
}
}, [namespace, options, nextPageToken, localCacheKey, getRuns]);
return result;
};

export const useTRPipelineRuns = (
namespace: string,
options?: TektonResultsOptions,
cacheKey?: string,
): [PipelineRunKind[], boolean, unknown, GetNextPage] =>
useTRRuns<PipelineRunKind>(getPipelineRuns, namespace, options, cacheKey);

export const useTRTaskRuns = (
namespace: string,
options?: TektonResultsOptions,
cacheKey?: string,
): [TaskRunKind[], boolean, unknown, GetNextPage] =>
useTRRuns<TaskRunKind>(getTaskRuns, namespace, options, cacheKey);

// export const useTRTaskRunLog = (
// namespace: string,
// taskRunName: string,
// ): [string, boolean, unknown] => {
// const [result, setResult] = React.useState<[string, boolean, unknown]>([null, false, undefined]);
// React.useEffect(() => {
// let disposed = false;
// if (namespace && taskRunName) {
// (async () => {
// try {
// const log = await getTaskRunLog(namespace, taskRunName);
// if (!disposed) {
// setResult([log, true, undefined]);
// }
// } catch (e) {
// if (!disposed) {
// setResult([null, false, e]);
// }
// }
// })();
// }
// return () => {
// disposed = true;
// };
// }, [namespace, taskRunName]);
// return result;
// };

0 comments on commit 3c42594

Please sign in to comment.