forked from kubernetes/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
197 lines (160 loc) · 6.64 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scale
import (
"fmt"
autoscaling "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/dynamic"
restclient "k8s.io/client-go/rest"
)
var scaleConverter = NewScaleConverter()
var codecs = serializer.NewCodecFactory(scaleConverter.Scheme())
// restInterfaceProvider turns a restclient.Config into a restclient.Interface.
// It's overridable for the purposes of testing.
type restInterfaceProvider func(*restclient.Config) (restclient.Interface, error)
// scaleClient is an implementation of ScalesGetter
// which makes use of a RESTMapper and a generic REST
// client to support an discoverable resource.
// It behaves somewhat similarly to the dynamic ClientPool,
// but is more specifically scoped to Scale.
type scaleClient struct {
mapper meta.RESTMapper
apiPathResolverFunc dynamic.APIPathResolverFunc
scaleKindResolver ScaleKindResolver
clientBase restclient.Interface
}
// NewForConfig creates a new ScalesGetter which resolves kinds
// to resources using the given RESTMapper, and API paths using
// the given dynamic.APIPathResolverFunc.
func NewForConfig(cfg *restclient.Config, mapper meta.RESTMapper, resolver dynamic.APIPathResolverFunc, scaleKindResolver ScaleKindResolver) (ScalesGetter, error) {
// so that the RESTClientFor doesn't complain
cfg.GroupVersion = &schema.GroupVersion{}
cfg.NegotiatedSerializer = serializer.DirectCodecFactory{
CodecFactory: codecs,
}
if len(cfg.UserAgent) == 0 {
cfg.UserAgent = restclient.DefaultKubernetesUserAgent()
}
client, err := restclient.RESTClientFor(cfg)
if err != nil {
return nil, err
}
return New(client, mapper, resolver, scaleKindResolver), nil
}
// New creates a new ScalesGetter using the given client to make requests.
// The GroupVersion on the client is ignored.
func New(baseClient restclient.Interface, mapper meta.RESTMapper, resolver dynamic.APIPathResolverFunc, scaleKindResolver ScaleKindResolver) ScalesGetter {
return &scaleClient{
mapper: mapper,
apiPathResolverFunc: resolver,
scaleKindResolver: scaleKindResolver,
clientBase: baseClient,
}
}
// pathAndVersionFor returns the appropriate base path and the associated full GroupVersionResource
// for the given GroupResource
func (c *scaleClient) pathAndVersionFor(resource schema.GroupResource) (string, schema.GroupVersionResource, error) {
gvr, err := c.mapper.ResourceFor(resource.WithVersion(""))
if err != nil {
return "", gvr, fmt.Errorf("unable to get full preferred group-version-resource for %s: %v", resource.String(), err)
}
groupVer := gvr.GroupVersion()
// we need to set the API path based on GroupVersion (defaulting to the legacy path if none is set)
// TODO: we "cheat" here since the API path really only depends on group ATM, but this should
// *probably* take GroupVersionResource and not GroupVersionKind.
apiPath := c.apiPathResolverFunc(groupVer.WithKind(""))
if apiPath == "" {
apiPath = "/api"
}
path := restclient.DefaultVersionedAPIPath(apiPath, groupVer)
return path, gvr, nil
}
// namespacedScaleClient is an ScaleInterface for fetching
// Scales in a given namespace.
type namespacedScaleClient struct {
client *scaleClient
namespace string
}
func (c *scaleClient) Scales(namespace string) ScaleInterface {
return &namespacedScaleClient{
client: c,
namespace: namespace,
}
}
func (c *namespacedScaleClient) Get(resource schema.GroupResource, name string) (*autoscaling.Scale, error) {
// Currently, a /scale endpoint can return different scale types.
// Until we have support for the alternative API representations proposal,
// we need to deal with accepting different API versions.
// In practice, this is autoscaling/v1.Scale and extensions/v1beta1.Scale
path, gvr, err := c.client.pathAndVersionFor(resource)
if err != nil {
return nil, fmt.Errorf("unable to get client for %s: %v", resource.String(), err)
}
rawObj, err := c.client.clientBase.Get().
AbsPath(path).
Namespace(c.namespace).
Resource(gvr.Resource).
Name(name).
SubResource("scale").
Do().
Get()
if err != nil {
return nil, err
}
// convert whatever this is to autoscaling/v1.Scale
scaleObj, err := scaleConverter.ConvertToVersion(rawObj, autoscaling.SchemeGroupVersion)
if err != nil {
return nil, fmt.Errorf("received an object from a /scale endpoint which was not convertible to autoscaling Scale: %v", err)
}
return scaleObj.(*autoscaling.Scale), nil
}
func (c *namespacedScaleClient) Update(resource schema.GroupResource, scale *autoscaling.Scale) (*autoscaling.Scale, error) {
path, gvr, err := c.client.pathAndVersionFor(resource)
if err != nil {
return nil, fmt.Errorf("unable to get client for %s: %v", resource.String(), err)
}
// Currently, a /scale endpoint can receive and return different scale types.
// Until we hvae support for the alternative API representations proposal,
// we need to deal with sending and accepting differnet API versions.
// figure out what scale we actually need here
desiredGVK, err := c.client.scaleKindResolver.ScaleForResource(gvr)
if err != nil {
return nil, fmt.Errorf("could not find proper group-version for scale subresource of %s: %v", gvr.String(), err)
}
// convert this to whatever this endpoint wants
scaleUpdate, err := scaleConverter.ConvertToVersion(scale, desiredGVK.GroupVersion())
if err != nil {
return nil, fmt.Errorf("could not convert scale update to internal Scale: %v", err)
}
rawObj, err := c.client.clientBase.Put().
AbsPath(path).
Namespace(c.namespace).
Resource(gvr.Resource).
Name(scale.Name).
SubResource("scale").
Body(scaleUpdate).
Do().
Get()
if err != nil {
return nil, fmt.Errorf("could not fetch the scale for %s %s: %v", resource.String(), scale.Name, err)
}
// convert whatever this is back to autoscaling/v1.Scale
scaleObj, err := scaleConverter.ConvertToVersion(rawObj, autoscaling.SchemeGroupVersion)
if err != nil {
return nil, fmt.Errorf("received an object from a /scale endpoint which was not convertible to autoscaling Scale: %v", err)
}
return scaleObj.(*autoscaling.Scale), err
}