Skip to content

Commit

Permalink
feat: add support for porch optimistic concurrency control (GoogleCon…
Browse files Browse the repository at this point in the history
…tainerTools#271)

This change updates the UI to support Porch optimistic concurrency control for package updates.
  • Loading branch information
ChristopherFry authored Jul 7, 2023
1 parent c06e5c5 commit a7ac6a5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions plugins/cad/src/components/AddPackagePage/AddPackagePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ const mapRepositoryToSelectItem = (
const getPackageResources = async (
api: ConfigAsDataApi,
packageName: string,
): Promise<[PackageResource[], PackageRevisionResourcesMap]> => {
): Promise<[PackageResource[], PackageRevisionResourcesMap, string]> => {
const packageResourcesResponse = await api.getPackageRevisionResources(
packageName,
);
const resourcesMap = packageResourcesResponse.spec.resources;
const resources = getPackageResourcesFromResourcesMap(resourcesMap);
const resourceVersion =
packageResourcesResponse.metadata.resourceVersion || '';

return [resources, resourcesMap];
return [resources, resourcesMap, resourceVersion];
};

export const AddPackagePage = ({ action }: AddPackagePageProps) => {
Expand Down Expand Up @@ -464,7 +466,10 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
const allKptFunctions = await api.listCatalogFunctions();
const kptFunctions = groupFunctionsByName(allKptFunctions);

const [_, resourcesMap] = await getPackageResources(api, newPackageName);
const [_, resourcesMap, resourceVersion] = await getPackageResources(
api,
newPackageName,
);

let updatedResourcesMap = resourcesMap;

Expand All @@ -484,6 +489,7 @@ export const AddPackagePage = ({ action }: AddPackagePageProps) => {
const packageRevisionResources = getPackageRevisionResourcesResource(
newPackageName,
updatedResourcesMap,
resourceVersion,
);

await api.replacePackageRevisionResources(packageRevisionResources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export const PackageRevisionPage = ({ mode }: PackageRevisionPageProps) => {
const [revisionSummaries, setRevisionSummaries] = useState<RevisionSummary[]>(
[],
);
const resourcesMapResourceVersion = useRef<string>('');
const [resourcesMap, setResourcesMap] = useState<PackageRevisionResourcesMap>(
{},
);
Expand Down Expand Up @@ -339,6 +340,8 @@ export const PackageRevisionPage = ({ mode }: PackageRevisionPageProps) => {
setRevisionSummaries(thisRevisionSummaries);
setPackageRevision(thisPackageRevision);
setResourcesMap(thisResources.spec.resources);
resourcesMapResourceVersion.current =
thisResources.metadata.resourceVersion || '';

let upgradeAvailable = false;
let thisUpstreamRepository: Repository | undefined = undefined;
Expand Down Expand Up @@ -665,6 +668,7 @@ export const PackageRevisionPage = ({ mode }: PackageRevisionPageProps) => {
const packageRevisionResources = getPackageRevisionResourcesResource(
thisPackageName,
resourcesMap,
resourcesMapResourceVersion.current,
);

await api.replacePackageRevisionResources(packageRevisionResources);
Expand Down Expand Up @@ -741,6 +745,7 @@ export const PackageRevisionPage = ({ mode }: PackageRevisionPageProps) => {
const packageRevisionResources = getPackageRevisionResourcesResource(
packageName,
resourcesMap,
resourcesMapResourceVersion.current,
);

const resourcesResponse = await api.replacePackageRevisionResources(
Expand Down
1 change: 1 addition & 0 deletions plugins/cad/src/types/PackageRevisionResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type PackageRevisionResources = {
export type PackageRevisionResourcesMetadata = {
name: string;
namespace?: string;
resourceVersion?: string;
};

export type PackageRevisionResourcesSpec = {
Expand Down
2 changes: 2 additions & 0 deletions plugins/cad/src/utils/packageRevisionResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ export const getPackageRevisionResources = (
export const getPackageRevisionResourcesResource = (
fullPackageName: string,
resourcesMap: PackageRevisionResourcesMap,
resourceVersion: string,
): PackageRevisionResources => {
const packageRevisionResources: PackageRevisionResources = {
apiVersion: 'porch.kpt.dev/v1alpha1',
kind: 'PackageRevisionResources',
metadata: {
name: fullPackageName,
resourceVersion: resourceVersion,
},
spec: {
resources: resourcesMap,
Expand Down

0 comments on commit a7ac6a5

Please sign in to comment.