From 209a944d96f7a283c071d50fa5bdf0621498d4f6 Mon Sep 17 00:00:00 2001 From: Joyce Yee Date: Wed, 21 Apr 2021 14:35:24 +0000 Subject: [PATCH] Reduce the amount of API calls to k8s API server to fetch secrets Signed-off-by: Joyce Yee --- metricproviders/datadog/datadog.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/metricproviders/datadog/datadog.go b/metricproviders/datadog/datadog.go index 70825c6f94..74e6979383 100644 --- a/metricproviders/datadog/datadog.go +++ b/metricproviders/datadog/datadog.go @@ -18,6 +18,7 @@ import ( "github.com/argoproj/argo-rollouts/utils/evaluate" metricutil "github.com/argoproj/argo-rollouts/utils/metric" log "github.com/sirupsen/logrus" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" ) @@ -167,34 +168,34 @@ func (p *Provider) GarbageCollect(run *v1alpha1.AnalysisRun, metric v1alpha1.Met return nil } -func getSecretValue(key, ns string, kubeclientset kubernetes.Interface, emptyValueOk bool) (string, error) { +func getSecretValue(key string, k8sSecret *v1.Secret, emptyValueOk bool) (string, error) { envKey := strings.ToUpper(strings.ReplaceAll(key, "-", "_")) if value, ok := os.LookupEnv(fmt.Sprintf("DD_%s", envKey)); ok { return value, nil } - secret, err := kubeclientset.CoreV1().Secrets(ns).Get(context.TODO(), DatadogTokensSecretName, metav1.GetOptions{}) - if err != nil { - return "", err - } - _, valueMaybe := secret.Data[key] + _, valueMaybe := k8sSecret.Data[key] if emptyValueOk && !valueMaybe { return "", nil } - return string(secret.Data[key]), nil + return string(k8sSecret.Data[key]), nil } func NewDatadogProvider(logCtx log.Entry, kubeclientset kubernetes.Interface) (*Provider, error) { ns := defaults.Namespace() - apiKey, err := getSecretValue("api-key", ns, kubeclientset, false) + secret, err := kubeclientset.CoreV1().Secrets(ns).Get(context.TODO(), DatadogTokensSecretName, metav1.GetOptions{}) + if err != nil { + return nil, err + } + apiKey, err := getSecretValue("api-key", secret, false) if err != nil { return nil, err } - appKey, err := getSecretValue("app-key", ns, kubeclientset, false) + appKey, err := getSecretValue("app-key", secret, false) if err != nil { return nil, err } - address, err := getSecretValue("address", ns, kubeclientset, true) + address, err := getSecretValue("address", secret, true) if err != nil { return nil, err }