Skip to content

Commit

Permalink
feat: update package revision page to normalize resources prior to di…
Browse files Browse the repository at this point in the history
…ff (#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.
  • Loading branch information
ChristopherFry committed Oct 10, 2022
1 parent a04c4b0 commit 57c7cbe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
38 changes: 38 additions & 0 deletions plugins/cad/src/utils/kubernetesResource.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
26 changes: 23 additions & 3 deletions plugins/cad/src/utils/packageRevisionResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import {
PackageRevisionResources,
PackageRevisionResourcesMap,
} from '../types/PackageRevisionResource';
import { removeInternalKptAnnotations } from './kubernetesResource';
import {
createMultiResourceYaml,
dumpYaml,
getResourcesFromMultiResourceYaml,
loadYaml,
} from './yaml';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 57c7cbe

Please sign in to comment.