Alerting: Tidy up k8s utilities/constants (#91571)

This commit is contained in:
Tom Ratcliffe
2024-08-06 14:57:43 +03:00
committed by GitHub
parent b4a23e097a
commit 2be2f8a214
7 changed files with 10 additions and 38 deletions
@@ -14,7 +14,7 @@ import {
import { BaseAlertmanagerArgs, Skippable } from 'app/features/alerting/unified/types/hooks';
import { cloudNotifierTypes } from 'app/features/alerting/unified/utils/cloud-alertmanager-notifier-types';
import { GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/datasource';
import { getNamespace, shouldUseK8sApi } from 'app/features/alerting/unified/utils/k8s/utils';
import { getK8sNamespace, shouldUseK8sApi } from 'app/features/alerting/unified/utils/k8s/utils';
import { alertmanagerApi } from '../../api/alertmanagerApi';
import { onCallApi } from '../../api/onCallApi';
@@ -94,7 +94,7 @@ const useK8sContactPoints = (...[hookParams, queryOptions]: Parameters<typeof us
* or the `/notifications/receivers` endpoint
*/
const useFetchGrafanaContactPoints = ({ skip }: Skippable = {}) => {
const namespace = getNamespace();
const namespace = getK8sNamespace();
const useK8sApi = shouldUseK8sApi(GRAFANA_RULES_SOURCE_NAME);
const grafanaResponse = useGetContactPointsListQuery(undefined, { skip: skip || useK8sApi });
@@ -11,7 +11,7 @@ import {
useUpdateMuteTiming,
useValidateMuteTiming,
} from 'app/features/alerting/unified/components/mute-timings/useMuteTimings';
import { shouldUseK8sApi } from 'app/features/alerting/unified/components/mute-timings/util';
import { shouldUseK8sApi } from 'app/features/alerting/unified/utils/k8s/utils';
import { useAlertmanager } from '../../state/AlertmanagerContext';
import { MuteTimingFields } from '../../types/mute-timing-form';
@@ -9,6 +9,7 @@ import {
ALL_MUTE_TIMINGS,
useExportMuteTimingsDrawer,
} from 'app/features/alerting/unified/components/mute-timings/useExportMuteTimingsDrawer';
import { PROVENANCE_ANNOTATION } from 'app/features/alerting/unified/utils/k8s/constants';
import { Authorize } from '../../components/Authorize';
import { AlertmanagerAction, useAlertmanagerAbilities, useAlertmanagerAbility } from '../../hooks/useAbilities';
@@ -18,7 +19,7 @@ import { EmptyAreaWithCTA } from '../EmptyAreaWithCTA';
import { ProvisioningBadge } from '../Provisioning';
import { Spacer } from '../Spacer';
import { MuteTiming, PROVENANCE_ANNOTATION, useMuteTimings } from './useMuteTimings';
import { MuteTiming, useMuteTimings } from './useMuteTimings';
import { renderTimeIntervals } from './util';
interface MuteTimingsTableProps {
@@ -3,11 +3,7 @@ import { useEffect } from 'react';
import { alertmanagerApi } from 'app/features/alerting/unified/api/alertmanagerApi';
import { timeIntervalsApi } from 'app/features/alerting/unified/api/timeIntervalsApi';
import {
getK8sNamespace,
mergeTimeIntervals,
shouldUseK8sApi,
} from 'app/features/alerting/unified/components/mute-timings/util';
import { mergeTimeIntervals } from 'app/features/alerting/unified/components/mute-timings/util';
import {
ComGithubGrafanaGrafanaPkgApisAlertingNotificationsV0Alpha1TimeInterval,
ReadNamespacedTimeIntervalApiResponse,
@@ -16,6 +12,8 @@ import { deleteMuteTimingAction, updateAlertManagerConfigAction } from 'app/feat
import { BaseAlertmanagerArgs } from 'app/features/alerting/unified/types/hooks';
import { renameMuteTimings } from 'app/features/alerting/unified/utils/alertmanager';
import { GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/datasource';
import { PROVENANCE_ANNOTATION, PROVENANCE_NONE } from 'app/features/alerting/unified/utils/k8s/constants';
import { getK8sNamespace, shouldUseK8sApi } from 'app/features/alerting/unified/utils/k8s/utils';
import { MuteTimeInterval } from 'app/plugins/datasource/alertmanager/types';
import { useDispatch } from 'app/types';
@@ -37,12 +35,6 @@ export type MuteTiming = MuteTimeInterval & {
metadata?: ReadNamespacedTimeIntervalApiResponse['metadata'];
};
/** Name of the custom annotation label used in k8s APIs for us to discern if a given entity was provisioned */
export const PROVENANCE_ANNOTATION = 'grafana.com/provenance';
/** Value of `PROVENANCE_ANNOTATION` given for non-provisioned intervals */
export const PROVENANCE_NONE = 'none';
/** Alias for generated kuberenetes Alerting API Server type */
type TimeIntervalV0Alpha1 = ComGithubGrafanaGrafanaPkgApisAlertingNotificationsV0Alpha1TimeInterval;
@@ -1,9 +1,7 @@
import moment from 'moment';
import { Fragment } from 'react';
import { config } from '@grafana/runtime';
import { Stack } from '@grafana/ui';
import { GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/datasource';
import { AlertmanagerConfig, MuteTimeInterval } from 'app/plugins/datasource/alertmanager/types';
import {
@@ -84,19 +82,3 @@ export function renderTimeIntervals(muteTiming: MuteTimeInterval) {
</Stack>
);
}
/**
* Get the correct namespace to use when using the K8S API.
*/
export const getK8sNamespace = () => config.namespace;
/**
* Should we call the kubernetes-style API for managing the time intervals?
*
* Requires the alertmanager referenced being the Grafana AM,
* and the `alertingApiServer` feature toggle being enabled
*/
export const shouldUseK8sApi = (alertmanager?: string) => {
const featureToggleEnabled = config.featureToggles.alertingApiServer;
return featureToggleEnabled && alertmanager === GRAFANA_RULES_SOURCE_NAME;
};
@@ -1,11 +1,8 @@
import { HttpResponse, http } from 'msw';
import {
PROVENANCE_ANNOTATION,
PROVENANCE_NONE,
} from 'app/features/alerting/unified/components/mute-timings/useMuteTimings';
import { ALERTING_API_SERVER_BASE_URL, getK8sResponse } from 'app/features/alerting/unified/mocks/server/utils';
import { ComGithubGrafanaGrafanaPkgApisAlertingNotificationsV0Alpha1TimeInterval } from 'app/features/alerting/unified/openapi/timeIntervalsApi.gen';
import { PROVENANCE_ANNOTATION, PROVENANCE_NONE } from 'app/features/alerting/unified/utils/k8s/constants';
/** UID of a time interval that we expect to follow all happy paths within tests/mocks */
export const TIME_INTERVAL_UID_HAPPY_PATH = 'f4eae7a4895fa786';
@@ -4,7 +4,7 @@ import { GRAFANA_RULES_SOURCE_NAME } from 'app/features/alerting/unified/utils/d
/**
* Get the correct namespace to use when using the K8S API.
*/
export const getNamespace = () => config.namespace;
export const getK8sNamespace = () => config.namespace;
/**
* Should we call the kubernetes-style API for managing alertmanager entities?