1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

fix: metrics config defaults (#3387)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-03-14 15:21:50 +01:00 committed by GitHub
parent 154cea21c3
commit 5de83edafa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,9 +12,9 @@ import (
"k8s.io/client-go/kubernetes"
)
// read the conifgMap with name in env:METRICS_CONFIG
// this configmap stores the information associated with Kyverno's metrics exposure
const metricsCmName string = "METRICS_CONFIG"
// metricsConfigEnvVar is the name of an environment variable containing the name of the configmap
// that stores the information associated with Kyverno's metrics exposure
const metricsConfigEnvVar string = "METRICS_CONFIG"
// MetricsConfigData stores the metrics-related configuration
type MetricsConfigData struct {
@ -57,43 +57,43 @@ func (mcd *MetricsConfigData) GetMetricsConfigMapName() string {
// NewMetricsConfigData ...
func NewMetricsConfigData(rclient kubernetes.Interface, log logr.Logger) (*MetricsConfigData, error) {
// environment var is read at start only
if metricsCmName == "" {
log.Info("ConfigMap name not defined in env:METRICS_CONFIG: loading no default configuration")
}
cmName := os.Getenv(metricsConfigEnvVar)
mcd := MetricsConfigData{
client: rclient,
cmName: os.Getenv(metricsCmName),
cmName: cmName,
log: log,
metricsConfig: MetricsConfig{
metricsRefreshInterval: 0,
namespaces: namespacesConfig{
IncludeNamespaces: []string{},
ExcludeNamespaces: []string{},
},
},
}
kyvernoNamespace := getKyvernoNameSpace()
configMap, err := rclient.CoreV1().ConfigMaps(kyvernoNamespace).Get(context.TODO(), mcd.cmName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred while fetching the metrics configmap at %s/%s: %w", kyvernoNamespace, mcd.cmName, err)
}
// parsing namespace-related config from the config map
namespacesConfigStr, found := configMap.Data["namespaces"]
if !found {
mcd.metricsConfig.namespaces.IncludeNamespaces = []string{}
mcd.metricsConfig.namespaces.ExcludeNamespaces = []string{}
} else {
mcd.metricsConfig.namespaces.IncludeNamespaces, mcd.metricsConfig.namespaces.ExcludeNamespaces, err = parseIncludeExcludeNamespacesFromNamespacesConfig(namespacesConfigStr)
if cmName != "" {
kyvernoNamespace := getKyvernoNameSpace()
configMap, err := rclient.CoreV1().ConfigMaps(kyvernoNamespace).Get(context.TODO(), mcd.cmName, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error occurred while parsing the 'namespaces' field of metrics config map: %w", err)
return nil, fmt.Errorf("error occurred while fetching the metrics configmap at %s/%s: %w", kyvernoNamespace, mcd.cmName, err)
}
}
// parsing metricsRefreshInterval from the config map
metricsRefreshInterval, found := configMap.Data["metricsRefreshInterval"]
if found {
mcd.metricsConfig.metricsRefreshInterval, err = time.ParseDuration(metricsRefreshInterval)
if err != nil {
return nil, fmt.Errorf("error occurred while parsing metricsRefreshInterval: %w", err)
namespacesConfigStr, found := configMap.Data["namespaces"]
if found {
mcd.metricsConfig.namespaces.IncludeNamespaces, mcd.metricsConfig.namespaces.ExcludeNamespaces, err = parseIncludeExcludeNamespacesFromNamespacesConfig(namespacesConfigStr)
if err != nil {
return nil, fmt.Errorf("error occurred while parsing the 'namespaces' field of metrics config map: %w", err)
}
}
metricsRefreshInterval, found := configMap.Data["metricsRefreshInterval"]
if found {
mcd.metricsConfig.metricsRefreshInterval, err = time.ParseDuration(metricsRefreshInterval)
if err != nil {
return nil, fmt.Errorf("error occurred while parsing metricsRefreshInterval: %w", err)
}
}
} else {
mcd.metricsConfig.metricsRefreshInterval = 0
log.Info("ConfigMap name not defined in env:METRICS_CONFIG: loading no default configuration")
}
return &mcd, nil