Skip to content

Commit fd3bb51

Browse files
committed
Refactoring
1 parent 17d7ee9 commit fd3bb51

File tree

5 files changed

+93
-134
lines changed

5 files changed

+93
-134
lines changed

src/components/camel-app-resources/CamelAppPods.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { K8sResourceKind, ResourceLink } from '@openshift-console/dynamic-plugin
44
import { podGVK } from '../../const';
55
import Status from '@openshift-console/dynamic-plugin-sdk/lib/app/components/status/Status';
66
import { useCamelAppPods } from './useCamelAppResources';
7-
import { getPodStatus } from '../../utils';
7+
import { getPodStatus } from './podStatus';
88
import { useTranslation } from 'react-i18next';
99
import { isHawtioEnabled, useHawtioConsolePlugin } from './useHawtio';
1010

@@ -82,7 +82,9 @@ const CamelAppPods: React.FC<CamelAppPodsProps> = ({ obj: camelInt }) => {
8282
<>
8383
<span className="col-xs-2 text-right">
8484
<TextContent>
85-
<a href={`/k8s/ns/${camelInt.metadata.namespace}/pods/${resource.name}/logs`}>
85+
<a
86+
href={`/k8s/ns/${camelInt.metadata.namespace}/pods/${resource.name}/logs`}
87+
>
8688
{t('View Logs')}
8789
</a>
8890
</TextContent>
@@ -100,7 +102,9 @@ const CamelAppPods: React.FC<CamelAppPodsProps> = ({ obj: camelInt }) => {
100102
) : (
101103
<span className="col-xs-4 text-right">
102104
<TextContent>
103-
<a href={`/k8s/ns/${camelInt.metadata.namespace}/pods/${resource.name}/logs`}>
105+
<a
106+
href={`/k8s/ns/${camelInt.metadata.namespace}/pods/${resource.name}/logs`}
107+
>
104108
{t('View Logs')}
105109
</a>
106110
</TextContent>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import * as _ from 'lodash';
2+
3+
const isContainerFailedFilter = (containerStatus) => {
4+
return containerStatus.state.terminated && containerStatus.state.terminated.exitCode !== 0;
5+
};
6+
7+
export const isContainerLoopingFilter = (containerStatus) => {
8+
return (
9+
containerStatus.state.waiting && containerStatus.state.waiting.reason === 'CrashLoopBackOff'
10+
);
11+
};
12+
13+
const numContainersReadyFilter = (pod) => {
14+
const {
15+
status: { containerStatuses },
16+
} = pod;
17+
let numReady = 0;
18+
_.forEach(containerStatuses, (status) => {
19+
if (status.ready) {
20+
numReady++;
21+
}
22+
});
23+
return numReady;
24+
};
25+
26+
const isReady = (pod) => {
27+
const {
28+
spec: { containers },
29+
} = pod;
30+
const numReady = numContainersReadyFilter(pod);
31+
const total = _.size(containers);
32+
33+
return numReady === total;
34+
};
35+
36+
const podWarnings = (pod) => {
37+
const {
38+
status: { phase, containerStatuses },
39+
} = pod;
40+
if (phase === 'Running' && containerStatuses) {
41+
return _.map(containerStatuses, (containerStatus) => {
42+
if (!containerStatus.state) {
43+
return null;
44+
}
45+
46+
if (isContainerFailedFilter(containerStatus)) {
47+
if (_.has(pod, ['metadata', 'deletionTimestamp'])) {
48+
return 'Failed';
49+
}
50+
return 'Warning';
51+
}
52+
if (isContainerLoopingFilter(containerStatus)) {
53+
return 'CrashLoopBackOff';
54+
}
55+
return null;
56+
}).filter((x) => x);
57+
}
58+
return null;
59+
};
60+
61+
export const getPodStatus = (pod) => {
62+
if (_.has(pod, ['metadata', 'deletionTimestamp'])) {
63+
return 'Terminating';
64+
}
65+
const warnings = podWarnings(pod);
66+
if (warnings !== null && warnings.length) {
67+
if (warnings.includes('CrashLoopBackOff')) {
68+
return 'CrashLoopBackOff';
69+
}
70+
if (warnings.includes('Failed')) {
71+
return 'Failed';
72+
}
73+
return 'Warning';
74+
}
75+
const phase = _.get(pod, ['status', 'phase'], 'Unknown');
76+
if (phase === 'Running' && !isReady(pod)) {
77+
return 'NotReady';
78+
}
79+
return phase;
80+
};

src/const.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,6 @@ import { K8sGroupVersionKind } from '@openshift-console/dynamic-plugin-sdk';
22

33
export const HAWTIO_CONSOLE_PLUGIN_NAME = 'hawtio-online-console-plugin';
44

5-
export const METADATA_LABEL_SELECTOR_CAMEL_APP_KEY = 'camel/integration-runtime';
6-
export const METADATA_LABEL_SELECTOR_CAMEL_APP_VALUE = 'camel';
7-
8-
export const METADATA_ANNOTATION_APP_VERSION = 'app.kubernetes.io/version';
9-
10-
export const METADATA_ANNOTATION_CAMEL_VERSION = 'camel/camel-core-version';
11-
12-
export const METADATA_ANNOTATION_CAMEL_QUARKUS_PLATFORM_VERSION = 'camel/quarkus-platform';
13-
export const METADATA_ANNOTATION_CAMEL_CEQ_VERSION = 'camel/camel-quarkus';
14-
15-
export const METADATA_ANNOTATION_QUARKUS_BUILD_TIMESTAMP = 'app.quarkus.io/build-timestamp';
16-
17-
export const METADATA_ANNOTATION_CAMEL_SPRINGBOOT_VERSION = 'camel/spring-boot-version';
18-
export const METADATA_ANNOTATION_CAMEL_CSB_VERSION = 'camel/camel-spring-boot-version';
19-
205
export const camelAppGVK: K8sGroupVersionKind = {
216
group: 'camel.apache.org',
227
version: 'v1alpha1',
@@ -83,3 +68,4 @@ export const consolePluginGVK: K8sGroupVersionKind = {
8368
};
8469

8570
export const ALL_NAMESPACES_KEY = '#ALL_NS#';
71+
export const LAST_LANGUAGE_LOCAL_STORAGE_KEY = 'bridge/last-language';

src/date-utils.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { useTranslation } from 'react-i18next';
2-
3-
4-
export const LAST_LANGUAGE_LOCAL_STORAGE_KEY = 'bridge/last-language';
5-
6-
export const getLastLanguage = (): string =>
7-
localStorage.getItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY) ?? navigator.language;
2+
import { getLastLanguage } from './utils';
83

94
export type Duration = {
105
days: number;

src/utils.ts

Lines changed: 4 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import * as _ from 'lodash';
21
import {
32
cronJobGVK,
43
deploymentConfigGVK,
54
deploymentGVK,
6-
METADATA_ANNOTATION_APP_VERSION,
7-
METADATA_ANNOTATION_QUARKUS_BUILD_TIMESTAMP,
5+
LAST_LANGUAGE_LOCAL_STORAGE_KEY,
86
} from './const';
9-
import { CamelAppKind } from './types';
107
import { K8sResourceKind } from '@openshift-console/dynamic-plugin-sdk';
118

9+
export const getLastLanguage = (): string =>
10+
localStorage.getItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY) ?? navigator.language;
11+
1212
export function CamelAppOwnerGVK(kind: string) {
1313
switch (kind) {
1414
case deploymentConfigGVK.kind:
@@ -20,31 +20,6 @@ export function CamelAppOwnerGVK(kind: string) {
2020
}
2121
}
2222

23-
export function getAppVersion(app: CamelAppKind): string | null {
24-
if (app && app.metadata) {
25-
return app.metadata.annotations?.[METADATA_ANNOTATION_APP_VERSION];
26-
}
27-
return null;
28-
}
29-
30-
export function getBuildTimestamp(app: CamelAppKind): string | null {
31-
if (app && app.metadata) {
32-
return app.metadata.annotations?.[METADATA_ANNOTATION_QUARKUS_BUILD_TIMESTAMP];
33-
}
34-
return null;
35-
}
36-
37-
export function getHealthEndpoints(framework: string): string[] {
38-
switch (framework) {
39-
case 'quarkus':
40-
return ['/observe/health/live', '/observe/health/ready', '/observe/health/started'];
41-
case 'Spring-Boot':
42-
return ['/observe/health/liveness', '/observe/health/readiness'];
43-
default:
44-
return [];
45-
}
46-
}
47-
4823
// TODO use something else than Unknown
4924
export function serviceMatchLabelValue(camelAppOwner: K8sResourceKind): string {
5025
if (camelAppOwner.kind == 'Deployment') {
@@ -56,84 +31,3 @@ export function serviceMatchLabelValue(camelAppOwner: K8sResourceKind): string {
5631
}
5732
return 'Unknown';
5833
}
59-
60-
// Pods status utils
61-
62-
const isContainerFailedFilter = (containerStatus) => {
63-
return containerStatus.state.terminated && containerStatus.state.terminated.exitCode !== 0;
64-
};
65-
66-
export const isContainerLoopingFilter = (containerStatus) => {
67-
return (
68-
containerStatus.state.waiting && containerStatus.state.waiting.reason === 'CrashLoopBackOff'
69-
);
70-
};
71-
72-
const numContainersReadyFilter = (pod) => {
73-
const {
74-
status: { containerStatuses },
75-
} = pod;
76-
let numReady = 0;
77-
_.forEach(containerStatuses, (status) => {
78-
if (status.ready) {
79-
numReady++;
80-
}
81-
});
82-
return numReady;
83-
};
84-
85-
const isReady = (pod) => {
86-
const {
87-
spec: { containers },
88-
} = pod;
89-
const numReady = numContainersReadyFilter(pod);
90-
const total = _.size(containers);
91-
92-
return numReady === total;
93-
};
94-
95-
const podWarnings = (pod) => {
96-
const {
97-
status: { phase, containerStatuses },
98-
} = pod;
99-
if (phase === 'Running' && containerStatuses) {
100-
return _.map(containerStatuses, (containerStatus) => {
101-
if (!containerStatus.state) {
102-
return null;
103-
}
104-
105-
if (isContainerFailedFilter(containerStatus)) {
106-
if (_.has(pod, ['metadata', 'deletionTimestamp'])) {
107-
return 'Failed';
108-
}
109-
return 'Warning';
110-
}
111-
if (isContainerLoopingFilter(containerStatus)) {
112-
return 'CrashLoopBackOff';
113-
}
114-
return null;
115-
}).filter((x) => x);
116-
}
117-
return null;
118-
};
119-
120-
export const getPodStatus = (pod) => {
121-
if (_.has(pod, ['metadata', 'deletionTimestamp'])) {
122-
return 'Terminating';
123-
}
124-
const warnings = podWarnings(pod);
125-
if (warnings !== null && warnings.length) {
126-
if (warnings.includes('CrashLoopBackOff')) {
127-
return 'CrashLoopBackOff';
128-
}
129-
if (warnings.includes('Failed')) {
130-
return 'Failed';
131-
}
132-
return 'Warning';
133-
}
134-
const phase = _.get(pod, ['status', 'phase'], 'Unknown');
135-
if (phase === 'Running' && !isReady(pod)) {
136-
return 'NotReady';
137-
}
138-
return phase;
139-
};

0 commit comments

Comments
 (0)