Skip to content

Commit a5e6300

Browse files
authored
Merge pull request #1227 from Joshna907/codex/prometheus-argocd-metrics-guard
prometheus: Safely detect Argo CD Application metrics
2 parents db65ddd + aff56e0 commit a5e6300

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

prometheus/src/util.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getTimeRangeAndStepSize, supportsPrometheusMetrics } from './util';
1+
import { getTimeRangeAndStepSize, isArgoCDApplication, supportsPrometheusMetrics } from './util';
22

33
beforeAll(async () => {
44
global.TextEncoder = require('util').TextEncoder;
@@ -138,9 +138,43 @@ describe('supportsPrometheusMetrics', () => {
138138
false,
139139
],
140140
['rejects Queues without apiVersion', { kind: 'Queue', jsonData: { kind: 'Queue' } }, false],
141+
[
142+
'does not show Application metrics before the chart UI is available',
143+
{
144+
kind: 'Application',
145+
jsonData: { kind: 'Application', apiVersion: 'argoproj.io/v1alpha1' },
146+
},
147+
false,
148+
],
149+
[
150+
'rejects non-Argo Applications with the same kind',
151+
{ kind: 'Application', jsonData: { kind: 'Application', apiVersion: 'example.com/v1' } },
152+
false,
153+
],
141154
['rejects unknown kinds', { kind: 'VolcanoJob', jsonData: { kind: 'VolcanoJob' } }, false],
142155
['rejects missing resources', undefined, false],
143156
])('%s', (_, resource, expected) => {
144157
expect(supportsPrometheusMetrics(resource)).toBe(expected);
145158
});
146159
});
160+
161+
describe('isArgoCDApplication', () => {
162+
test.each([
163+
[
164+
'recognizes the Argo CD Application CRD',
165+
{
166+
kind: 'Application',
167+
jsonData: { kind: 'Application', apiVersion: 'argoproj.io/v1alpha1' },
168+
},
169+
true,
170+
],
171+
[
172+
'rejects another Application kind',
173+
{ kind: 'Application', jsonData: { kind: 'Application', apiVersion: 'example.com/v1' } },
174+
false,
175+
],
176+
['rejects an Application without an API version', { kind: 'Application' }, false],
177+
])('%s', (_, resource, expected) => {
178+
expect(isArgoCDApplication(resource)).toBe(expected);
179+
});
180+
});

prometheus/src/util.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ const resourceApiVersionRules: Record<string, RegExp> = {
174174
Revision: /^serving\.knative\.dev\/v1$/,
175175
};
176176

177+
/**
178+
* Returns whether a resource is the Argo CD Application CRD that exposes the
179+
* Application-specific Prometheus metrics. This is deliberately separate from
180+
* the chart allowlist: the chart UI is added by the follow-up feature.
181+
*/
182+
export function isArgoCDApplication(resource?: ResourceIdentity): boolean {
183+
return (
184+
getResourceKind(resource) === 'Application' &&
185+
getResourceApiVersion(resource) === 'argoproj.io/v1alpha1'
186+
);
187+
}
188+
177189
export function supportsPrometheusMetrics(resource?: ResourceIdentity): boolean {
178190
const kind = getResourceKind(resource);
179191

0 commit comments

Comments
 (0)