Skip to content

Commit 4f6e8f1

Browse files
committed
merged changes to QPOptions branch (pull request knative#13133)
2 parents c1385e3 + 9a4d6d9 commit 4f6e8f1

File tree

2 files changed

+63
-28
lines changed

2 files changed

+63
-28
lines changed

cmd/queue/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ limitations under the License.
1616

1717
package main
1818

19-
import "knative.dev/serving/pkg/queue/sharedmain"
19+
import (
20+
"os"
21+
22+
"knative.dev/serving/pkg/queue/sharedmain"
23+
)
2024

2125
func main() {
22-
sharedmain.Main()
26+
if sharedmain.Main() != nil {
27+
os.Exit(1)
28+
}
2329
}

pkg/queue/sharedmain/main.go

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const (
7373
keyPath = queue.CertDirectory + "/" + certificates.SecretPKKey
7474
)
7575

76-
type privateEnv struct {
76+
type config struct {
7777
ContainerConcurrency int `split_words:"true" required:"true"`
7878
QueueServingPort string `split_words:"true" required:"true"`
7979
QueueServingTLSPort string `split_words:"true" required:"true"`
@@ -108,20 +108,51 @@ type privateEnv struct {
108108
Env
109109
}
110110

111+
// Env exposes parsed QP environment variables for use by Options (QP Extensions)
111112
type Env struct {
112-
ServingNamespace string `split_words:"true" required:"true"`
113-
ServingRevision string `split_words:"true" required:"true"`
113+
// ServingNamespace is the namespace in which the service is defined
114+
ServingNamespace string `split_words:"true" required:"true"`
115+
116+
// ServingService is the name of the service served by this pod
117+
ServingService string `split_words:"true"` // optional
118+
119+
// ServingConfiguration is the name of service configuration served by this pod
114120
ServingConfiguration string `split_words:"true" required:"true"`
115-
ServingPodIP string `split_words:"true" required:"true"`
116-
ServingPod string `split_words:"true" required:"true"`
117-
ServingService string `split_words:"true"` // optional
121+
122+
// ServingRevision is the name of service revision served by this pod
123+
ServingRevision string `split_words:"true" required:"true"`
124+
125+
// ServingPod is the pod name
126+
ServingPod string `split_words:"true" required:"true"`
127+
128+
// ServingPodIP is the pod ip address
129+
ServingPodIP string `split_words:"true" required:"true"`
118130
}
119131

132+
// Defaults provides Options (QP Extensions) with the default bahaviour of QP
133+
// Some attributes of Defaults may be modified by Options
134+
// Modifying Defaults mutates the behavior of QP
120135
type Defaults struct {
121-
Ctx context.Context
122-
Logger *zap.SugaredLogger
136+
// Logger enables Options to use the QP pre-configured logger
137+
// It is expected that Options will use the provided Logger when logging
138+
// Options should not modify the provided Default Logger
139+
Logger *zap.SugaredLogger
140+
141+
// Env exposes parsed QP environment variables for use by Options
142+
// Options should not modify the provided environment parameters
143+
Env Env
144+
145+
// Ctx provides Options with the QP context
146+
// An Option may derive a new context from Ctx. If a new context is derived,
147+
// the derived context should replace the value of Ctx.
148+
// The new Ctx will then be used by other Options (called next) and by QP.
149+
Ctx context.Context
150+
151+
// Transport provides Options with the QP RoundTripper
152+
// An Option may wrap the provided Transport to add a Roundtripper.
153+
// If Transport is wrapped, the new RoundTripper should replace the value of Transport.
154+
// The new Transport will then be used by other Options (called next) and by QP.
123155
Transport http.RoundTripper
124-
Env Env
125156
}
126157

127158
type Option func(*Defaults)
@@ -131,19 +162,19 @@ func init() {
131162
}
132163

133164
func Main(opts ...Option) error {
134-
d := &Defaults{
165+
d := Defaults{
135166
Ctx: signals.NewContext(),
136167
}
137168

138169
// Parse the environment.
139-
var env privateEnv
170+
var env config
140171
if err := envconfig.Process("", &env); err != nil {
141172
return err
142173
}
143174

144175
d.Env = env.Env
145176

146-
// Setup the logger.
177+
// Setup the Logger.
147178
logger, _ := pkglogging.NewLogger(env.ServingLoggingConfig, env.ServingLoggingLevel)
148179
defer flush(logger)
149180

@@ -159,13 +190,11 @@ func Main(opts ...Option) error {
159190

160191
// allow extensions to read d and return modified context and transport
161192
for _, opts := range opts {
162-
opts(d)
193+
opts(&d)
163194
}
164-
ctx := d.Ctx
165-
transport := d.Transport
166195

167196
// Report stats on Go memory usage every 30 seconds.
168-
metrics.MemStatsOrDie(ctx)
197+
metrics.MemStatsOrDie(d.Ctx)
169198

170199
protoStatReporter := queue.NewProtobufStatsReporter(env.ServingPod, reportingPeriod)
171200

@@ -196,7 +225,7 @@ func Main(opts ...Option) error {
196225
// Enable TLS when certificate is mounted.
197226
tlsEnabled := exists(logger, certPath) && exists(logger, keyPath)
198227

199-
mainServer, drain := buildServer(ctx, env, transport, probe, stats, logger, concurrencyendpoint, false)
228+
mainServer, drain := buildServer(d.Ctx, env, d.Transport, probe, stats, logger, concurrencyendpoint, false)
200229
httpServers := map[string]*http.Server{
201230
"main": mainServer,
202231
"metrics": buildMetricsServer(protoStatReporter),
@@ -211,7 +240,7 @@ func Main(opts ...Option) error {
211240
// See also https://github.com/knative/serving/issues/12808.
212241
var tlsServers map[string]*http.Server
213242
if tlsEnabled {
214-
mainTLSServer, drain := buildServer(ctx, env, transport, probe, stats, logger, concurrencyendpoint, true /* enable TLS */)
243+
mainTLSServer, drain := buildServer(d.Ctx, env, d.Transport, probe, stats, logger, concurrencyendpoint, true /* enable TLS */)
215244
tlsServers = map[string]*http.Server{
216245
"tlsMain": mainTLSServer,
217246
"tlsAdmin": buildAdminServer(logger, drain),
@@ -248,7 +277,7 @@ func Main(opts ...Option) error {
248277
case err := <-errCh:
249278
logger.Errorw("Failed to bring up queue-proxy, shutting down.", zap.Error(err))
250279
return err
251-
case <-ctx.Done():
280+
case <-d.Ctx.Done():
252281
if env.ConcurrencyStateEndpoint != "" {
253282
concurrencyendpoint.Terminating(logger)
254283
}
@@ -289,7 +318,7 @@ func buildProbe(logger *zap.SugaredLogger, encodedProbe string, autodetectHTTP2
289318
return readiness.NewProbe(coreProbe)
290319
}
291320

292-
func buildServer(ctx context.Context, env privateEnv, transport http.RoundTripper, probeContainer func() bool, stats *netstats.RequestStats, logger *zap.SugaredLogger,
321+
func buildServer(ctx context.Context, env config, transport http.RoundTripper, probeContainer func() bool, stats *netstats.RequestStats, logger *zap.SugaredLogger,
293322
ce *queue.ConcurrencyEndpoint, enableTLS bool) (server *http.Server, drain func()) {
294323
// TODO: If TLS is enabled, execute probes twice and tracking two different sets of container health.
295324

@@ -362,7 +391,7 @@ func buildServer(ctx context.Context, env privateEnv, transport http.RoundTrippe
362391
return pkgnet.NewServer(":"+env.QueueServingPort, composedHandler), drainer.Drain
363392
}
364393

365-
func buildTransport(env privateEnv, logger *zap.SugaredLogger) http.RoundTripper {
394+
func buildTransport(env config, logger *zap.SugaredLogger) http.RoundTripper {
366395
maxIdleConns := 1000 // TODO: somewhat arbitrary value for CC=0, needs experimental validation.
367396
if env.ContainerConcurrency > 0 {
368397
maxIdleConns = env.ContainerConcurrency
@@ -388,7 +417,7 @@ func buildTransport(env privateEnv, logger *zap.SugaredLogger) http.RoundTripper
388417
}
389418
}
390419

391-
func buildBreaker(logger *zap.SugaredLogger, env privateEnv) *queue.Breaker {
420+
func buildBreaker(logger *zap.SugaredLogger, env config) *queue.Breaker {
392421
if env.ContainerConcurrency < 1 {
393422
return nil
394423
}
@@ -405,7 +434,7 @@ func buildBreaker(logger *zap.SugaredLogger, env privateEnv) *queue.Breaker {
405434
return queue.NewBreaker(params)
406435
}
407436

408-
func supportsMetrics(ctx context.Context, logger *zap.SugaredLogger, env privateEnv, enableTLS bool) bool {
437+
func supportsMetrics(ctx context.Context, logger *zap.SugaredLogger, env config, enableTLS bool) bool {
409438
// Keep it on HTTP because Metrics needs to be registered on either TLS server or non-TLS server.
410439
if enableTLS {
411440
return false
@@ -444,7 +473,7 @@ func buildMetricsServer(protobufStatReporter *queue.ProtobufStatsReporter) *http
444473
}
445474
}
446475

447-
func requestLogHandler(logger *zap.SugaredLogger, currentHandler http.Handler, env privateEnv) http.Handler {
476+
func requestLogHandler(logger *zap.SugaredLogger, currentHandler http.Handler, env config) http.Handler {
448477
revInfo := &pkghttp.RequestLogRevision{
449478
Name: env.ServingRevision,
450479
Namespace: env.ServingNamespace,
@@ -462,7 +491,7 @@ func requestLogHandler(logger *zap.SugaredLogger, currentHandler http.Handler, e
462491
return handler
463492
}
464493

465-
func requestMetricsHandler(logger *zap.SugaredLogger, currentHandler http.Handler, env privateEnv) http.Handler {
494+
func requestMetricsHandler(logger *zap.SugaredLogger, currentHandler http.Handler, env config) http.Handler {
466495
h, err := queue.NewRequestMetricsHandler(currentHandler, env.ServingNamespace,
467496
env.ServingService, env.ServingConfiguration, env.ServingRevision, env.ServingPod)
468497
if err != nil {
@@ -472,7 +501,7 @@ func requestMetricsHandler(logger *zap.SugaredLogger, currentHandler http.Handle
472501
return h
473502
}
474503

475-
func requestAppMetricsHandler(logger *zap.SugaredLogger, currentHandler http.Handler, breaker *queue.Breaker, env privateEnv) http.Handler {
504+
func requestAppMetricsHandler(logger *zap.SugaredLogger, currentHandler http.Handler, breaker *queue.Breaker, env config) http.Handler {
476505
h, err := queue.NewAppRequestMetricsHandler(currentHandler, breaker, env.ServingNamespace,
477506
env.ServingService, env.ServingConfiguration, env.ServingRevision, env.ServingPod)
478507
if err != nil {

0 commit comments

Comments
 (0)