| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dyncfg |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "unicode" |
| 9 | ) |
| 10 | |
| 11 | // JobNameRuleStrict rejects spaces, dots, and colons. |
| 12 | // Use for collector job names, which must not conflict with dyncfg template/job |
| 13 | // ID separators (':') or module hierarchy ('.'). |
| 14 | func JobNameRuleStrict(name string) error { |
| 15 | if err := rejectSpacesAndColons(name); err != nil { |
| 16 | return err |
| 17 | } |
| 18 | for _, r := range name { |
| 19 | if r == '.' { |
| 20 | return fmt.Errorf("contains '%c'", r) |
| 21 | } |
| 22 | } |
| 23 | return nil |
| 24 | } |
| 25 | |
| 26 | // JobNameRuleAllowDots rejects spaces and colons but allows dots. |
| 27 | // Use for service discovery, vnode, and secretstore names where dotted identifiers |
| 28 | // are legitimate (e.g. hostnames, FQDNs). |
| 29 | func JobNameRuleAllowDots(name string) error { |
| 30 | return rejectSpacesAndColons(name) |
| 31 | } |
| 32 | |
| 33 | func rejectSpacesAndColons(name string) error { |
| 34 | for _, r := range name { |
| 35 | if unicode.IsSpace(r) { |
| 36 | return errors.New("contains spaces") |
| 37 | } |
| 38 | if r == ':' { |
| 39 | return fmt.Errorf("contains '%c'", r) |
| 40 | } |
| 41 | } |
| 42 | return nil |
| 43 | } |