Files
client-go/openapi/client.go
Patrick Ohly 8101e94f49 restmapper + discovery: add context-aware APIs
The main purpose is to replace context.TODO with a context provided by the
caller. A secondary purpose is to enable contextual logging.

Modifying the existing interfaces and APIs would have a big impact on the
ecosystem. This is a no-go. Instead, the following approach was taken:

- All interfaces get duplicated in a *WithContext variant where the methods
  also have a *WithContext suffix and the ctx parameter. All methods are
  treated this way except for obvious local get methods (like RESTClient)
  because it cannot be ruled out entirely that some implementation may
  need a context.

- Implementations of these interfaces implement both method variants
  which is possible because the method names are different.
  The old methods are implemented as thin wrappers around the updated
  code which is now the body of the new methods or shared helpers.
  In some cases there is additional overhead (type checks, potentially
  additional allocations) when using the old methods.

- To*WithContext helpers bridge from the old to the new interfaces. They
  try a type cast first. Because the in-tree implementations implement
  both, they can be used directly. For other implementations wrappers
  are used.

- All old APIs and interfaces are marked as deprecated. There is no
  intent to ever remove them, but consumers should be made aware
  that there are now better alternatives. Implementations also
  get marked this way even if nothing ever calls them directly
  because it shows which code, at least theoretically, could get
  removed.

- Existing unit tests do not get updated to the new APIs. This gives
  us unit test coverage of the old and new API because the old
  APIs call the new ones.

- In-tree consumers will be updated in follow-up PRs. This is likely
  to be a longer process. Because of the deprecation comment,
  `hack/golangci-lint.sh -n` can be used to find code which needs
  to be updated.

Kubernetes-commit: 025b844bcabe0212c4dd56395ee18481602d7c65
2024-12-06 17:38:06 +01:00

124 lines
3.6 KiB
Go

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openapi
import (
"context"
"encoding/json"
"strings"
"k8s.io/client-go/rest"
"k8s.io/kube-openapi/pkg/handler3"
)
// Deprecated: use ClientWithContext instead.
type Client interface {
Paths() (map[string]GroupVersion, error)
}
type ClientWithContext interface {
PathsWithContext(ctx context.Context) (map[string]GroupVersionWithContext, error)
}
func ToClientWithContext(c Client) ClientWithContext {
if c == nil {
return nil
}
if c, ok := c.(ClientWithContext); ok {
return c
}
return &clientWrapper{
delegate: c,
}
}
type clientWrapper struct {
delegate Client
}
func (c *clientWrapper) PathsWithContext(ctx context.Context) (map[string]GroupVersionWithContext, error) {
resultWithoutContext, err := c.delegate.Paths()
result := make(map[string]GroupVersionWithContext, len(resultWithoutContext))
for key, entry := range resultWithoutContext {
result[key] = ToGroupVersionWithContext(entry)
}
return result, err
}
type client struct {
// URL includes the `hash` query param to take advantage of cache busting
restClient rest.Interface
}
// Deprecated: use NewClientWithContext instead.
func NewClient(restClient rest.Interface) Client {
return newClient(restClient)
}
func NewClientWithContext(restClient rest.Interface) ClientWithContext {
return newClient(restClient)
}
func newClient(restClient rest.Interface) *client {
return &client{
restClient: restClient,
}
}
// Deprecated: use PathsWithContext instead.
func (c *client) Paths() (map[string]GroupVersion, error) {
resultWithContext, err := c.PathsWithContext(context.Background())
result := make(map[string]GroupVersion, len(resultWithContext))
for key, entry := range resultWithContext {
// We know that this is a *groupversion which implements GroupVersion.
result[key] = entry.(GroupVersion)
}
return result, err
}
func (c *client) PathsWithContext(ctx context.Context) (map[string]GroupVersionWithContext, error) {
data, err := c.restClient.Get().
AbsPath("/openapi/v3").
Do(ctx).
Raw()
if err != nil {
return nil, err
}
discoMap := &handler3.OpenAPIV3Discovery{}
err = json.Unmarshal(data, discoMap)
if err != nil {
return nil, err
}
// Calculate the client-side prefix for a "root" request
rootPrefix := strings.TrimSuffix(c.restClient.Get().AbsPath("/").URL().Path, "/")
// Create GroupVersions for each element of the result
result := make(map[string]GroupVersionWithContext, len(discoMap.Paths))
for k, v := range discoMap.Paths {
// Trim off the prefix that will always be added in client-side
v.ServerRelativeURL = strings.TrimPrefix(v.ServerRelativeURL, rootPrefix)
// If the server returned a URL rooted at /openapi/v3, preserve any additional client-side prefix.
// If the server returned a URL not rooted at /openapi/v3, treat it as an actual server-relative URL.
// See https://github.com/kubernetes/kubernetes/issues/117463 for details
useClientPrefix := strings.HasPrefix(v.ServerRelativeURL, "/openapi/v3")
result[k] = newGroupVersion(c, v, useClientPrefix)
}
return result, nil
}