generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Provider.go
59 lines (49 loc) · 1.75 KB
/
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
package kubeconfig
import (
"context"
authenticationv1alpha1 "github.com/gardener/gardener/pkg/apis/authentication/v1alpha1"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/pkg/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gardenerClient "sigs.k8s.io/controller-runtime/pkg/client"
)
type Provider struct {
shootNamespace string
shootClient ShootClient
dynamicKubeconfigAPI DynamicKubeconfigAPI
expirationInSeconds int64
}
type ShootClient interface {
Get(ctx context.Context, name string, opts v1.GetOptions) (*v1beta1.Shoot, error)
}
type DynamicKubeconfigAPI interface {
Create(ctx context.Context, obj gardenerClient.Object, subResource gardenerClient.Object, opts ...gardenerClient.SubResourceCreateOption) error
}
func NewKubeconfigProvider(
shootClient ShootClient,
dynamicKubeconfigAPI DynamicKubeconfigAPI,
shootNamespace string,
expirationInSeconds int64) Provider {
return Provider{
shootClient: shootClient,
dynamicKubeconfigAPI: dynamicKubeconfigAPI,
shootNamespace: shootNamespace,
expirationInSeconds: expirationInSeconds,
}
}
func (kp Provider) Fetch(ctx context.Context, shootName string) (string, error) {
shoot, err := kp.shootClient.Get(ctx, shootName, v1.GetOptions{})
if err != nil {
return "", errors.Wrap(err, "failed to get shoot")
}
adminKubeconfigRequest := authenticationv1alpha1.AdminKubeconfigRequest{
Spec: authenticationv1alpha1.AdminKubeconfigRequestSpec{
ExpirationSeconds: &kp.expirationInSeconds,
},
}
err = kp.dynamicKubeconfigAPI.Create(context.Background(), shoot, &adminKubeconfigRequest)
if err != nil {
return "", errors.Wrap(err, "failed to create AdminKubeconfigRequest")
}
return string(adminKubeconfigRequest.Status.Kubeconfig), nil
}