Skip to content

Commit

Permalink
Reduce the amount of API calls to k8s API server to fetch secrets
Browse files Browse the repository at this point in the history
Signed-off-by: Joyce Yee <[email protected]>
  • Loading branch information
Joyce Yee committed Apr 21, 2021
1 parent 6aad40f commit 209a944
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions metricproviders/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 209a944

Please sign in to comment.