groupversion.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  23. type groupversion struct {
  24. client *client
  25. item handler3.OpenAPIV3DiscoveryGroupVersion
  26. useClientPrefix bool
  27. }
  28. func newGroupVersion(client *client, item handler3.OpenAPIV3DiscoveryGroupVersion, useClientPrefix bool) *groupversion {
  29. return &groupversion{client: client, item: item, useClientPrefix: useClientPrefix}
  30. }
  31. func (g *groupversion) Schema(contentType string) ([]byte, error) {
  32. if !g.useClientPrefix {
  33. return g.client.restClient.Get().
  34. RequestURI(g.item.ServerRelativeURL).
  35. SetHeader("Accept", contentType).
  36. Do(context.TODO()).
  37. Raw()
  38. }
  39. locator, err := url.Parse(g.item.ServerRelativeURL)
  40. if err != nil {
  41. return nil, err
  42. }
  43. path := g.client.restClient.Get().
  44. AbsPath(locator.Path).
  45. SetHeader("Accept", contentType)
  46. // Other than root endpoints(openapiv3/apis), resources have hash query parameter to support etags.
  47. // However, absPath does not support handling query parameters internally,
  48. // so that hash query parameter is added manually
  49. for k, value := range locator.Query() {
  50. for _, v := range value {
  51. path.Param(k, v)
  52. }
  53. }
  54. return path.Do(context.TODO()).Raw()
  55. }