Massive refactor (#165)

This commit is contained in:
Oliver King
2022-02-02 09:07:53 -05:00
committed by GitHub
parent 5cbd4acaca
commit ca8d2604ac
139 changed files with 19176 additions and 17005 deletions
+71
View File
@@ -0,0 +1,71 @@
import {
InputObjectKindNotDefinedError,
isServiceEntity,
KubernetesWorkload,
NullInputObjectError,
} from "../types/kubernetesTypes";
export function updateSpecLabels(
inputObject: any,
newLabels: Map<string, string>,
override: boolean
) {
if (!inputObject) throw NullInputObjectError;
if (!inputObject.kind) throw InputObjectKindNotDefinedError;
if (!newLabels) return;
let existingLabels = getSpecLabels(inputObject);
if (override) {
existingLabels = newLabels;
} else {
existingLabels = existingLabels || new Map<string, string>();
Object.keys(newLabels).forEach(
(key) => (existingLabels[key] = newLabels[key])
);
}
setSpecLabels(inputObject, existingLabels);
}
function getSpecLabels(inputObject: any) {
if (!inputObject) return null;
if (inputObject.kind.toLowerCase() === KubernetesWorkload.POD.toLowerCase())
return inputObject.metadata.labels;
if (inputObject?.spec?.template?.metadata)
return inputObject.spec.template.metadata.labels;
return null;
}
function setSpecLabels(inputObject: any, newLabels: any) {
if (!inputObject || !newLabels) return null;
if (inputObject.kind.toLowerCase() === KubernetesWorkload.POD.toLowerCase()) {
inputObject.metadata.labels = newLabels;
return;
}
if (inputObject?.spec?.template?.metatada) {
inputObject.spec.template.metatada.labels = newLabels;
return;
}
}
export function getSpecSelectorLabels(inputObject: any) {
if (inputObject?.spec?.selector) {
if (isServiceEntity(inputObject.kind)) return inputObject.spec.selector;
else return inputObject.spec.selector.matchLabels;
}
}
export function setSpecSelectorLabels(inputObject: any, newLabels: any) {
if (inputObject?.spec?.selector) {
if (isServiceEntity(inputObject.kind))
inputObject.spec.selector = newLabels;
else inputObject.spec.selector.matchLabels = newLabels;
}
}