github.com/docker/compose-on-kubernetes@v0.5.0/internal/requestaddons/skipvalidation.go (about)

     1  package requestaddons
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"k8s.io/apiserver/pkg/endpoints/request"
     8  )
     9  
    10  type skipValidationKeyType int
    11  
    12  const skipValidationKey skipValidationKeyType = iota
    13  
    14  // WithSkipValidationHandler adds a query parameter parsing for skipping validation
    15  func WithSkipValidationHandler(handler http.Handler) http.Handler {
    16  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    17  		skipValidation := req.FormValue("skip-validation") == "1"
    18  		ctx := req.Context()
    19  		req = req.WithContext(WithSkipValidation(ctx, skipValidation))
    20  		handler.ServeHTTP(w, req)
    21  	})
    22  }
    23  
    24  // WithSkipValidation adds the skip-validation info in the request context
    25  func WithSkipValidation(parent context.Context, skipValidation bool) context.Context {
    26  	return request.WithValue(parent, skipValidationKey, skipValidation)
    27  }
    28  
    29  // SkipValidationFrom gets the skip-validation option value from the context
    30  func SkipValidationFrom(ctx context.Context) bool {
    31  	val := ctx.Value(skipValidationKey)
    32  	if val == nil {
    33  		return false
    34  	}
    35  	if v, ok := val.(bool); ok {
    36  		return v
    37  	}
    38  	return false
    39  }