versions.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 restclient
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "net/http"
  18. "path"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. )
  21. const (
  22. legacyAPIPath = "/api"
  23. defaultAPIPath = "/apis"
  24. )
  25. // TODO: Is this obsoleted by the discovery client?
  26. // ServerAPIVersions returns the GroupVersions supported by the API server.
  27. // It creates a RESTClient based on the passed in config, but it doesn't rely
  28. // on the Version and Codec of the config, because it uses AbsPath and
  29. // takes the raw response.
  30. func ServerAPIVersions(c *Config) (groupVersions []string, err error) {
  31. transport, err := TransportFor(c)
  32. if err != nil {
  33. return nil, err
  34. }
  35. client := http.Client{Transport: transport}
  36. configCopy := *c
  37. configCopy.GroupVersion = nil
  38. configCopy.APIPath = ""
  39. baseURL, _, err := defaultServerUrlFor(&configCopy)
  40. if err != nil {
  41. return nil, err
  42. }
  43. // Get the groupVersions exposed at /api
  44. originalPath := baseURL.Path
  45. baseURL.Path = path.Join(originalPath, legacyAPIPath)
  46. resp, err := client.Get(baseURL.String())
  47. if err != nil {
  48. return nil, err
  49. }
  50. var v unversioned.APIVersions
  51. defer resp.Body.Close()
  52. err = json.NewDecoder(resp.Body).Decode(&v)
  53. if err != nil {
  54. return nil, fmt.Errorf("unexpected error: %v", err)
  55. }
  56. groupVersions = append(groupVersions, v.Versions...)
  57. // Get the groupVersions exposed at /apis
  58. baseURL.Path = path.Join(originalPath, defaultAPIPath)
  59. resp2, err := client.Get(baseURL.String())
  60. if err != nil {
  61. return nil, err
  62. }
  63. var apiGroupList unversioned.APIGroupList
  64. defer resp2.Body.Close()
  65. err = json.NewDecoder(resp2.Body).Decode(&apiGroupList)
  66. if err != nil {
  67. return nil, fmt.Errorf("unexpected error: %v", err)
  68. }
  69. for _, g := range apiGroupList.Groups {
  70. for _, gv := range g.Versions {
  71. groupVersions = append(groupVersions, gv.GroupVersion)
  72. }
  73. }
  74. return groupVersions, nil
  75. }