Debian Codegraph @

pkg/registry/generic/rest/streamer.go

raw
1 /*
2 Copyright 2014 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 rest
18
19 import (
20 "context"
21 "errors"
22 "fmt"
23 "io"
24 "net/http"
25 "net/url"
26 "strings"
27
28 "k8s.io/apimachinery/pkg/runtime"
29 "k8s.io/apimachinery/pkg/runtime/schema"
30 "k8s.io/apiserver/pkg/registry/rest"
31 )
32
33 type CounterMetric interface {
34 Inc()
35 }
36
37 // LocationStreamer is a resource that streams the contents of a particular
38 // location URL.
39 type LocationStreamer struct {
40 Location *url.URL
41 Transport http.RoundTripper
42 ContentType string
43 Flush bool
44 ResponseChecker HttpResponseChecker
45 RedirectChecker func(req *http.Request, via []*http.Request) error
46 // TLSVerificationErrorCounter is an optional value that will Inc every time a TLS error is encountered. This can
47 // be wired a single prometheus counter instance to get counts overall.
48 TLSVerificationErrorCounter CounterMetric
49 // DeprecatedTLSVerificationErrorCounter is a temporary field used to rename
50 // the kube_apiserver_pod_logs_pods_logs_backend_tls_failure_total metric
51 // with a one release deprecation period in 1.27.0.
52 DeprecatedTLSVerificationErrorCounter CounterMetric
53 }
54
55 // a LocationStreamer must implement a rest.ResourceStreamer
56 var _ rest.ResourceStreamer = &LocationStreamer{}
57
58 func (obj *LocationStreamer) GetObjectKind() schema.ObjectKind {
59 return schema.EmptyObjectKind
60 }
61 func (obj *LocationStreamer) DeepCopyObject() runtime.Object {
62 panic("rest.LocationStreamer does not implement DeepCopyObject")
63 }
64
65 // InputStream returns a stream with the contents of the URL location. If no location is provided,
66 // a null stream is returned.
67 func (s *LocationStreamer) InputStream(ctx context.Context, apiVersion, acceptHeader string) (stream io.ReadCloser, flush bool, contentType string, err error) {
68 if s.Location == nil {
69 // If no location was provided, return a null stream
70 return nil, false, "", nil
71 }
72 transport := s.Transport
73 if transport == nil {
74 transport = http.DefaultTransport
75 }
76
77 client := &http.Client{
78 Transport: transport,
79 CheckRedirect: s.RedirectChecker,
80 }
81 req, err := http.NewRequest("GET", s.Location.String(), nil)
82 if err != nil {
83 return nil, false, "", fmt.Errorf("failed to construct request for %s, got %v", s.Location.String(), err)
84 }
85 // Pass the parent context down to the request to ensure that the resources
86 // will be release properly.
87 req = req.WithContext(ctx)
88
89 resp, err := client.Do(req)
90 if err != nil {
91 // TODO prefer segregate TLS errors more reliably, but we do want to increment a count
92 if strings.Contains(err.Error(), "x509:") && s.TLSVerificationErrorCounter != nil {
93 s.TLSVerificationErrorCounter.Inc()
94 if s.DeprecatedTLSVerificationErrorCounter != nil {
95 s.DeprecatedTLSVerificationErrorCounter.Inc()
96 }
97 }
98 return nil, false, "", err
99 }
100
101 if s.ResponseChecker != nil {
102 if err = s.ResponseChecker.Check(resp); err != nil {
103 return nil, false, "", err
104 }
105 }
106
107 contentType = s.ContentType
108 if len(contentType) == 0 {
109 contentType = resp.Header.Get("Content-Type")
110 if len(contentType) > 0 {
111 contentType = strings.TrimSpace(strings.SplitN(contentType, ";", 2)[0])
112 }
113 }
114 flush = s.Flush
115 stream = resp.Body
116 return
117 }
118
119 // PreventRedirects is a redirect checker that prevents the client from following a redirect.
120 func PreventRedirects(_ *http.Request, _ []*http.Request) error {
121 return errors.New("redirects forbidden")
122 }