2021-05-15 18:10:11 +05:30
package policyruleinfo
import (
"fmt"
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/metrics"
prom "github.com/prometheus/client_golang/prometheus"
)
2021-09-11 03:09:12 +05:30
func ( pc PromConfig ) registerPolicyRuleInfoMetric (
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 ( )
if ( policyNamespace != "" && policyNamespace != "-" ) && metrics . ElementInSlice ( policyNamespace , excludeNamespaces ) {
pc . Log . 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 ) )
return nil
}
if ( policyNamespace != "" && policyNamespace != "-" ) && len ( includeNamespaces ) > 0 && ! metrics . ElementInSlice ( policyNamespace , includeNamespaces ) {
pc . Log . 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 ) )
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
}
2021-09-11 03:09:12 +05:30
func ( pc PromConfig ) AddPolicy ( policy interface { } ) error {
2021-05-15 18:10:11 +05:30
switch inputPolicy := policy . ( type ) {
case * kyverno . ClusterPolicy :
policyValidationMode , err := metrics . ParsePolicyValidationMode ( inputPolicy . Spec . ValidationFailureAction )
if err != nil {
return err
}
2021-06-14 13:42:57 -07:00
policyBackgroundMode := metrics . ParsePolicyBackgroundMode ( inputPolicy . Spec . Background )
2021-05-15 18:10:11 +05:30
policyType := metrics . Cluster
policyNamespace := "" // doesn't matter for cluster policy
policyName := inputPolicy . ObjectMeta . Name
2021-10-05 00:15:09 -07:00
ready := inputPolicy . Status . Ready
2021-05-15 18:10:11 +05:30
// registering the metrics on a per-rule basis
for _ , rule := range inputPolicy . Spec . Rules {
ruleName := rule . Name
ruleType := metrics . ParseRuleType ( rule )
2021-10-05 00:15:09 -07:00
if err = pc . registerPolicyRuleInfoMetric ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , ruleName , ruleType , PolicyRuleCreated , ready ) ; err != nil {
2021-05-15 18:10:11 +05:30
return err
}
}
return nil
case * kyverno . Policy :
policyValidationMode , err := metrics . ParsePolicyValidationMode ( inputPolicy . Spec . ValidationFailureAction )
if err != nil {
return err
}
2021-06-14 13:42:57 -07:00
policyBackgroundMode := metrics . ParsePolicyBackgroundMode ( inputPolicy . Spec . Background )
2021-05-15 18:10:11 +05:30
policyType := metrics . Namespaced
policyNamespace := inputPolicy . ObjectMeta . Namespace
policyName := inputPolicy . ObjectMeta . Name
2021-10-05 00:15:09 -07:00
ready := inputPolicy . Status . Ready
2021-05-15 18:10:11 +05:30
// registering the metrics on a per-rule basis
for _ , rule := range inputPolicy . Spec . Rules {
ruleName := rule . Name
ruleType := metrics . ParseRuleType ( rule )
2021-10-05 00:15:09 -07:00
if err = pc . registerPolicyRuleInfoMetric ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , ruleName , ruleType , PolicyRuleCreated , ready ) ; err != nil {
2021-05-15 18:10:11 +05:30
return err
}
}
return nil
default :
return fmt . Errorf ( "wrong input type provided %T. Only kyverno.Policy and kyverno.ClusterPolicy allowed" , inputPolicy )
}
}
2021-09-11 03:09:12 +05:30
func ( pc PromConfig ) RemovePolicy ( policy interface { } ) error {
2021-05-15 18:10:11 +05:30
switch inputPolicy := policy . ( type ) {
case * kyverno . ClusterPolicy :
for _ , rule := range inputPolicy . Spec . Rules {
policyValidationMode , err := metrics . ParsePolicyValidationMode ( inputPolicy . Spec . ValidationFailureAction )
if err != nil {
return err
}
2021-06-14 13:42:57 -07:00
policyBackgroundMode := metrics . ParsePolicyBackgroundMode ( inputPolicy . Spec . Background )
2021-05-15 18:10:11 +05:30
policyType := metrics . Cluster
policyNamespace := "" // doesn't matter for cluster policy
policyName := inputPolicy . ObjectMeta . Name
ruleName := rule . Name
ruleType := metrics . ParseRuleType ( rule )
2021-10-05 00:15:09 -07:00
ready := inputPolicy . Status . Ready
2021-05-15 18:10:11 +05:30
2021-10-05 00:15:09 -07:00
if err = pc . registerPolicyRuleInfoMetric ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , ruleName , ruleType , PolicyRuleDeleted , ready ) ; err != nil {
2021-05-15 18:10:11 +05:30
return err
}
}
return nil
case * kyverno . Policy :
for _ , rule := range inputPolicy . Spec . Rules {
policyValidationMode , err := metrics . ParsePolicyValidationMode ( inputPolicy . Spec . ValidationFailureAction )
if err != nil {
return err
}
2021-06-14 13:42:57 -07:00
policyBackgroundMode := metrics . ParsePolicyBackgroundMode ( inputPolicy . Spec . Background )
2021-05-15 18:10:11 +05:30
policyType := metrics . Namespaced
policyNamespace := inputPolicy . ObjectMeta . Namespace
policyName := inputPolicy . ObjectMeta . Name
ruleName := rule . Name
ruleType := metrics . ParseRuleType ( rule )
2021-10-05 00:15:09 -07:00
ready := inputPolicy . Status . Ready
2021-05-15 18:10:11 +05:30
2021-10-05 00:15:09 -07:00
if err = pc . registerPolicyRuleInfoMetric ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , ruleName , ruleType , PolicyRuleDeleted , ready ) ; err != nil {
2021-05-15 18:10:11 +05:30
return err
}
}
return nil
default :
return fmt . Errorf ( "wrong input type provided %T. Only kyverno.Policy and kyverno.ClusterPolicy allowed" , inputPolicy )
}
}