Skip to content

Commit

Permalink
feat: add apply replacements function automatically (#56)
Browse files Browse the repository at this point in the history
The change adds the apply replacements function automatically to the Kptfile when a new Apply Replacements resource is added to a package.
  • Loading branch information
ChristopherFry committed Jun 28, 2022
1 parent 8519a26 commit 1ebc9b8
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import {
PackageRevisionsTable,
RevisionSummary,
} from './components/PackageRevisionsTable';
import { processUpdatedResourcesMap } from './updatedResourcesMap/processUpdatedResourcesMap';

export enum PackageRevisionPageMode {
EDIT = 'edit',
Expand Down Expand Up @@ -838,10 +839,16 @@ export const PackageRevisionPage = ({ mode }: PackageRevisionPageProps) => {
? ResourcesTableMode.EDIT
: ResourcesTableMode.VIEW;

const handleUpdatedResourcesMap = (
const handleUpdatedResourcesMap = async (
latestResources: PackageRevisionResourcesMap,
): void => {
setResourcesMap(latestResources);
): Promise<void> => {
const updatedResources = await processUpdatedResourcesMap(
api,
resourcesMap,
latestResources,
);

setResourcesMap(updatedResources);
};

const isViewMode = mode === PackageRevisionPageMode.VIEW;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { cloneDeep } from 'lodash';
import { ConfigAsDataApi } from '../../../apis';
import { PackageRevisionResourcesMap } from '../../../types/PackageRevisionResource';
import { processApplyReplacementsUpdates } from './resources/applyReplacements';

type ProcessMapFn = (
api: ConfigAsDataApi,
originalMap: PackageRevisionResourcesMap,
currentMap: PackageRevisionResourcesMap,
) => Promise<PackageRevisionResourcesMap>;

export const processUpdatedResourcesMap = async (
api: ConfigAsDataApi,
originalMap: PackageRevisionResourcesMap,
currentMap: PackageRevisionResourcesMap,
): Promise<PackageRevisionResourcesMap> => {
const processMapFns: ProcessMapFn[] = [processApplyReplacementsUpdates];

let resourcesMap = cloneDeep(currentMap);

for (const processMapFn of processMapFns) {
resourcesMap = await processMapFn(api, originalMap, resourcesMap);
}

return resourcesMap;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ConfigAsDataApi } from '../../../../apis';
import { Kptfile, KptfileFunction } from '../../../../types/Kptfile';
import { PackageRevisionResourcesMap } from '../../../../types/PackageRevisionResource';
import {
getFunctionNameFromImage,
groupFunctionsByName,
} from '../../../../utils/function';
import {
diffPackageResources,
getPackageResourcesFromResourcesMap,
ResourceDiffStatus,
updateResourceInResourcesMap,
} from '../../../../utils/packageRevisionResources';
import { dumpYaml, loadYaml } from '../../../../utils/yaml';

export const processApplyReplacementsUpdates = async (
api: ConfigAsDataApi,
originalResourcesMap: PackageRevisionResourcesMap,
currentResourcesMap: PackageRevisionResourcesMap,
): Promise<PackageRevisionResourcesMap> => {
const originalResources =
getPackageResourcesFromResourcesMap(originalResourcesMap);
const currentResources =
getPackageResourcesFromResourcesMap(currentResourcesMap);

const changedResources = diffPackageResources(
originalResources,
currentResources,
);

const applyReplacementsResourceDiffs = changedResources.filter(
resource =>
(resource.originalResource ?? resource.currentResource).kind ===
'ApplyReplacements' &&
(resource.diffStatus === ResourceDiffStatus.ADDED ||
resource.diffStatus === ResourceDiffStatus.REMOVED),
);

if (applyReplacementsResourceDiffs.length > 0) {
const kptfileResource = currentResources.find(r => r.kind === 'Kptfile');

if (kptfileResource) {
const kptfileYaml = loadYaml(kptfileResource.yaml) as Kptfile;

let mutatorsUpdated = false;
let mutators = kptfileYaml.pipeline?.mutators ?? [];

const findApplyReplacementsMutator = (
configPath: string,
): KptfileFunction | undefined =>
mutators.find(
mutator =>
getFunctionNameFromImage(mutator.image) === 'apply-replacements' &&
mutator.configPath === configPath,
);

for (const applyReplacementsResourceDiff of applyReplacementsResourceDiffs) {
const diffStatus = applyReplacementsResourceDiff.diffStatus;
const isResourceAdded = diffStatus === ResourceDiffStatus.ADDED;
const isResourceRemoved = diffStatus === ResourceDiffStatus.REMOVED;

if (isResourceAdded) {
const path = applyReplacementsResourceDiff.currentResource.filename;
const applyReplacementsMutator = findApplyReplacementsMutator(path);

if (!applyReplacementsMutator) {
const allKptFunctions = await api.listCatalogFunctions();
const kptFunctions = groupFunctionsByName(allKptFunctions);
const applyReplacementsFn = kptFunctions['apply-replacements'];

mutators.push({
image: applyReplacementsFn[0].spec.image,
configPath: path,
});

mutatorsUpdated = true;
}
}

if (isResourceRemoved) {
const path = applyReplacementsResourceDiff.originalResource.filename;
const applyReplacementsMutator = findApplyReplacementsMutator(path);

if (applyReplacementsMutator) {
const anotherApplyReplacementsResource = currentResources.find(
resource =>
resource.filename === path &&
resource.kind === 'ApplyReplacements',
);

if (!anotherApplyReplacementsResource) {
mutators = mutators.filter(
mutator => mutator !== applyReplacementsMutator,
);

mutatorsUpdated = true;
}
}
}
}

if (mutatorsUpdated) {
kptfileYaml.pipeline = {
...(kptfileYaml.pipeline ?? {}),
mutators,
};

const updatedKptfileYaml = dumpYaml(kptfileYaml);

const updatedResourcesMap = updateResourceInResourcesMap(
currentResourcesMap,
kptfileResource,
updatedKptfileYaml,
);

return updatedResourcesMap;
}
}
}

return currentResourcesMap;
};

0 comments on commit 1ebc9b8

Please sign in to comment.