-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add apply replacements function automatically (#56)
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
1 parent
8519a26
commit 1ebc9b8
Showing
3 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../cad/src/components/PackageRevisionPage/updatedResourcesMap/processUpdatedResourcesMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
138 changes: 138 additions & 0 deletions
138
...cad/src/components/PackageRevisionPage/updatedResourcesMap/resources/applyReplacements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |