Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client for external metrics #60220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions hack/.golint_failures
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@ staging/src/k8s.io/metrics/pkg/apis/metrics/v1alpha1
staging/src/k8s.io/metrics/pkg/apis/metrics/v1beta1
staging/src/k8s.io/metrics/pkg/client/custom_metrics
staging/src/k8s.io/metrics/pkg/client/custom_metrics/fake
staging/src/k8s.io/metrics/pkg/client/external_metrics
staging/src/k8s.io/metrics/pkg/client/external_metrics/fake
staging/src/k8s.io/sample-apiserver/pkg/apis/wardle
staging/src/k8s.io/sample-apiserver/pkg/apis/wardle/v1alpha1
staging/src/k8s.io/sample-apiserver/pkg/apiserver
Expand Down
1 change: 1 addition & 0 deletions staging/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ filegroup(
"//staging/src/k8s.io/metrics/pkg/apis/metrics:all-srcs",
"//staging/src/k8s.io/metrics/pkg/client/clientset_generated/clientset:all-srcs",
"//staging/src/k8s.io/metrics/pkg/client/custom_metrics:all-srcs",
"//staging/src/k8s.io/metrics/pkg/client/external_metrics:all-srcs",
"//staging/src/k8s.io/sample-apiserver:all-srcs",
"//staging/src/k8s.io/sample-controller:all-srcs",
],
Expand Down
40 changes: 40 additions & 0 deletions staging/src/k8s.io/metrics/pkg/client/external_metrics/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package(default_visibility = ["//visibility:public"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)

go_library(
name = "go_default_library",
srcs = [
"client.go",
"interfaces.go",
],
importpath = "k8s.io/metrics/pkg/client/external_metrics",
deps = [
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
"//vendor/k8s.io/client-go/rest:go_default_library",
"//vendor/k8s.io/client-go/util/flowcontrol:go_default_library",
"//vendor/k8s.io/metrics/pkg/apis/external_metrics/v1beta1:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//staging/src/k8s.io/metrics/pkg/client/external_metrics/fake:all-srcs",
],
tags = ["automanaged"],
)
95 changes: 95 additions & 0 deletions staging/src/k8s.io/metrics/pkg/client/external_metrics/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2018 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 external_metrics

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/metrics/pkg/apis/external_metrics/v1beta1"
)

type externalMetricsClient struct {
client rest.Interface
}

func New(client rest.Interface) ExternalMetricsClient {
return &externalMetricsClient{
client: client,
}
}

func NewForConfig(c *rest.Config) (ExternalMetricsClient, error) {
configShallowCopy := *c
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
}
configShallowCopy.APIPath = "/apis"
if configShallowCopy.UserAgent == "" {
configShallowCopy.UserAgent = rest.DefaultKubernetesUserAgent()
}
configShallowCopy.GroupVersion = &v1beta1.SchemeGroupVersion
configShallowCopy.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}

client, err := rest.RESTClientFor(&configShallowCopy)
if err != nil {
return nil, err
}

return New(client), nil
}

func NewForConfigOrDie(c *rest.Config) ExternalMetricsClient {
client, err := NewForConfig(c)
if err != nil {
panic(err)
}
return client
}

func (c *externalMetricsClient) NamespacedMetrics(namespace string) MetricsInterface {
return &namespacedMetrics{
client: c,
namespace: namespace,
}
}

type namespacedMetrics struct {
client *externalMetricsClient
namespace string
}

func (m *namespacedMetrics) List(metricName string, metricSelector labels.Selector) (*v1beta1.ExternalMetricValueList, error) {
res := &v1beta1.ExternalMetricValueList{}
err := m.client.client.Get().
Namespace(m.namespace).
Resource(metricName).
VersionedParams(&metav1.ListOptions{
LabelSelector: metricSelector.String(),
}, metav1.ParameterCodec).
Do().
Into(res)

if err != nil {
return nil, err
}

return res, nil
}
33 changes: 33 additions & 0 deletions staging/src/k8s.io/metrics/pkg/client/external_metrics/fake/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package(default_visibility = ["//visibility:public"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)

go_library(
name = "go_default_library",
srcs = ["fake_client.go"],
importpath = "k8s.io/metrics/pkg/client/external_metrics/fake",
deps = [
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/client-go/testing:go_default_library",
"//vendor/k8s.io/metrics/pkg/apis/external_metrics/v1beta1:go_default_library",
"//vendor/k8s.io/metrics/pkg/client/external_metrics:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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 fake

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/testing"
"k8s.io/metrics/pkg/apis/external_metrics/v1beta1"
eclient "k8s.io/metrics/pkg/client/external_metrics"
)

type FakeExternalMetricsClient struct {
testing.Fake
}

func (c *FakeExternalMetricsClient) NamespacedMetrics(namespace string) eclient.MetricsInterface {
return &fakeNamespacedMetrics{
Fake: c,
ns: namespace,
}
}

type fakeNamespacedMetrics struct {
Fake *FakeExternalMetricsClient
ns string
}

func (m *fakeNamespacedMetrics) List(metricName string, metricSelector labels.Selector) (*v1beta1.ExternalMetricValueList, error) {
resource := schema.GroupResource{
Group: v1beta1.SchemeGroupVersion.Group,
Resource: metricName,
}
kind := schema.GroupKind{
Group: v1beta1.SchemeGroupVersion.Group,
Kind: "ExternalMetricValue",
}
action := testing.NewListAction(resource.WithVersion(""), kind.WithVersion(""), m.ns, metav1.ListOptions{LabelSelector: metricSelector.String()})
obj, err := m.Fake.
Invokes(action, &v1beta1.ExternalMetricValueList{})

if obj == nil {
return nil, err
}

return obj.(*v1beta1.ExternalMetricValueList), err
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2018 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 external_metrics

import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/metrics/pkg/apis/external_metrics/v1beta1"
)

// ExternalMetricsClient is a client for fetching external metrics.
type ExternalMetricsClient interface {
NamespacedMetricsGetter
}

// NamespacedMetricsGetter provides access to an interface for fetching
// metrics in a particular namespace.
type NamespacedMetricsGetter interface {
NamespacedMetrics(namespace string) MetricsInterface
}

// MetricsInterface provides access to external metrics.
type MetricsInterface interface {
// Get fetches the metric for the given namespace that maches the given
// metricSelector.
List(metricName string, metricSelector labels.Selector) (*v1beta1.ExternalMetricValueList, error)
}