forked from kyma-project/control-plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_provider.go
61 lines (50 loc) · 2.02 KB
/
account_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package hyperscaler
import (
"fmt"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type AccountProvider interface {
GardenerCredentials(hyperscalerType Type, tenantName string) (Credentials, error)
}
type accountProvider struct {
kubernetesInterface kubernetes.Interface
compassPool AccountPool
gardenerPool AccountPool
}
// NewAccountProvider returns a new AccountProvider
func NewAccountProvider(kubernetesInterface kubernetes.Interface, compassPool AccountPool, gardenerPool AccountPool) AccountProvider {
return &accountProvider{
kubernetesInterface: kubernetesInterface,
compassPool: compassPool,
gardenerPool: gardenerPool,
}
}
// GardenerCredentials returns credentials for Gardener account
func (p *accountProvider) GardenerCredentials(hyperscalerType Type, tenantName string) (Credentials, error) {
if p.gardenerPool == nil {
return Credentials{},
fmt.Errorf("failed to get Gardener Credentials. Gardener Account pool is not configured for tenant: %s", tenantName)
}
secretBinding, err := p.gardenerPool.CredentialsSecretBinding(hyperscalerType, tenantName)
if err != nil {
return Credentials{}, errors.Wrap(err, "getting credentials secret binding")
}
return p.credentialsFromBoundSecret(secretBinding, hyperscalerType, tenantName)
}
func (p *accountProvider) credentialsFromBoundSecret(secretBinding *v1beta1.SecretBinding, hyperscalerType Type, tenantName string) (Credentials, error) {
secretClient := p.kubernetesInterface.CoreV1().Secrets(secretBinding.SecretRef.Namespace)
secret, err := secretClient.Get(secretBinding.SecretRef.Name, metav1.GetOptions{})
if err != nil {
return Credentials{}, errors.Wrapf(err, "getting %s/%s secret",
secretBinding.SecretRef.Namespace, secretBinding.SecretRef.Name)
}
return Credentials{
Name: secret.Name,
TenantName: tenantName,
HyperscalerType: hyperscalerType,
CredentialData: secret.Data,
}, nil
}