2021-05-15 18:48:38 +05:30
package policychanges
import (
"fmt"
2021-06-14 13:42:57 -07:00
2021-10-29 18:13:20 +02:00
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
2021-05-15 18:48:38 +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:48:38 +05:30
prom "github.com/prometheus/client_golang/prometheus"
)
2021-09-11 03:09:12 +05:30
func ( pc PromConfig ) registerPolicyChangesMetric (
2021-05-15 18:48:38 +05:30
policyValidationMode metrics . PolicyValidationMode ,
policyType metrics . PolicyType ,
policyBackgroundMode metrics . PolicyBackgroundMode ,
policyNamespace , policyName string ,
policyChangeType PolicyChangeType ,
) error {
if policyType == metrics . Cluster {
policyNamespace = "-"
}
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 ) {
2021-09-11 03:09:12 +05:30
pc . Log . Info ( fmt . Sprintf ( "Skipping the registration of kyverno_policy_changes_total metric as the operation belongs to the namespace '%s' which is one of 'namespaces.exclude' %+v in values.yaml" , policyNamespace , excludeNamespaces ) )
return nil
}
2022-04-04 17:31:33 +02:00
if ( policyNamespace != "" && policyNamespace != "-" ) && len ( includeNamespaces ) > 0 && ! utils . ContainsString ( includeNamespaces , policyNamespace ) {
2021-09-11 03:09:12 +05:30
pc . Log . Info ( fmt . Sprintf ( "Skipping the registration of kyverno_policy_changes_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
}
pc . Metrics . PolicyChanges . With ( prom . Labels {
2021-05-15 18:48:38 +05:30
"policy_validation_mode" : string ( policyValidationMode ) ,
"policy_type" : string ( policyType ) ,
"policy_background_mode" : string ( policyBackgroundMode ) ,
"policy_namespace" : policyNamespace ,
"policy_name" : policyName ,
"policy_change_type" : string ( policyChangeType ) ,
2021-07-23 21:46:50 +05:30
} ) . Inc ( )
2021-05-15 18:48:38 +05:30
return nil
}
2021-09-11 03:09:12 +05:30
func ( pc PromConfig ) RegisterPolicy ( policy interface { } , policyChangeType PolicyChangeType ) error {
2021-05-15 18:48:38 +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:48:38 +05:30
policyType := metrics . Cluster
policyNamespace := "" // doesn't matter for cluster policy
2022-03-21 10:18:54 +01:00
policyName := inputPolicy . GetName ( )
2021-09-11 03:09:12 +05:30
if err = pc . registerPolicyChangesMetric ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , policyChangeType ) ; err != nil {
2021-05-15 18:48:38 +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:48:38 +05:30
policyType := metrics . Namespaced
2022-03-21 10:18:54 +01:00
policyNamespace := inputPolicy . GetNamespace ( )
policyName := inputPolicy . GetName ( )
2021-09-11 03:09:12 +05:30
if err = pc . registerPolicyChangesMetric ( policyValidationMode , policyType , policyBackgroundMode , policyNamespace , policyName , policyChangeType ) ; err != nil {
2021-05-15 18:48:38 +05:30
return err
}
return nil
default :
return fmt . Errorf ( "wrong input type provided %T. Only kyverno.Policy and kyverno.ClusterPolicy allowed" , inputPolicy )
}
}