client.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package rest
  14. import (
  15. "fmt"
  16. "mime"
  17. "net/http"
  18. "net/url"
  19. "os"
  20. "strconv"
  21. "strings"
  22. "sync/atomic"
  23. "time"
  24. "github.com/munnerz/goautoneg"
  25. "k8s.io/apimachinery/pkg/runtime"
  26. "k8s.io/apimachinery/pkg/runtime/schema"
  27. "k8s.io/apimachinery/pkg/types"
  28. clientfeatures "k8s.io/client-go/features"
  29. "k8s.io/client-go/util/flowcontrol"
  30. )
  31. const (
  32. // Environment variables: Note that the duration should be long enough that the backoff
  33. // persists for some reasonable time (i.e. 120 seconds). The typical base might be "1".
  34. envBackoffBase = "KUBE_CLIENT_BACKOFF_BASE"
  35. envBackoffDuration = "KUBE_CLIENT_BACKOFF_DURATION"
  36. )
  37. // Interface captures the set of operations for generically interacting with Kubernetes REST apis.
  38. type Interface interface {
  39. GetRateLimiter() flowcontrol.RateLimiter
  40. Verb(verb string) *Request
  41. Post() *Request
  42. Put() *Request
  43. Patch(pt types.PatchType) *Request
  44. Get() *Request
  45. Delete() *Request
  46. APIVersion() schema.GroupVersion
  47. }
  48. // ClientContentConfig controls how RESTClient communicates with the server.
  49. //
  50. // TODO: ContentConfig will be updated to accept a Negotiator instead of a
  51. // NegotiatedSerializer and NegotiatedSerializer will be removed.
  52. type ClientContentConfig struct {
  53. // AcceptContentTypes specifies the types the client will accept and is optional.
  54. // If not set, ContentType will be used to define the Accept header
  55. AcceptContentTypes string
  56. // ContentType specifies the wire format used to communicate with the server.
  57. // This value will be set as the Accept header on requests made to the server if
  58. // AcceptContentTypes is not set, and as the default content type on any object
  59. // sent to the server. If not set, "application/json" is used.
  60. ContentType string
  61. // GroupVersion is the API version to talk to. Must be provided when initializing
  62. // a RESTClient directly. When initializing a Client, will be set with the default
  63. // code version. This is used as the default group version for VersionedParams.
  64. GroupVersion schema.GroupVersion
  65. // Negotiator is used for obtaining encoders and decoders for multiple
  66. // supported media types.
  67. Negotiator runtime.ClientNegotiator
  68. }
  69. // RESTClient imposes common Kubernetes API conventions on a set of resource paths.
  70. // The baseURL is expected to point to an HTTP or HTTPS path that is the parent
  71. // of one or more resources. The server should return a decodable API resource
  72. // object, or an api.Status object which contains information about the reason for
  73. // any failure.
  74. //
  75. // Most consumers should use client.New() to get a Kubernetes API client.
  76. type RESTClient struct {
  77. // base is the root URL for all invocations of the client
  78. base *url.URL
  79. // versionedAPIPath is a path segment connecting the base URL to the resource root
  80. versionedAPIPath string
  81. // content describes how a RESTClient encodes and decodes responses.
  82. content requestClientContentConfigProvider
  83. // creates BackoffManager that is passed to requests.
  84. createBackoffMgr func() BackoffManagerWithContext
  85. // rateLimiter is shared among all requests created by this client unless specifically
  86. // overridden.
  87. rateLimiter flowcontrol.RateLimiter
  88. // warningHandler is shared among all requests created by this client.
  89. // If not set, defaultWarningHandler is used.
  90. warningHandler WarningHandlerWithContext
  91. // Set specific behavior of the client. If not set http.DefaultClient will be used.
  92. Client *http.Client
  93. }
  94. // NewRESTClient creates a new RESTClient. This client performs generic REST functions
  95. // such as Get, Put, Post, and Delete on specified paths.
  96. func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ClientContentConfig, rateLimiter flowcontrol.RateLimiter, client *http.Client) (*RESTClient, error) {
  97. base := *baseURL
  98. if !strings.HasSuffix(base.Path, "/") {
  99. base.Path += "/"
  100. }
  101. base.RawQuery = ""
  102. base.Fragment = ""
  103. return &RESTClient{
  104. base: &base,
  105. versionedAPIPath: versionedAPIPath,
  106. content: requestClientContentConfigProvider{base: scrubCBORContentConfigIfDisabled(config)},
  107. createBackoffMgr: readExpBackoffConfig,
  108. rateLimiter: rateLimiter,
  109. Client: client,
  110. }, nil
  111. }
  112. func scrubCBORContentConfigIfDisabled(content ClientContentConfig) ClientContentConfig {
  113. if clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) {
  114. content.Negotiator = clientNegotiatorWithCBORSequenceStreamDecoder{content.Negotiator}
  115. return content
  116. }
  117. if mediatype, _, err := mime.ParseMediaType(content.ContentType); err == nil && mediatype == "application/cbor" {
  118. content.ContentType = "application/json"
  119. }
  120. clauses := goautoneg.ParseAccept(content.AcceptContentTypes)
  121. scrubbed := false
  122. for i, clause := range clauses {
  123. if clause.Type == "application" && clause.SubType == "cbor" {
  124. scrubbed = true
  125. clauses[i].SubType = "json"
  126. }
  127. }
  128. if !scrubbed {
  129. // No application/cbor in AcceptContentTypes, nothing more to do.
  130. return content
  131. }
  132. parts := make([]string, 0, len(clauses))
  133. for _, clause := range clauses {
  134. // ParseAccept does not store the parameter "q" in Params.
  135. params := clause.Params
  136. if clause.Q < 1 { // omit q=1, it's the default
  137. if params == nil {
  138. params = make(map[string]string, 1)
  139. }
  140. params["q"] = strconv.FormatFloat(clause.Q, 'g', 3, 32)
  141. }
  142. parts = append(parts, mime.FormatMediaType(fmt.Sprintf("%s/%s", clause.Type, clause.SubType), params))
  143. }
  144. content.AcceptContentTypes = strings.Join(parts, ",")
  145. return content
  146. }
  147. // GetRateLimiter returns rate limiter for a given client, or nil if it's called on a nil client
  148. func (c *RESTClient) GetRateLimiter() flowcontrol.RateLimiter {
  149. if c == nil {
  150. return nil
  151. }
  152. return c.rateLimiter
  153. }
  154. // readExpBackoffConfig handles the internal logic of determining what the
  155. // backoff policy is. By default if no information is available, NoBackoff.
  156. // TODO Generalize this see #17727 .
  157. func readExpBackoffConfig() BackoffManagerWithContext {
  158. backoffBase := os.Getenv(envBackoffBase)
  159. backoffDuration := os.Getenv(envBackoffDuration)
  160. backoffBaseInt, errBase := strconv.ParseInt(backoffBase, 10, 64)
  161. backoffDurationInt, errDuration := strconv.ParseInt(backoffDuration, 10, 64)
  162. if errBase != nil || errDuration != nil {
  163. return &NoBackoff{}
  164. }
  165. return &URLBackoff{
  166. Backoff: flowcontrol.NewBackOff(
  167. time.Duration(backoffBaseInt)*time.Second,
  168. time.Duration(backoffDurationInt)*time.Second)}
  169. }
  170. // Verb begins a request with a verb (GET, POST, PUT, DELETE).
  171. //
  172. // Example usage of RESTClient's request building interface:
  173. // c, err := NewRESTClient(...)
  174. // if err != nil { ... }
  175. // resp, err := c.Verb("GET").
  176. //
  177. // Path("pods").
  178. // SelectorParam("labels", "area=staging").
  179. // Timeout(10*time.Second).
  180. // Do()
  181. //
  182. // if err != nil { ... }
  183. // list, ok := resp.(*api.PodList)
  184. func (c *RESTClient) Verb(verb string) *Request {
  185. return NewRequest(c).Verb(verb)
  186. }
  187. // Post begins a POST request. Short for c.Verb("POST").
  188. func (c *RESTClient) Post() *Request {
  189. return c.Verb("POST")
  190. }
  191. // Put begins a PUT request. Short for c.Verb("PUT").
  192. func (c *RESTClient) Put() *Request {
  193. return c.Verb("PUT")
  194. }
  195. // Patch begins a PATCH request. Short for c.Verb("Patch").
  196. func (c *RESTClient) Patch(pt types.PatchType) *Request {
  197. return c.Verb("PATCH").SetHeader("Content-Type", string(pt))
  198. }
  199. // Get begins a GET request. Short for c.Verb("GET").
  200. func (c *RESTClient) Get() *Request {
  201. return c.Verb("GET")
  202. }
  203. // Delete begins a DELETE request. Short for c.Verb("DELETE").
  204. func (c *RESTClient) Delete() *Request {
  205. return c.Verb("DELETE")
  206. }
  207. // APIVersion returns the APIVersion this RESTClient is expected to use.
  208. func (c *RESTClient) APIVersion() schema.GroupVersion {
  209. config, _ := c.content.GetClientContentConfig()
  210. return config.GroupVersion
  211. }
  212. // requestClientContentConfigProvider observes HTTP 415 (Unsupported Media Type) responses to detect
  213. // that the server does not understand CBOR. Once this has happened, future requests are forced to
  214. // use JSON so they can succeed. This is convenient for client users that want to prefer CBOR, but
  215. // also need to interoperate with older servers so requests do not permanently fail. The clients
  216. // will not default to using CBOR until at least all supported kube-apiservers have enable-CBOR
  217. // locked to true, so this path will be rarely taken. Additionally, all generated clients accessing
  218. // built-in kube resources are forced to protobuf, so those will not degrade to JSON.
  219. type requestClientContentConfigProvider struct {
  220. base ClientContentConfig
  221. // Becomes permanently true if a server responds with HTTP 415 (Unsupported Media Type) to a
  222. // request with "Content-Type" header containing the CBOR media type.
  223. sawUnsupportedMediaTypeForCBOR atomic.Bool
  224. }
  225. // GetClientContentConfig returns the ClientContentConfig that should be used for new requests by
  226. // this client and true if the request ContentType was selected by default.
  227. func (p *requestClientContentConfigProvider) GetClientContentConfig() (ClientContentConfig, bool) {
  228. config := p.base
  229. defaulted := config.ContentType == ""
  230. if defaulted {
  231. config.ContentType = "application/json"
  232. }
  233. if !clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) {
  234. return config, defaulted
  235. }
  236. if defaulted && clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsPreferCBOR) {
  237. config.ContentType = "application/cbor"
  238. }
  239. if sawUnsupportedMediaTypeForCBOR := p.sawUnsupportedMediaTypeForCBOR.Load(); !sawUnsupportedMediaTypeForCBOR {
  240. return config, defaulted
  241. }
  242. if mediaType, _, _ := mime.ParseMediaType(config.ContentType); mediaType != runtime.ContentTypeCBOR {
  243. return config, defaulted
  244. }
  245. // The effective ContentType is CBOR and the client has previously received an HTTP 415 in
  246. // response to a CBOR request. Override ContentType to JSON.
  247. config.ContentType = runtime.ContentTypeJSON
  248. return config, defaulted
  249. }
  250. // UnsupportedMediaType reports that the server has responded to a request with HTTP 415 Unsupported
  251. // Media Type.
  252. func (p *requestClientContentConfigProvider) UnsupportedMediaType(requestContentType string) {
  253. if !clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) {
  254. return
  255. }
  256. // This could be extended to consider the Content-Encoding request header, the Accept and
  257. // Accept-Encoding response headers, the request method, and URI (as mentioned in
  258. // https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.16). The request Content-Type
  259. // header is sufficient to implement a blanket CBOR fallback mechanism.
  260. requestContentType, _, _ = mime.ParseMediaType(requestContentType)
  261. switch requestContentType {
  262. case runtime.ContentTypeCBOR, string(types.ApplyCBORPatchType):
  263. p.sawUnsupportedMediaTypeForCBOR.Store(true)
  264. }
  265. }
  266. // clientNegotiatorWithCBORSequenceStreamDecoder is a ClientNegotiator that delegates to another
  267. // ClientNegotiator to select the appropriate Encoder or Decoder for a given media type. As a
  268. // special case, it will resolve "application/cbor-seq" (a CBOR Sequence, the concatenation of zero
  269. // or more CBOR data items) as an alias for "application/cbor" (exactly one CBOR data item) when
  270. // selecting a stream decoder.
  271. type clientNegotiatorWithCBORSequenceStreamDecoder struct {
  272. negotiator runtime.ClientNegotiator
  273. }
  274. func (n clientNegotiatorWithCBORSequenceStreamDecoder) Encoder(contentType string, params map[string]string) (runtime.Encoder, error) {
  275. return n.negotiator.Encoder(contentType, params)
  276. }
  277. func (n clientNegotiatorWithCBORSequenceStreamDecoder) Decoder(contentType string, params map[string]string) (runtime.Decoder, error) {
  278. return n.negotiator.Decoder(contentType, params)
  279. }
  280. func (n clientNegotiatorWithCBORSequenceStreamDecoder) StreamDecoder(contentType string, params map[string]string) (runtime.Decoder, runtime.Serializer, runtime.Framer, error) {
  281. if !clientfeatures.FeatureGates().Enabled(clientfeatures.ClientsAllowCBOR) {
  282. return n.negotiator.StreamDecoder(contentType, params)
  283. }
  284. switch contentType {
  285. case runtime.ContentTypeCBORSequence:
  286. return n.negotiator.StreamDecoder(runtime.ContentTypeCBOR, params)
  287. case runtime.ContentTypeCBOR:
  288. // This media type is only appropriate for exactly one data item, not the zero or
  289. // more events of a watch stream.
  290. return nil, nil, nil, runtime.NegotiateError{ContentType: contentType, Stream: true}
  291. default:
  292. return n.negotiator.StreamDecoder(contentType, params)
  293. }
  294. }