| 1 |
/* |
| 2 |
Copyright 2018 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 external_metrics |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
|
| 23 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 |
"k8s.io/apimachinery/pkg/labels" |
| 25 |
"k8s.io/client-go/kubernetes/scheme" |
| 26 |
"k8s.io/client-go/rest" |
| 27 |
"k8s.io/client-go/util/flowcontrol" |
| 28 |
"k8s.io/metrics/pkg/apis/external_metrics/v1beta1" |
| 29 |
) |
| 30 |
|
| 31 |
type externalMetricsClient struct { |
| 32 |
client rest.Interface |
| 33 |
} |
| 34 |
|
| 35 |
func New(client rest.Interface) ExternalMetricsClient { |
| 36 |
return &externalMetricsClient{ |
| 37 |
client: client, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func NewForConfig(c *rest.Config) (ExternalMetricsClient, error) { |
| 42 |
configShallowCopy := *c |
| 43 |
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 { |
| 44 |
if configShallowCopy.Burst <= 0 { |
| 45 |
return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0") |
| 46 |
} |
| 47 |
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst) |
| 48 |
} |
| 49 |
configShallowCopy.APIPath = "/apis" |
| 50 |
if configShallowCopy.UserAgent == "" { |
| 51 |
configShallowCopy.UserAgent = rest.DefaultKubernetesUserAgent() |
| 52 |
} |
| 53 |
configShallowCopy.GroupVersion = &v1beta1.SchemeGroupVersion |
| 54 |
configShallowCopy.NegotiatedSerializer = scheme.Codecs.WithoutConversion() |
| 55 |
|
| 56 |
client, err := rest.RESTClientFor(&configShallowCopy) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
|
| 61 |
return New(client), nil |
| 62 |
} |
| 63 |
|
| 64 |
func NewForConfigOrDie(c *rest.Config) ExternalMetricsClient { |
| 65 |
client, err := NewForConfig(c) |
| 66 |
if err != nil { |
| 67 |
panic(err) |
| 68 |
} |
| 69 |
return client |
| 70 |
} |
| 71 |
|
| 72 |
func (c *externalMetricsClient) NamespacedMetrics(namespace string) MetricsInterface { |
| 73 |
return &namespacedMetrics{ |
| 74 |
client: c, |
| 75 |
namespace: namespace, |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
type namespacedMetrics struct { |
| 80 |
client *externalMetricsClient |
| 81 |
namespace string |
| 82 |
} |
| 83 |
|
| 84 |
func (m *namespacedMetrics) List(metricName string, metricSelector labels.Selector) (*v1beta1.ExternalMetricValueList, error) { |
| 85 |
res := &v1beta1.ExternalMetricValueList{} |
| 86 |
err := m.client.client.Get(). |
| 87 |
Namespace(m.namespace). |
| 88 |
Resource(metricName). |
| 89 |
VersionedParams(&metav1.ListOptions{ |
| 90 |
LabelSelector: metricSelector.String(), |
| 91 |
}, metav1.ParameterCodec). |
| 92 |
Do(context.TODO()). |
| 93 |
Into(res) |
| 94 |
|
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
|
| 99 |
return res, nil |
| 100 |
} |