Skip to content

Commit d7d60e1

Browse files
committed
HPA Form View in RHOCP Web Console Incorrectly Requires Both CPU and Memory Metrics
1 parent 199697f commit d7d60e1

File tree

4 files changed

+56
-49
lines changed

4 files changed

+56
-49
lines changed

frontend/packages/dev-console/src/components/hpa/HPADetailsForm.tsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,47 @@ const HPADetailsForm: React.FC = () => {
2424
_event: React.FormEvent<HTMLInputElement>,
2525
value: string,
2626
) => {
27-
const numValue = parseInt(value, 10);
28-
2927
const hpa: HorizontalPodAutoscalerKind = field.value;
3028
const { metric, index } = getMetricByType(hpa, type);
3129
const hpaMetrics = hpa.spec.metrics || [];
3230

33-
const updatedMetrics = [...hpaMetrics];
34-
updatedMetrics[index] = {
35-
...metric,
36-
resource: {
37-
...metric.resource,
38-
target: {
39-
...metric.resource.target,
40-
averageUtilization: numValue,
31+
// If field is undefined, remove metric
32+
if (!value) {
33+
const updatedMetrics = hpaMetrics.filter((_, i) => i !== index);
34+
setFieldValue(name, {
35+
...hpa,
36+
spec: {
37+
...hpa.spec,
38+
metrics: updatedMetrics,
4139
},
42-
},
43-
};
40+
});
41+
return;
42+
}
43+
44+
// Create or update metric
45+
const numValue = parseInt(value, 10);
46+
const updatedMetrics = [...hpaMetrics];
47+
updatedMetrics[index] = metric
48+
? {
49+
...metric,
50+
resource: {
51+
...metric.resource,
52+
target: {
53+
...metric.resource.target,
54+
averageUtilization: numValue,
55+
},
56+
},
57+
}
58+
: {
59+
type: 'Resource',
60+
resource: {
61+
name: type,
62+
target: {
63+
type: 'Utilization',
64+
averageUtilization: numValue,
65+
},
66+
},
67+
};
4468

4569
setFieldValue(name, {
4670
...hpa,

frontend/packages/dev-console/src/components/hpa/__tests__/hpa-utils.spec.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,15 @@ describe('getYAMLData gets back an hpa structured editor string', () => {
103103
});
104104

105105
describe('getMetricByType returns an appropriate metric and the index it is at', () => {
106-
it('expect no metrics to return a default metric as a new metric on the end', () => {
107-
const { metric } = getMetricByType(hpaExamples.noMetrics, 'memory');
108-
expect(metric).toBeTruthy();
109-
expect(metric.resource.name).toBe('memory');
110-
expect(metric.resource.target.averageUtilization).toBe(50);
106+
it('expect no metrics to return null when metric does not exist', () => {
107+
const { metric, index } = getMetricByType(hpaExamples.noMetrics, 'memory');
108+
expect(metric).toBeNull();
109+
expect(index).toBe(0);
111110
});
112111

113-
it('expect to get back a default memory metric when only cpu metric is available', () => {
112+
it('expect to get back null when only cpu metric is available and requesting memory', () => {
114113
const { metric, index } = getMetricByType(hpaExamples.cpuScaled, 'memory');
115-
expect(metric).toBeTruthy();
116-
expect(metric.resource.name).toBe('memory');
117-
expect(metric.resource.target.averageUtilization).toBe(50);
114+
expect(metric).toBeNull();
118115
expect(index).toBe(1);
119116
});
120117

frontend/packages/dev-console/src/components/hpa/hpa-utils.ts

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ const defaultHPAYAML = baseTemplates
5050
.get(referenceForModel(HorizontalPodAutoscalerModel))
5151
.get('default');
5252

53-
const getDefaultMetric = (type: SupportedMetricTypes): HPAMetric => ({
54-
type: 'Resource',
55-
resource: {
56-
name: type,
57-
target: {
58-
averageUtilization: 50,
59-
type: 'Utilization',
60-
},
61-
},
62-
});
63-
6453
const createScaleTargetRef = (resource: K8sResourceKind) => ({
6554
apiVersion: resource.apiVersion,
6655
kind: resource.kind,
@@ -72,10 +61,6 @@ export const getFormData = (
7261
existingHPA?: HorizontalPodAutoscalerKind,
7362
): HorizontalPodAutoscalerKind => {
7463
const hpa: HorizontalPodAutoscalerKind = existingHPA || safeYAMLToJS(defaultHPAYAML);
75-
if (!existingHPA) {
76-
// If we are working off the default, zero out cpu metrics to make it easier to use non-cpu metrics with the default
77-
hpa.spec.metrics = [getDefaultMetric('cpu')];
78-
}
7964

8065
return {
8166
...hpa,
@@ -95,10 +80,10 @@ export const getYAMLData = (
9580
export const getMetricByType = (
9681
hpa: HorizontalPodAutoscalerKind,
9782
type: SupportedMetricTypes,
98-
): { metric: HPAMetric; index: number } => {
83+
): { metric: HPAMetric | null; index: number } => {
9984
const hpaMetrics = hpa.spec.metrics || [];
10085
const metricIndex = hpaMetrics.findIndex((m) => m.resource?.name?.toLowerCase() === type);
101-
const metric: HPAMetric = hpaMetrics[metricIndex] || getDefaultMetric(type);
86+
const metric: HPAMetric | null = hpaMetrics[metricIndex] || null;
10287

10388
return { metric, index: metricIndex === -1 ? hpaMetrics.length : metricIndex };
10489
};
@@ -116,16 +101,17 @@ export const sanityForSubmit = (
116101
targetResource: K8sResourceKind,
117102
hpa: HorizontalPodAutoscalerKind,
118103
): HorizontalPodAutoscalerKind => {
119-
const validHPA = merge(
120-
{},
121-
hpa,
122-
// Make sure it's against _this_ namespace
123-
{ metadata: { namespace: targetResource.metadata.namespace } },
124-
// Make sure we kept the target we started with
125-
{ spec: { scaleTargetRef: createScaleTargetRef(targetResource) } },
126-
);
127-
128-
return validHPA;
104+
return {
105+
...hpa,
106+
metadata: {
107+
...hpa.metadata,
108+
namespace: targetResource.metadata.namespace,
109+
},
110+
spec: {
111+
...hpa.spec,
112+
scaleTargetRef: createScaleTargetRef(targetResource),
113+
},
114+
};
129115
};
130116

131117
export const hasCustomMetrics = (hpa?: HorizontalPodAutoscalerKind): boolean => {

frontend/public/models/yaml-templates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ spec:
218218
resource:
219219
name: cpu
220220
target:
221-
averageUtilization: 50
221+
averageUtilization: 80
222222
type: Utilization
223223
`,
224224
)

0 commit comments

Comments
 (0)