Skip to content

Commit

Permalink
Merge pull request #1343 from kubeshop/olelensmar/fix/old-kustomizati…
Browse files Browse the repository at this point in the history
…on-fixes
  • Loading branch information
olensmar authored Feb 11, 2022
2 parents b8a89fe + 72b9821 commit c0ea593
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/components/molecules/Monaco/Monaco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ window.MonacoEnvironment = {
const {yaml} = languages || {};

function isValidResourceDocument(d: Document.Parsed<ParsedNode>) {
return (
// @ts-ignore
d.errors.length === 0 && d.contents && isMap(d.contents) && d.contents.has('apiVersion') && d.contents.has('kind')
);
return d.errors.length === 0 && isMap(d.contents);
}

const Monaco = (props: {diffSelectedResource: () => void; applySelection: () => void}) => {
Expand Down Expand Up @@ -191,7 +188,8 @@ const Monaco = (props: {diffSelectedResource: () => void; applySelection: () =>
if (selectedResourceId) {
// this will slow things down if document gets large - need to find a better solution...
const documents = parseAllDocuments(newValue);
setValid(documents.length > 0 && !documents.some(d => !isValidResourceDocument(d)));
// only accept single document changes for now
setValid(documents.length === 1 && isValidResourceDocument(documents[0]));
} else {
setValid(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type RefDropdownMenuProps = {

const sortWarnings = (warnings: Warning[]) =>
warnings.sort((a, b) => {
if (a.type !== b.type) {
if (a.type && b.type && a.type !== b.type) {
return a.type.localeCompare(b.type);
}
if (a.namespace && !b.namespace) {
Expand All @@ -41,7 +41,7 @@ const sortWarnings = (warnings: Warning[]) =>
if (a.namespace && b.namespace && a.namespace !== b.namespace) {
return a.namespace.localeCompare(b.namespace);
}
return a.name.localeCompare(b.name);
return a.name && b.name ? a.name.localeCompare(b.name) : 0;
});

const RefDropdownMenu = (props: RefDropdownMenuProps) => {
Expand Down
2 changes: 2 additions & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export const TOOLTIP_DELAY = 1.0;
export const NAVIGATOR_HEIGHT_OFFSET = 116;
export const FILE_TREE_HEIGHT_OFFSET = 185;
export const REF_PATH_SEPARATOR = '#';
export const KUSTOMIZATION_FILE_NAME = 'kustomization.yaml';
export const KUSTOMIZATION_KIND = 'Kustomization';
export const KUSTOMIZATION_API_GROUP = 'kustomize.config.k8s.io';
export const KUSTOMIZATION_API_VERSION = `${KUSTOMIZATION_API_GROUP}/v1beta1`;
export const KUSTOMIZE_HELP_URL = 'https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/';
export const DEFAULT_EDITOR_DEBOUNCE = 500;
export const DEFAULT_KUBECONFIG_DEBOUNCE = 1000;
Expand Down
4 changes: 2 additions & 2 deletions src/redux/services/kustomize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import {KUSTOMIZATION_API_GROUP, KUSTOMIZATION_KIND} from '@constants/constants';
import {KUSTOMIZATION_API_GROUP, KUSTOMIZATION_FILE_NAME, KUSTOMIZATION_KIND} from '@constants/constants';

import {FileMapType, ResourceMapType} from '@models/appstate';
import {FileEntry} from '@models/fileentry';
Expand Down Expand Up @@ -52,7 +52,7 @@ export function isKustomizationPatch(r: K8sResource | undefined) {
*/

export function isKustomizationFile(fileEntry: FileEntry, resourceMap: ResourceMapType) {
if (fileEntry.name.toLowerCase() === 'kustomization.yaml') {
if (fileEntry.name.toLowerCase() === KUSTOMIZATION_FILE_NAME) {
const resources = getResourcesForPath(fileEntry.filePath, resourceMap);
return resources.length === 1 && isKustomizationResource(resources[0]);
}
Expand Down
16 changes: 12 additions & 4 deletions src/redux/services/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {Document, LineCounter, ParsedNode, Scalar, YAMLSeq, parseAllDocuments, p
import {
CLUSTER_DIFF_PREFIX,
KUSTOMIZATION_API_GROUP,
KUSTOMIZATION_API_VERSION,
KUSTOMIZATION_FILE_NAME,
KUSTOMIZATION_KIND,
PREVIEW_PREFIX,
UNSAVED_PREFIX,
Expand Down Expand Up @@ -457,8 +459,14 @@ export function reprocessResources(
resource.name = `Patch: ${resource.name}`;
}

resource.kind = resource.content.kind;
resource.version = resource.content.apiVersion;
const isKustomziationFile = resource.filePath.toLowerCase().endsWith(KUSTOMIZATION_FILE_NAME);
const kindHandler = resource.content.kind ? getResourceKindHandler(resource.content.kind) : undefined;

resource.kind = resource.content.kind || (isKustomziationFile ? KUSTOMIZATION_KIND : 'Unknown');
resource.version =
resource.content.apiVersion ||
(isKustomziationFile ? KUSTOMIZATION_API_VERSION : kindHandler ? kindHandler.clusterApiVersion : 'Unknown');

resource.namespace = extractNamespace(resource.content);

// clear caches
Expand Down Expand Up @@ -692,15 +700,15 @@ export function extractK8sResources(fileContent: string, relativePath: string) {
result.push(resource);
}
// handle special case of untyped kustomization.yaml files
else if (content && relativePath.endsWith('/kustomization.yaml') && documents.length === 1) {
else if (content && relativePath.toLowerCase().endsWith(KUSTOMIZATION_FILE_NAME) && documents.length === 1) {
let resource: K8sResource = {
name: createResourceName(relativePath, content, KUSTOMIZATION_KIND),
filePath: relativePath,
id: uuidv4(),
isHighlighted: false,
isSelected: false,
kind: KUSTOMIZATION_KIND,
version: `${KUSTOMIZATION_API_GROUP}/v1beta1`,
version: KUSTOMIZATION_API_VERSION,
content,
text: fileContent,
};
Expand Down

0 comments on commit c0ea593

Please sign in to comment.