Skip to content

Commit

Permalink
Merge pull request #5254 from rohitkrai03/helm-redirects
Browse files Browse the repository at this point in the history
Bug 1830046: Helm redirects based on the origin of the action
  • Loading branch information
openshift-merge-robot committed May 6, 2020
2 parents 2e80055 + a0847b0 commit d0e9c45
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 32 deletions.
22 changes: 17 additions & 5 deletions frontend/packages/dev-console/src/actions/modify-helm-release.ts
Expand Up @@ -20,16 +20,28 @@ export const deleteHelmRelease = (releaseName: string, namespace: string, redire
};
};

export const upgradeHelmRelease = (releaseName: string, namespace: string) => ({
label: 'Upgrade Helm Release',
export const upgradeHelmRelease = (
releaseName: string,
namespace: string,
actionOrigin: string,
) => ({
label: 'Upgrade',
callback: () => {
history.push(`/helm-releases/ns/${namespace}/${releaseName}/upgrade`);
history.push(
`/helm-releases/ns/${namespace}/${releaseName}/upgrade?actionOrigin=${actionOrigin}`,
);
},
});

export const rollbackHelmRelease = (releaseName: string, namespace: string) => ({
export const rollbackHelmRelease = (
releaseName: string,
namespace: string,
actionOrigin: string,
) => ({
label: 'Rollback',
callback: () => {
history.push(`/helm-releases/ns/${namespace}/${releaseName}/rollback`);
history.push(
`/helm-releases/ns/${namespace}/${releaseName}/rollback?actionOrigin=${actionOrigin}`,
);
},
});
Expand Up @@ -10,7 +10,13 @@ import { PageBody } from '@console/shared';
import { SecretModel } from '@console/internal/models';
import { k8sGet } from '@console/internal/module/k8s';

import { HelmActionType, HelmChart, HelmRelease, HelmActionConfigType } from './helm-types';
import {
HelmActionType,
HelmChart,
HelmRelease,
HelmActionConfigType,
HelmActionOrigins,
} from './helm-types';
import { getHelmActionValidationSchema } from './helm-validation-utils';
import { getHelmActionConfig, getChartValuesYAML } from './helm-utils';
import NamespacedPage, { NamespacedPageVariants } from '../NamespacedPage';
Expand Down Expand Up @@ -39,17 +45,20 @@ const HelmInstallUpgradePage: React.FunctionComponent<HelmInstallUpgradePageProp
const namespace = match.params.ns || searchParams.get('preselected-ns');
const releaseName = match.params.releaseName || '';
const helmChartName = searchParams.get('chartName');
const helmActionOrigin = searchParams.get('actionOrigin') as HelmActionOrigins;

const [chartDataLoaded, setChartDataLoaded] = React.useState<boolean>(false);
const [chartName, setChartName] = React.useState<string>('');
const [chartHasValues, setChartHasValues] = React.useState<boolean>(false);
const [YAMLData, setYAMLData] = React.useState<string>('');
const [activeChartVersion, setActiveChartVersion] = React.useState<string>('');

const helmAction: HelmActionType =
chartURL !== 'null' ? HelmActionType.Install : HelmActionType.Upgrade;

const config = React.useMemo<HelmActionConfigType>(
() => getHelmActionConfig(helmAction, releaseName, namespace, chartURL),
[chartURL, helmAction, namespace, releaseName],
() => getHelmActionConfig(helmAction, releaseName, namespace, helmActionOrigin, chartURL),
[chartURL, helmAction, helmActionOrigin, namespace, releaseName],
);

React.useEffect(() => {
Expand Down Expand Up @@ -117,12 +126,15 @@ const HelmInstallUpgradePage: React.FunctionComponent<HelmInstallUpgradePageProp
...(valuesObj ? { values: valuesObj } : {}),
};

const isGoingToTopology =
helmAction === HelmActionType.Install || helmActionOrigin === HelmActionOrigins.topology;

config
.fetch('/api/helm/release', payload)
.then(async (res: HelmRelease) => {
let redirect = config.redirectURL;

if (helmAction === HelmActionType.Install && res?.info?.notes) {
if (isGoingToTopology && res?.info?.notes) {
const options = {
queryParams: { labelSelector: `name=${res.name},version=${res.version},owner=helm` },
};
Expand Down
Expand Up @@ -4,9 +4,10 @@ import { Formik } from 'formik';
import { RouteComponentProps } from 'react-router';
import { PageBody } from '@console/shared';
import { coFetchJSON } from '@console/internal/co-fetch';
import { PageHeading, history } from '@console/internal/components/utils';
import { PageHeading, history, getQueryArgument } from '@console/internal/components/utils';

import { HelmRelease } from './helm-types';
import { HelmRelease, HelmActionType, HelmActionOrigins } from './helm-types';
import { getHelmActionConfig } from './helm-utils';
import NamespacedPage, { NamespacedPageVariants } from '../NamespacedPage';
import HelmReleaseRollbackForm from './form/HelmReleaseRollbackForm';

Expand All @@ -21,15 +22,21 @@ type HelmRollbackFormData = {

const HelmReleaseRollbackPage: React.FC<HelmReleaseRollbackPageProps> = ({ match }) => {
const { releaseName, ns: namespace } = match.params;
const actionOrigin = getQueryArgument('actionOrigin') as HelmActionOrigins;
const [releaseHistory, setReleaseHistory] = React.useState<HelmRelease[]>(null);

const config = React.useMemo(
() => getHelmActionConfig(HelmActionType.Rollback, releaseName, namespace, actionOrigin),
[actionOrigin, namespace, releaseName],
);

React.useEffect(() => {
let ignore = false;

const fetchReleaseHistory = async () => {
let res: HelmRelease[];
try {
res = await coFetchJSON(`/api/helm/release/history?ns=${namespace}&name=${releaseName}`);
res = await coFetchJSON(config.helmReleaseApi);
} catch {} // eslint-disable-line no-empty
if (ignore) return;

Expand All @@ -41,7 +48,7 @@ const HelmReleaseRollbackPage: React.FC<HelmReleaseRollbackPageProps> = ({ match
return () => {
ignore = true;
};
}, [namespace, releaseName]);
}, [config.helmReleaseApi]);

const initialValues: HelmRollbackFormData = {
revision: -1,
Expand All @@ -55,11 +62,11 @@ const HelmReleaseRollbackPage: React.FC<HelmReleaseRollbackPageProps> = ({ match
version: values.revision,
};

coFetchJSON
.patch('/api/helm/release', payload)
config
.fetch('/api/helm/release', payload)
.then(() => {
actions.setStatus({ isSubmitting: false });
history.push(`/helm-releases/ns/${namespace}`);
history.push(config.redirectURL);
})
.catch((err) => {
actions.setSubmitting(false);
Expand All @@ -70,9 +77,9 @@ const HelmReleaseRollbackPage: React.FC<HelmReleaseRollbackPageProps> = ({ match
return (
<NamespacedPage variant={NamespacedPageVariants.light} disabled hideApplications>
<Helmet>
<title>Rollback Helm Release</title>
<title>{config.title}</title>
</Helmet>
<PageHeading title="Rollback Helm Release">
<PageHeading title={config.title}>
Select the version to rollback <strong>{releaseName}</strong> to, from the table below:
</PageHeading>
<PageBody>
Expand Down
Expand Up @@ -23,7 +23,7 @@ import {
rollbackHelmRelease,
} from '../../../actions/modify-helm-release';
import HelmReleaseNotes from './HelmReleaseNotes';
import { HelmRelease } from '../helm-types';
import { HelmRelease, HelmActionOrigins } from '../helm-types';

const SecretReference: K8sResourceKindReference = 'Secret';
const HelmReleaseReference = 'HelmRelease';
Expand Down Expand Up @@ -77,8 +77,8 @@ export const LoadedHelmReleaseDetails: React.FC<LoadedHelmReleaseDetailsProps> =
);

const menuActions = [
() => rollbackHelmRelease(releaseName, namespace),
() => upgradeHelmRelease(releaseName, namespace),
() => upgradeHelmRelease(releaseName, namespace, HelmActionOrigins.details),
() => rollbackHelmRelease(releaseName, namespace, HelmActionOrigins.details),
() => deleteHelmRelease(releaseName, namespace, `/helm-releases/ns/${namespace}`),
];

Expand Down
Expand Up @@ -63,6 +63,7 @@ export enum HelmReleaseStatus {
export enum HelmActionType {
Install = 'Install',
Upgrade = 'Upgrade',
Rollback = 'Rollback',
}

export interface HelmActionConfigType {
Expand All @@ -72,3 +73,9 @@ export interface HelmActionConfigType {
fetch: (url: any, json: any, options?: {}) => Promise<any>;
redirectURL: string;
}

export enum HelmActionOrigins {
details = 'details',
list = 'list',
topology = 'topology',
}
37 changes: 33 additions & 4 deletions frontend/packages/dev-console/src/components/helm/helm-utils.ts
Expand Up @@ -10,6 +10,7 @@ import {
HelmChartMetaData,
HelmActionType,
HelmActionConfigType,
HelmActionOrigins,
} from './helm-types';
import { CustomResourceListRowFilter } from '../custom-resource-list/custom-resource-list-types';

Expand Down Expand Up @@ -93,11 +94,29 @@ export const getChartVersions = (chartEntries: HelmChartMetaData[]) => {
return chartVersions;
};

export const getOriginRedirectURL = (
actionOrigin: string,
namespace: string,
releaseName?: string,
) => {
switch (actionOrigin) {
case HelmActionOrigins.topology:
return `/topology/ns/${namespace}/graph`;
case HelmActionOrigins.list:
return `/helm-releases/ns/${namespace}`;
case HelmActionOrigins.details:
return `/helm-releases/ns/${namespace}/release/${releaseName}`;
default:
return `/helm-releases/ns/${namespace}`;
}
};

export const getHelmActionConfig = (
helmAction: HelmActionType,
releaseName: string,
namespace: string,
chartURL: string,
actionOrigin?: HelmActionOrigins,
chartURL?: string,
): HelmActionConfigType | undefined => {
switch (helmAction) {
case HelmActionType.Install:
Expand All @@ -106,15 +125,25 @@ export const getHelmActionConfig = (
subTitle: 'The helm chart will be installed using the YAML shown in the editor below.',
helmReleaseApi: `/api/helm/chart?url=${chartURL}`,
fetch: coFetchJSON.post,
redirectURL: `/topology/ns/${namespace}/graph`,
redirectURL: getOriginRedirectURL(HelmActionOrigins.topology, namespace, releaseName),
};
case HelmActionType.Upgrade:
return {
title: 'Upgrade Helm Release',
subTitle: '',
subTitle:
'Upgrade by selecting a new chart version or manually changing the YAML shown in the editor below.',
helmReleaseApi: `/api/helm/release?ns=${namespace}&release_name=${releaseName}`,
fetch: coFetchJSON.put,
redirectURL: `/helm-releases/ns/${namespace}`,
redirectURL: getOriginRedirectURL(actionOrigin, namespace, releaseName),
};

case HelmActionType.Rollback:
return {
title: 'Rollback Helm Release',
subTitle: ``,
helmReleaseApi: `/api/helm/release/history?ns=${namespace}&name=${releaseName}`,
fetch: coFetchJSON.patch,
redirectURL: getOriginRedirectURL(actionOrigin, namespace, releaseName),
};
default:
return undefined;
Expand Down
Expand Up @@ -4,7 +4,7 @@ import { Status } from '@console/shared';
import { TableRow, TableData, RowFunction } from '@console/internal/components/factory';
import { Timestamp, Kebab, ResourceIcon } from '@console/internal/components/utils';
import { Link } from 'react-router-dom';
import { HelmRelease } from '../helm-types';
import { HelmRelease, HelmActionOrigins } from '../helm-types';
import { tableColumnClasses } from './HelmReleaseListHeader';
import {
deleteHelmRelease,
Expand All @@ -14,8 +14,8 @@ import {

const HelmReleaseListRow: RowFunction<HelmRelease> = ({ obj, index, key, style }) => {
const menuActions = [
rollbackHelmRelease(obj.name, obj.namespace),
upgradeHelmRelease(obj.name, obj.namespace),
upgradeHelmRelease(obj.name, obj.namespace, HelmActionOrigins.list),
rollbackHelmRelease(obj.name, obj.namespace, HelmActionOrigins.list),
deleteHelmRelease(obj.name, obj.namespace),
];
return (
Expand Down
@@ -1,21 +1,20 @@
import { KebabOption } from '@console/internal/components/utils/kebab';
import { Node } from '@console/topology';
import { regroupActions } from './regroupActions';
import {
deleteHelmRelease,
upgradeHelmRelease,
rollbackHelmRelease,
} from '../../../actions/modify-helm-release';
import { HelmActionOrigins } from '../../helm/helm-types';

export const helmReleaseActions = (helmRelease: Node): KebabOption[] => {
const name = helmRelease.getLabel();
const { namespace } = helmRelease.getData().groupResources[0].resources.obj.metadata;
return name && namespace
? [
rollbackHelmRelease(name, namespace),
upgradeHelmRelease(name, namespace),
upgradeHelmRelease(name, namespace, HelmActionOrigins.topology),
rollbackHelmRelease(name, namespace, HelmActionOrigins.topology),
deleteHelmRelease(name, namespace),
...regroupActions(helmRelease),
]
: [];
};

0 comments on commit d0e9c45

Please sign in to comment.