forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacenter.go
146 lines (119 loc) · 4.9 KB
/
datacenter.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"context"
"fmt"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kubermaticlog "github.com/kubermatic/kubermatic/api/pkg/log"
"github.com/kubermatic/kubermatic/api/pkg/util/restmapper"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// DefaultSeedName is the name of the Seed resource that is used
// in the Community Edition, which is limited to a single seed.
DefaultSeedName = "kubermatic"
)
var (
// emptySeedMap is returned when the default seed is not present.
emptySeedMap = map[string]*kubermaticv1.Seed{}
)
// SeedGetter is a function to retrieve a single seed
type SeedGetter = func() (*kubermaticv1.Seed, error)
// SeedsGetter is a function to retrieve a list of seeds
type SeedsGetter = func() (map[string]*kubermaticv1.Seed, error)
// SeedKubeconfigGetter is used to fetch the kubeconfig for a given seed
type SeedKubeconfigGetter = func(seed *kubermaticv1.Seed) (*rest.Config, error)
// SeedClientGetter is used to get a ctrlruntimeclient for a given seed
type SeedClientGetter = func(seed *kubermaticv1.Seed) (ctrlruntimeclient.Client, error)
// ClusterProviderGetter is used to get a clusterProvider
type ClusterProviderGetter = func(seed *kubermaticv1.Seed) (ClusterProvider, error)
// AddonProviderGetterr is used to get an AddonProvider
type AddonProviderGetter = func(seed *kubermaticv1.Seed) (AddonProvider, error)
// SeedGetterFactory returns a SeedGetter. It has validation of all its arguments
func SeedGetterFactory(ctx context.Context, client ctrlruntimeclient.Client, seedName string, namespace string) (SeedGetter, error) {
return func() (*kubermaticv1.Seed, error) {
seed := &kubermaticv1.Seed{}
if err := client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: seedName}, seed); err != nil {
// allow callers to handle this gracefully
if kerrors.IsNotFound(err) {
return nil, err
}
return nil, fmt.Errorf("failed to get seed %q: %v", seedName, err)
}
seed.SetDefaults()
return seed, nil
}, nil
}
func SeedsGetterFactory(ctx context.Context, client ctrlruntimeclient.Client, namespace string) (SeedsGetter, error) {
return func() (map[string]*kubermaticv1.Seed, error) {
seed := &kubermaticv1.Seed{}
if err := client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: DefaultSeedName}, seed); err != nil {
if kerrors.IsNotFound(err) {
// We should not fail if no seed exists and just return an
// empty map.
return emptySeedMap, nil
}
return nil, fmt.Errorf("failed to get seed %q: %v", DefaultSeedName, err)
}
seed.SetDefaults()
return map[string]*kubermaticv1.Seed{
DefaultSeedName: seed,
}, nil
}, nil
}
func SeedKubeconfigGetterFactory(ctx context.Context, client ctrlruntimeclient.Client) (SeedKubeconfigGetter, error) {
return func(seed *kubermaticv1.Seed) (*rest.Config, error) {
secret := &corev1.Secret{}
name := types.NamespacedName{
Namespace: seed.Spec.Kubeconfig.Namespace,
Name: seed.Spec.Kubeconfig.Name,
}
if name.Namespace == "" {
name.Namespace = seed.Namespace
}
if err := client.Get(ctx, name, secret); err != nil {
return nil, fmt.Errorf("failed to get kubeconfig secret %q: %v", name.String(), err)
}
fieldPath := seed.Spec.Kubeconfig.FieldPath
if len(fieldPath) == 0 {
fieldPath = DefaultKubeconfigFieldPath
}
if _, exists := secret.Data[fieldPath]; !exists {
return nil, fmt.Errorf("secret %q has no key %q", name.String(), fieldPath)
}
cfg, err := clientcmd.RESTConfigFromKubeConfig(secret.Data[fieldPath])
if err != nil {
return nil, fmt.Errorf("failed to load kubeconfig: %v", err)
}
kubermaticlog.Logger.With("seed", seed.Name).Debug("Successfully got kubeconfig")
return cfg, nil
}, nil
}
// SeedClientGetterFactory returns a SeedClientGetter. It uses a RestMapperCache to cache
// the discovery data, which considerably speeds up client creation.
func SeedClientGetterFactory(kubeconfigGetter SeedKubeconfigGetter) SeedClientGetter {
cache := restmapper.New()
return func(seed *kubermaticv1.Seed) (ctrlruntimeclient.Client, error) {
cfg, err := kubeconfigGetter(seed)
if err != nil {
return nil, err
}
return cache.Client(cfg)
}
}