/
app_probe.go
276 lines (248 loc) · 8.44 KB
/
app_probe.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
// Copyright Istio Authors
//
// 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 inject implements kube-inject or webhoook autoinject feature to inject sidecar.
// This file is focused on rewriting Kubernetes app probers to support mutual TLS.
package inject
import (
"encoding/json"
"strconv"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"istio.io/api/annotation"
"istio.io/istio/pilot/cmd/pilot-agent/status"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/slices"
)
// ShouldRewriteAppHTTPProbers returns if we should rewrite apps' probers config.
func ShouldRewriteAppHTTPProbers(annotations map[string]string, specSetting bool) bool {
if annotations != nil {
if value, ok := annotations[annotation.SidecarRewriteAppHTTPProbers.Name]; ok {
if isSetInAnnotation, err := strconv.ParseBool(value); err == nil {
return isSetInAnnotation
}
}
}
return specSetting
}
// FindSidecar returns the pointer to the first container whose name matches the "istio-proxy".
func FindSidecar(pod *corev1.Pod) *corev1.Container {
return FindContainerFromPod(ProxyContainerName, pod)
}
// FindContainerFromPod returns the pointer to the first container whose name matches in init containers or regular containers
func FindContainerFromPod(name string, pod *corev1.Pod) *corev1.Container {
if c := FindContainer(name, pod.Spec.Containers); c != nil {
return c
}
return FindContainer(name, pod.Spec.InitContainers)
}
// FindContainer returns the pointer to the first container whose name matches.
func FindContainer(name string, containers []corev1.Container) *corev1.Container {
for i := range containers {
if containers[i].Name == name {
return &containers[i]
}
}
return nil
}
// convertAppProber returns an overwritten `Probe` for pilot agent to take over.
func convertAppProber(probe *corev1.Probe, newURL string, statusPort int) *corev1.Probe {
if probe == nil {
return nil
}
if probe.HTTPGet != nil {
return convertAppProberHTTPGet(probe, newURL, statusPort)
} else if probe.TCPSocket != nil {
return convertAppProberTCPSocket(probe, newURL, statusPort)
} else if probe.GRPC != nil {
return convertAppProberGRPC(probe, newURL, statusPort)
}
return nil
}
// convertAppProberHTTPGet returns an overwritten `Probe` (HttpGet) for pilot agent to take over.
func convertAppProberHTTPGet(probe *corev1.Probe, newURL string, statusPort int) *corev1.Probe {
p := probe.DeepCopy()
// Change the application container prober config.
p.HTTPGet.Port = intstr.FromInt32(int32(statusPort))
p.HTTPGet.Path = newURL
// For HTTPS prober, we change to HTTP,
// and pilot agent uses https to request application prober endpoint.
// Kubelet -> HTTP -> Pilot Agent -> HTTPS -> Application
if p.HTTPGet.Scheme == corev1.URISchemeHTTPS {
p.HTTPGet.Scheme = corev1.URISchemeHTTP
}
return p
}
// convertAppProberTCPSocket returns an overwritten `Probe` (TcpSocket) for pilot agent to take over.
func convertAppProberTCPSocket(probe *corev1.Probe, newURL string, statusPort int) *corev1.Probe {
p := probe.DeepCopy()
// the sidecar intercepts all tcp connections, so we change it to a HTTP probe and the sidecar will check tcp
p.HTTPGet = &corev1.HTTPGetAction{}
p.HTTPGet.Port = intstr.FromInt32(int32(statusPort))
p.HTTPGet.Path = newURL
p.TCPSocket = nil
return p
}
// convertAppProberGRPC returns an overwritten `Probe` (gRPC) for pilot agent to take over.
func convertAppProberGRPC(probe *corev1.Probe, newURL string, statusPort int) *corev1.Probe {
p := probe.DeepCopy()
// the sidecar intercepts all gRPC connections, so we change it to a HTTP probe and the sidecar will check gRPC
p.HTTPGet = &corev1.HTTPGetAction{}
p.HTTPGet.Port = intstr.FromInt32(int32(statusPort))
p.HTTPGet.Path = newURL
// For gRPC prober, we change to HTTP,
// and pilot agent uses gRPC to request application prober endpoint.
// Kubelet -> HTTP -> Pilot Agent -> gRPC -> Application
p.GRPC = nil
return p
}
type KubeAppProbers map[string]*Prober
// Prober represents a single container prober
type Prober struct {
HTTPGet *corev1.HTTPGetAction `json:"httpGet,omitempty"`
TCPSocket *corev1.TCPSocketAction `json:"tcpSocket,omitempty"`
GRPC *corev1.GRPCAction `json:"grpc,omitempty"`
TimeoutSeconds int32 `json:"timeoutSeconds,omitempty"`
}
// DumpAppProbers returns a json encoded string as `status.KubeAppProbers`.
// Also update the probers so that all usages of named port will be resolved to integer.
func DumpAppProbers(pod *corev1.Pod, targetPort int32) string {
out := KubeAppProbers{}
updateNamedPort := func(p *Prober, portMap map[string]int32) *Prober {
if p == nil {
return nil
}
if p.GRPC != nil {
// don't need to update for gRPC probe port as it only supports integer
return p
}
if p.HTTPGet == nil && p.TCPSocket == nil {
return nil
}
var probePort *intstr.IntOrString
if p.HTTPGet != nil {
probePort = &p.HTTPGet.Port
} else {
probePort = &p.TCPSocket.Port
}
if probePort.Type == intstr.String {
port, exists := portMap[probePort.StrVal]
if !exists {
return nil
}
*probePort = intstr.FromInt32(port)
} else if probePort.IntVal == targetPort {
// Already is rewritten
return nil
}
return p
}
for _, c := range allContainers(pod) {
if c.Name == ProxyContainerName {
continue
}
readyz, livez, startupz := status.FormatProberURL(c.Name)
portMap := map[string]int32{}
for _, p := range c.Ports {
if p.Name != "" {
portMap[p.Name] = p.ContainerPort
}
}
if h := updateNamedPort(kubeProbeToInternalProber(c.ReadinessProbe), portMap); h != nil {
out[readyz] = h
}
if h := updateNamedPort(kubeProbeToInternalProber(c.LivenessProbe), portMap); h != nil {
out[livez] = h
}
if h := updateNamedPort(kubeProbeToInternalProber(c.StartupProbe), portMap); h != nil {
out[startupz] = h
}
}
// prevent generate '{}'
if len(out) == 0 {
return ""
}
b, err := json.Marshal(out)
if err != nil {
log.Errorf("failed to serialize the app prober config %v", err)
return ""
}
return string(b)
}
func allContainers(pod *corev1.Pod) []corev1.Container {
return append(slices.Clone(pod.Spec.InitContainers), pod.Spec.Containers...)
}
// patchRewriteProbe generates the patch for webhook.
func patchRewriteProbe(annotations map[string]string, pod *corev1.Pod, defaultPort int32) {
statusPort := int(defaultPort)
if v, f := annotations[annotation.SidecarStatusPort.Name]; f {
p, err := strconv.Atoi(v)
if err != nil {
log.Errorf("Invalid annotation %v=%v: %v", annotation.SidecarStatusPort.Name, v, err)
}
statusPort = p
}
for i, c := range pod.Spec.Containers {
// Skip sidecar container.
if c.Name == ProxyContainerName {
continue
}
convertProbe(&c, statusPort)
pod.Spec.Containers[i] = c
}
for i, c := range pod.Spec.InitContainers {
// Skip sidecar container.
if c.Name == ProxyContainerName {
continue
}
convertProbe(&c, statusPort)
pod.Spec.InitContainers[i] = c
}
}
func convertProbe(c *corev1.Container, statusPort int) {
readyz, livez, startupz := status.FormatProberURL(c.Name)
if probePatch := convertAppProber(c.ReadinessProbe, readyz, statusPort); probePatch != nil {
c.ReadinessProbe = probePatch
}
if probePatch := convertAppProber(c.LivenessProbe, livez, statusPort); probePatch != nil {
c.LivenessProbe = probePatch
}
if probePatch := convertAppProber(c.StartupProbe, startupz, statusPort); probePatch != nil {
c.StartupProbe = probePatch
}
}
// kubeProbeToInternalProber converts a Kubernetes Probe to an Istio internal Prober
func kubeProbeToInternalProber(probe *corev1.Probe) *Prober {
if probe == nil {
return nil
}
if probe.HTTPGet != nil {
return &Prober{
HTTPGet: probe.HTTPGet,
TimeoutSeconds: probe.TimeoutSeconds,
}
}
if probe.TCPSocket != nil {
return &Prober{
TCPSocket: probe.TCPSocket,
TimeoutSeconds: probe.TimeoutSeconds,
}
}
if probe.GRPC != nil {
return &Prober{
GRPC: probe.GRPC,
TimeoutSeconds: probe.TimeoutSeconds,
}
}
return nil
}