Skip to content

Commit

Permalink
Adds Pipelines back to Topology
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewballantyne committed Jul 15, 2020
1 parent 238dce6 commit 7cc7a12
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createOverviewItemsForType } from '@console/shared';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { getImageForIconClass } from '@console/internal/components/catalog/catalog-item-icon';
import { Model, EdgeModel } from '@console/topology';
import { getPipelinesAndPipelineRunsForResource } from '../../../utils/pipeline-plugin-utils';
import {
TopologyDataResources,
TrafficData,
Expand Down Expand Up @@ -71,8 +72,11 @@ const getBaseTopologyDataModel = (resources: TopologyDataResources): Model => {

createOverviewItemsForType(key, resources).forEach((item) => {
const { obj: deploymentConfig } = item;

const pipelineData = getPipelinesAndPipelineRunsForResource(deploymentConfig, resources);

const data = createTopologyNodeData(
item,
{ ...item, ...pipelineData },
TYPE_WORKLOAD,
getImageForIconClass(`icon-openshift`),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TYPE_KNATIVE_REVISION,
} from '@console/knative-plugin/src/topology/const';
import { edgesFromAnnotations } from '../../../utils/application-utils';
import { tknPipelineAndPipelineRunsWatchResources } from '../../../utils/pipeline-plugin-utils';
import {
TopologyDataObject,
TopologyOverviewItem,
Expand Down Expand Up @@ -407,5 +408,6 @@ export const getBaseWatchedResources = (namespace: string): WatchK8sResources<an
namespace,
optional: true,
},
...tknPipelineAndPipelineRunsWatchResources(namespace),
};
};
43 changes: 30 additions & 13 deletions frontend/packages/dev-console/src/utils/pipeline-plugin-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import * as _ from 'lodash';
import { referenceForModel, K8sResourceKind } from '@console/internal/module/k8s';
import { FirehoseResource } from '@console/internal/components/utils';
import { WatchK8sResources } from '@console/internal/components/utils/k8s-watch-hook';
import { PipelineRunModel, PipelineModel } from '../models';
import { Pipeline, PipelineRun } from './pipeline-augment';

// label to get the pipelines
export const INSTANCE_LABEL = 'app.kubernetes.io/instance';

export const tknPipelineAndPipelineRunsWatchResources = (
namespace: string,
): WatchK8sResources<any> => {
return {
pipelines: {
isList: true,
kind: referenceForModel(PipelineModel),
namespace,
optional: true,
},
pipelineRuns: {
isList: true,
kind: referenceForModel(PipelineRunModel),
namespace,
optional: true,
},
};
};

export const tknPipelineAndPipelineRunsResources = (namespace: string): FirehoseResource[] => {
const resources = [
{
Expand All @@ -28,13 +47,13 @@ export const tknPipelineAndPipelineRunsResources = (namespace: string): Firehose
};

type PipelineItem = {
pipelines: K8sResourceKind[];
pipelineRuns: K8sResourceKind[];
pipelines: Pipeline[];
pipelineRuns: PipelineRun[];
};

const byCreationTime = (left: K8sResourceKind, right: K8sResourceKind): number => {
const leftCreationTime = new Date(_.get(left, ['metadata', 'creationTimestamp'], Date.now()));
const rightCreationTime = new Date(_.get(right, ['metadata', 'creationTimestamp'], Date.now()));
const leftCreationTime = new Date(left?.metadata?.creationTimestamp || Date.now());
const rightCreationTime = new Date(right?.metadata?.creationTimestamp || Date.now());
return rightCreationTime.getTime() - leftCreationTime.getTime();
};

Expand All @@ -46,9 +65,7 @@ const getPipelineRunsForPipeline = (pipeline: Pipeline, props): PipelineRun[] =>
return pipelineRunsData
.filter((pr: PipelineRun) => {
return (
pipelineName ===
(_.get(pr, ['spec', 'pipelineRef', 'name'], null) ||
_.get(pr, ['metadata', 'labels', PIPELINE_RUN_LABEL], null))
pipelineName === (pr.spec?.pipelineRef?.name || pr?.metadata?.labels?.[PIPELINE_RUN_LABEL])
);
})
.sort(byCreationTime);
Expand All @@ -58,12 +75,12 @@ export const getPipelinesAndPipelineRunsForResource = (
resource: K8sResourceKind,
props,
): PipelineItem => {
if (!_.has(props, ['pipelines', 'data'])) return null;
const pipelinesData = props.pipelines.data;
const resourceIntanceName = _.get(resource, ['metadata', 'labels', INSTANCE_LABEL], null);
if (!resourceIntanceName) return null;
const pipelinesData = props?.pipelines?.data;
if (!pipelinesData) return null;
const resourceInstanceName = resource?.metadata?.labels?.[INSTANCE_LABEL] || null;
if (!resourceInstanceName) return null;
const resourcePipeline = pipelinesData.find(
(pl) => _.get(pl, ['metadata', 'labels', INSTANCE_LABEL], null) === resourceIntanceName,
(pl) => pl?.metadata?.labels?.[INSTANCE_LABEL] === resourceInstanceName,
);
if (!resourcePipeline) return null;
return {
Expand Down

0 comments on commit 7cc7a12

Please sign in to comment.