-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_probe.go
154 lines (140 loc) · 4.75 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
// Copyright 2018 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
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/gogo/protobuf/proto"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"istio.io/istio/pilot/cmd/pilot-agent/status"
"istio.io/istio/pkg/log"
)
const (
// StatusPortCmdFlagName is the name of the command line flag passed to pilot-agent for sidecar readiness probe.
// We reuse it for taking over application's readiness probing as well.
// TODO: replace the hardcoded statusPort elsewhere by this variable as much as possible.
StatusPortCmdFlagName = "statusPort"
// TODO: any constant refers to this container's name?
istioProxyContainerName = "istio-proxy"
)
var (
// regex pattern for to extract the pilot agent probing port.
// Supported format, --statusPort, -statusPort, --statusPort=15020.
statusPortPattern = regexp.MustCompile(fmt.Sprintf(`^-{1,2}%s(=(?P<port>\d+))?$`, StatusPortCmdFlagName))
)
// extractStatusPort accepts the sidecar container spec and returns its port for healthiness probing.
func extractStatusPort(sidecar *corev1.Container) int {
for i, arg := range sidecar.Args {
// Skip for unrelated args.
match := statusPortPattern.FindAllStringSubmatch(strings.TrimSpace(arg), -1)
if len(match) != 1 {
continue
}
groups := statusPortPattern.SubexpNames()
portStr := ""
for ind, s := range match[0] {
if groups[ind] == "port" {
portStr = s
break
}
}
// Port not found from current arg, extract from next arg.
if portStr == "" {
// Matches the regex pattern, but without actual values provided.
if len(sidecar.Args) <= i+1 {
log.Errorf("No statusPort value provided, skip app probe rewriting")
return -1
}
portStr = sidecar.Args[i+1]
}
p, err := strconv.Atoi(portStr)
if err != nil {
log.Errorf("Failed to convert statusPort to int %v, err %v", portStr, err)
return -1
}
return p
}
return -1
}
// rewriteProbe changes application containers' probe to point to sidecar's status port.
func rewriteProbe(probe *corev1.Probe, appProbers *status.KubeAppProbers,
newURL string, statusPort int, portMap map[string]int32) {
if probe == nil || probe.HTTPGet == nil {
return
}
httpGet := probe.HTTPGet
// Save app probe config to pass to pilot agent later.
savedProbe := proto.Clone(probe.HTTPGet).(*corev1.HTTPGetAction)
(*appProbers)[newURL] = savedProbe
// A named port, resolve by looking at port map.
if httpGet.Port.Type == intstr.String {
port, exists := portMap[httpGet.Port.StrVal]
if !exists {
log.Errorf("named port not found in the map skip rewriting probing %v", *probe)
return
}
savedProbe.Port = intstr.FromInt(int(port))
}
// Change the application csince ontainer prober config.
httpGet.Port = intstr.FromInt(statusPort)
httpGet.Path = newURL
}
func rewriteAppHTTPProbe(spec *SidecarInjectionSpec, podSpec *corev1.PodSpec) {
if spec == nil || podSpec == nil {
return
}
if !spec.RewriteAppHTTPProbe {
return
}
var sidecar *corev1.Container
for i := range podSpec.Containers {
if podSpec.Containers[i].Name == istioProxyContainerName {
sidecar = &podSpec.Containers[i]
break
}
}
if sidecar == nil {
return
}
statusPort := extractStatusPort(sidecar)
// Pilot agent statusPort is not defined, skip changing application http probe.
if statusPort == -1 {
return
}
appProberInfo := status.KubeAppProbers{}
for _, c := range podSpec.Containers {
// Skip sidecar container.
if c.Name == istioProxyContainerName {
continue
}
portMap := map[string]int32{}
for _, p := range c.Ports {
portMap[p.Name] = p.ContainerPort
}
rewriteProbe(c.ReadinessProbe, &appProberInfo, fmt.Sprintf("/app-health/%v/readyz", c.Name), statusPort, portMap)
rewriteProbe(c.LivenessProbe, &appProberInfo, fmt.Sprintf("/app-health/%v/livez", c.Name), statusPort, portMap)
}
// Finally propagate app prober config to `istio-proxy` through command line flag.
b, err := json.Marshal(appProberInfo)
if err != nil {
log.Errorf("failed to serialize the app prober config %v", err)
return
}
// We don't have to escape json encoding here when using golang libraries.
sidecar.Args = append(sidecar.Args, []string{fmt.Sprintf("--%v", status.KubeAppProberCmdFlagName), string(b)}...)
}