<commit_before>package collector

import (
	"github.com/giantswarm/microerror"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"

	"github.com/giantswarm/azure-operator/client"
	"github.com/giantswarm/azure-operator/service/credential"
)

const (
	// credentialNamespace is the namespace in which we store credentials.
	credentialNamespace = "giantswarm"

	// credentialLabelSelector is the label selector we use to retrieve credentials.
	credentialLabelSelector = "app=credentiald"
)

// getClientSets fetches all unique Azure clients.
func getClientSets(k8sClient kubernetes.Interface, environmentName string) ([]*client.AzureClientSet, error) {
	credentialList, err := k8sClient.CoreV1().Secrets(credentialNamespace).List(metav1.ListOptions{
		LabelSelector: credentialLabelSelector,
	})
	if err != nil {
		return nil, microerror.Mask(err)
	}

	clientSets := []*client.AzureClientSet{}

	for _, secret := range credentialList.Items {
		config, err := credential.GetAzureConfig(k8sClient, secret.Name, secret.Namespace)
		if err != nil {
			return nil, microerror.Mask(err)
		}

		config.EnvironmentName = environmentName

		clientSet, err := client.NewAzureClientSet(*config)
		if err != nil {
			return nil, microerror.Mask(err)
		}

		clientSets = append(clientSets, clientSet)
	}

	return clientSets, nil
}<commit_msg>Avoid collecting metrics twice (#716)<commit_after>package collector

import (
	"github.com/giantswarm/microerror"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"

	"github.com/giantswarm/azure-operator/client"
	"github.com/giantswarm/azure-operator/service/credential"
)

const (
	// credentialNamespace is the namespace in which we store credentials.
	credentialNamespace = "giantswarm"

	// credentialLabelSelector is the label selector we use to retrieve credentials.
	credentialLabelSelector = "app=credentiald"
)

// getClientSets fetches all Azure clients, grouped by subscription id.
// If two secrets use the same subscription but different client id, only one is returned
func getClientSets(k8sClient kubernetes.Interface, environmentName string) (map[string]*client.AzureClientSet, error) {
	credentialList, err := k8sClient.CoreV1().Secrets(credentialNamespace).List(metav1.ListOptions{
		LabelSelector: credentialLabelSelector,
	})
	if err != nil {
		return nil, microerror.Mask(err)
	}

	clientSets := map[string]*client.AzureClientSet{}

	for _, secret := range credentialList.Items {
		config, err := credential.GetAzureConfig(k8sClient, secret.Name, secret.Namespace)
		if err != nil {
			return nil, microerror.Mask(err)
		}

		config.EnvironmentName = environmentName

		clientSet, err := client.NewAzureClientSet(*config)
		if err != nil {
			return nil, microerror.Mask(err)
		}

		clientSets[config.SubscriptionID] = clientSet
	}

	return clientSets, nil
}
{'fraction_non_alphanumeric': 0.10664335664335664, 'fraction_numerical': 0.0073426573426573424, 'mean_word_length': 5.0358649789029535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7291344', 'n_tokens_mistral': 954, 'n_tokens_neox': 889, 'n_words': 203}