...

Source file src/k8s.io/kube-aggregator/pkg/controllers/openapiv3/aggregator/downloader.go

Documentation: k8s.io/kube-aggregator/pkg/controllers/openapiv3/aggregator

     1  /*
     2  Copyright 2021 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 aggregator
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"k8s.io/apiserver/pkg/authentication/user"
    25  	"k8s.io/apiserver/pkg/endpoints/request"
    26  	"k8s.io/kube-openapi/pkg/handler3"
    27  )
    28  
    29  type NotFoundError struct {
    30  }
    31  
    32  func (e *NotFoundError) Error() string {
    33  	return ""
    34  }
    35  
    36  // Downloader is the OpenAPI downloader type. It will try to download spec from /openapi/v3 and /openap/v3/<group>/<version> endpoints.
    37  type Downloader struct {
    38  }
    39  
    40  // NewDownloader creates a new OpenAPI Downloader.
    41  func NewDownloader() Downloader {
    42  	return Downloader{}
    43  }
    44  
    45  func (s *Downloader) handlerWithUser(handler http.Handler, info user.Info) http.Handler {
    46  	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    47  		req = req.WithContext(request.WithUser(req.Context(), info))
    48  		handler.ServeHTTP(w, req)
    49  	})
    50  }
    51  
    52  // OpenAPIV3Root downloads the OpenAPI V3 root document from an APIService
    53  func (s *Downloader) OpenAPIV3Root(handler http.Handler) (*handler3.OpenAPIV3Discovery, int, error) {
    54  	handler = s.handlerWithUser(handler, &user.DefaultInfo{Name: aggregatorUser})
    55  	handler = http.TimeoutHandler(handler, specDownloadTimeout, "request timed out")
    56  
    57  	req, err := http.NewRequest("GET", "/openapi/v3", nil)
    58  	if err != nil {
    59  		return nil, 0, err
    60  	}
    61  	writer := newInMemoryResponseWriter()
    62  	handler.ServeHTTP(writer, req)
    63  
    64  	switch writer.respCode {
    65  	case http.StatusNotFound:
    66  		return nil, writer.respCode, nil
    67  	case http.StatusOK:
    68  		groups := handler3.OpenAPIV3Discovery{}
    69  		if err := json.Unmarshal(writer.data, &groups); err != nil {
    70  			return nil, writer.respCode, err
    71  		}
    72  		return &groups, writer.respCode, nil
    73  	}
    74  	return nil, writer.respCode, fmt.Errorf("Error, could not get list of group versions for APIService")
    75  }
    76  
    77  // inMemoryResponseWriter is a http.Writer that keep the response in memory.
    78  type inMemoryResponseWriter struct {
    79  	writeHeaderCalled bool
    80  	header            http.Header
    81  	respCode          int
    82  	data              []byte
    83  }
    84  
    85  func newInMemoryResponseWriter() *inMemoryResponseWriter {
    86  	return &inMemoryResponseWriter{header: http.Header{}}
    87  }
    88  
    89  func (r *inMemoryResponseWriter) Header() http.Header {
    90  	return r.header
    91  }
    92  
    93  func (r *inMemoryResponseWriter) WriteHeader(code int) {
    94  	r.writeHeaderCalled = true
    95  	r.respCode = code
    96  }
    97  
    98  func (r *inMemoryResponseWriter) Write(in []byte) (int, error) {
    99  	if !r.writeHeaderCalled {
   100  		r.WriteHeader(http.StatusOK)
   101  	}
   102  	r.data = append(r.data, in...)
   103  	return len(in), nil
   104  }
   105  
   106  func (r *inMemoryResponseWriter) String() string {
   107  	s := fmt.Sprintf("ResponseCode: %d", r.respCode)
   108  	if r.data != nil {
   109  		s += fmt.Sprintf(", Body: %s", string(r.data))
   110  	}
   111  	if r.header != nil {
   112  		s += fmt.Sprintf(", Header: %s", r.header)
   113  	}
   114  	return s
   115  }
   116  

View as plain text