-
Notifications
You must be signed in to change notification settings - Fork 458
/
kube_apiserver_service.go
215 lines (188 loc) · 6.81 KB
/
kube_apiserver_service.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
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 kubeapiserverexposure
import (
"context"
"fmt"
"time"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
"github.com/gardener/gardener/pkg/controllerutils"
"github.com/gardener/gardener/pkg/operation/botanist/component"
"github.com/gardener/gardener/pkg/operation/botanist/component/kubeapiserver"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/gardener/gardener/pkg/utils/retry"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
servicePortName = "kube-apiserver"
servicePort = 443
)
// ServiceValues configure the kube-apiserver service.
type ServiceValues struct {
Annotations map[string]string
SNIPhase component.Phase
}
// serviceValues configure the kube-apiserver service.
// this one is not exposed as not all values should be configured
// from the outside.
type serviceValues struct {
annotations map[string]string
serviceType corev1.ServiceType
enableSNI bool
gardenerManaged bool
}
// NewService creates a new instance of DeployWaiter for the Service used to expose the kube-apiserver.
// <waiter> is optional and it's defaulted to github.com/gardener/gardener/pkg/utils/retry.DefaultOps().
func NewService(
logger logrus.FieldLogger,
crclient client.Client,
values *ServiceValues,
serviceKey client.ObjectKey,
sniServiceKey client.ObjectKey,
waiter retry.Ops,
clusterIPFunc func(clusterIP string),
ingressFunc func(ingressIP string),
) component.DeployWaiter {
if waiter == nil {
waiter = retry.DefaultOps()
}
if clusterIPFunc == nil {
clusterIPFunc = func(_ string) {}
}
if ingressFunc == nil {
ingressFunc = func(_ string) {}
}
var (
internalValues = &serviceValues{}
loadBalancerServiceKey client.ObjectKey
)
if values != nil {
switch values.SNIPhase {
case component.PhaseEnabled:
internalValues.serviceType = corev1.ServiceTypeClusterIP
internalValues.enableSNI = true
internalValues.gardenerManaged = true
loadBalancerServiceKey = sniServiceKey
case component.PhaseEnabling:
// existing traffic must still access the old loadbalancer
// IP (due to DNS cache).
internalValues.serviceType = corev1.ServiceTypeLoadBalancer
internalValues.enableSNI = true
internalValues.gardenerManaged = false
loadBalancerServiceKey = sniServiceKey
case component.PhaseDisabling:
internalValues.serviceType = corev1.ServiceTypeLoadBalancer
internalValues.enableSNI = true
internalValues.gardenerManaged = true
loadBalancerServiceKey = serviceKey
default:
internalValues.serviceType = corev1.ServiceTypeLoadBalancer
internalValues.enableSNI = false
internalValues.gardenerManaged = false
loadBalancerServiceKey = serviceKey
}
internalValues.annotations = values.Annotations
}
return &service{
logger: logger,
client: crclient,
values: internalValues,
serviceKey: serviceKey,
loadBalancerServiceKey: loadBalancerServiceKey,
waiter: waiter,
clusterIPFunc: clusterIPFunc,
ingressFunc: ingressFunc,
}
}
type service struct {
logger logrus.FieldLogger
client client.Client
values *serviceValues
serviceKey client.ObjectKey
loadBalancerServiceKey client.ObjectKey
waiter retry.Ops
clusterIPFunc func(clusterIP string)
ingressFunc func(ingressIP string)
}
func (s *service) Deploy(ctx context.Context) error {
obj := s.emptyService()
if _, err := controllerutils.GetAndCreateOrMergePatch(ctx, s.client, obj, func() error {
obj.Annotations = s.values.annotations
if s.values.enableSNI {
metav1.SetMetaDataAnnotation(&obj.ObjectMeta, "networking.istio.io/exportTo", "*")
}
obj.Labels = getLabels()
if s.values.gardenerManaged {
metav1.SetMetaDataLabel(&obj.ObjectMeta, v1beta1constants.LabelAPIServerExposure, v1beta1constants.LabelAPIServerExposureGardenerManaged)
}
obj.Spec.Type = s.values.serviceType
obj.Spec.Selector = getLabels()
obj.Spec.Ports = kutil.ReconcileServicePorts(obj.Spec.Ports, []corev1.ServicePort{
{
Name: servicePortName,
Protocol: corev1.ProtocolTCP,
Port: servicePort,
TargetPort: intstr.FromInt(kubeapiserver.Port),
},
}, s.values.serviceType)
return nil
}); err != nil {
return err
}
s.clusterIPFunc(obj.Spec.ClusterIP)
return nil
}
func (s *service) Destroy(ctx context.Context) error {
return client.IgnoreNotFound(s.client.Delete(ctx, s.emptyService()))
}
func (s *service) Wait(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
return s.waiter.Until(ctx, 5*time.Second, func(ctx context.Context) (done bool, err error) {
// this ingress can be either the kube-apiserver's service or istio's IGW loadbalancer.
loadBalancerIngress, err := kutil.GetLoadBalancerIngress(
ctx,
s.client,
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: s.loadBalancerServiceKey.Name,
Namespace: s.loadBalancerServiceKey.Namespace,
},
},
)
if err != nil {
s.logger.Info("Waiting until the KubeAPI Server ingress LoadBalancer deployed in the Seed cluster is ready...")
// TODO(AC): This is a quite optimistic check / we should differentiate here
return retry.MinorError(fmt.Errorf("KubeAPI Server ingress LoadBalancer deployed in the Seed cluster is ready: %v", err))
}
s.ingressFunc(loadBalancerIngress)
return retry.Ok()
})
}
func (s *service) WaitCleanup(ctx context.Context) error {
return kutil.WaitUntilResourceDeleted(ctx, s.client, s.emptyService(), 5*time.Second)
}
func (s *service) emptyService() *corev1.Service {
return &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: s.serviceKey.Name, Namespace: s.serviceKey.Namespace}}
}
func getLabels() map[string]string {
return map[string]string{
v1beta1constants.LabelApp: v1beta1constants.LabelKubernetes,
v1beta1constants.LabelRole: v1beta1constants.LabelAPIServer,
}
}