Skip to content

Commit

Permalink
Merge pull request #4653 from suomiy/patchRequest
Browse files Browse the repository at this point in the history
k8sPatch: do not send unnecesary requests when no patches are present
  • Loading branch information
openshift-merge-robot committed Mar 5, 2020
2 parents feb8b65 + 6c5af4b commit f051348
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 4 additions & 1 deletion frontend/packages/console-shared/src/k8s/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ export class PatchBuilder {
other === updatedValue,
) => {
if (items) {
this.value = item;
const foundIndex = items.findIndex((other) => updatedItemEquals(other, item));
if (foundIndex < 0) {
this.value = item;
this.valueIndex = items.length;
this.operation = PatchOperation.ADD;
} else if (_.isEqual(items[foundIndex], item)) {
this.valid = false; // no change
} else {
this.value = item;
this.valueIndex = foundIndex;
this.operation = PatchOperation.REPLACE;
}
Expand Down
26 changes: 18 additions & 8 deletions frontend/public/module/k8s/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,30 @@ export const k8sUpdate = (kind, data, ns, name) =>
data,
);

export const k8sPatch = (kind, resource, data, opts = {}) =>
coFetchJSON.patch(
export const k8sPatch = (kind, resource, data, opts = {}) => {
const patches = _.compact(data);

if (_.isEmpty(patches)) {
return Promise.resolve(resource);
}

return coFetchJSON.patch(
resourceURL(
kind,
Object.assign({ ns: resource.metadata.namespace, name: resource.metadata.name }, opts),
Object.assign(
{
ns: resource.metadata.namespace,
name: resource.metadata.name,
},
opts,
),
),
_.compact(data),
patches,
);
};

export const k8sPatchByName = (kind, name, namespace, data, opts = {}) =>
coFetchJSON.patch(
resourceURL(kind, Object.assign({ ns: namespace, name }, opts)),
_.compact(data),
);
k8sPatch(kind, { metadata: { name, namespace } }, data, opts);

export const k8sKill = (kind, resource, opts = {}, json = null) =>
coFetchJSON.delete(
Expand Down

0 comments on commit f051348

Please sign in to comment.