vendor: revendor metrics-server, custom-metrics-apiserver

This commit is contained in:
Sergiusz Urbaniak 2020-10-28 15:52:52 +01:00
parent 752ce84723
commit 523aa52367
1010 changed files with 91458 additions and 29107 deletions

View file

@ -0,0 +1,38 @@
// Copyright 2020 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 api
import (
"k8s.io/component-base/metrics"
)
var (
metricFreshness = metrics.NewHistogramVec(
&metrics.HistogramOpts{
Namespace: "metrics_server",
Subsystem: "api",
Name: "metric_freshness_seconds",
Help: "Freshness of metrics exported",
Buckets: metrics.ExponentialBuckets(1, 1.364, 20),
},
[]string{},
)
)
// RegisterAPIMetrics registers a histogram metric for the freshness of
// exported metrics.
func RegisterAPIMetrics(registrationFunc func(metrics.Registerable) error) error {
return registrationFunc(metricFreshness)
}

View file

@ -18,7 +18,6 @@ import (
"context"
"fmt"
"sort"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
@ -32,7 +31,7 @@ import (
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
v1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/klog"
"k8s.io/klog/v2"
"k8s.io/metrics/pkg/apis/metrics"
_ "k8s.io/metrics/pkg/apis/metrics/install"
)
@ -200,12 +199,13 @@ func (m *nodeMetrics) getNodeMetrics(names ...string) ([]metrics.NodeMetrics, er
res = append(res, metrics.NodeMetrics{
ObjectMeta: metav1.ObjectMeta{
Name: name,
CreationTimestamp: metav1.NewTime(time.Now()),
CreationTimestamp: metav1.NewTime(myClock.Now()),
},
Timestamp: metav1.NewTime(timestamps[i].Timestamp),
Window: metav1.Duration{Duration: timestamps[i].Window},
Usage: usages[i],
})
metricFreshness.WithLabelValues().Observe(myClock.Since(timestamps[i].Timestamp).Seconds())
}
return res, nil

View file

@ -18,7 +18,6 @@ import (
"context"
"fmt"
"sort"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
@ -34,7 +33,7 @@ import (
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
v1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/klog"
"k8s.io/klog/v2"
"k8s.io/metrics/pkg/apis/metrics"
_ "k8s.io/metrics/pkg/apis/metrics/install"
)
@ -246,12 +245,13 @@ func (m *podMetrics) getPodMetrics(pods ...*v1.Pod) ([]metrics.PodMetrics, error
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Namespace: pod.Namespace,
CreationTimestamp: metav1.NewTime(time.Now()),
CreationTimestamp: metav1.NewTime(myClock.Now()),
},
Timestamp: metav1.NewTime(timestamps[i].Timestamp),
Window: metav1.Duration{Duration: timestamps[i].Window},
Containers: containerMetrics[i],
})
metricFreshness.WithLabelValues().Observe(myClock.Since(timestamps[i].Timestamp).Seconds())
}
return res, nil
}

39
vendor/sigs.k8s.io/metrics-server/pkg/api/time.go generated vendored Normal file
View file

@ -0,0 +1,39 @@
/*
Copyright 2020 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 api
import (
"time"
)
var myClock clock = &realClock{}
type clock interface {
Now() time.Time
Since(time.Time) time.Duration
}
type realClock struct{}
func (realClock) Now() time.Time { return time.Now() }
func (realClock) Since(d time.Time) time.Duration { return time.Since(d) }
type fakeClock struct {
now time.Time
}
func (c fakeClock) Now() time.Time { return c.now }
func (c fakeClock) Since(d time.Time) time.Duration { return c.now.Sub(d) }