Skip to content

Commit 31bbf1e

Browse files
Merge pull request #677 from PeterYurkovich/ou-1113
OU-1113: handle all ns selected for tenancy user
2 parents e35027e + 552c203 commit 31bbf1e

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

web/locales/en/plugin__monitoring-plugin.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
"1d": "1d",
8484
"2d": "2d",
8585
"1w": "1w",
86+
"Comment is required.": "Comment is required.",
87+
"Alertmanager URL not set": "Alertmanager URL not set",
88+
"Error saving Silence": "Error saving Silence",
89+
"Forbidden: Missing permissions for silences": "Forbidden: Missing permissions for silences",
8690
"Silences temporarily mute alerts based on a set of label selectors that you define. Notifications will not be sent for alerts that match all the listed values or regular expressions.": "Silences temporarily mute alerts based on a set of label selectors that you define. Notifications will not be sent for alerts that match all the listed values or regular expressions.",
8791
"Duration": "Duration",
8892
"Silence alert from...": "Silence alert from...",
@@ -135,9 +139,6 @@
135139
"404: Not Found": "404: Not Found",
136140
"{{labels}} content is not available in the catalog at this time due to loading failures.": "{{labels}} content is not available in the catalog at this time due to loading failures.",
137141
"No datapoints found.": "No datapoints found.",
138-
"Namespaces": "Namespaces",
139-
"Project": "Project",
140-
"Projects": "Projects",
141142
"Create new option \"{{option}}\"": "Create new option \"{{option}}\"",
142143
"Filter options": "Filter options",
143144
"Clear input value": "Clear input value",
@@ -177,6 +178,8 @@
177178
"No results match the filter criteria.": "No results match the filter criteria.",
178179
"Clear filters": "Clear filters",
179180
"Select project...": "Select project...",
181+
"Projects": "Projects",
182+
"Project": "Project",
180183
"Dashboard": "Dashboard",
181184
"Refresh off": "Refresh off",
182185
"{{count}} second_one": "{{count}} second",

web/src/components/alerting/SilenceForm.tsx

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
consoleFetchJSON,
33
DocumentTitle,
4-
NamespaceBar,
54
useActiveNamespace,
65
} from '@openshift-console/dynamic-plugin-sdk';
76
import {
@@ -51,7 +50,7 @@ import { ExternalLink } from '../console/utils/link';
5150
import { useBoolean } from '../hooks/useBoolean';
5251
import { getSilenceAlertUrl, usePerspective } from '../hooks/usePerspective';
5352
import { DataTestIDs } from '../data-test';
54-
import { getAlertmanagerSilencesUrl } from '../utils';
53+
import { ALL_NAMESPACES_KEY, getAlertmanagerSilencesUrl } from '../utils';
5554
import { useAlerts } from '../../hooks/useAlerts';
5655
import { useMonitoring } from '../../hooks/useMonitoring';
5756

@@ -136,6 +135,7 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
136135
const [namespace] = useActiveNamespace();
137136
const { prometheus } = useMonitoring();
138137
const navigate = useNavigate();
138+
const isPageNamespaceLocked = isNamespaced && namespace !== ALL_NAMESPACES_KEY;
139139

140140
const durations = useMemo(() => {
141141
return {
@@ -187,7 +187,7 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
187187
// Since the namespace matcher MUST be the same as the namespace the request is being
188188
// made in, we remove the namespace value here and re-add it before sending the request
189189
const [matchers, setMatchers] = useState<Array<Matcher>>(
190-
(isNamespaced
190+
(isPageNamespaceLocked
191191
? (defaults.matchers as Matcher[])?.filter((matcher) => matcher.name !== 'namespace')
192192
: defaults.matchers) ?? [{ isRegex: false, isEqual: true, name: '', value: '' }],
193193
);
@@ -211,7 +211,7 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
211211
};
212212

213213
const setMatcherField = (i: number, field: string, v: string | boolean): void => {
214-
const newMatchers = _.clone(matchers);
214+
const newMatchers = _.cloneDeep(matchers);
215215
_.set(newMatchers, [i, field], v);
216216
setMatchers(newMatchers);
217217
};
@@ -221,11 +221,6 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
221221
};
222222

223223
const removeMatcher = (i: number): void => {
224-
// If we require the namespace don't allow removing it
225-
if (isNamespaced && i === 0) {
226-
return;
227-
}
228-
229224
const newMatchers = _.clone(matchers);
230225
newMatchers.splice(i, 1);
231226

@@ -242,17 +237,17 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
242237

243238
// Don't allow comments to only contain whitespace
244239
if (_.trim(comment) === '') {
245-
setError('Comment is required.');
240+
setError(t('Comment is required.'));
246241
return;
247242
}
248243

249244
const url = getAlertmanagerSilencesUrl({
250245
prometheus,
251246
namespace,
252-
useTenancyPath: isNamespaced,
247+
useTenancyPath: isPageNamespaceLocked,
253248
});
254249
if (!url) {
255-
setError('Alertmanager URL not set');
250+
setError(t('Alertmanager URL not set'));
256251
return;
257252
}
258253

@@ -269,7 +264,7 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
269264
createdBy,
270265
endsAt: saveEndsAt.toISOString(),
271266
id: defaults.id,
272-
matchers: isNamespaced
267+
matchers: isPageNamespaceLocked
273268
? matchers.concat({
274269
name: 'namespace',
275270
value: namespace,
@@ -282,7 +277,11 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
282277

283278
consoleFetchJSON
284279
.post(
285-
getAlertmanagerSilencesUrl({ prometheus, namespace, useTenancyPath: isNamespaced }),
280+
getAlertmanagerSilencesUrl({
281+
prometheus,
282+
namespace,
283+
useTenancyPath: isPageNamespaceLocked,
284+
}),
286285
body,
287286
)
288287
.then(({ silenceID }) => {
@@ -291,10 +290,13 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
291290
navigate(getSilenceAlertUrl(perspective, silenceID));
292291
})
293292
.catch((err) => {
294-
const errorMessage =
293+
let errorMessage =
295294
typeof _.get(err, 'json') === 'string'
296295
? _.get(err, 'json')
297-
: err.message || 'Error saving Silence';
296+
: err.message || t('Error saving Silence');
297+
if (errorMessage === 'Forbidden') {
298+
errorMessage = t('Forbidden: Missing permissions for silences');
299+
}
298300
setError(errorMessage);
299301
setInProgress(false);
300302
});
@@ -309,7 +311,6 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
309311
return (
310312
<>
311313
<DocumentTitle>{title}</DocumentTitle>
312-
{isNamespaced && <NamespaceBar />}
313314
<PageSection hasBodyWrapper={false}>
314315
<Title headingLevel="h1">{title}</Title>
315316
<HelperText>
@@ -425,7 +426,7 @@ const SilenceForm_: FC<SilenceFormProps> = ({ defaults, Info, title, isNamespace
425426
</HelperText>
426427
</FormHelperText>
427428

428-
{isNamespaced && (
429+
{isPageNamespaceLocked && (
429430
<Grid key={'namespace'} sm={12} md={4} hasGutter>
430431
<GridItem>
431432
<FormGroup label={t('Label name')}>

0 commit comments

Comments
 (0)