groupversion.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2017 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 openapi
  14. import (
  15. "context"
  16. "net/url"
  17. "k8s.io/kube-openapi/pkg/handler3"
  18. )
  19. const ContentTypeOpenAPIV3PB = "application/com.github.proto-openapi.spec.v3@v1.0+protobuf"
  20. type GroupVersion interface {
  21. Schema(contentType string) ([]byte, error)
  22. // ServerRelativeURL. Returns the path and parameters used to fetch the schema.
  23. // You should use the Schema method to fetch it, but this value can be used
  24. // to key the current version of the schema in a cache since it contains a
  25. // hash string which changes upon schema update.
  26. ServerRelativeURL() string
  27. }
  28. type groupversion struct {
  29. client *client
  30. item handler3.OpenAPIV3DiscoveryGroupVersion
  31. useClientPrefix bool
  32. }
  33. func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion, useClientPrefix bool) *groupversion {
  34. return &groupversion{client: client, item: item, useClientPrefix: useClientPrefix}
  35. }
  36. func (g *groupversion) Schema(contentType string) ([]byte, error) {
  37. if !g.useClientPrefix {
  38. return g.client.restClient.Get().
  39. RequestURI(g.item.ServerRelativeURL).
  40. SetHeader("Accept", contentType).
  41. Do(context.TODO()).
  42. Raw()
  43. }
  44. locator, err := url.Parse(g.item.ServerRelativeURL)
  45. if err != nil {
  46. return nil, err
  47. }
  48. path := g.client.restClient.Get().
  49. AbsPath(locator.Path).
  50. SetHeader("Accept", contentType)
  51. // Other than root endpoints(openapiv3/apis), resources have hash query parameter to support etags.
  52. // However, absPath does not support handling query parameters internally,
  53. // so that hash query parameter is added manually
  54. for k, value := range locator.Query() {
  55. for _, v := range value {
  56. path.Param(k, v)
  57. }
  58. }
  59. return path.Do(context.TODO()).Raw()
  60. }
  61. // URL used for fetching the schema. The URL includes a hash and can be used
  62. // to key the current version of the schema in a cache.
  63. func (g *groupversion) ServerRelativeURL() string {
  64. return g.item.ServerRelativeURL
  65. }