Skip to content

Commit 3fc1bc7

Browse files
authored
Merge pull request #94481 from wojtek-t/fix_custom_metrics
Cleanup custom metrics conversion functions
2 parents 92042fe + 615f903 commit 3fc1bc7

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

staging/src/k8s.io/metrics/pkg/apis/custom_metrics/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load(
88
go_library(
99
name = "go_default_library",
1010
srcs = [
11+
"conversion.go",
1112
"doc.go",
1213
"register.go",
1314
"types.go",
@@ -16,8 +17,10 @@ go_library(
1617
importmap = "k8s.io/kubernetes/vendor/k8s.io/metrics/pkg/apis/custom_metrics",
1718
importpath = "k8s.io/metrics/pkg/apis/custom_metrics",
1819
deps = [
20+
"//staging/src/k8s.io/api/core/v1:go_default_library",
1921
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
2022
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
23+
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
2124
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2225
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
2326
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2020 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+
"k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/conversion"
22+
)
23+
24+
func Convert_v1_ObjectReference_To_custom_metrics_ObjectReference(in *v1.ObjectReference, out *ObjectReference, s conversion.Scope) error {
25+
out.APIVersion = in.APIVersion
26+
27+
out.Kind = in.Kind
28+
out.Namespace = in.Namespace
29+
out.Name = in.Name
30+
out.UID = in.UID
31+
out.ResourceVersion = in.ResourceVersion
32+
out.FieldPath = in.FieldPath
33+
return nil
34+
}
35+
36+
func Convert_custom_metrics_ObjectReference_To_v1_ObjectReference(in *ObjectReference, out *v1.ObjectReference, s conversion.Scope) error {
37+
out.APIVersion = in.APIVersion
38+
39+
out.Kind = in.Kind
40+
out.Namespace = in.Namespace
41+
out.Name = in.Name
42+
out.UID = in.UID
43+
out.ResourceVersion = in.ResourceVersion
44+
out.FieldPath = in.FieldPath
45+
return nil
46+
}

staging/src/k8s.io/metrics/pkg/apis/custom_metrics/types.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ type MetricValueList struct {
4747

4848
// a metric value for some object
4949
type MetricValue struct {
50-
metav1.TypeMeta `json:",inline"`
50+
metav1.TypeMeta
5151

5252
// a reference to the described object
53-
DescribedObject ObjectReference `json:"describedObject"`
53+
DescribedObject ObjectReference
5454

5555
Metric MetricIdentifier
5656

5757
// indicates the time at which the metrics were produced
58-
Timestamp metav1.Time `json:"timestamp"`
58+
Timestamp metav1.Time
5959

6060
// indicates the window ([Timestamp-Window, Timestamp]) from
6161
// which these metrics were calculated, when returning rate
6262
// metrics calculated from cumulative metrics (or zero for
6363
// non-calculated instantaneous metrics).
64-
WindowSeconds *int64 `json:"window,omitempty"`
64+
WindowSeconds *int64
6565

6666
// the value of the metric for this
67-
Value resource.Quantity `json:"value"`
67+
Value resource.Quantity
6868
}
6969

7070
// allObjects is a wildcard used to select metrics
@@ -75,16 +75,16 @@ const AllObjects = "*"
7575

7676
// MetricListOptions is used to select metrics by their label selectors
7777
type MetricListOptions struct {
78-
metav1.TypeMeta `json:",inline"`
78+
metav1.TypeMeta
7979

8080
// A selector to restrict the list of returned objects by their labels.
8181
// Defaults to everything.
8282
// +optional
83-
LabelSelector string `json:"labelSelector,omitempty" protobuf:"bytes,1,opt,name=labelSelector"`
83+
LabelSelector string
8484

8585
// A selector to restrict the list of returned metrics by their labels
8686
// +optional
87-
MetricLabelSelector string `json:"metricLabelSelector,omitempty" protobuf:"bytes,2,opt,name=metricLabelSelector"`
87+
MetricLabelSelector string
8888
}
8989

9090
// NOTE: ObjectReference is copied from k8s.io/kubernetes/pkg/api/types.go. We

staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta1/conversion.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,23 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
"k8s.io/api/core/v1"
2120
"k8s.io/apimachinery/pkg/conversion"
2221
"k8s.io/metrics/pkg/apis/custom_metrics"
2322
)
2423

2524
func Convert_v1beta1_MetricValue_To_custom_metrics_MetricValue(in *MetricValue, out *custom_metrics.MetricValue, s conversion.Scope) error {
26-
out.TypeMeta = in.TypeMeta
27-
out.DescribedObject = custom_metrics.ObjectReference{
28-
Kind: in.DescribedObject.Kind,
29-
Namespace: in.DescribedObject.Namespace,
30-
Name: in.DescribedObject.Name,
31-
UID: in.DescribedObject.UID,
32-
APIVersion: in.DescribedObject.APIVersion,
33-
ResourceVersion: in.DescribedObject.ResourceVersion,
34-
FieldPath: in.DescribedObject.FieldPath,
25+
if err := autoConvert_v1beta1_MetricValue_To_custom_metrics_MetricValue(in, out, s); err != nil {
26+
return err
3527
}
36-
out.Timestamp = in.Timestamp
37-
out.WindowSeconds = in.WindowSeconds
38-
out.Value = in.Value
3928
out.Metric.Name = in.MetricName
4029
out.Metric.Selector = in.Selector
4130
return nil
4231
}
4332

4433
func Convert_custom_metrics_MetricValue_To_v1beta1_MetricValue(in *custom_metrics.MetricValue, out *MetricValue, s conversion.Scope) error {
45-
out.TypeMeta = in.TypeMeta
46-
out.DescribedObject = v1.ObjectReference{
47-
Kind: in.DescribedObject.Kind,
48-
Namespace: in.DescribedObject.Namespace,
49-
Name: in.DescribedObject.Name,
50-
UID: in.DescribedObject.UID,
51-
APIVersion: in.DescribedObject.APIVersion,
52-
ResourceVersion: in.DescribedObject.ResourceVersion,
53-
FieldPath: in.DescribedObject.FieldPath,
34+
if err := autoConvert_custom_metrics_MetricValue_To_v1beta1_MetricValue(in, out, s); err != nil {
35+
return err
5436
}
55-
out.Timestamp = in.Timestamp
56-
out.WindowSeconds = in.WindowSeconds
57-
out.Value = in.Value
5837
out.MetricName = in.Metric.Name
5938
out.Selector = in.Metric.Selector
6039
return nil

staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta1/zz_generated.conversion.go

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/metrics/pkg/apis/custom_metrics/v1beta2/zz_generated.conversion.go

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)