| 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 |
"fmt" |
| 21 |
"sync" |
| 22 |
|
| 23 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 25 |
"k8s.io/client-go/discovery" |
| 26 |
|
| 27 |
cmint "k8s.io/metrics/pkg/apis/custom_metrics" |
| 28 |
) |
| 29 |
|
| 30 |
var ( |
| 31 |
// metricVersionsToGV is the map of string group-versions |
| 32 |
// accepted by the converter to group-version objects (so |
| 33 |
// we don't have to re-parse) |
| 34 |
metricVersionsToGV map[string]schema.GroupVersion |
| 35 |
) |
| 36 |
|
| 37 |
func init() { |
| 38 |
metricVersionsToGV = make(map[string]schema.GroupVersion) |
| 39 |
for _, ver := range MetricVersions { |
| 40 |
metricVersionsToGV[ver.String()] = ver |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// NewAvailableAPIsGetter creates an AvailableAPIsGetter that checks discovery |
| 45 |
// to find the available versions of the custom metrics api. |
| 46 |
func NewAvailableAPIsGetter(client discovery.DiscoveryInterface) AvailableAPIsGetter { |
| 47 |
return &apiVersionsFromDiscovery{ |
| 48 |
client: client, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// apiVersionsFromDiscovery caches a preferred version of the custom metrics api. |
| 53 |
type apiVersionsFromDiscovery struct { |
| 54 |
client discovery.DiscoveryInterface |
| 55 |
|
| 56 |
// just cache the group directly since the discovery interface doesn't yet allow |
| 57 |
// asking for a single API group's versions. |
| 58 |
prefVersion *schema.GroupVersion |
| 59 |
mu sync.RWMutex |
| 60 |
} |
| 61 |
|
| 62 |
// fetchVersions fetches the versions, but doesn't try to invalidate on cache misses. |
| 63 |
func (d *apiVersionsFromDiscovery) fetchVersions() (*metav1.APIGroup, error) { |
| 64 |
// TODO(directxman12): amend the discovery interface to ask for a particular group (/apis/foo) |
| 65 |
groups, err := d.client.ServerGroups() |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
|
| 70 |
// Determine the preferred version on the server by first finding the custom metrics group |
| 71 |
var apiGroup *metav1.APIGroup |
| 72 |
for _, group := range groups.Groups { |
| 73 |
if group.Name == cmint.GroupName { |
| 74 |
apiGroup = &group |
| 75 |
break |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if apiGroup == nil { |
| 80 |
return nil, fmt.Errorf("no custom metrics API (%s) registered", cmint.GroupName) |
| 81 |
} |
| 82 |
|
| 83 |
return apiGroup, nil |
| 84 |
} |
| 85 |
|
| 86 |
// chooseVersion sets a preferred version of the custom metrics api based on available versions. |
| 87 |
func (d *apiVersionsFromDiscovery) chooseVersion(apiGroup *metav1.APIGroup) (schema.GroupVersion, error) { |
| 88 |
var preferredVersion *schema.GroupVersion |
| 89 |
if gv, present := metricVersionsToGV[apiGroup.PreferredVersion.GroupVersion]; present && len(apiGroup.PreferredVersion.GroupVersion) != 0 { |
| 90 |
preferredVersion = &gv |
| 91 |
} else { |
| 92 |
for _, version := range apiGroup.Versions { |
| 93 |
if gv, present := metricVersionsToGV[version.GroupVersion]; present { |
| 94 |
preferredVersion = &gv |
| 95 |
break |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if preferredVersion == nil { |
| 101 |
return schema.GroupVersion{}, fmt.Errorf("no known available metric versions found") |
| 102 |
} |
| 103 |
return *preferredVersion, nil |
| 104 |
} |
| 105 |
|
| 106 |
// PreferredVersion returns the current preferred version of the custom metrics api. |
| 107 |
// If none is specified, it will use the first known one. |
| 108 |
func (d *apiVersionsFromDiscovery) PreferredVersion() (schema.GroupVersion, error) { |
| 109 |
d.mu.RLock() |
| 110 |
if d.prefVersion != nil { |
| 111 |
// if we've already got one, proceed with that |
| 112 |
defer d.mu.RUnlock() |
| 113 |
return *d.prefVersion, nil |
| 114 |
} |
| 115 |
d.mu.RUnlock() |
| 116 |
|
| 117 |
d.mu.Lock() |
| 118 |
defer d.mu.Unlock() |
| 119 |
|
| 120 |
// double check, someone might have beaten us to it |
| 121 |
if d.prefVersion != nil { |
| 122 |
return *d.prefVersion, nil |
| 123 |
} |
| 124 |
|
| 125 |
// populate our cache |
| 126 |
groupInfo, err := d.fetchVersions() |
| 127 |
if err != nil { |
| 128 |
return schema.GroupVersion{}, err |
| 129 |
} |
| 130 |
prefVersion, err := d.chooseVersion(groupInfo) |
| 131 |
if err != nil { |
| 132 |
return schema.GroupVersion{}, err |
| 133 |
} |
| 134 |
|
| 135 |
d.prefVersion = &prefVersion |
| 136 |
return *d.prefVersion, nil |
| 137 |
} |
| 138 |
|
| 139 |
// Invalidate refreshes the preferred version information. |
| 140 |
func (d *apiVersionsFromDiscovery) Invalidate() { |
| 141 |
d.mu.Lock() |
| 142 |
defer d.mu.Unlock() |
| 143 |
|
| 144 |
d.prefVersion = nil |
| 145 |
} |