mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 23:15:14 +00:00
kube-aggregator: move helper code down
This commit is contained in:
parent
749d98c2a8
commit
1c67881c37
@ -38,46 +38,6 @@ func NewDownloader() Downloader {
|
|||||||
return Downloader{}
|
return Downloader{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// inMemoryResponseWriter is a http.Writer that keep the response in memory.
|
|
||||||
type inMemoryResponseWriter struct {
|
|
||||||
writeHeaderCalled bool
|
|
||||||
header http.Header
|
|
||||||
respCode int
|
|
||||||
data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func newInMemoryResponseWriter() *inMemoryResponseWriter {
|
|
||||||
return &inMemoryResponseWriter{header: http.Header{}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *inMemoryResponseWriter) Header() http.Header {
|
|
||||||
return r.header
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *inMemoryResponseWriter) WriteHeader(code int) {
|
|
||||||
r.writeHeaderCalled = true
|
|
||||||
r.respCode = code
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *inMemoryResponseWriter) Write(in []byte) (int, error) {
|
|
||||||
if !r.writeHeaderCalled {
|
|
||||||
r.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
r.data = append(r.data, in...)
|
|
||||||
return len(in), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *inMemoryResponseWriter) String() string {
|
|
||||||
s := fmt.Sprintf("ResponseCode: %d", r.respCode)
|
|
||||||
if r.data != nil {
|
|
||||||
s += fmt.Sprintf(", Body: %s", string(r.data))
|
|
||||||
}
|
|
||||||
if r.header != nil {
|
|
||||||
s += fmt.Sprintf(", Header: %s", r.header)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Downloader) handlerWithUser(handler http.Handler, info user.Info) http.Handler {
|
func (s *Downloader) handlerWithUser(handler http.Handler, info user.Info) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
req = req.WithContext(request.WithUser(req.Context(), info))
|
req = req.WithContext(request.WithUser(req.Context(), info))
|
||||||
@ -141,3 +101,43 @@ func (s *Downloader) Download(handler http.Handler, etag string) (returnSpec *sp
|
|||||||
return nil, "", 0, fmt.Errorf("failed to retrieve openAPI spec, http error: %s", writer.String())
|
return nil, "", 0, fmt.Errorf("failed to retrieve openAPI spec, http error: %s", writer.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// inMemoryResponseWriter is a http.Writer that keep the response in memory.
|
||||||
|
type inMemoryResponseWriter struct {
|
||||||
|
writeHeaderCalled bool
|
||||||
|
header http.Header
|
||||||
|
respCode int
|
||||||
|
data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInMemoryResponseWriter() *inMemoryResponseWriter {
|
||||||
|
return &inMemoryResponseWriter{header: http.Header{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inMemoryResponseWriter) Header() http.Header {
|
||||||
|
return r.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inMemoryResponseWriter) WriteHeader(code int) {
|
||||||
|
r.writeHeaderCalled = true
|
||||||
|
r.respCode = code
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inMemoryResponseWriter) Write(in []byte) (int, error) {
|
||||||
|
if !r.writeHeaderCalled {
|
||||||
|
r.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
r.data = append(r.data, in...)
|
||||||
|
return len(in), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inMemoryResponseWriter) String() string {
|
||||||
|
s := fmt.Sprintf("ResponseCode: %d", r.respCode)
|
||||||
|
if r.data != nil {
|
||||||
|
s += fmt.Sprintf(", Body: %s", string(r.data))
|
||||||
|
}
|
||||||
|
if r.header != nil {
|
||||||
|
s += fmt.Sprintf(", Header: %s", r.header)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user