k8s.io/client-go@v0.31.1/openapi/client.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package openapi 18 19 import ( 20 "context" 21 "encoding/json" 22 "strings" 23 24 "k8s.io/client-go/rest" 25 "k8s.io/kube-openapi/pkg/handler3" 26 ) 27 28 type Client interface { 29 Paths() (map[string]GroupVersion, error) 30 } 31 32 type client struct { 33 // URL includes the `hash` query param to take advantage of cache busting 34 restClient rest.Interface 35 } 36 37 func NewClient(restClient rest.Interface) Client { 38 return &client{ 39 restClient: restClient, 40 } 41 } 42 43 func (c *client) Paths() (map[string]GroupVersion, error) { 44 data, err := c.restClient.Get(). 45 AbsPath("/openapi/v3"). 46 Do(context.TODO()). 47 Raw() 48 49 if err != nil { 50 return nil, err 51 } 52 53 discoMap := &handler3.OpenAPIV3Discovery{} 54 err = json.Unmarshal(data, discoMap) 55 if err != nil { 56 return nil, err 57 } 58 59 // Create GroupVersions for each element of the result 60 result := map[string]GroupVersion{} 61 for k, v := range discoMap.Paths { 62 // If the server returned a URL rooted at /openapi/v3, preserve any additional client-side prefix. 63 // If the server returned a URL not rooted at /openapi/v3, treat it as an actual server-relative URL. 64 // See https://github.com/kubernetes/kubernetes/issues/117463 for details 65 useClientPrefix := strings.HasPrefix(v.ServerRelativeURL, "/openapi/v3") 66 result[k] = newGroupVersion(c, v, useClientPrefix) 67 } 68 return result, nil 69 }