@@ -93,7 +93,8 @@ func extractStatusPort(spec *SidecarInjectionSpec) int {
9393 return statusPort
9494}
9595
96- func calculateProbeRewrite (podSpec * corev1.PodSpec , spec * SidecarInjectionSpec , modify bool ) []rfc6902PatchOperation {
96+ // createProbeRewritePatch generates the patch for webhook.
97+ func createProbeRewritePatch (podSpec * corev1.PodSpec , spec * SidecarInjectionSpec ) []rfc6902PatchOperation {
9798 patch := []rfc6902PatchOperation {}
9899 if spec == nil || podSpec == nil {
99100 return patch
@@ -103,7 +104,6 @@ func calculateProbeRewrite(podSpec *corev1.PodSpec, spec *SidecarInjectionSpec,
103104 if statusPort == - 1 {
104105 return patch
105106 }
106-
107107 // Change the application containers' probe to point to sidecar's status port.
108108 rewriteProbe := func (probe * corev1.Probe , portMap map [string ]int32 , path string ) * rfc6902PatchOperation {
109109 if probe == nil || probe .HTTPGet == nil {
@@ -127,9 +127,6 @@ func calculateProbeRewrite(podSpec *corev1.PodSpec, spec *SidecarInjectionSpec,
127127 }
128128 httpGet .HTTPHeaders = append (httpGet .HTTPHeaders , header )
129129 httpGet .Port = intstr .FromInt (statusPort )
130- if modify {
131- probe .HTTPGet = httpGet
132- }
133130 return & rfc6902PatchOperation {
134131 Op : "replace" ,
135132 Path : path ,
@@ -155,11 +152,53 @@ func calculateProbeRewrite(podSpec *corev1.PodSpec, spec *SidecarInjectionSpec,
155152 return patch
156153}
157154
158- // createProbeRewritePatch generates the patch for webhook.
159- func createProbeRewritePatch (podSpec * corev1.PodSpec , sic * SidecarInjectionSpec ) []rfc6902PatchOperation {
160- return calculateProbeRewrite (podSpec , sic , false )
161- }
162-
155+ // rewriteAppHTTPProbe modifies the podSpec HTTP probers to redirect to pilot agent.
163156func rewriteAppHTTPProbe (podSpec * corev1.PodSpec , spec * SidecarInjectionSpec ) {
164- calculateProbeRewrite (podSpec , spec , true )
157+ if spec == nil || podSpec == nil {
158+ return
159+ }
160+ statusPort := extractStatusPort (spec )
161+ // Pilot agent statusPort is not defined, skip changing application http probe.
162+ if statusPort == - 1 {
163+ return
164+ }
165+
166+ // Change the application containers' probe to point to sidecar's status port.
167+ rewriteProbe := func (probe * corev1.Probe , portMap map [string ]int32 ) {
168+ if probe == nil || probe .HTTPGet == nil {
169+ return
170+ }
171+ httpGet := probe .HTTPGet
172+ // Walkaround... proto.Clone can't copy corev1.IntOrStr somehow...
173+ httpGet .Port = probe .HTTPGet .Port
174+ header := corev1.HTTPHeader {
175+ Name : status .IstioAppPortHeader ,
176+ Value : httpGet .Port .String (),
177+ }
178+ // A named port, resolve by looking at port map.
179+ if httpGet .Port .Type == intstr .String {
180+ port , exists := portMap [httpGet .Port .StrVal ]
181+ if ! exists {
182+ log .Errorf ("named port not found in the map skip rewriting probing %v" , * probe )
183+ return
184+ }
185+ header .Value = strconv .Itoa (int (port ))
186+ }
187+ httpGet .HTTPHeaders = append (httpGet .HTTPHeaders , header )
188+ httpGet .Port = intstr .FromInt (statusPort )
189+ probe .HTTPGet = httpGet
190+ return
191+ }
192+ for _ , c := range podSpec .Containers {
193+ // Skip sidecar container.
194+ if c .Name == istioProxyContainerName {
195+ continue
196+ }
197+ portMap := map [string ]int32 {}
198+ for _ , p := range c .Ports {
199+ portMap [p .Name ] = p .ContainerPort
200+ }
201+ rewriteProbe (c .ReadinessProbe , portMap )
202+ rewriteProbe (c .LivenessProbe , portMap )
203+ }
165204}
0 commit comments