2021-05-15 18:10:11 +05:30
package policyruleinfo
import (
"fmt"
2022-05-17 13:12:43 +02:00
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
2022-03-28 16:01:27 +02:00
"github.com/kyverno/kyverno/pkg/autogen"
2021-05-15 18:10:11 +05:30
"github.com/kyverno/kyverno/pkg/metrics"
2022-04-04 17:31:33 +02:00
"github.com/kyverno/kyverno/pkg/utils"
2021-05-15 18:10:11 +05:30
prom "github.com/prometheus/client_golang/prometheus"
)
2022-04-06 20:14:13 +02:00
func registerPolicyRuleInfoMetric (
pc * metrics . PromConfig ,
2021-05-15 18:10:11 +05:30
policyValidationMode metrics . PolicyValidationMode ,
policyType metrics . PolicyType ,
policyBackgroundMode metrics . PolicyBackgroundMode ,
policyNamespace , policyName , ruleName string ,
ruleType metrics . RuleType ,
metricChangeType PolicyRuleInfoMetricChangeType ,
2021-10-05 00:15:09 -07:00
ready bool ,
2021-05-15 18:10:11 +05:30
) error {
var metricValue float64
switch metricChangeType {
case PolicyRuleCreated :
metricValue = float64 ( 1 )
case PolicyRuleDeleted :
metricValue = float64 ( 0 )
default :
return fmt . Errorf ( "unknown metric change type found: %s" , metricChangeType )
}
2021-09-11 03:09:12 +05:30
includeNamespaces , excludeNamespaces := pc . Config . GetIncludeNamespaces ( ) , pc . Config . GetExcludeNamespaces ( )
2022-04-04 17:31:33 +02:00
if ( policyNamespace != "" && policyNamespace != "-" ) && utils . ContainsString ( excludeNamespaces , policyNamespace ) {
2022-04-29 19:33:08 +02:00
metrics . Logger ( ) . Info ( fmt . Sprintf ( "Skipping the registration of kyverno_policy_rule_info_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml" , policyNamespace , excludeNamespaces ) )
2021-09-11 03:09:12 +05:30
return nil
}
2022-04-04 17:31:33 +02:00
if ( policyNamespace != "" && policyNamespace != "-" ) && len ( includeNamespaces ) > 0 && ! utils . ContainsString ( includeNamespaces , policyNamespace ) {
2022-04-29 19:33:08 +02:00
metrics . Logger ( ) . Info ( fmt . Sprintf ( "Skipping the registration of kyverno_policy_rule_info_total metric as the operation belongs to the namespace '%s' which is not one of 'namespaces.include' %+v in values.yaml" , policyNamespace , includeNamespaces ) )
2021-09-11 03:09:12 +05:30
return nil
}
2021-05-15 18:10:11 +05:30
if policyType == metrics . Cluster {
policyNamespace = "-"
}
2021-10-05 00:15:09 -07:00
status := "false"
if ready {
status = "true"
}
2021-09-11 03:09:12 +05:30
pc . Metrics . PolicyRuleInfo . With ( prom . Labels {
2021-05-15 18:10:11 +05:30
"policy_validation_mode" : string ( policyValidationMode ) ,
"policy_type" : string ( policyType ) ,
"policy_background_mode" : string ( policyBackgroundMode ) ,
"policy_namespace" : policyNamespace ,
"policy_name" : policyName ,
"rule_name" : ruleName ,
"rule_type" : string ( ruleType ) ,
2021-10-05 00:15:09 -07:00
"status_ready" : status ,
2021-05-15 18:10:11 +05:30
} ) . Set ( metricValue )
return nil
}
2022-05-17 13:12:43 +02:00
func AddPolicy ( pc * metrics . PromConfig , policy kyvernov1 . PolicyInterface ) error {
2022-04-06 20:14:13 +02:00
name , namespace , policyType , backgroundMode , validationMode , err := metrics . GetPolicyInfos ( policy )
if err != nil {
return err
}
ready := policy . IsReady ( )
for _ , rule := range autogen . ComputeRules ( policy ) {
ruleName := rule . Name
ruleType := metrics . ParseRuleType ( rule )
if err = registerPolicyRuleInfoMetric ( pc , validationMode , policyType , backgroundMode , namespace , name , ruleName , ruleType , PolicyRuleCreated , ready ) ; err != nil {
2021-05-15 18:10:11 +05:30
return err
}
}
2022-04-06 20:14:13 +02:00
return nil
2021-05-15 18:10:11 +05:30
}
2022-05-17 13:12:43 +02:00
func RemovePolicy ( pc * metrics . PromConfig , policy kyvernov1 . PolicyInterface ) error {
2022-04-06 20:14:13 +02:00
name , namespace , policyType , backgroundMode , validationMode , err := metrics . GetPolicyInfos ( policy )
if err != nil {
return err
}
ready := policy . IsReady ( )
for _ , rule := range autogen . ComputeRules ( policy ) {
ruleName := rule . Name
ruleType := metrics . ParseRuleType ( rule )
if err = registerPolicyRuleInfoMetric ( pc , validationMode , policyType , backgroundMode , namespace , name , ruleName , ruleType , PolicyRuleDeleted , ready ) ; err != nil {
return err
2021-05-15 18:10:11 +05:30
}
}
2022-04-06 20:14:13 +02:00
return nil
2021-05-15 18:10:11 +05:30
}