Skip to content

Commit

Permalink
Resource Requirements specDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
cyril-ui-developer committed Feb 4, 2020
1 parent c3a96f0 commit 0363b2e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,14 @@ export const CreateOperandForm: React.FC<CreateOperandFormProps> = ({
<ResourceRequirements
cpu={_.get(formValues, [field.path, 'limits', 'cpu'])}
memory={_.get(formValues, [field.path, 'limits', 'memory'])}
storage={_.get(formValues, [field.path, 'limits', 'ephemeral-storage'])}
onChangeCPU={(cpu) => updateFormValues([field.path, 'limits', 'cpu'], cpu)}
onChangeMemory={(memory) =>
updateFormValues([field.path, 'limits', 'memory'], memory)
}
onChangeStorage={(storage) =>
updateFormValues([field.path, 'limits', 'ephemeral-storage'], storage)
}
path={`${field.path}.limits`}
/>
</dd>
Expand All @@ -506,10 +510,14 @@ export const CreateOperandForm: React.FC<CreateOperandFormProps> = ({
<ResourceRequirements
cpu={_.get(formValues, [field.path, 'requests', 'cpu'])}
memory={_.get(formValues, [field.path, 'requests', 'memory'])}
storage={_.get(formValues, [field.path, 'requests', 'ephemeral-storage'])}
onChangeCPU={(cpu) => updateFormValues([field.path, 'requests', 'cpu'], cpu)}
onChangeMemory={(memory) =>
updateFormValues([field.path, 'requests', 'memory'], memory)
}
onChangeStorage={(storage) =>
updateFormValues([field.path, 'requests', 'ephemeral-storage'], storage)
}
path={`${field.path}.requests`}
/>
</dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,18 @@ describe(ResourceRequirementsModal.name, () => {
it('calls function to update resource instance when form is submitted', (done) => {
wrapper.find('input[name="cpu"]').simulate('change', { target: { value: '200m' } });
wrapper.find('input[name="memory"]').simulate('change', { target: { value: '20Mi' } });
wrapper
.find('input[name="ephemeral-storage"]')
.simulate('change', { target: { value: '50Mi' } });

spyAndExpect(spyOn(k8s, 'k8sUpdate'))(Promise.resolve())
.then(([model, newObj]: [k8s.K8sKind, k8s.K8sResourceKind]) => {
expect(model).toEqual(testModel);
expect(newObj.spec.resources.requests).toEqual({ cpu: '200m', memory: '20Mi' });
expect(newObj.spec.resources.requests).toEqual({
cpu: '200m',
memory: '20Mi',
'ephemeral-storage': '50Mi',
});
done();
})
.catch((err) => fail(err));
Expand All @@ -82,8 +89,8 @@ describe(ResourceRequirementsModalLink.displayName, () => {
beforeEach(() => {
obj = _.cloneDeep(testResourceInstance);
obj.spec.resources = {
limits: { memory: '50Mi', cpu: '500m' },
requests: { memory: '50Mi', cpu: '500m' },
limits: { memory: '50Mi', cpu: '500m', 'ephemeral-storage': '50Mi' },
requests: { memory: '50Mi', cpu: '500m', 'ephemeral-storage': '50Mi' },
};
wrapper = shallow(
<ResourceRequirementsModalLink.WrappedComponent
Expand All @@ -96,25 +103,25 @@ describe(ResourceRequirementsModalLink.displayName, () => {
});

it('renders a button link with the resource requests limits', () => {
const { memory, cpu } = obj.spec.resources.limits;
const { memory, cpu, 'ephemeral-storage': storage } = obj.spec.resources.limits;
wrapper = wrapper.setProps({ type: 'requests' });

expect(
wrapper
.find('button')
.childAt(0)
.text(),
).toEqual(`CPU: ${cpu}, Memory: ${memory}`);
).toEqual(`CPU: ${cpu}, Memory: ${memory}, Storage: ${storage}`);
});

it('renders a button link with the resource limits', () => {
const { memory, cpu } = obj.spec.resources.requests;
const { memory, cpu, 'ephemeral-storage': storage } = obj.spec.resources.requests;
expect(
wrapper
.find('button')
.childAt(0)
.text(),
).toEqual(`CPU: ${cpu}, Memory: ${memory}`);
).toEqual(`CPU: ${cpu}, Memory: ${memory}, Storage: ${storage}`);
});

it('renders default values if undefined', () => {
Expand All @@ -126,7 +133,7 @@ describe(ResourceRequirementsModalLink.displayName, () => {
.find('button')
.childAt(0)
.text(),
).toEqual('CPU: none, Memory: none');
).toEqual('CPU: none, Memory: none, Storage: none');
});

it('opens resource requirements modal when clicked', (done) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
import { RootState } from '@console/internal/redux';

export const ResourceRequirements: React.FC<ResourceRequirementsProps> = (props) => {
const { cpu, memory, onChangeCPU, onChangeMemory, path = '' } = props;
const { cpu, memory, storage, onChangeCPU, onChangeMemory, onChangeStorage, path = '' } = props;

return (
<div className="row co-m-form-row">
<div className="col-xs-5">
<div className="col-xs-4">
<label
style={{ fontWeight: 300 }}
className="text-muted text-uppercase"
Expand All @@ -36,7 +36,7 @@ export const ResourceRequirements: React.FC<ResourceRequirementsProps> = (props)
placeholder="500m"
/>
</div>
<div className="col-xs-5">
<div className="col-xs-4">
<label
style={{ fontWeight: 300 }}
className="text-muted text-uppercase"
Expand All @@ -54,6 +54,24 @@ export const ResourceRequirements: React.FC<ResourceRequirementsProps> = (props)
placeholder="50Mi"
/>
</div>
<div className="col-xs-4">
<label
style={{ fontWeight: 300 }}
className="text-muted text-uppercase"
htmlFor={`${path}.ephemeral-storage`}
>
Storage
</label>
<input
value={storage}
onChange={(e) => onChangeStorage(e.target.value)}
id={`${path}.ephemeral-storage`}
name="ephemeral-storage"
type="text"
className="pf-c-form-control"
placeholder="50Mi"
/>
</div>
</div>
);
};
Expand All @@ -65,13 +83,20 @@ export const ResourceRequirementsModal = withHandlePromise(
const [memory, setMemory] = React.useState<string>(
_.get(obj.spec, `${path}.${type}.memory`, ''),
);
const [storage, setStorage] = React.useState<string>(
_.get(obj.spec, `${path}.${type}.ephemeral-storage`),
);

const submit = (e) => {
e.preventDefault();

let newObj = _.cloneDeep(obj);
if (cpu !== '' || memory !== '') {
newObj = _.set(newObj, `spec.${path}.${type}`, { cpu, memory });
if (cpu !== '' || memory !== '' || storage !== '') {
newObj = _.set(newObj, `spec.${path}.${type}`, {
cpu,
memory,
'ephemeral-storage': storage,
});
}

return props.handlePromise(k8sUpdate(model, newObj)).then(props.close);
Expand All @@ -87,8 +112,10 @@ export const ResourceRequirementsModal = withHandlePromise(
<ResourceRequirements
cpu={cpu}
memory={memory}
storage={storage}
onChangeCPU={setCPU}
onChangeMemory={setMemory}
onChangeStorage={setStorage}
path={path}
/>
</ModalBody>
Expand All @@ -110,7 +137,11 @@ const stateToProps = ({ k8s }: RootState, { obj }) => ({
export const ResourceRequirementsModalLink = connect(stateToProps)(
(props: ResourceRequirementsModalLinkProps) => {
const { obj, type, path, model } = props;
const { cpu, memory } = _.get(obj.spec, `${path}.${type}`, { cpu: 'none', memory: 'none' });
const { cpu, memory, 'ephemeral-storage': storage } = _.get(obj.spec, `${path}.${type}`, {
cpu: 'none',
memory: 'none',
'ephemeral-storage': 'none',
});

const onClick = () => {
const modal = createModalLauncher(ResourceRequirementsModal);
Expand All @@ -128,7 +159,7 @@ export const ResourceRequirementsModalLink = connect(stateToProps)(
onClick={onClick}
variant="link"
>
{`CPU: ${cpu}, Memory: ${memory}`}
{`CPU: ${cpu}, Memory: ${memory}, Storage: ${storage}`}
<PencilAltIcon className="co-icon-space-l pf-c-button-icon--plain" />
</Button>
);
Expand All @@ -152,8 +183,10 @@ export type ResourceRequirementsModalProps = {
export type ResourceRequirementsProps = {
cpu: string;
memory: string;
storage: string;
onChangeCPU: (value: string) => void;
onChangeMemory: (value: string) => void;
onChangeStorage: (value: string) => void;
path?: string;
};

Expand Down

0 comments on commit 0363b2e

Please sign in to comment.