Skip to content

Commit

Permalink
Convert Pipeline StartedBy Label to Annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewballantyne committed Sep 15, 2020
1 parent 0a073c6 commit 314a5ab
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 37 deletions.
Expand Up @@ -5,12 +5,14 @@ import { pipelineRunStatus } from '../../utils/pipeline-filter-reducer';
import { getPipelineRunKebabActions } from '../../utils/pipeline-actions';
import { PipelineRunDetails } from './detail-page-tabs/PipelineRunDetails';
import { PipelineRunLogsWithActiveTask } from './detail-page-tabs/PipelineRunLogs';
import { useMenuActionsWithUserLabel } from './triggered-by';
import { useMenuActionsWithUserAnnotation } from './triggered-by';
import { usePipelinesBreadcrumbsFor } from '../pipelines/hooks';

const PipelineRunDetailsPage: React.FC<DetailsPageProps> = (props) => {
const { kindObj, match } = props;
const menuActions: KebabAction[] = useMenuActionsWithUserLabel(getPipelineRunKebabActions(true));
const menuActions: KebabAction[] = useMenuActionsWithUserAnnotation(
getPipelineRunKebabActions(true),
);
const breadcrumbsFor = usePipelinesBreadcrumbsFor(kindObj, match);

return (
Expand Down
Expand Up @@ -3,7 +3,7 @@ import { ResourceLink } from '@console/internal/components/utils';
import { referenceForModel } from '@console/internal/module/k8s';
import { EventListenerModel } from '../../../models';
import { PipelineRun } from '../../../utils/pipeline-augment';
import { StartedByLabel } from '../../pipelines/const';
import { StartedByAnnotation, StartedByLabel } from '../../pipelines/const';

type TriggeredByProps = {
pipelineRun: PipelineRun;
Expand All @@ -12,11 +12,11 @@ type TriggeredByProps = {
const TriggeredBySection: React.FC<TriggeredByProps> = (props) => {
const {
pipelineRun: {
metadata: { namespace, labels },
metadata: { annotations, namespace, labels },
},
} = props;

const manualTrigger = labels[StartedByLabel.user];
const manualTrigger = annotations[StartedByAnnotation.user];
const autoTrigger = labels[StartedByLabel.triggers];

if (!manualTrigger && !autoTrigger) {
Expand Down
@@ -1,12 +1,12 @@
import * as React from 'react';
import { KebabAction, ResourceKebab } from '@console/internal/components/utils';
import { useMenuActionsWithUserLabel } from './hooks';
import { useMenuActionsWithUserAnnotation } from './hooks';

const ResourceKebabWithUserLabel: React.FC<React.ComponentProps<typeof ResourceKebab>> = ({
actions,
...otherProps
}) => {
const augmentedMenuActions: KebabAction[] = useMenuActionsWithUserLabel(actions);
const augmentedMenuActions: KebabAction[] = useMenuActionsWithUserAnnotation(actions);

return <ResourceKebab {...otherProps} actions={augmentedMenuActions} />;
};
Expand Down
Expand Up @@ -6,15 +6,15 @@ import { useSelector } from 'react-redux';
import { KebabAction, Kebab } from '@console/internal/components/utils';
import { K8sResourceCommon } from '@console/internal/module/k8s';
import { PipelineRun } from '../../../utils/pipeline-augment';
import { StartedByLabel } from '../../pipelines/const';
import { StartedByAnnotation } from '../../pipelines/const';

type LabelMap = { [labelKey: string]: string };
type AnnotationMap = { [annotationKey: string]: string };

const mergeLabelsWithResource = (labels: LabelMap, resource: K8sResourceCommon) => {
return merge({}, resource, { metadata: { labels } });
const mergeAnnotationsWithResource = (annotations: AnnotationMap, resource: K8sResourceCommon) => {
return merge({}, resource, { metadata: { annotations } });
};

export const useUserLabelForManualStart = (): LabelMap => {
export const useUserAnnotationForManualStart = (): AnnotationMap => {
const user = useSelector((state) => state.UI.get('user'));

if (!user?.metadata?.name) {
Expand All @@ -23,24 +23,24 @@ export const useUserLabelForManualStart = (): LabelMap => {

return {
// kube:admin is an invalid k8s label value
[StartedByLabel.user]: user.metadata.name.replace(/:/, ''),
[StartedByAnnotation.user]: user.metadata.name,
};
};

export const usePipelineRunWithUserLabel = (plr: PipelineRun): PipelineRun => {
const labels = useUserLabelForManualStart();
export const usePipelineRunWithUserAnnotation = (plr: PipelineRun): PipelineRun => {
const annotations = useUserAnnotationForManualStart();

return plr && mergeLabelsWithResource(labels, plr);
return plr && mergeAnnotationsWithResource(annotations, plr);
};

export const useMenuActionsWithUserLabel = (menuActions: KebabAction[]): KebabAction[] => {
const labels = useUserLabelForManualStart();
export const useMenuActionsWithUserAnnotation = (menuActions: KebabAction[]): KebabAction[] => {
const annotations = useUserAnnotationForManualStart();

return menuActions.map((kebabAction) => {
if (Object.values(Kebab.factory).includes(kebabAction)) {
return kebabAction;
}
return (kind, resource, ...rest) =>
kebabAction(kind, mergeLabelsWithResource(labels, resource), ...rest);
kebabAction(kind, mergeAnnotationsWithResource(annotations, resource), ...rest);
});
};
Expand Up @@ -6,7 +6,7 @@ import { ErrorPage404 } from '@console/internal/components/error';
import { getPipelineKebabActions } from '../../utils/pipeline-actions';
import { getLatestRun, PipelineRun } from '../../utils/pipeline-augment';
import { PipelineRunModel, PipelineModel } from '../../models';
import { useMenuActionsWithUserLabel } from '../pipelineruns/triggered-by';
import { useMenuActionsWithUserAnnotation } from '../pipelineruns/triggered-by';
import {
PipelineDetails,
PipelineForm,
Expand Down Expand Up @@ -45,7 +45,7 @@ const PipelineDetailsPage: React.FC<DetailsPageProps> = (props) => {
.catch((error) => setErrorCode(error.response.status));
}, [name, namespace]);

const augmentedMenuActions: KebabAction[] = useMenuActionsWithUserLabel(
const augmentedMenuActions: KebabAction[] = useMenuActionsWithUserAnnotation(
getPipelineKebabActions(latestPipelineRun, templateNames.length > 0),
);

Expand Down
@@ -1,7 +1,9 @@
export enum StartedByLabel {
user = 'pipeline.openshift.io/started-by',
triggers = 'triggers.tekton.dev/eventlistener',
}
export enum StartedByAnnotation {
user = 'pipeline.openshift.io/started-by',
}

export enum PipelineResourceType {
git = 'git',
Expand Down
Expand Up @@ -124,7 +124,7 @@ describe('PipelineAction testing getPipelineRunFromForm', () => {
anotherlabel: 'another-label-value',
};

const runData = getPipelineRunFromForm(actionPipelines[0], formValues, labels, {
const runData = getPipelineRunFromForm(actionPipelines[0], formValues, labels, null, {
generateName: true,
});
expect(runData).toEqual({
Expand Down
Expand Up @@ -70,6 +70,7 @@ export const getPipelineRunData = (
: {
name: `${pipelineName}-${getRandomChars()}`,
}),
annotations: _.merge({}, pipeline?.metadata?.annotations, latestRun?.metadata?.annotations),
namespace: pipeline ? pipeline.metadata.namespace : latestRun.metadata.namespace,
labels: _.merge({}, pipeline?.metadata?.labels, latestRun?.metadata?.labels, {
'tekton.dev/pipeline': pipelineName,
Expand Down Expand Up @@ -144,12 +145,14 @@ export const getPipelineRunFromForm = (
pipeline: Pipeline,
formValues: CommonPipelineModalFormikValues,
labels?: { [key: string]: string },
annotations?: { [key: string]: string },
options?: { generateName: boolean },
) => {
const { parameters, resources, workspaces } = formValues;

const pipelineRunData: PipelineRun = {
metadata: {
annotations,
labels,
},
spec: {
Expand Down
Expand Up @@ -6,7 +6,7 @@ import {
} from '@console/internal/components/factory/modal';
import { errorModal } from '@console/internal/components/modals';
import { Pipeline, PipelineRun, PipelineWorkspace } from '../../../../utils/pipeline-augment';
import { useUserLabelForManualStart } from '../../../pipelineruns/triggered-by';
import { useUserAnnotationForManualStart } from '../../../pipelineruns/triggered-by';
import ModalStructure from '../common/ModalStructure';
import { convertPipelineToModalData } from '../common/utils';
import { startPipelineSchema } from '../common/validation-utils';
Expand All @@ -23,7 +23,7 @@ const StartPipelineModal: React.FC<StartPipelineModalProps & ModalComponentProps
close,
onSubmit,
}) => {
const userStartedLabel = useUserLabelForManualStart();
const userStartedLabel = useUserAnnotationForManualStart();

const initialValues: StartPipelineFormValues = {
...convertPipelineToModalData(pipeline),
Expand Down
Expand Up @@ -28,7 +28,8 @@ export const resourceSubmit = async (
export const submitStartPipeline = async (
values: StartPipelineFormValues,
pipeline: Pipeline,
labels: { [key: string]: string },
labels?: { [key: string]: string },
annotations?: { [key: string]: string },
): Promise<PipelineRun> => {
const { namespace, resources } = values;

Expand Down Expand Up @@ -64,7 +65,7 @@ export const submitStartPipeline = async (

const pipelineRunResource: PipelineRun = await k8sCreate(
PipelineRunModel,
getPipelineRunFromForm(pipeline, formValues, labels),
getPipelineRunFromForm(pipeline, formValues, labels, annotations),
);

return Promise.resolve(pipelineRunResource);
Expand Down
Expand Up @@ -57,12 +57,9 @@ export const submitTrigger = async (
const { triggerBinding } = formValues;
const thisNamespace = pipeline.metadata.namespace;

const pipelineRun: PipelineRun = getPipelineRunFromForm(
pipeline,
formValues,
{},
{ generateName: true },
);
const pipelineRun: PipelineRun = getPipelineRunFromForm(pipeline, formValues, null, null, {
generateName: true,
});
const triggerTemplateParams: TriggerTemplateKindParam[] = triggerBinding.resource.spec.params.map(
({ name }) => ({ name } as TriggerTemplateKindParam),
);
Expand Down
Expand Up @@ -7,7 +7,7 @@ import { useAccessReview } from '@console/internal/components/utils';
import { AccessReviewResourceAttributes } from '@console/internal/module/k8s';
import { rerunPipelineAndStay } from '../../../utils/pipeline-actions';
import { PipelineRunModel } from '../../../models';
import { usePipelineRunWithUserLabel } from '../../pipelineruns/triggered-by';
import { usePipelineRunWithUserAnnotation } from '../../pipelineruns/triggered-by';
import { getLatestRun, PipelineRun } from '../../../utils/pipeline-augment';

type TriggerLastRunButtonProps = {
Expand All @@ -21,7 +21,7 @@ const TriggerLastRunButton: React.FC<TriggerLastRunButtonProps> = ({
namespace,
impersonate,
}) => {
const latestRun = usePipelineRunWithUserLabel(
const latestRun = usePipelineRunWithUserAnnotation(
getLatestRun({ data: pipelineRuns }, 'startTimestamp'),
);
const { label, callback, accessReview: utilityAccessReview } = rerunPipelineAndStay(
Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/dev-console/src/utils/pipeline-actions.tsx
Expand Up @@ -13,7 +13,7 @@ import {
removeTriggerModal,
} from '../components/pipelines/modals';
import { getPipelineRunData } from '../components/pipelines/modals/common/utils';
import { StartedByLabel } from '../components/pipelines/const';
import { StartedByAnnotation } from '../components/pipelines/const';
import { EventListenerModel, PipelineModel, PipelineRunModel } from '../models';
import { Pipeline, PipelineRun } from './pipeline-augment';
import { pipelineRunFilterReducer } from './pipeline-filter-reducer';
Expand Down Expand Up @@ -201,7 +201,7 @@ const addTrigger: KebabAction = (kind: K8sKind, pipeline: Pipeline) => ({
...pipeline,
metadata: {
...pipeline.metadata,
labels: _.omit(pipeline.metadata.labels, [StartedByLabel.user]),
annotations: _.omit(pipeline.metadata.annotations, [StartedByAnnotation.user]),
},
};
addTriggerModal({ pipeline: cleanPipeline, modalClassName: 'modal-lg' });
Expand Down

0 comments on commit 314a5ab

Please sign in to comment.