-
Notifications
You must be signed in to change notification settings - Fork 462
/
istiod.go
333 lines (283 loc) · 12 KB
/
istiod.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
328
329
330
331
332
333
// Copyright 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 istio
import (
"context"
"embed"
"path/filepath"
"strings"
"time"
networkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/chartrenderer"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/controllerutils"
"github.com/gardener/gardener/pkg/operation/botanist/component"
"github.com/gardener/gardener/pkg/utils/flow"
"github.com/gardener/gardener/pkg/utils/managedresources"
)
const (
// DefaultZoneKey is the label key for the istio default ingress gateway.
DefaultZoneKey = "istio"
// IstiodServiceName is the name of the istiod service.
IstiodServiceName = "istiod"
// PortWebhookServer is the port of the validating webhook server.
PortWebhookServer = 10250
// managedResourceControlName is the name of the ManagedResource containing the resource specifications.
managedResourceControlName = "istio"
// managedResourceIstioSystemName is the name of the ManagedResource containing Istio-System resource specifications.
managedResourceIstioSystemName = "istio-system"
istiodServicePortNameMetrics = "metrics"
releaseName = "istio"
)
var (
//go:embed charts/istio/istio-istiod
chartIstiod embed.FS
chartPathIstiod = filepath.Join("charts", "istio", "istio-istiod")
)
type istiod struct {
client client.Client
chartRenderer chartrenderer.Interface
values Values
managedResourceIstioIngressName string
}
// IstiodValues contains configuration values for the Istiod component.
type IstiodValues struct {
// Enabled controls if `istiod` is deployed.
Enabled bool
// Namespace (a.k.a. Istio-System namespace) is the namespace `istiod` is deployed to.
Namespace string
// Image is the image used for the `istiod` deployment.
Image string
// PriorityClassName is the name of the priority class used for the Istiod deployment.
PriorityClassName string
// TrustDomain is the domain used for service discovery, e.g. `cluster.local`.
TrustDomain string
// Zones are the availability zones used for this `istiod` deployment.
Zones []string
}
// Values contains configuration values for the Istio component.
type Values struct {
// Istiod are configuration values for the istiod chart.
Istiod IstiodValues
// IngressGateway are configuration values for ingress gateway deployments and objects.
IngressGateway []IngressGatewayValues
// NamePrefix can be used to prepend arbitrary identifiers to resources which are deployed to common namespaces.
NamePrefix string
}
// Interface contains functions for an Istio deployer.
type Interface interface {
component.DeployWaiter
// AddIngressGateway adds another ingress gateway to the existing Istio deployer.
AddIngressGateway(values IngressGatewayValues)
// GetValues returns the configured values of the Istio deployer.
GetValues() Values
}
var _ Interface = (*istiod)(nil)
// NewIstio can be used to deploy istio's istiod in a namespace.
// Destroy does nothing.
func NewIstio(
client client.Client,
chartRenderer chartrenderer.Interface,
values Values,
) Interface {
return &istiod{
client: client,
chartRenderer: chartRenderer,
values: values,
managedResourceIstioIngressName: values.NamePrefix + managedResourceControlName,
}
}
func (i *istiod) deployIstiod(ctx context.Context) error {
if !i.values.Istiod.Enabled {
return nil
}
istiodNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: i.values.Istiod.Namespace}}
if _, err := controllerutils.CreateOrGetAndMergePatch(ctx, i.client, istiodNamespace, func() error {
metav1.SetMetaDataLabel(&istiodNamespace.ObjectMeta, "istio-operator-managed", "Reconcile")
metav1.SetMetaDataLabel(&istiodNamespace.ObjectMeta, "istio-injection", "disabled")
metav1.SetMetaDataLabel(&istiodNamespace.ObjectMeta, resourcesv1alpha1.HighAvailabilityConfigConsider, "true")
metav1.SetMetaDataLabel(&istiodNamespace.ObjectMeta, v1beta1constants.GardenRole, v1beta1constants.GardenRoleIstioSystem)
metav1.SetMetaDataAnnotation(&istiodNamespace.ObjectMeta, resourcesv1alpha1.HighAvailabilityConfigZones, strings.Join(i.values.Istiod.Zones, ","))
return nil
}); err != nil {
return err
}
renderedIstiodChart, err := i.generateIstiodChart(false)
if err != nil {
return err
}
return managedresources.CreateForSeed(ctx, i.client, i.values.Istiod.Namespace, managedResourceIstioSystemName, false, renderedIstiodChart.AsSecretData())
}
func (i *istiod) Deploy(ctx context.Context) error {
if err := i.deployIstiod(ctx); err != nil {
return err
}
var renderedChart = &chartrenderer.RenderedChart{}
// TODO(timuthy): Block to be removed in a future version. Only required to move istiod assets to separate ManagedResource.
if i.values.Istiod.Enabled {
renderedIstiodChartIgnore, err := i.generateIstiodChart(true)
if err != nil {
return err
}
renderedChart.Manifests = append(renderedChart.Manifests, renderedIstiodChartIgnore.Manifests...)
}
// TODO(mvladev): Rotate this on every istio version upgrade.
for _, ingressGateway := range i.values.IngressGateway {
for _, filterName := range []string{"tcp-stats-filter-1.11", "stats-filter-1.11", "tcp-stats-filter-1.12", "stats-filter-1.12"} {
if err := client.IgnoreNotFound(i.client.Delete(ctx, &networkingv1alpha3.EnvoyFilter{
ObjectMeta: metav1.ObjectMeta{Name: filterName, Namespace: ingressGateway.Namespace},
})); err != nil {
return err
}
}
}
renderedIstioIngressGatewayChart, err := i.generateIstioIngressGatewayChart()
if err != nil {
return err
}
for _, istioIngressGateway := range i.values.IngressGateway {
gatewayNamespace := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: istioIngressGateway.Namespace}}
if _, err := controllerutils.CreateOrGetAndMergePatch(ctx, i.client, gatewayNamespace, func() error {
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, "istio-operator-managed", "Reconcile")
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, "istio-injection", "disabled")
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, v1beta1constants.GardenRole, v1beta1constants.GardenRoleIstioIngress)
if value, ok := istioIngressGateway.Labels[v1beta1constants.GardenRole]; ok && strings.HasPrefix(value, v1beta1constants.GardenRoleExposureClassHandler) {
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, v1beta1constants.GardenRole, value)
}
if value, ok := istioIngressGateway.Labels[v1beta1constants.LabelExposureClassHandlerName]; ok {
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, v1beta1constants.LabelExposureClassHandlerName, value)
}
if value, ok := istioIngressGateway.Labels[DefaultZoneKey]; ok {
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, DefaultZoneKey, value)
}
metav1.SetMetaDataLabel(&gatewayNamespace.ObjectMeta, resourcesv1alpha1.HighAvailabilityConfigConsider, "true")
zones := i.values.Istiod.Zones
if len(istioIngressGateway.Zones) > 0 {
zones = istioIngressGateway.Zones
}
metav1.SetMetaDataAnnotation(&gatewayNamespace.ObjectMeta, resourcesv1alpha1.HighAvailabilityConfigZones, strings.Join(zones, ","))
if len(zones) == 1 {
metav1.SetMetaDataAnnotation(&gatewayNamespace.ObjectMeta, resourcesv1alpha1.HighAvailabilityConfigZonePinning, "true")
}
return nil
}); err != nil {
return err
}
}
renderedChart.Manifests = append(renderedChart.Manifests, renderedIstioIngressGatewayChart.Manifests...)
var (
registry = managedresources.NewRegistry(kubernetes.SeedScheme, kubernetes.SeedCodec, kubernetes.SeedSerializer)
chartsMap = renderedChart.AsSecretData()
objMap = registry.SerializedObjects()
)
for key := range objMap {
chartsMap[key] = objMap[key]
}
return managedresources.CreateForSeed(ctx, i.client, i.values.Istiod.Namespace, i.managedResourceIstioIngressName, false, chartsMap)
}
func (i *istiod) Destroy(ctx context.Context) error {
for _, mr := range ManagedResourceNames(i.values.Istiod.Enabled, i.values.NamePrefix) {
if err := managedresources.DeleteForSeed(ctx, i.client, i.values.Istiod.Namespace, mr); err != nil {
return err
}
}
if i.values.Istiod.Enabled {
if err := i.client.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: i.values.Istiod.Namespace,
},
}); client.IgnoreNotFound(err) != nil {
return err
}
}
for _, istioIngressGateway := range i.values.IngressGateway {
if err := i.client.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: istioIngressGateway.Namespace,
},
}); client.IgnoreNotFound(err) != nil {
return err
}
}
return nil
}
// TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy
// or deleted.
var TimeoutWaitForManagedResource = 2 * time.Minute
func (i *istiod) Wait(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource)
defer cancel()
managedResources := ManagedResourceNames(i.values.Istiod.Enabled, i.values.NamePrefix)
taskFns := make([]flow.TaskFn, 0, len(managedResources))
for _, mr := range managedResources {
name := mr
taskFns = append(taskFns, func(ctx context.Context) error {
return managedresources.WaitUntilHealthy(ctx, i.client, i.values.Istiod.Namespace, name)
})
}
return flow.Parallel(taskFns...)(timeoutCtx)
}
func (i *istiod) WaitCleanup(ctx context.Context) error {
timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource)
defer cancel()
managedResources := ManagedResourceNames(i.values.Istiod.Enabled, i.values.NamePrefix)
taskFns := make([]flow.TaskFn, 0, len(managedResources))
for _, mr := range managedResources {
name := mr
taskFns = append(taskFns, func(ctx context.Context) error {
return managedresources.WaitUntilDeleted(timeoutCtx, i.client, i.values.Istiod.Namespace, name)
})
}
return flow.Parallel(taskFns...)(timeoutCtx)
}
func (i *istiod) AddIngressGateway(values IngressGatewayValues) {
i.values.IngressGateway = append(i.values.IngressGateway, values)
}
func (i *istiod) GetValues() Values {
return i.values
}
func (i *istiod) generateIstiodChart(ignoreMode bool) (*chartrenderer.RenderedChart, error) {
istiodValues := i.values.Istiod
return i.chartRenderer.RenderEmbeddedFS(chartIstiod, chartPathIstiod, releaseName, i.values.Istiod.Namespace, map[string]interface{}{
"serviceName": IstiodServiceName,
"trustDomain": istiodValues.TrustDomain,
"labels": map[string]interface{}{
"app": "istiod",
"istio": "pilot",
},
"deployNamespace": false,
"priorityClassName": istiodValues.PriorityClassName,
"ports": map[string]interface{}{
"https": PortWebhookServer,
},
"portsNames": map[string]interface{}{
"metrics": istiodServicePortNameMetrics,
},
"image": istiodValues.Image,
"ignoreMode": ignoreMode,
})
}
// ManagedResourceNames returns the names of the `ManagedResource`s being used by Istio.
func ManagedResourceNames(istiodEnabled bool, namePrefix string) []string {
names := []string{namePrefix + managedResourceControlName}
if istiodEnabled {
names = append(names, managedResourceIstioSystemName)
}
return names
}