This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
forked from knative/serving
/
queue.go
366 lines (325 loc) · 11 KB
/
queue.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2018 The Knative 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 resources
import (
"fmt"
"math"
"strconv"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"knative.dev/pkg/logging"
pkgmetrics "knative.dev/pkg/metrics"
"knative.dev/pkg/profiling"
"knative.dev/pkg/ptr"
"knative.dev/pkg/system"
tracingconfig "knative.dev/pkg/tracing/config"
"knative.dev/serving/pkg/apis/networking"
"knative.dev/serving/pkg/apis/serving"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
"knative.dev/serving/pkg/deployment"
"knative.dev/serving/pkg/metrics"
"knative.dev/serving/pkg/network"
"knative.dev/serving/pkg/queue"
"knative.dev/serving/pkg/queue/readiness"
)
const (
localAddress = "127.0.0.1"
requestQueueHTTPPortName = "queue-port"
profilingPortName = "profiling-port"
)
var (
queueHTTPPort = corev1.ContainerPort{
Name: requestQueueHTTPPortName,
ContainerPort: int32(networking.BackendHTTPPort),
}
queueHTTP2Port = corev1.ContainerPort{
Name: requestQueueHTTPPortName,
ContainerPort: int32(networking.BackendHTTP2Port),
}
queueNonServingPorts = []corev1.ContainerPort{{
// Provides health checks and lifecycle hooks.
Name: v1alpha1.QueueAdminPortName,
ContainerPort: int32(networking.QueueAdminPort),
}, {
Name: v1alpha1.AutoscalingQueueMetricsPortName,
ContainerPort: int32(networking.AutoscalingQueueMetricsPort),
}, {
Name: v1alpha1.UserQueueMetricsPortName,
ContainerPort: int32(networking.UserQueueMetricsPort),
}}
profilingPort = corev1.ContainerPort{
Name: profilingPortName,
ContainerPort: int32(profiling.ProfilingPort),
}
queueSecurityContext = &corev1.SecurityContext{
AllowPrivilegeEscalation: ptr.Bool(false),
}
)
func createQueueResources(annotations map[string]string, userContainer *corev1.Container) corev1.ResourceRequirements {
resourceRequests := corev1.ResourceList{corev1.ResourceCPU: queueContainerCPU}
resourceLimits := corev1.ResourceList{}
var requestCPU, limitCPU, requestMemory, limitMemory resource.Quantity
if resourceFraction, ok := fractionFromPercentage(annotations, serving.QueueSideCarResourcePercentageAnnotation); ok {
if ok, requestCPU = computeResourceRequirements(userContainer.Resources.Requests.Cpu(), resourceFraction, queueContainerRequestCPU); ok {
resourceRequests[corev1.ResourceCPU] = requestCPU
}
if ok, limitCPU = computeResourceRequirements(userContainer.Resources.Limits.Cpu(), resourceFraction, queueContainerLimitCPU); ok {
resourceLimits[corev1.ResourceCPU] = limitCPU
}
if ok, requestMemory = computeResourceRequirements(userContainer.Resources.Requests.Memory(), resourceFraction, queueContainerRequestMemory); ok {
resourceRequests[corev1.ResourceMemory] = requestMemory
}
if ok, limitMemory = computeResourceRequirements(userContainer.Resources.Limits.Memory(), resourceFraction, queueContainerLimitMemory); ok {
resourceLimits[corev1.ResourceMemory] = limitMemory
}
}
resources := corev1.ResourceRequirements{
Requests: resourceRequests,
}
if len(resourceLimits) != 0 {
resources.Limits = resourceLimits
}
return resources
}
func computeResourceRequirements(resourceQuantity *resource.Quantity, fraction float64, boundary resourceBoundary) (bool, resource.Quantity) {
if resourceQuantity.IsZero() {
return false, resource.Quantity{}
}
// In case the resourceQuantity MilliValue overflows int64 we use MaxInt64
// https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go
scaledValue := resourceQuantity.Value()
scaledMilliValue := int64(math.MaxInt64 - 1)
if scaledValue < (math.MaxInt64 / 1000) {
scaledMilliValue = resourceQuantity.MilliValue()
}
// float64(math.MaxInt64) > math.MaxInt64, to avoid overflow
percentageValue := float64(scaledMilliValue) * fraction
newValue := int64(math.MaxInt64)
if percentageValue < math.MaxInt64 {
newValue = int64(percentageValue)
}
newquantity := boundary.applyBoundary(*resource.NewMilliQuantity(newValue, resource.BinarySI))
return true, newquantity
}
func fractionFromPercentage(m map[string]string, k string) (float64, bool) {
value, err := strconv.ParseFloat(m[k], 64)
return float64(value / 100), err == nil
}
func makeQueueProbe(in *corev1.Probe) *corev1.Probe {
if in == nil || in.PeriodSeconds == 0 {
out := &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"/ko-app/queue", "-probe-period", "0"},
},
},
// We want to mark the service as not ready as soon as the
// PreStop handler is called, so we need to check a little
// bit more often than the default. It is a small
// sacrifice for a low rate of 503s.
PeriodSeconds: 1,
// We keep the connection open for a while because we're
// actively probing the user-container on that endpoint and
// thus don't want to be limited by K8s granularity here.
TimeoutSeconds: 10,
}
if in != nil {
out.InitialDelaySeconds = in.InitialDelaySeconds
}
return out
}
timeout := 1
if in.TimeoutSeconds > 1 {
timeout = int(in.TimeoutSeconds)
}
return &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{"/ko-app/queue", "-probe-period", strconv.Itoa(timeout)},
},
},
PeriodSeconds: in.PeriodSeconds,
TimeoutSeconds: int32(timeout),
SuccessThreshold: in.SuccessThreshold,
FailureThreshold: in.FailureThreshold,
InitialDelaySeconds: in.InitialDelaySeconds,
}
}
// makeQueueContainer creates the container spec for the queue sidecar.
func makeQueueContainer(rev *v1alpha1.Revision, loggingConfig *logging.Config, tracingConfig *tracingconfig.Config, observabilityConfig *metrics.ObservabilityConfig,
deploymentConfig *deployment.Config) (*corev1.Container, error) {
configName := ""
if owner := metav1.GetControllerOf(rev); owner != nil && owner.Kind == "Configuration" {
configName = owner.Name
}
serviceName := rev.Labels[serving.ServiceLabelKey]
userPort := getUserPort(rev)
var loggingLevel string
if ll, ok := loggingConfig.LoggingLevel["queueproxy"]; ok {
loggingLevel = ll.String()
}
ts := int64(0)
if rev.Spec.TimeoutSeconds != nil {
ts = *rev.Spec.TimeoutSeconds
}
ports := queueNonServingPorts
if observabilityConfig.EnableProfiling {
ports = append(ports, profilingPort)
}
// We need to configure only one serving port for the Queue proxy, since
// we know the protocol that is being used by this application.
servingPort := queueHTTPPort
if rev.GetProtocol() == networking.ProtocolH2C {
servingPort = queueHTTP2Port
}
ports = append(ports, servingPort)
var volumeMounts []corev1.VolumeMount
if observabilityConfig.EnableVarLogCollection {
volumeMounts = append(volumeMounts, internalVolumeMount)
}
rp := rev.Spec.GetContainer().ReadinessProbe.DeepCopy()
applyReadinessProbeDefaults(rp, userPort)
probeJSON, err := readiness.EncodeProbe(rp)
if err != nil {
return nil, fmt.Errorf("failed to serialize readiness probe: %w", err)
}
return &corev1.Container{
Name: QueueContainerName,
Image: deploymentConfig.QueueSidecarImage,
Resources: createQueueResources(rev.GetAnnotations(), rev.Spec.GetContainer()),
Ports: ports,
ReadinessProbe: makeQueueProbe(rp),
VolumeMounts: volumeMounts,
SecurityContext: queueSecurityContext,
Env: []corev1.EnvVar{{
Name: "SERVING_NAMESPACE",
Value: rev.Namespace,
}, {
Name: "SERVING_SERVICE",
Value: serviceName,
}, {
Name: "SERVING_CONFIGURATION",
Value: configName,
}, {
Name: "SERVING_REVISION",
Value: rev.Name,
}, {
Name: "QUEUE_SERVING_PORT",
Value: strconv.Itoa(int(servingPort.ContainerPort)),
}, {
Name: "CONTAINER_CONCURRENCY",
Value: strconv.Itoa(int(rev.Spec.GetContainerConcurrency())),
}, {
Name: "REVISION_TIMEOUT_SECONDS",
Value: strconv.Itoa(int(ts)),
}, {
Name: "SERVING_POD",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
}, {
Name: "SERVING_POD_IP",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "status.podIP",
},
},
}, {
Name: "SERVING_LOGGING_CONFIG",
Value: loggingConfig.LoggingConfig,
}, {
Name: "SERVING_LOGGING_LEVEL",
Value: loggingLevel,
}, {
Name: "SERVING_REQUEST_LOG_TEMPLATE",
Value: observabilityConfig.RequestLogTemplate,
}, {
Name: "SERVING_REQUEST_METRICS_BACKEND",
Value: observabilityConfig.RequestMetricsBackend,
}, {
Name: "TRACING_CONFIG_BACKEND",
Value: string(tracingConfig.Backend),
}, {
Name: "TRACING_CONFIG_ZIPKIN_ENDPOINT",
Value: tracingConfig.ZipkinEndpoint,
}, {
Name: "TRACING_CONFIG_STACKDRIVER_PROJECT_ID",
Value: tracingConfig.StackdriverProjectID,
}, {
Name: "TRACING_CONFIG_DEBUG",
Value: strconv.FormatBool(tracingConfig.Debug),
}, {
Name: "TRACING_CONFIG_SAMPLE_RATE",
Value: fmt.Sprintf("%f", tracingConfig.SampleRate),
}, {
Name: "USER_PORT",
Value: strconv.Itoa(int(userPort)),
}, {
Name: system.NamespaceEnvKey,
Value: system.Namespace(),
}, {
Name: pkgmetrics.DomainEnv,
Value: pkgmetrics.Domain(),
}, {
Name: "USER_CONTAINER_NAME",
Value: rev.Spec.GetContainer().Name,
}, {
Name: "ENABLE_VAR_LOG_COLLECTION",
Value: strconv.FormatBool(observabilityConfig.EnableVarLogCollection),
}, {
Name: "VAR_LOG_VOLUME_NAME",
Value: varLogVolumeName,
}, {
Name: "INTERNAL_VOLUME_PATH",
Value: internalVolumePath,
}, {
Name: "SERVING_READINESS_PROBE",
Value: probeJSON,
}, {
Name: "ENABLE_PROFILING",
Value: strconv.FormatBool(observabilityConfig.EnableProfiling),
}, {
Name: "SERVING_ENABLE_PROBE_REQUEST_LOG",
Value: strconv.FormatBool(observabilityConfig.EnableProbeRequestLog),
}},
}, nil
}
func applyReadinessProbeDefaults(p *corev1.Probe, port int32) {
switch {
case p == nil:
return
case p.HTTPGet != nil:
p.HTTPGet.Host = localAddress
p.HTTPGet.Port = intstr.FromInt(int(port))
if p.HTTPGet.Scheme == "" {
p.HTTPGet.Scheme = corev1.URISchemeHTTP
}
p.HTTPGet.HTTPHeaders = append(p.HTTPGet.HTTPHeaders, corev1.HTTPHeader{
Name: network.KubeletProbeHeaderName,
Value: queue.Name,
})
case p.TCPSocket != nil:
p.TCPSocket.Host = localAddress
p.TCPSocket.Port = intstr.FromInt(int(port))
case p.Exec != nil:
//User-defined ExecProbe will still be run on user-container.
p.Exec = nil
}
if p.PeriodSeconds > 0 && p.TimeoutSeconds < 1 {
p.TimeoutSeconds = 1
}
}