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
)
2022-04-06 20:14:13 +02:00
func registerPolicyRuleInfoMetric (
2022-07-11 23:19:47 +05:30
m * metrics . MetricsConfig ,
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 )
}
2022-07-11 23:19:47 +05:30
includeNamespaces , excludeNamespaces := m . Config . GetIncludeNamespaces ( ) , m . Config . GetExcludeNamespaces ( )
2022-04-04 17:31:33 +02:00
if ( policyNamespace != "" && policyNamespace != "-" ) && utils . ContainsString ( excludeNamespaces , policyNamespace ) {
2022-08-18 18:54:59 +05:30
m . Log . V ( 2 ) . 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-08-18 18:54:59 +05:30
m . Log . V ( 2 ) . 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"
}
2022-07-11 23:19:47 +05:30
m . RecordPolicyRuleInfo ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , ruleName , ruleType , status , metricValue )
2021-05-15 18:10:11 +05:30
return nil
}
2022-07-11 23:19:47 +05:30
func AddPolicy ( m * metrics . MetricsConfig , 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 )
2022-07-11 23:19:47 +05:30
if err = registerPolicyRuleInfoMetric ( m , 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-07-11 23:19:47 +05:30
func RemovePolicy ( m * metrics . MetricsConfig , 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 )
2022-07-11 23:19:47 +05:30
if err = registerPolicyRuleInfoMetric ( m , validationMode , policyType , backgroundMode , namespace , name , ruleName , ruleType , PolicyRuleDeleted , ready ) ; err != nil {
2022-04-06 20:14:13 +02:00
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
}