Add eslint rule padding-line-between-statements (#1593)

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti authored and GitHub committed 2020-12-02 09:55:52 +02:00
1 parent 7b77f18376
commit dcf253e7d5
401 files changed
+2018 -40

No files matched your search

@@ -90,6 +90,7 @@ export class Cluster extends KubeObject {
if (this.metadata.deletionTimestamp) return ClusterStatus.REMOVING;
if (!this.status || !this.status) return ClusterStatus.CREATING;
if (this.status.errorMessage) return ClusterStatus.ERROR;
return ClusterStatus.ACTIVE;
}
}
+5
View File
@@ -75,6 +75,7 @@ export class CustomResourceDefinition extends KubeObject {
getResourceApiBase() {
const { group } = this.spec;
return `/apis/${group}/${this.getVersion()}/${this.getPluralName()}`;
}
@@ -88,6 +89,7 @@ export class CustomResourceDefinition extends KubeObject {
getResourceTitle() {
const name = this.getPluralName();
return name[0].toUpperCase() + name.substr(1);
}
@@ -124,6 +126,7 @@ export class CustomResourceDefinition extends KubeObject {
const columns = this.spec.versions.find(a => this.getVersion() == a.name)?.additionalPrinterColumns
?? this.spec.additionalPrinterColumns?.map(({ JSONPath, ...rest }) => ({ ...rest, jsonPath: JSONPath })) // map to V1 shape
?? [];
return columns
.filter(column => column.name != "Age")
.filter(column => ignorePriority ? true : !column.priority);
@@ -135,8 +138,10 @@ export class CustomResourceDefinition extends KubeObject {
getConditions() {
if (!this.status?.conditions) return [];
return this.status.conditions.map(condition => {
const { message, reason, lastTransitionTime, status } = condition;
return {
...condition,
isReady: status === "True",
@@ -71,6 +71,7 @@ export class CronJob extends KubeObject {
getLastScheduleTime() {
if (!this.status.lastScheduleTime) return "-";
const diff = moment().diff(this.status.lastScheduleTime);
return formatDuration(diff, true);
}
@@ -84,7 +85,9 @@ export class CronJob extends KubeObject {
const stamps = schedule.split(" ");
const day = Number(stamps[stamps.length - 3]); // 1-31
const month = Number(stamps[stamps.length - 2]); // 1-12
if (schedule.startsWith("@")) return false;
return day > daysInMonth[month - 1];
}
}
@@ -66,6 +66,7 @@ export class DaemonSet extends WorkloadKubeObject {
getImages() {
const containers: IPodContainer[] = get(this, "spec.template.spec.containers", []);
const initContainers: IPodContainer[] = get(this, "spec.template.spec.initContainers", []);
return [...containers, ...initContainers].map(container => container.image);
}
}
@@ -171,10 +171,13 @@ export class Deployment extends WorkloadKubeObject {
getConditions(activeOnly = false) {
const { conditions } = this.status;
if (!conditions) return [];
if (activeOnly) {
return conditions.filter(c => c.status === "True");
}
return conditions;
}
@@ -73,11 +73,13 @@ export class EndpointSubset implements IEndpointSubset {
getAddresses(): EndpointAddress[] {
const addresses = this.addresses || [];
return addresses.map(a => new EndpointAddress(a));
}
getNotReadyAddresses(): EndpointAddress[] {
const notReadyAddresses = this.notReadyAddresses || [];
return notReadyAddresses.map(a => new EndpointAddress(a));
}
@@ -85,10 +87,12 @@ export class EndpointSubset implements IEndpointSubset {
if(!this.addresses) {
return "";
}
return this.addresses.map(address => {
if (!this.ports) {
return address.ip;
}
return this.ports.map(port => {
return `${address.ip}:${port.port}`;
}).join(", ");
@@ -106,6 +110,7 @@ export class Endpoint extends KubeObject {
getEndpointSubsets(): EndpointSubset[] {
const subsets = this.subsets || [];
return subsets.map(s => new EndpointSubset(s));
}
+3
View File
@@ -39,16 +39,19 @@ export class KubeEvent extends KubeObject {
getSource() {
const { component, host } = this.source;
return `${component} ${host || ""}`;
}
getFirstSeenTime() {
const diff = moment().diff(this.firstTimestamp);
return formatDuration(diff, true);
}
getLastSeenTime() {
const diff = moment().diff(this.lastTimestamp);
return formatDuration(diff, true);
}
}
@@ -33,11 +33,13 @@ export const helmChartsApi = {
get(repo: string, name: string, readmeVersion?: string) {
const path = endpoint({ repo, name });
return apiBase
.get<IHelmChartDetails>(`${path}?${stringify({ version: readmeVersion })}`)
.then(data => {
const versions = data.versions.map(HelmChart.create);
const readme = data.readme;
return {
readme,
versions,
@@ -76,9 +76,11 @@ export const helmReleasesApi = {
get(name: string, namespace: string) {
const path = endpoint({ name, namespace });
return apiBase.get<IReleaseRawDetails>(path).then(details => {
const items: KubeObject[] = JSON.parse(details.resources).items;
const resources = items.map(item => KubeObject.create(item));
return {
...details,
resources
@@ -88,35 +90,43 @@ export const helmReleasesApi = {
create(payload: IReleaseCreatePayload): Promise<IReleaseUpdateDetails> {
const { repo, ...data } = payload;
data.chart = `${repo}/${data.chart}`;
data.values = jsYaml.safeLoad(data.values);
return apiBase.post(endpoint(), { data });
},
update(name: string, namespace: string, payload: IReleaseUpdatePayload): Promise<IReleaseUpdateDetails> {
const { repo, ...data } = payload;
data.chart = `${repo}/${data.chart}`;
data.values = jsYaml.safeLoad(data.values);
return apiBase.put(endpoint({ name, namespace }), { data });
},
async delete(name: string, namespace: string) {
const path = endpoint({ name, namespace });
return apiBase.del(path);
},
getValues(name: string, namespace: string) {
const path = `${endpoint({ name, namespace })}/values`;
return apiBase.get<string>(path);
},
getHistory(name: string, namespace: string): Promise<IReleaseRevision[]> {
const path = `${endpoint({ name, namespace })}/history`;
return apiBase.get(path);
},
rollback(name: string, namespace: string, revision: number) {
const path = `${endpoint({ name, namespace })}/rollback`;
return apiBase.put(path, {
data: {
revision
@@ -157,10 +167,13 @@ export class HelmRelease implements ItemObject {
getChart(withVersion = false) {
let chart = this.chart;
if(!withVersion && this.getVersion() != "" ) {
const search = new RegExp(`-${this.getVersion()}`);
chart = chart.replace(search, "");
}
return chart;
}
@@ -174,6 +187,7 @@ export class HelmRelease implements ItemObject {
getVersion() {
const versions = this.chart.match(/(v?\d+)[^-].*$/);
if (versions) {
return versions[0];
}
@@ -187,9 +201,11 @@ export class HelmRelease implements ItemObject {
const updated = this.updated.replace(/\s\w*$/, ""); // 2019-11-26 10:58:09 +0300 MSK -> 2019-11-26 10:58:09 +0300 to pass into Date()
const updatedDate = new Date(updated).getTime();
const diff = now - updatedDate;
if (humanize) {
return formatDuration(diff, compact);
}
return diff;
}
@@ -200,6 +216,7 @@ export class HelmRelease implements ItemObject {
const version = this.getVersion();
const versions = await helmChartStore.getVersions(chartName);
const chartVersion = versions.find(chartVersion => chartVersion.version === version);
return chartVersion ? chartVersion.repo : "";
}
}
+6
View File
@@ -80,8 +80,10 @@ export class HorizontalPodAutoscaler extends KubeObject {
getConditions() {
if (!this.status.conditions) return [];
return this.status.conditions.map(condition => {
const { message, reason, lastTransitionTime, status } = condition;
return {
...condition,
isReady: status === "True",
@@ -100,6 +102,7 @@ export class HorizontalPodAutoscaler extends KubeObject {
protected getMetricName(metric: IHpaMetric): string {
const { type, resource, pods, object, external } = metric;
switch (type) {
case HpaMetricType.Resource:
return resource.name;
@@ -122,14 +125,17 @@ export class HorizontalPodAutoscaler extends KubeObject {
const target = metric[metricType];
let currentValue = "unknown";
let targetValue = "unknown";
if (current) {
currentValue = current.currentAverageUtilization || current.currentAverageValue || current.currentValue;
if (current.currentAverageUtilization) currentValue += "%";
}
if (target) {
targetValue = target.targetAverageUtilization || target.targetAverageValue || target.targetValue;
if (target.targetAverageUtilization) targetValue += "%";
}
return `${currentValue} / ${targetValue}`;
}
}
+7 -1
View File
@@ -6,6 +6,7 @@ import { KubeApi } from "../kube-api";
export class IngressApi extends KubeApi<Ingress> {
getMetrics(ingress: string, namespace: string): Promise<IIngressMetrics> {
const opts = { category: "ingress", ingress };
return metricsApi.getMetrics({
bytesSentSuccess: opts,
bytesSentFailure: opts,
@@ -98,15 +99,18 @@ export class Ingress extends KubeObject {
getRoutes() {
const { spec: { tls, rules } } = this;
if (!rules) return [];
let protocol = "http";
const routes: string[] = [];
if (tls && tls.length > 0) {
protocol += "s";
}
rules.map(rule => {
const host = rule.host ? rule.host : "*";
if (rule.http && rule.http.paths) {
rule.http.paths.forEach(path => {
const { serviceName, servicePort } = getBackendServiceNamePort(path.backend);
@@ -132,7 +136,9 @@ export class Ingress extends KubeObject {
getHosts() {
const { spec: { rules } } = this;
if (!rules) return [];
return rules.filter(rule => rule.host).map(rule => rule.host);
}
@@ -141,7 +147,6 @@ export class Ingress extends KubeObject {
const { spec: { tls, rules, backend, defaultBackend } } = this;
const httpPort = 80;
const tlsPort = 443;
// Note: not using the port name (string)
const servicePort = defaultBackend?.service.port.number ?? backend?.servicePort;
@@ -152,6 +157,7 @@ export class Ingress extends KubeObject {
} else if (servicePort !== undefined) {
ports.push(Number(servicePort));
}
if (tls && tls.length > 0) {
ports.push(tlsPort);
}
+4
View File
@@ -86,12 +86,15 @@ export class Job extends WorkloadKubeObject {
// Type of Job condition could be only Complete or Failed
// https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#jobcondition-v1-batch
const { conditions } = this.status;
if (!conditions) return;
return conditions.find(({ status }) => status === "True");
}
getImages() {
const containers: IPodContainer[] = get(this, "spec.template.spec.containers", []);
return [...containers].map(container => container.image);
}
@@ -99,6 +102,7 @@ export class Job extends WorkloadKubeObject {
const params: JsonApiParams = {
query: { propagationPolicy: "Background" }
};
return super.delete(params);
}
}
@@ -41,6 +41,7 @@ export const metricsApi = {
if (!start && !end) {
const timeNow = Date.now() / 1000;
const now = moment.unix(timeNow).startOf("minute").unix(); // round date to minutes
start = now - range;
end = now;
}
@@ -76,8 +77,10 @@ export function normalizeMetrics(metrics: IMetrics, frames = 60): IMetrics {
// fill the gaps
result.forEach(res => {
if (!res.values || !res.values.length) return;
while (res.values.length < frames) {
const timestamp = moment.unix(res.values[0][0]).subtract(1, "minute").unix();
res.values.unshift([timestamp, "0"]);
}
});
@@ -101,14 +104,17 @@ export function isMetricsEmpty(metrics: { [key: string]: IMetrics }) {
export function getItemMetrics(metrics: { [key: string]: IMetrics }, itemName: string): { [key: string]: IMetrics } {
if (!metrics) return;
const itemMetrics = { ...metrics };
for (const metric in metrics) {
if (!metrics[metric]?.data?.result) {
continue;
}
const results = metrics[metric].data.result;
const result = results.find(res => Object.values(res.metric)[0] == itemName);
itemMetrics[metric].data.result = result ? [result] : [];
}
return itemMetrics;
}
@@ -118,11 +124,13 @@ export function getMetricLastPoints(metrics: { [key: string]: IMetrics }) {
Object.keys(metrics).forEach(metricName => {
try {
const metric = metrics[metricName];
if (metric.data.result.length) {
result[metricName] = +metric.data.result[0].values.slice(-1)[0][1];
}
} catch (e) {
}
return result;
}, {});
@@ -55,6 +55,7 @@ export class NetworkPolicy extends KubeObject {
getMatchLabels(): string[] {
if (!this.spec.podSelector || !this.spec.podSelector.matchLabels) return [];
return Object
.entries(this.spec.podSelector.matchLabels)
.map(data => data.join(":"));
@@ -62,6 +63,7 @@ export class NetworkPolicy extends KubeObject {
getTypes(): string[] {
if (!this.spec.policyTypes) return [];
return this.spec.policyTypes;
}
}
+9
View File
@@ -87,9 +87,12 @@ export class Node extends KubeObject {
getNodeConditionText() {
const { conditions } = this.status;
if (!conditions) return "";
return conditions.reduce((types, current) => {
if (current.status !== "True") return "";
return types += ` ${current.type}`;
}, "");
}
@@ -112,19 +115,23 @@ export class Node extends KubeObject {
getCpuCapacity() {
if (!this.status.capacity || !this.status.capacity.cpu) return 0;
return cpuUnitsToNumber(this.status.capacity.cpu);
}
getMemoryCapacity() {
if (!this.status.capacity || !this.status.capacity.memory) return 0;
return unitsToBytes(this.status.capacity.memory);
}
getConditions() {
const conditions = this.status.conditions || [];
if (this.isUnschedulable()) {
return [{ type: "SchedulingDisabled", status: "True" }, ...conditions];
}
return conditions;
}
@@ -134,6 +141,7 @@ export class Node extends KubeObject {
getWarningConditions() {
const goodConditions = ["Ready", "HostUpgrades", "SchedulingDisabled"];
return this.getActiveConditions().filter(condition => {
return !goodConditions.includes(condition.type);
});
@@ -145,6 +153,7 @@ export class Node extends KubeObject {
getOperatingSystem(): string {
const label = this.getLabels().find(label => label.startsWith("kubernetes.io/os="));
if (label) {
return label.split("=", 2)[1];
}
@@ -52,6 +52,7 @@ export class PersistentVolumeClaim extends KubeObject {
getPods(allPods: Pod[]): Pod[] {
const pods = allPods.filter(pod => pod.getNs() === this.getNs());
return pods.filter(pod => {
return pod.getVolumes().filter(volume =>
volume.persistentVolumeClaim &&
@@ -62,22 +63,26 @@ export class PersistentVolumeClaim extends KubeObject {
getStorage(): string {
if (!this.spec.resources || !this.spec.resources.requests) return "-";
return this.spec.resources.requests.storage;
}
getMatchLabels(): string[] {
if (!this.spec.selector || !this.spec.selector.matchLabels) return [];
return Object.entries(this.spec.selector.matchLabels)
.map(([name, val]) => `${name}:${val}`);
}
getMatchExpressions() {
if (!this.spec.selector || !this.spec.selector.matchExpressions) return [];
return this.spec.selector.matchExpressions;
}
getStatus(): string {
if (this.status) return this.status.phase;
return "-";
}
}
@@ -47,20 +47,25 @@ export class PersistentVolume extends KubeObject {
getCapacity(inBytes = false) {
const capacity = this.spec.capacity;
if (capacity) {
if (inBytes) return unitsToBytes(capacity.storage);
return capacity.storage;
}
return 0;
}
getStatus() {
if (!this.status) return;
return this.status.phase || "-";
}
getClaimRefName() {
const { claimRef } = this.spec;
return claimRef ? claimRef.name : "";
}
}
@@ -22,6 +22,7 @@ export class PodDisruptionBudget extends KubeObject {
getSelectors() {
const selector = this.spec.selector;
return KubeObject.stringifyLabels(selector ? selector.matchLabels : null);
}
+27
View File
@@ -6,6 +6,7 @@ import { KubeApi } from "../kube-api";
export class PodsApi extends KubeApi<Pod> {
async getLogs(params: { namespace: string; name: string }, query?: IPodLogsQuery): Promise<string> {
const path = `${this.getUrl(params)}/log`;
return this.request.get(path, { query });
}
@@ -247,6 +248,7 @@ export class Pod extends WorkloadKubeObject {
getRunningContainers() {
const statuses = this.getContainerStatuses();
return this.getAllContainers().filter(container => {
return statuses.find(status => status.name === container.name && !!status.state["running"]);
}
@@ -256,18 +258,23 @@ export class Pod extends WorkloadKubeObject {
getContainerStatuses(includeInitContainers = true) {
const statuses: IPodContainerStatus[] = [];
const { containerStatuses, initContainerStatuses } = this.status;
if (containerStatuses) {
statuses.push(...containerStatuses);
}
if (includeInitContainers && initContainerStatuses) {
statuses.push(...initContainerStatuses);
}
return statuses;
}
getRestartsCount(): number {
const { containerStatuses } = this.status;
if (!containerStatuses) return 0;
return containerStatuses.reduce((count, item) => count + item.restartCount, 0);
}
@@ -290,18 +297,23 @@ export class Pod extends WorkloadKubeObject {
const goodConditions = ["Initialized", "Ready"].every(condition =>
!!this.getConditions().find(item => item.type === condition && item.status === "True")
);
if (reason === PodStatus.EVICTED) {
return PodStatus.EVICTED;
}
if (phase === PodStatus.FAILED) {
return PodStatus.FAILED;
}
if (phase === PodStatus.SUCCEEDED) {
return PodStatus.SUCCEEDED;
}
if (phase === PodStatus.RUNNING && goodConditions) {
return PodStatus.RUNNING;
}
return PodStatus.PENDING;
}
@@ -312,20 +324,26 @@ export class Pod extends WorkloadKubeObject {
let message = "";
const statuses = this.getContainerStatuses(false); // not including initContainers
if (statuses.length) {
statuses.forEach(status => {
const { state } = status;
if (state.waiting) {
const { reason } = state.waiting;
message = reason ? reason : "Waiting";
}
if (state.terminated) {
const { reason } = state.terminated;
message = reason ? reason : "Terminated";
}
});
}
if (message) return message;
return this.getStatusPhase();
}
@@ -349,7 +367,9 @@ export class Pod extends WorkloadKubeObject {
getNodeSelectors(): string[] {
const { nodeSelector } = this.spec;
if (!nodeSelector) return [];
return Object.entries(nodeSelector).map(values => values.join(": "));
}
@@ -367,8 +387,10 @@ export class Pod extends WorkloadKubeObject {
});
const crashLoop = !!this.getContainerStatuses().find(condition => {
const waiting = condition.state.waiting;
return (waiting && waiting.reason == "CrashLoopBackOff");
});
return (
notReady ||
crashLoop ||
@@ -391,18 +413,22 @@ export class Pod extends WorkloadKubeObject {
periodSeconds, successThreshold, failureThreshold
} = probeData;
const probe = [];
// HTTP Request
if (httpGet) {
const { path, port, host, scheme } = httpGet;
probe.push(
"http-get",
`${scheme.toLowerCase()}://${host || ""}:${port || ""}${path || ""}`,
);
}
// Command
if (exec && exec.command) {
probe.push(`exec [${exec.command.join(" ")}]`);
}
// TCP Probe
if (tcpSocket && tcpSocket.port) {
probe.push(`tcp-socket :${tcpSocket.port}`);
@@ -414,6 +440,7 @@ export class Pod extends WorkloadKubeObject {
`#success=${successThreshold || "0"}`,
`#failure=${failureThreshold || "0"}`,
);
return probe;
}
@@ -78,6 +78,7 @@ export class PodSecurityPolicy extends KubeObject {
getRules() {
const { fsGroup, runAsGroup, runAsUser, supplementalGroups, seLinux } = this.spec;
return {
fsGroup: fsGroup ? fsGroup.rule : "",
runAsGroup: runAsGroup ? runAsGroup.rule : "",
@@ -48,6 +48,7 @@ export class ReplicaSet extends WorkloadKubeObject {
getImages() {
const containers: IPodContainer[] = get(this, "spec.template.spec.containers", []);
return [...containers].map(container => container.image);
}
}
@@ -13,17 +13,20 @@ export const resourceApplierApi = {
if (typeof resource === "string") {
resource = jsYaml.safeLoad(resource);
}
return apiBase
.post<KubeJsonApiData[]>("/stack", { data: resource })
.then(data => {
const items = data.map(obj => {
const api = apiManager.getApi(obj.metadata.selfLink);
if (api) {
return new api.objectConstructor(obj);
} else {
return new KubeObject(obj);
}
});
return items.length === 1 ? items[0] : items;
});
}
@@ -58,6 +58,7 @@ export class ResourceQuota extends KubeObject {
getScopeSelector() {
const { matchExpressions = [] } = this.spec.scopeSelector || {};
return matchExpressions;
}
}
@@ -38,16 +38,19 @@ export class SelfSubjectRulesReview extends KubeObject {
getResourceRules() {
const rules = this.status && this.status.resourceRules || [];
return rules.map(rule => this.normalize(rule));
}
getNonResourceRules() {
const rules = this.status && this.status.nonResourceRules || [];
return rules.map(rule => this.normalize(rule));
}
protected normalize(rule: ISelfSubjectReviewRule): ISelfSubjectReviewRule {
const { apiGroups = [], resourceNames = [], verbs = [], nonResourceURLs = [], resources = [] } = rule;
return {
apiGroups,
nonResourceURLs,
@@ -56,6 +59,7 @@ export class SelfSubjectRulesReview extends KubeObject {
resources: resources.map((resource, index) => {
const apiGroup = apiGroups.length >= index + 1 ? apiGroups[index] : apiGroups.slice(-1)[0];
const separator = apiGroup == "" ? "" : ".";
return resource + separator + apiGroup;
})
};
@@ -61,9 +61,11 @@ export class Service extends KubeObject {
getExternalIps() {
const lb = this.getLoadBalancer();
if (lb && lb.ingress) {
return lb.ingress.map(val => val.ip || val.hostname);
}
return this.spec.externalIPs || [];
}
@@ -73,11 +75,13 @@ export class Service extends KubeObject {
getSelector(): string[] {
if (!this.spec.selector) return [];
return Object.entries(this.spec.selector).map(val => val.join("="));
}
getPorts(): ServicePort[] {
const ports = this.spec.ports || [];
return ports.map(p => new ServicePort(p));
}
@@ -102,6 +102,7 @@ export class StatefulSet extends WorkloadKubeObject {
getImages() {
const containers: IPodContainer[] = get(this, "spec.template.spec.containers", []);
return [...containers].map(container => container.image);
}
}
@@ -18,6 +18,7 @@ export class StorageClass extends KubeObject {
isDefault() {
const annotations = this.metadata.annotations || {};
return (
annotations["storageclass.kubernetes.io/is-default-class"] === "true" ||
annotations["storageclass.beta.kubernetes.io/is-default-class"] === "true"