k8s.io/apiserver@v0.31.1/pkg/server/filters/waitgroup.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package filters
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"k8s.io/api/core/v1"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
    28  	apirequest "k8s.io/apiserver/pkg/endpoints/request"
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  )
    31  
    32  // RequestWaitGroup helps with the accounting of request(s) that are in
    33  // flight: the caller is expected to invoke Add(1) before executing the
    34  // request handler and then invoke Done() when the handler finishes.
    35  // NOTE: implementations must ensure that it is thread-safe
    36  // when invoked from multiple goroutines.
    37  type RequestWaitGroup interface {
    38  	// Add adds delta, which may be negative, similar to sync.WaitGroup.
    39  	// If Add with a positive delta happens after Wait, it will return error,
    40  	// which prevent unsafe Add.
    41  	Add(delta int) error
    42  
    43  	// Done decrements the WaitGroup counter.
    44  	Done()
    45  }
    46  
    47  // WithWaitGroup adds all non long-running requests to wait group, which is used for graceful shutdown.
    48  func WithWaitGroup(handler http.Handler, longRunning apirequest.LongRunningRequestCheck, wg RequestWaitGroup) http.Handler {
    49  	// NOTE: both WithWaitGroup and WithRetryAfter must use the same exact isRequestExemptFunc 'isRequestExemptFromRetryAfter,
    50  	// otherwise SafeWaitGroup might wait indefinitely and will prevent the server from shutting down gracefully.
    51  	return withWaitGroup(handler, longRunning, wg, isRequestExemptFromRetryAfter)
    52  }
    53  
    54  func withWaitGroup(handler http.Handler, longRunning apirequest.LongRunningRequestCheck, wg RequestWaitGroup, isRequestExemptFn isRequestExemptFunc) http.Handler {
    55  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    56  		ctx := req.Context()
    57  		requestInfo, ok := apirequest.RequestInfoFrom(ctx)
    58  		if !ok {
    59  			// if this happens, the handler chain isn't setup correctly because there is no request info
    60  			responsewriters.InternalError(w, req, errors.New("no RequestInfo found in the context"))
    61  			return
    62  		}
    63  
    64  		if longRunning(req, requestInfo) {
    65  			handler.ServeHTTP(w, req)
    66  			return
    67  		}
    68  
    69  		if err := wg.Add(1); err != nil {
    70  			// shutdown delay duration has elapsed and SafeWaitGroup.Wait has been invoked,
    71  			// this means 'WithRetryAfter' has started sending Retry-After response.
    72  			// we are going to exempt the same set of requests that WithRetryAfter are
    73  			// exempting from being rejected with a Retry-After response.
    74  			if isRequestExemptFn(req) {
    75  				handler.ServeHTTP(w, req)
    76  				return
    77  			}
    78  
    79  			// When apiserver is shutting down, signal clients to retry
    80  			// There is a good chance the client hit a different server, so a tight retry is good for client responsiveness.
    81  			waitGroupWriteRetryAfterToResponse(w)
    82  			return
    83  		}
    84  
    85  		defer wg.Done()
    86  		handler.ServeHTTP(w, req)
    87  	})
    88  }
    89  
    90  func waitGroupWriteRetryAfterToResponse(w http.ResponseWriter) {
    91  	w.Header().Add("Retry-After", "1")
    92  	w.Header().Set("Content-Type", runtime.ContentTypeJSON)
    93  	w.Header().Set("X-Content-Type-Options", "nosniff")
    94  	statusErr := apierrors.NewServiceUnavailable("apiserver is shutting down").Status()
    95  	w.WriteHeader(int(statusErr.Code))
    96  	fmt.Fprintln(w, runtime.EncodeOrDie(scheme.Codecs.LegacyCodec(v1.SchemeGroupVersion), &statusErr))
    97  }