client.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "encoding/json"
  17. "k8s.io/client-go/rest"
  18. "k8s.io/kube-openapi/pkg/handler3"
  19. )
  20. type Client interface {
  21. Paths() (map[string]GroupVersion, error)
  22. }
  23. type client struct {
  24. // URL includes the `hash` query param to take advantage of cache busting
  25. restClient rest.Interface
  26. }
  27. func NewClient(restClient rest.Interface) Client {
  28. return &client{
  29. restClient: restClient,
  30. }
  31. }
  32. func (c *client) Paths() (map[string]GroupVersion, error) {
  33. data, err := c.restClient.Get().
  34. AbsPath("/openapi/v3").
  35. Do(context.TODO()).
  36. Raw()
  37. if err != nil {
  38. return nil, err
  39. }
  40. discoMap := &handler3.OpenAPIV3Discovery{}
  41. err = json.Unmarshal(data, discoMap)
  42. if err != nil {
  43. return nil, err
  44. }
  45. // Create GroupVersions for each element of the result
  46. result := map[string]GroupVersion{}
  47. for k, v := range discoMap.Paths {
  48. result[k] = newGroupVersion(c, v)
  49. }
  50. return result, nil
  51. }