Skip to content

Commit

Permalink
enable edit for kn svc created via cli and edit route option
Browse files Browse the repository at this point in the history
  • Loading branch information
invincibleJai committed Mar 11, 2020
1 parent 6e7c07a commit 367b474
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { DeploymentModel } from '@console/internal/models';
import { ServiceModel } from '@console/knative-plugin';
import { knativeServiceObj } from '@console/dev-console/src/components/topology/__tests__/topology-knative-test-data';
import { sampleDeployments } from '@console/dev-console/src/components/topology/__tests__/topology-test-data';
import { EditApplication } from '../modify-application';

describe('modify application edit flow', () => {
it('expect EditApplication to return hidden false for knative service without annotations', () => {
const editAppData = EditApplication(ServiceModel, knativeServiceObj);
expect(editAppData.hidden).toBe(false);
});

it('expect EditApplication to return hidden false for knative service with annotations', () => {
const knativeServiceObjWithAnnoations = {
...knativeServiceObj,
metadata: {
...knativeServiceObj.metadata,
annotations: {
'openshift.io/generated-by': 'OpenShiftWebConsole',
},
},
};
const editAppData = EditApplication(ServiceModel, knativeServiceObjWithAnnoations);
expect(editAppData.hidden).toBe(false);
});

it('expect EditApplication to return hidden true for Deployments without annotations', () => {
const editAppData = EditApplication(DeploymentModel, sampleDeployments.data[0]);
expect(editAppData.hidden).toBe(true);
});

it('expect EditApplication to return hidden false for Deployments with annotations', () => {
const sampleDeploymentWithAnnoations = {
...sampleDeployments.data[0],
metadata: {
...sampleDeployments.data[0].metadata,
annotations: {
'openshift.io/generated-by': 'OpenShiftWebConsole',
},
},
};
const editAppData = EditApplication(DeploymentModel, sampleDeploymentWithAnnoations);
expect(editAppData.hidden).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { KebabOption } from '@console/internal/components/utils';
import { truncateMiddle } from '@console/internal/components/utils/truncate-middle';
import { K8sResourceKind, K8sKind } from '@console/internal/module/k8s';
import { ServiceModel } from '@console/knative-plugin';
import { RESOURCE_NAME_TRUNCATE_LENGTH } from '../const';
import { editApplicationModal } from '../components/modals';

Expand Down Expand Up @@ -28,7 +29,7 @@ export const EditApplication = (model: K8sKind, obj: K8sResourceKind): KebabOpti
const annotation = obj?.metadata?.annotations?.['openshift.io/generated-by'];
return {
label: `Edit ${truncateMiddle(obj.metadata.name, { length: RESOURCE_NAME_TRUNCATE_LENGTH })}`,
hidden: annotation !== 'OpenShiftWebConsole',
hidden: obj.kind !== ServiceModel.kind && annotation !== 'OpenShiftWebConsole',
href: `/edit/ns/${obj.metadata.namespace}?name=${obj.metadata.name}&kind=${obj.kind ||
model.kind}`,
accessReview: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,81 @@ export const internalImageValues: DeployImageFormData = {
build: { env: [], triggers: {}, strategy: '' },
isSearchingForImage: false,
};

export const knAppResources: AppResources = {
editAppResource: {
loaded: true,
loadError: '',
data: knativeService,
},
route: {
loaded: false,
loadError: 'routes.route.openshift.io "greeter" not found',
data: {},
},
buildConfig: {
loaded: false,
loadError: 'Error: buildconfigs.build.openshift.io "greeter" not found',
data: {},
},
imageStream: {
loaded: true,
loadError: '',
data: [],
},
};

export const knExternalImageValues: DeployImageFormData = {
application: { name: '', selectedKey: '#UNASSIGNED_KEY#' },
build: { env: [], strategy: '', triggers: {} },
deployment: { env: [], replicas: 1, triggers: { image: false } },
formType: 'edit',
image: { image: {}, name: '', ports: [], status: { metadata: {}, status: '' }, tag: '' },
imageStream: { grantAccess: true, image: '', namespace: '', tag: '' },
isSearchingForImage: false,
isi: { image: {}, name: '', ports: [], status: { metadata: {}, status: '' }, tag: '' },
labels: {},
limits: {
cpu: {
defaultLimitUnit: '',
defaultRequestUnit: '',
limit: '',
limitUnit: '',
request: '',
requestUnit: '',
},
memory: {
defaultLimitUnit: 'Mi',
defaultRequestUnit: 'Mi',
limit: '',
limitUnit: 'Mi',
request: '',
requestUnit: 'Mi',
},
},
name: 'nationalparks-py',
pipeline: { enabled: false },
project: { name: 'div' },
registry: 'external',
resources: Resources.KnativeService,
route: {
create: true,
defaultUnknownPort: 8080,
disable: true,
hostname: '',
path: '',
secure: false,
targetPort: '',
tls: {
caCertificate: '',
certificate: '',
destinationCACertificate: '',
insecureEdgeTerminationPolicy: '',
privateKey: '',
termination: '',
},
unknownTargetPort: '',
},
searchTerm: 'openshift/hello-openshift',
serverless: { scaling: { concurrencylimit: '', concurrencytarget: '', maxpods: '', minpods: 0 } },
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import * as _ from 'lodash';
import { BuildStrategyType } from '@console/internal/components/build';
import {
getResourcesType,
getPageHeading,
CreateApplicationFlow,
getInitialValues,
getExternalImagelValues,
} from '../edit-application-utils';
import { Resources } from '../../import/import-types';
import {
knativeService,
knAppResources,
knExternalImageValues,
appResources,
gitImportInitialValues,
externalImageValues,
Expand Down Expand Up @@ -35,4 +39,16 @@ describe('Edit Application Utils', () => {
internalImageValues,
);
});

it('getExternalImagelValues should return image name in search term', () => {
const externalImageData = getExternalImagelValues(knativeService);
expect(_.get(externalImageData, 'searchTerm')).toEqual('openshift/hello-openshift');
});

it('getInitialValues should return values externalImageValues on the resources', () => {
const { route, editAppResource, imageStream } = knAppResources;
expect(
getInitialValues({ editAppResource, route, imageStream }, 'nationalparks-py', 'div'),
).toEqual(knExternalImageValues);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const getRouteData = (route: K8sResourceKind, resource: K8sResourceKind)
_.get(resource, 'metadata.labels["serving.knative.dev/visibility"]', '') !==
'cluster-local',
unknownTargetPort: _.toString(port),
targetPort: _.toString(port),
};
}
return routeData;
Expand Down Expand Up @@ -314,11 +315,13 @@ const deployImageInitialValues = {
isSearchingForImage: false,
};

export const getExternalImageInitialValues = (imageStream: K8sResourceKind) => {
if (_.isEmpty(imageStream)) {
export const getExternalImageInitialValues = (appResources: AppResources) => {
const imageStreamList = appResources?.imageStream?.data;
if (_.isEmpty(imageStreamList)) {
return {};
}
const name = _.get(imageStream, 'spec.tags[0].from.name');
const imageStream = _.orderBy(imageStreamList, ['metadata.resourceVersion'], ['desc']);
const name = imageStream.length && imageStream[0]?.spec?.tags?.[0]?.from?.name;
return {
...deployImageInitialValues,
searchTerm: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,16 @@ export const createOrUpdateDeployImageResources = async (
let imageStreamUrl: string = image?.dockerImageReference;
if (registry === RegistryType.External) {
let generatedImageStreamName: string = '';
if (imageStreamList && imageStreamList.length && verb === 'update') {
const originalImageStreamTag = _.find(originalImageStream?.status?.tags, [
'tag',
imageStreamTag,
]);
if (!_.isEmpty(originalImageStreamTag)) {
if (verb === 'update') {
if (imageStreamList && imageStreamList.length) {
const originalImageStreamTag = _.find(originalImageStream?.status?.tags, [
'tag',
imageStreamTag,
]);
if (!_.isEmpty(originalImageStreamTag)) {
generatedImageStreamName = `${name}-${getRandomChars()}`;
}
} else {
generatedImageStreamName = `${name}-${getRandomChars()}`;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useFormikContext, FormikValues } from 'formik';
import { InputField, DropdownField } from '@console/shared';
import FormSection from '../section/FormSection';
import { RouteData } from '../import-types';
import { makePortName } from '../../../utils/imagestream-utils';

export interface ServerlessRouteSectionProps {
route: RouteData;
Expand All @@ -15,12 +14,15 @@ const ServerlessRouteSection: React.FC<ServerlessRouteSectionProps> = ({ route }
const {
values: {
image: { ports },
route: { defaultUnknownPort, targetPort },
route: { defaultUnknownPort, targetPort: routeTargetPort },
},
} = useFormikContext<FormikValues>();
const targetPort = routeTargetPort.split('-')[0];
const portOptions = ports.reduce((acc, port) => {
const name = makePortName(port);
acc[name] = <>{port.containerPort}</>;
const name = port?.containerPort;
if (name) {
acc[name] = <>{port.containerPort}</>;
}
return acc;
}, {});
return (
Expand Down

0 comments on commit 367b474

Please sign in to comment.