From 57c7cbee8f501a9288d23ffe903b02c0db08b889 Mon Sep 17 00:00:00 2001 From: Christopher Fry Date: Mon, 10 Oct 2022 13:47:14 -0700 Subject: [PATCH] feat: update package revision page to normalize resources prior to diff (#173) This change updates the Package Revision Page to normalize resources prior to the package revisions's resources being compared with another revision. Normalizing resources includes removing any internal kpt annotations, any comments, and standardizing the yaml indentation. --- plugins/cad/src/utils/kubernetesResource.ts | 38 +++++++++++++++++++ .../cad/src/utils/packageRevisionResources.ts | 26 +++++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 plugins/cad/src/utils/kubernetesResource.ts diff --git a/plugins/cad/src/utils/kubernetesResource.ts b/plugins/cad/src/utils/kubernetesResource.ts new file mode 100644 index 00000000..87637696 --- /dev/null +++ b/plugins/cad/src/utils/kubernetesResource.ts @@ -0,0 +1,38 @@ +/** + * 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 { KubernetesResource } from '../types/KubernetesResource'; + +export const removeInternalKptAnnotations = ( + resource: KubernetesResource, +): void => { + const resourceMetadata = resource.metadata; + + if (resourceMetadata.annotations) { + const internalAnnotations = Object.keys( + resourceMetadata.annotations, + ).filter(annotation => annotation.startsWith('internal.kpt.dev/')); + + internalAnnotations.forEach( + internalAnnotation => + delete resourceMetadata.annotations?.[internalAnnotation], + ); + + if (Object.keys(resourceMetadata.annotations).length === 0) { + delete resourceMetadata.annotations; + } + } +}; diff --git a/plugins/cad/src/utils/packageRevisionResources.ts b/plugins/cad/src/utils/packageRevisionResources.ts index 4a993871..1c85ff1d 100644 --- a/plugins/cad/src/utils/packageRevisionResources.ts +++ b/plugins/cad/src/utils/packageRevisionResources.ts @@ -21,8 +21,10 @@ import { PackageRevisionResources, PackageRevisionResourcesMap, } from '../types/PackageRevisionResource'; +import { removeInternalKptAnnotations } from './kubernetesResource'; import { createMultiResourceYaml, + dumpYaml, getResourcesFromMultiResourceYaml, loadYaml, } from './yaml'; @@ -78,6 +80,14 @@ export type PackageResourceDiff = | PackageResourceDiffUpdated | PackageResourceDiffRemoved; +const normalizeYamlForDiff = (yaml: string): string => { + const resource: KubernetesResource = loadYaml(yaml); + + removeInternalKptAnnotations(resource); + + return dumpYaml(resource); +}; + export const getPackageRevisionResources = ( packageRevisionResources: PackageRevisionResources[], packageRevisionName: string, @@ -281,14 +291,24 @@ export const diffPackageResource = ( }; } - const thisDiff = diffLines(originalResource.yaml, currentResource.yaml, { - ignoreWhitespace: true, - }); + const thisDiff = diffLines( + normalizeYamlForDiff(originalResource.yaml), + normalizeYamlForDiff(currentResource.yaml), + ); + const linesAdded = sum(thisDiff.filter(d => !!d.added).map(d => d.count)); const linesRemoved = sum( thisDiff.filter(d => !!d.removed).map(d => d.count), ); + if (linesAdded === 0 && linesRemoved === 0) { + return { + diffStatus: ResourceDiffStatus.UNCHANGED, + originalResource, + currentResource, + }; + } + return { diffStatus: ResourceDiffStatus.UPDATED, originalResource,