Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ func (m *Manager) Run() {
wg := new(sync.WaitGroup)
processingCh := make(chan struct{}, flags.LintersLimit)

recordNames := collectRecordingRuleNames(m.Modules)

for _, module := range m.Modules {
module.SetRecordingRuleNames(recordNames)

processingCh <- struct{}{}

wg.Add(1)
Expand Down Expand Up @@ -195,6 +199,69 @@ func getLintersForModule(cfg *pkg.LintersSettings, errList *errors.LintRuleError
}
}

func collectRecordingRuleNames(modules []*module.Module) map[string]struct{} {
names := make(map[string]struct{})

for _, m := range modules {
for _, obj := range m.GetStorage() {
if obj.Unstructured.GetKind() != "PrometheusRule" {
continue
}

ispec, ok := obj.Unstructured.Object["spec"]
if !ok {
continue
}

spec, ok := ispec.(map[string]any)
if !ok {
continue
}

igroups, ok := spec["groups"]
if !ok {
continue
}

groups, ok := igroups.([]any)
if !ok {
continue
}

for _, ig := range groups {
group, ok := ig.(map[string]any)
if !ok {
continue
}

irules, ok := group["rules"]
if !ok {
continue
}

rules, ok := irules.([]any)
if !ok {
continue
}

for _, ir := range rules {
rule, ok := ir.(map[string]any)
if !ok {
continue
}

record, ok := rule["record"].(string)
if ok && record != "" {
names[record] = struct{}{}
}
}
}
}
}

return names
}

func (m *Manager) PrintResult() {
errs := m.errors.GetErrors()

Expand Down
11 changes: 11 additions & 0 deletions internal/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ func (m *Module) GetModuleConfig() *pkg.LintersSettings {
return m.linterConfig
}

func (m *Module) SetRecordingRuleNames(names map[string]struct{}) {
if m == nil || m.linterConfig == nil {
return
}

m.linterConfig.Templates.SourceLabelSettings.RecordingRuleNames = names
}

// remapLinterSettings converts configuration settings from the config package format
// to the pkg package format, mapping both rule-level configurations and exclusion rules
// across all linter domains (Container, Image, NoCyrillic, OpenAPI, Templates, RBAC, Hooks, Module).
Expand Down Expand Up @@ -339,6 +347,7 @@ func mapTemplatesRules(linterSettings *pkg.LintersSettings, configSettings *conf
rules.ServicePortRule.SetLevel(globalRules.ServicePortRule.Impact, fallbackImpact)
rules.ClusterDomainRule.SetLevel(globalRules.ClusterDomainRule.Impact, fallbackImpact)
rules.RegistryRule.SetLevel(globalRules.RegistryRule.Impact, fallbackImpact)
rules.SourceLabelRule.SetLevel(globalRules.SourceLabelRule.Impact, fallbackImpact)
}

// mapOpenAPIRules configures OpenAPI linter rules
Expand Down Expand Up @@ -454,6 +463,8 @@ func mapTemplatesExclusionsAndSettings(linterSettings *pkg.LintersSettings, conf
// Additional settings
linterSettings.Templates.PrometheusRuleSettings.Disable = configSettings.Templates.PrometheusRules.Disable
linterSettings.Templates.GrafanaDashboardsSettings.Disable = configSettings.Templates.GrafanaDashboards.Disable
linterSettings.Templates.SourceLabelSettings.Disable = configSettings.Templates.SourceLabel.Disable
linterSettings.Templates.SourceLabelSettings.AllowedMetrics = configSettings.Templates.SourceLabel.AllowedMetrics
}

// mapRBACExclusions maps RBAC linter exclusion rules
Expand Down
4 changes: 2 additions & 2 deletions internal/values/global-openapi/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ properties:
items:
type: string
x-examples:
- ["cert-manager", "vertical-pod-autoscaler", "vertical-pod-autoscaler-crd", "prometheus", "priority-class", "prometheus-crd", "operator-prometheus", "operator-prometheus"]
- ["cert-manager", "vertical-pod-autoscaler", "vertical-pod-autoscaler-crd", "prometheus", "priority-class", "prometheus-crd", "operator-prometheus", "operator-prometheus-crd"]
- ["cert-manager", "prometheus", "priority-class"]
x-dmt-default:
["cert-manager", "vertical-pod-autoscaler", "vertical-pod-autoscaler-crd", "prometheus", "priority-class", "prometheus-crd", "operator-prometheus", "operator-prometheus"]
["cert-manager", "vertical-pod-autoscaler", "vertical-pod-autoscaler-crd", "prometheus", "priority-class", "prometheus-crd", "operator-prometheus", "operator-prometheus-crd"]
Comment thread
ldmonster marked this conversation as resolved.
discovery:
additionalProperties: true
type: object
Expand Down
8 changes: 8 additions & 0 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ type TemplatesLinterConfig struct {
ExcludeRules TemplatesExcludeRules
PrometheusRuleSettings PrometheusRuleSettings
GrafanaDashboardsSettings GrafanaDashboardsSettings
SourceLabelSettings SourceLabelSettings
}

type SourceLabelSettings struct {
Disable bool
AllowedMetrics []string
RecordingRuleNames map[string]struct{}
}
type TemplatesLinterRules struct {
VPARule RuleConfig
Expand All @@ -140,6 +147,7 @@ type TemplatesLinterRules struct {
ServicePortRule RuleConfig
ClusterDomainRule RuleConfig
RegistryRule RuleConfig
SourceLabelRule RuleConfig
}

type PrometheusRuleSettings struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type TemplatesLinterRules struct {
ServicePortRule RuleConfig `mapstructure:"service-port"`
ClusterDomainRule RuleConfig `mapstructure:"cluster-domain"`
RegistryRule RuleConfig `mapstructure:"registry"`
SourceLabelRule RuleConfig `mapstructure:"source-label"`
}

func (c LinterConfig) IsWarn() bool {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,17 @@ type TemplatesSettings struct {
ExcludeRules TemplatesExcludeRules `mapstructure:"exclude-rules"`
GrafanaDashboards GrafanaDashboardsExcludeList `mapstructure:"grafana-dashboards"`
PrometheusRules PrometheusRulesExcludeList `mapstructure:"prometheus-rules"`
SourceLabel SourceLabelSettings `mapstructure:"source-label"`
Rules TemplatesLinterRules `mapstructure:"rules"`

Impact string `mapstructure:"impact"`
}

type SourceLabelSettings struct {
Disable bool `mapstructure:"disable"`
AllowedMetrics []string `mapstructure:"allowed-metrics"`
}

type TemplatesLinterRules struct {
VPARule RuleConfig `mapstructure:"vpa"`
PDBRule RuleConfig `mapstructure:"pdb"`
Expand All @@ -204,6 +210,7 @@ type TemplatesLinterRules struct {
ServicePortRule RuleConfig `mapstructure:"service-port"`
ClusterDomainRule RuleConfig `mapstructure:"cluster-domain"`
RegistryRule RuleConfig `mapstructure:"registry"`
SourceLabelRule RuleConfig `mapstructure:"source-label"`
}

type TemplatesExcludeRules struct {
Expand Down
Loading
Loading