|
8 | 8 | "fmt" |
9 | 9 | "os" |
10 | 10 | "reflect" |
| 11 | + "regexp" |
11 | 12 | "strconv" |
12 | 13 | "strings" |
13 | 14 | "time" |
@@ -556,9 +557,9 @@ func SanitizeName(input string) string { |
556 | 557 | }, strings.ToLower(input)) |
557 | 558 | } |
558 | 559 |
|
559 | | -func GetClusterName(conf *confmap.Conf) string { |
560 | | - val, ok := GetString(conf, ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, "cluster_name")) |
561 | | - if ok && val != "" { |
| 560 | +func GetClusterName(conf *confmap.Conf, key string) string { |
| 561 | + // Check any config keys passed |
| 562 | + if val, ok := GetString(conf, key); ok && val != "" { |
562 | 563 | return val |
563 | 564 | } |
564 | 565 |
|
@@ -598,3 +599,17 @@ func EscapeDollarDigit(s string) string { |
598 | 599 |
|
599 | 600 | // OtelClusterNameKey is the config key for the root-level cluster name under opentelemetry. |
600 | 601 | var OtelClusterNameKey = ConfigKey(OpenTelemetryKey, ClusterNameKey) |
| 602 | + |
| 603 | +// LegacyClusterNameKey is the config key for the cluster name in the V1 config path. |
| 604 | +var LegacyClusterNameKey = ConfigKey(LogsKey, MetricsCollectedKey, KubernetesKey, ClusterNameKey) |
| 605 | + |
| 606 | +// ClusterNameRegex validates cluster names. |
| 607 | +var ClusterNameRegex = regexp.MustCompile(`^[0-9A-Za-z][A-Za-z0-9\-_]*$`) |
| 608 | + |
| 609 | +// ValidateClusterName returns an error if the cluster name does not match the expected pattern. |
| 610 | +func ValidateClusterName(name string) error { |
| 611 | + if !ClusterNameRegex.MatchString(name) { |
| 612 | + return fmt.Errorf("cluster_name %q is invalid: must match pattern %s", name, ClusterNameRegex.String()) |
| 613 | + } |
| 614 | + return nil |
| 615 | +} |
0 commit comments