forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
327 lines (271 loc) · 11.4 KB
/
resources.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
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 common
import (
"context"
"fmt"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
operatorv1alpha1 "github.com/kubermatic/kubermatic/api/pkg/crd/operator/v1alpha1"
"github.com/kubermatic/kubermatic/api/pkg/resources"
"github.com/kubermatic/kubermatic/api/pkg/resources/certificates"
"github.com/kubermatic/kubermatic/api/pkg/resources/certificates/servingcerthelper"
"github.com/kubermatic/kubermatic/api/pkg/resources/certificates/triple"
"github.com/kubermatic/kubermatic/api/pkg/resources/reconciling"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// NameLabel is the label containing the application's name.
NameLabel = "app.kubernetes.io/name"
// VersionLabel is the label containing the application's version.
VersionLabel = "app.kubernetes.io/version"
// VersionsFileName is the name of the YAML file containing the enabled and
// default Kubernetes and Openshift versions.
VersionsFileName = "versions.yaml"
// UpdatesFileName is the name of the YAML file containing the configured
// cluster upgrade rules.
UpdatesFileName = "updates.yaml"
// OpenshiftAddonsFileName is the name of the openshift addons manifest file
// in the master files.
OpenshiftAddonsFileName = "openshift-addons.yaml"
// KubernetesAddonsFileName is the name of the kubernetes addons manifest file
// in the master files.
KubernetesAddonsFileName = "kubernetes-addons.yaml"
DockercfgSecretName = "dockercfg"
DexCASecretName = "dex-ca"
ExtraFilesSecretName = "extra-files"
SeedWebhookServingCASecretName = "seed-webhook-ca"
SeedWebhookServingCertSecretName = "seed-webhook-cert"
seedWebhookCommonName = "seed-webhook"
seedWebhookServiceName = "seed-webhook"
IngressName = "kubermatic"
MasterControllerManagerDeploymentName = "kubermatic-master-controller-manager"
SeedControllerManagerDeploymentName = "kubermatic-seed-controller-manager"
CleanupFinalizer = "operator.kubermatic.io/cleanup"
)
func NamespaceCreator(cfg *operatorv1alpha1.KubermaticConfiguration) reconciling.NamedNamespaceCreatorGetter {
return func() (string, reconciling.NamespaceCreator) {
return cfg.Namespace, func(n *corev1.Namespace) (*corev1.Namespace, error) {
if n.Labels == nil {
n.Labels = map[string]string{}
}
n.Labels[NameLabel] = cfg.Namespace
return n, nil
}
}
}
func DockercfgSecretCreator(cfg *operatorv1alpha1.KubermaticConfiguration) reconciling.NamedSecretCreatorGetter {
return func() (string, reconciling.SecretCreator) {
return DockercfgSecretName, func(s *corev1.Secret) (*corev1.Secret, error) {
s.Type = corev1.SecretTypeDockerConfigJson
return createSecretData(s, map[string]string{
corev1.DockerConfigJsonKey: cfg.Spec.ImagePullSecret,
}), nil
}
}
}
func DexCASecretCreator(cfg *operatorv1alpha1.KubermaticConfiguration) reconciling.NamedSecretCreatorGetter {
return func() (string, reconciling.SecretCreator) {
return DexCASecretName, func(s *corev1.Secret) (*corev1.Secret, error) {
return createSecretData(s, map[string]string{
"caBundle.pem": cfg.Spec.Auth.CABundle,
}), nil
}
}
}
func ExtraFilesSecretCreator(cfg *operatorv1alpha1.KubermaticConfiguration) reconciling.NamedSecretCreatorGetter {
return func() (string, reconciling.SecretCreator) {
return ExtraFilesSecretName, func(s *corev1.Secret) (*corev1.Secret, error) {
versions, err := CreateVersionsYAML(&cfg.Spec.Versions)
if err != nil {
return s, fmt.Errorf("failed to encode versions as YAML: %v", err)
}
updates, err := CreateUpdatesYAML(&cfg.Spec.Versions)
if err != nil {
return s, fmt.Errorf("failed to encode updates as YAML: %v", err)
}
data := map[string]string{
OpenshiftAddonsFileName: cfg.Spec.UserCluster.Addons.Openshift.DefaultManifests,
KubernetesAddonsFileName: cfg.Spec.UserCluster.Addons.Kubernetes.DefaultManifests,
VersionsFileName: versions,
UpdatesFileName: updates,
}
return createSecretData(s, data), nil
}
}
}
func SeedWebhookServingCASecretCreator(cfg *operatorv1alpha1.KubermaticConfiguration) reconciling.NamedSecretCreatorGetter {
creator := certificates.GetCACreator(seedWebhookCommonName)
return func() (string, reconciling.SecretCreator) {
return SeedWebhookServingCASecretName, func(s *corev1.Secret) (*corev1.Secret, error) {
s, err := creator(s)
if err != nil {
return s, fmt.Errorf("failed to reconcile seed-webhook CA: %v", err)
}
return s, nil
}
}
}
func SeedWebhookServingCertSecretCreator(cfg *operatorv1alpha1.KubermaticConfiguration, client ctrlruntimeclient.Client) reconciling.NamedSecretCreatorGetter {
altNames := []string{
fmt.Sprintf("%s.%s", seedWebhookCommonName, cfg.Namespace),
fmt.Sprintf("%s.%s.svc", seedWebhookCommonName, cfg.Namespace),
}
caGetter := func() (*triple.KeyPair, error) {
se := corev1.Secret{}
key := types.NamespacedName{
Namespace: cfg.Namespace,
Name: SeedWebhookServingCASecretName,
}
if err := client.Get(context.Background(), key, &se); err != nil {
return nil, fmt.Errorf("CA certificate could not be retrieved: %v", err)
}
keypair, err := triple.ParseRSAKeyPair(se.Data[resources.CACertSecretKey], se.Data[resources.CAKeySecretKey])
if err != nil {
return nil, fmt.Errorf("CA certificate secret contains no valid key pair: %v", err)
}
return keypair, nil
}
return servingcerthelper.ServingCertSecretCreator(caGetter, SeedWebhookServingCertSecretName, seedWebhookCommonName, altNames, nil)
}
func SeedAdmissionWebhookName(cfg *operatorv1alpha1.KubermaticConfiguration) string {
return fmt.Sprintf("kubermatic-seeds-%s", cfg.Namespace)
}
func SeedAdmissionWebhookCreator(cfg *operatorv1alpha1.KubermaticConfiguration, client ctrlruntimeclient.Client) reconciling.NamedValidatingWebhookConfigurationCreatorGetter {
return func() (string, reconciling.ValidatingWebhookConfigurationCreator) {
return SeedAdmissionWebhookName(cfg), func(hook *admissionregistrationv1beta1.ValidatingWebhookConfiguration) (*admissionregistrationv1beta1.ValidatingWebhookConfiguration, error) {
matchPolicy := admissionregistrationv1beta1.Exact
failurePolicy := admissionregistrationv1beta1.Fail
sideEffects := admissionregistrationv1beta1.SideEffectClassUnknown
scope := admissionregistrationv1beta1.AllScopes
ca, err := seedWebhookCABundle(cfg, client)
if err != nil {
return nil, fmt.Errorf("cannot find Seed Admission CA bundle: %v", err)
}
hook.Webhooks = []admissionregistrationv1beta1.ValidatingWebhook{
{
Name: "seeds.kubermatic.io", // this should be a FQDN
AdmissionReviewVersions: []string{admissionregistrationv1beta1.SchemeGroupVersion.Version},
MatchPolicy: &matchPolicy,
FailurePolicy: &failurePolicy,
SideEffects: &sideEffects,
TimeoutSeconds: pointer.Int32Ptr(30),
ClientConfig: admissionregistrationv1beta1.WebhookClientConfig{
CABundle: ca,
Service: &admissionregistrationv1beta1.ServiceReference{
Name: seedWebhookServiceName,
Namespace: cfg.Namespace,
Port: pointer.Int32Ptr(443),
},
},
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
NameLabel: cfg.Namespace,
},
},
ObjectSelector: &metav1.LabelSelector{},
Rules: []admissionregistrationv1beta1.RuleWithOperations{
{
Rule: admissionregistrationv1beta1.Rule{
APIGroups: []string{kubermaticv1.GroupName},
APIVersions: []string{"*"},
Resources: []string{"seeds"},
Scope: &scope,
},
Operations: []admissionregistrationv1beta1.OperationType{
admissionregistrationv1beta1.OperationAll,
},
},
},
},
}
return hook, nil
}
}
}
func SeedAdmissionServiceCreator(cfg *operatorv1alpha1.KubermaticConfiguration, client ctrlruntimeclient.Client) reconciling.NamedServiceCreatorGetter {
return func() (string, reconciling.ServiceCreator) {
return seedWebhookServiceName, func(s *corev1.Service) (*corev1.Service, error) {
s.Spec.Type = corev1.ServiceTypeClusterIP
if len(s.Spec.Ports) != 1 {
s.Spec.Ports = make([]corev1.ServicePort, 1)
}
s.Spec.Ports[0].Name = "https"
s.Spec.Ports[0].Port = 443
s.Spec.Ports[0].TargetPort = intstr.FromInt(8100)
s.Spec.Ports[0].Protocol = corev1.ProtocolTCP
selector, err := determineWebhookServiceSelector(cfg, client)
if err != nil {
return s, fmt.Errorf("failed to determine SeedAdmissionWebhook target service: %v", err)
}
s.Spec.Selector = selector
return s, nil
}
}
}
// On master clusters, point to the master-controller-manager, otherwise
// point to the seed-controller-manager. On combined master+seeds, the
// master has precedence.
func determineWebhookServiceSelector(cfg *operatorv1alpha1.KubermaticConfiguration, client ctrlruntimeclient.Client) (map[string]string, error) {
deployment := appsv1.Deployment{}
key := types.NamespacedName{
Name: MasterControllerManagerDeploymentName,
Namespace: cfg.Namespace,
}
err := client.Get(context.Background(), key, &deployment)
if err != nil && !kerrors.IsNotFound(err) {
return nil, fmt.Errorf("failed to probe for %s: %v", key, err)
}
if err == nil {
return map[string]string{
NameLabel: MasterControllerManagerDeploymentName,
}, nil
}
key = types.NamespacedName{
Name: SeedControllerManagerDeploymentName,
Namespace: cfg.Namespace,
}
err = client.Get(context.Background(), key, &deployment)
if err != nil && !kerrors.IsNotFound(err) {
return nil, fmt.Errorf("failed to probe for %s: %v", key, err)
}
if err == nil {
return map[string]string{
NameLabel: SeedControllerManagerDeploymentName,
}, nil
}
return nil, fmt.Errorf("neither master- nor seed-controller-manager exist in namespace %s", cfg.Namespace)
}
func seedWebhookCABundle(cfg *operatorv1alpha1.KubermaticConfiguration, client ctrlruntimeclient.Client) ([]byte, error) {
secret := corev1.Secret{}
key := types.NamespacedName{
Name: SeedWebhookServingCASecretName,
Namespace: cfg.Namespace,
}
err := client.Get(context.Background(), key, &secret)
if err != nil {
return nil, fmt.Errorf("cannot retrieve seed admission webhook CA Secret %s: %v", SeedWebhookServingCASecretName, err)
}
cert, ok := secret.Data[resources.CACertSecretKey]
if !ok {
return nil, fmt.Errorf("Secret %s does not contain CA certificate at key %s", SeedWebhookServingCASecretName, resources.CACertSecretKey)
}
return cert, nil
}