Debian Codegraph @

pkg/client/custom_metrics/multi_client.go

raw
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 custom_metrics
18
19 import (
20 "sync"
21 "time"
22
23 "k8s.io/apimachinery/pkg/api/meta"
24 "k8s.io/apimachinery/pkg/labels"
25 "k8s.io/apimachinery/pkg/runtime/schema"
26 "k8s.io/client-go/rest"
27
28 "k8s.io/metrics/pkg/apis/custom_metrics/v1beta2"
29 )
30
31 // AvailableAPIsGetter knows how to fetch and cache the preferred custom metrics API version,
32 // and invalidate that cache when asked.
33 type AvailableAPIsGetter interface {
34 PreferredVersion() (schema.GroupVersion, error)
35 Invalidate()
36 }
37
38 // PeriodicallyInvalidate periodically invalidates the preferred version cache until
39 // told to stop.
40 func PeriodicallyInvalidate(cache AvailableAPIsGetter, interval time.Duration, stopCh <-chan struct{}) {
41 ticker := time.NewTicker(interval)
42 defer ticker.Stop()
43
44 for {
45 select {
46 case <-ticker.C:
47 cache.Invalidate()
48 case <-stopCh:
49 return
50 }
51 }
52 }
53
54 // NewForConfig creates a new custom metrics client which delegates to a client which
55 // uses the preferred api version.
56 func NewForConfig(baseConfig *rest.Config, mapper meta.RESTMapper, availableAPIs AvailableAPIsGetter) CustomMetricsClient {
57 return &multiClient{
58 clients: make(map[schema.GroupVersion]CustomMetricsClient),
59 availableAPIs: availableAPIs,
60
61 newClient: func(ver schema.GroupVersion) (CustomMetricsClient, error) {
62 return NewForVersionForConfig(rest.CopyConfig(baseConfig), mapper, ver)
63 },
64 }
65 }
66
67 // multiClient is a CustomMetricsClient that can work with *any* metrics API version.
68 type multiClient struct {
69 newClient func(schema.GroupVersion) (CustomMetricsClient, error)
70 clients map[schema.GroupVersion]CustomMetricsClient
71 availableAPIs AvailableAPIsGetter
72 mu sync.RWMutex
73 }
74
75 // getPreferredClient returns a custom metrics client of the preferred api version.
76 func (c *multiClient) getPreferredClient() (CustomMetricsClient, error) {
77 pref, err := c.availableAPIs.PreferredVersion()
78 if err != nil {
79 return nil, err
80 }
81
82 c.mu.RLock()
83 client, present := c.clients[pref]
84 c.mu.RUnlock()
85 if present {
86 return client, nil
87 }
88
89 c.mu.Lock()
90 defer c.mu.Unlock()
91 client, err = c.newClient(pref)
92 if err != nil {
93 return nil, err
94 }
95 c.clients[pref] = client
96
97 return client, nil
98 }
99
100 func (c *multiClient) RootScopedMetrics() MetricsInterface {
101 return &multiClientInterface{clients: c}
102 }
103
104 func (c *multiClient) NamespacedMetrics(namespace string) MetricsInterface {
105 return &multiClientInterface{
106 clients: c,
107 namespace: &namespace,
108 }
109 }
110
111 type multiClientInterface struct {
112 clients *multiClient
113 namespace *string
114 }
115
116 func (m *multiClientInterface) GetForObject(groupKind schema.GroupKind, name string, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValue, error) {
117 client, err := m.clients.getPreferredClient()
118 if err != nil {
119 return nil, err
120 }
121 if m.namespace == nil {
122 return client.RootScopedMetrics().GetForObject(groupKind, name, metricName, metricSelector)
123 } else {
124 return client.NamespacedMetrics(*m.namespace).GetForObject(groupKind, name, metricName, metricSelector)
125 }
126 }
127
128 func (m *multiClientInterface) GetForObjects(groupKind schema.GroupKind, selector labels.Selector, metricName string, metricSelector labels.Selector) (*v1beta2.MetricValueList, error) {
129 client, err := m.clients.getPreferredClient()
130 if err != nil {
131 return nil, err
132 }
133 if m.namespace == nil {
134 return client.RootScopedMetrics().GetForObjects(groupKind, selector, metricName, metricSelector)
135 } else {
136 return client.NamespacedMetrics(*m.namespace).GetForObjects(groupKind, selector, metricName, metricSelector)
137 }
138 }