build: move e2e dependencies into e2e/go.mod

Several packages are only used while running the e2e suite. These
packages are less important to update, as the they can not influence the
final executable that is part of the Ceph-CSI container-image.

By moving these dependencies out of the main Ceph-CSI go.mod, it is
easier to identify if a reported CVE affects Ceph-CSI, or only the
testing (like most of the Kubernetes CVEs).

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos
2025-03-04 08:57:28 +01:00
committed by mergify[bot]
parent 15da101b1b
commit bec6090996
8047 changed files with 1407827 additions and 3453 deletions

View File

@ -0,0 +1,475 @@
/*
Copyright 2019 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 testutil
import (
"fmt"
"io"
"math"
"reflect"
"sort"
"strings"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
"k8s.io/component-base/metrics"
)
var (
// MetricNameLabel is label under which model.Sample stores metric name
MetricNameLabel model.LabelName = model.MetricNameLabel
// QuantileLabel is label under which model.Sample stores latency quantile value
QuantileLabel model.LabelName = model.QuantileLabel
)
// Metrics is generic metrics for other specific metrics
type Metrics map[string]model.Samples
// Equal returns true if all metrics are the same as the arguments.
func (m *Metrics) Equal(o Metrics) bool {
var leftKeySet []string
var rightKeySet []string
for k := range *m {
leftKeySet = append(leftKeySet, k)
}
for k := range o {
rightKeySet = append(rightKeySet, k)
}
if !reflect.DeepEqual(leftKeySet, rightKeySet) {
return false
}
for _, k := range leftKeySet {
if !(*m)[k].Equal(o[k]) {
return false
}
}
return true
}
// NewMetrics returns new metrics which are initialized.
func NewMetrics() Metrics {
result := make(Metrics)
return result
}
// ParseMetrics parses Metrics from data returned from prometheus endpoint
func ParseMetrics(data string, output *Metrics) error {
dec := expfmt.NewDecoder(strings.NewReader(data), expfmt.NewFormat(expfmt.TypeTextPlain))
decoder := expfmt.SampleDecoder{
Dec: dec,
Opts: &expfmt.DecodeOptions{},
}
for {
var v model.Vector
if err := decoder.Decode(&v); err != nil {
if err == io.EOF {
// Expected loop termination condition.
return nil
}
continue
}
for _, metric := range v {
name := string(metric.Metric[MetricNameLabel])
(*output)[name] = append((*output)[name], metric)
}
}
}
// TextToMetricFamilies reads 'in' as the simple and flat text-based exchange
// format and creates MetricFamily proto messages. It returns the MetricFamily
// proto messages in a map where the metric names are the keys, along with any
// error encountered.
func TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricFamily, error) {
var textParser expfmt.TextParser
return textParser.TextToMetricFamilies(in)
}
// PrintSample returns formatted representation of metric Sample
func PrintSample(sample *model.Sample) string {
buf := make([]string, 0)
// Id is a VERY special label. For 'normal' container it's useless, but it's necessary
// for 'system' containers (e.g. /docker-daemon, /kubelet, etc.). We know if that's the
// case by checking if there's a label "kubernetes_container_name" present. It's hacky
// but it works...
_, normalContainer := sample.Metric["kubernetes_container_name"]
for k, v := range sample.Metric {
if strings.HasPrefix(string(k), "__") {
continue
}
if string(k) == "id" && normalContainer {
continue
}
buf = append(buf, fmt.Sprintf("%v=%v", string(k), v))
}
return fmt.Sprintf("[%v] = %v", strings.Join(buf, ","), sample.Value)
}
// ComputeHistogramDelta computes the change in histogram metric for a selected label.
// Results are stored in after samples
func ComputeHistogramDelta(before, after model.Samples, label model.LabelName) {
beforeSamplesMap := make(map[string]*model.Sample)
for _, bSample := range before {
beforeSamplesMap[makeKey(bSample.Metric[label], bSample.Metric["le"])] = bSample
}
for _, aSample := range after {
if bSample, found := beforeSamplesMap[makeKey(aSample.Metric[label], aSample.Metric["le"])]; found {
aSample.Value = aSample.Value - bSample.Value
}
}
}
func makeKey(a, b model.LabelValue) string {
return string(a) + "___" + string(b)
}
// GetMetricValuesForLabel returns value of metric for a given dimension
func GetMetricValuesForLabel(ms Metrics, metricName, label string) map[string]int64 {
samples, found := ms[metricName]
result := make(map[string]int64, len(samples))
if !found {
return result
}
for _, sample := range samples {
count := int64(sample.Value)
dimensionName := string(sample.Metric[model.LabelName(label)])
result[dimensionName] = count
}
return result
}
// ValidateMetrics verifies if every sample of metric has all expected labels
func ValidateMetrics(metrics Metrics, metricName string, expectedLabels ...string) error {
samples, ok := metrics[metricName]
if !ok {
return fmt.Errorf("metric %q was not found in metrics", metricName)
}
for _, sample := range samples {
for _, l := range expectedLabels {
if _, ok := sample.Metric[model.LabelName(l)]; !ok {
return fmt.Errorf("metric %q is missing label %q, sample: %q", metricName, l, sample.String())
}
}
}
return nil
}
// Histogram wraps prometheus histogram DTO (data transfer object)
type Histogram struct {
*dto.Histogram
}
// HistogramVec wraps a slice of Histogram.
// Note that each Histogram must have the same number of buckets.
type HistogramVec []*Histogram
// GetAggregatedSampleCount aggregates the sample count of each inner Histogram.
func (vec HistogramVec) GetAggregatedSampleCount() uint64 {
var count uint64
for _, hist := range vec {
count += hist.GetSampleCount()
}
return count
}
// GetAggregatedSampleSum aggregates the sample sum of each inner Histogram.
func (vec HistogramVec) GetAggregatedSampleSum() float64 {
var sum float64
for _, hist := range vec {
sum += hist.GetSampleSum()
}
return sum
}
// Quantile first aggregates inner buckets of each Histogram, and then
// computes q-th quantile of a cumulative histogram.
func (vec HistogramVec) Quantile(q float64) float64 {
var buckets []bucket
for i, hist := range vec {
for j, bckt := range hist.Bucket {
if i == 0 {
buckets = append(buckets, bucket{
count: float64(bckt.GetCumulativeCount()),
upperBound: bckt.GetUpperBound(),
})
} else {
buckets[j].count += float64(bckt.GetCumulativeCount())
}
}
}
if len(buckets) == 0 || buckets[len(buckets)-1].upperBound != math.Inf(+1) {
// The list of buckets in dto.Histogram doesn't include the final +Inf bucket, so we
// add it here for the rest of the samples.
buckets = append(buckets, bucket{
count: float64(vec.GetAggregatedSampleCount()),
upperBound: math.Inf(+1),
})
}
return bucketQuantile(q, buckets)
}
// Average computes wrapped histograms' average value.
func (vec HistogramVec) Average() float64 {
return vec.GetAggregatedSampleSum() / float64(vec.GetAggregatedSampleCount())
}
// Validate makes sure the wrapped histograms have all necessary fields set and with valid values.
func (vec HistogramVec) Validate() error {
bucketSize := 0
for i, hist := range vec {
if err := hist.Validate(); err != nil {
return err
}
if i == 0 {
bucketSize = len(hist.GetBucket())
} else if bucketSize != len(hist.GetBucket()) {
return fmt.Errorf("found different bucket size: expect %v, but got %v at index %v", bucketSize, len(hist.GetBucket()), i)
}
}
return nil
}
// GetHistogramVecFromGatherer collects a metric, that matches the input labelValue map,
// from a gatherer implementing k8s.io/component-base/metrics.Gatherer interface.
// Used only for testing purposes where we need to gather metrics directly from a running binary (without metrics endpoint).
func GetHistogramVecFromGatherer(gatherer metrics.Gatherer, metricName string, lvMap map[string]string) (HistogramVec, error) {
var metricFamily *dto.MetricFamily
m, err := gatherer.Gather()
if err != nil {
return nil, err
}
metricFamily = findMetricFamily(m, metricName)
if metricFamily == nil {
return nil, fmt.Errorf("metric %q not found", metricName)
}
if len(metricFamily.GetMetric()) == 0 {
return nil, fmt.Errorf("metric %q is empty", metricName)
}
vec := make(HistogramVec, 0)
for _, metric := range metricFamily.GetMetric() {
if LabelsMatch(metric, lvMap) {
if hist := metric.GetHistogram(); hist != nil {
vec = append(vec, &Histogram{hist})
}
}
}
return vec, nil
}
func uint64Ptr(u uint64) *uint64 {
return &u
}
// Bucket of a histogram
type bucket struct {
upperBound float64
count float64
}
func bucketQuantile(q float64, buckets []bucket) float64 {
if q < 0 {
return math.Inf(-1)
}
if q > 1 {
return math.Inf(+1)
}
if len(buckets) < 2 {
return math.NaN()
}
rank := q * buckets[len(buckets)-1].count
b := sort.Search(len(buckets)-1, func(i int) bool { return buckets[i].count >= rank })
if b == 0 {
return buckets[0].upperBound * (rank / buckets[0].count)
}
if b == len(buckets)-1 && math.IsInf(buckets[b].upperBound, 1) {
return buckets[len(buckets)-2].upperBound
}
// linear approximation of b-th bucket
brank := rank - buckets[b-1].count
bSize := buckets[b].upperBound - buckets[b-1].upperBound
bCount := buckets[b].count - buckets[b-1].count
return buckets[b-1].upperBound + bSize*(brank/bCount)
}
// Quantile computes q-th quantile of a cumulative histogram.
// It's expected the histogram is valid (by calling Validate)
func (hist *Histogram) Quantile(q float64) float64 {
var buckets []bucket
for _, bckt := range hist.Bucket {
buckets = append(buckets, bucket{
count: float64(bckt.GetCumulativeCount()),
upperBound: bckt.GetUpperBound(),
})
}
if len(buckets) == 0 || buckets[len(buckets)-1].upperBound != math.Inf(+1) {
// The list of buckets in dto.Histogram doesn't include the final +Inf bucket, so we
// add it here for the rest of the samples.
buckets = append(buckets, bucket{
count: float64(hist.GetSampleCount()),
upperBound: math.Inf(+1),
})
}
return bucketQuantile(q, buckets)
}
// Average computes histogram's average value
func (hist *Histogram) Average() float64 {
return hist.GetSampleSum() / float64(hist.GetSampleCount())
}
// Validate makes sure the wrapped histogram has all necessary fields set and with valid values.
func (hist *Histogram) Validate() error {
if hist.SampleCount == nil || hist.GetSampleCount() == 0 {
return fmt.Errorf("nil or empty histogram SampleCount")
}
if hist.SampleSum == nil || hist.GetSampleSum() == 0 {
return fmt.Errorf("nil or empty histogram SampleSum")
}
for _, bckt := range hist.Bucket {
if bckt == nil {
return fmt.Errorf("empty histogram bucket")
}
if bckt.UpperBound == nil || bckt.GetUpperBound() < 0 {
return fmt.Errorf("nil or negative histogram bucket UpperBound")
}
}
return nil
}
// GetGaugeMetricValue extracts metric value from GaugeMetric
func GetGaugeMetricValue(m metrics.GaugeMetric) (float64, error) {
metricProto := &dto.Metric{}
if err := m.Write(metricProto); err != nil {
return 0, fmt.Errorf("error writing m: %v", err)
}
return metricProto.Gauge.GetValue(), nil
}
// GetCounterMetricValue extracts metric value from CounterMetric
func GetCounterMetricValue(m metrics.CounterMetric) (float64, error) {
metricProto := &dto.Metric{}
if err := m.(metrics.Metric).Write(metricProto); err != nil {
return 0, fmt.Errorf("error writing m: %v", err)
}
return metricProto.Counter.GetValue(), nil
}
// GetHistogramMetricValue extracts sum of all samples from ObserverMetric
func GetHistogramMetricValue(m metrics.ObserverMetric) (float64, error) {
metricProto := &dto.Metric{}
if err := m.(metrics.Metric).Write(metricProto); err != nil {
return 0, fmt.Errorf("error writing m: %v", err)
}
return metricProto.Histogram.GetSampleSum(), nil
}
// GetHistogramMetricCount extracts count of all samples from ObserverMetric
func GetHistogramMetricCount(m metrics.ObserverMetric) (uint64, error) {
metricProto := &dto.Metric{}
if err := m.(metrics.Metric).Write(metricProto); err != nil {
return 0, fmt.Errorf("error writing m: %v", err)
}
return metricProto.Histogram.GetSampleCount(), nil
}
// LabelsMatch returns true if metric has all expected labels otherwise false
func LabelsMatch(metric *dto.Metric, labelFilter map[string]string) bool {
metricLabels := map[string]string{}
for _, labelPair := range metric.Label {
metricLabels[labelPair.GetName()] = labelPair.GetValue()
}
// length comparison then match key to values in the maps
if len(labelFilter) > len(metricLabels) {
return false
}
for labelName, labelValue := range labelFilter {
if value, ok := metricLabels[labelName]; !ok || value != labelValue {
return false
}
}
return true
}
// GetCounterVecFromGatherer collects a counter that matches the given name
// from a gatherer implementing k8s.io/component-base/metrics.Gatherer interface.
// It returns all counter values that had a label with a certain name in a map
// that uses the label value as keys.
//
// Used only for testing purposes where we need to gather metrics directly from a running binary (without metrics endpoint).
func GetCounterValuesFromGatherer(gatherer metrics.Gatherer, metricName string, lvMap map[string]string, labelName string) (map[string]float64, error) {
m, err := gatherer.Gather()
if err != nil {
return nil, err
}
metricFamily := findMetricFamily(m, metricName)
if metricFamily == nil {
return nil, fmt.Errorf("metric %q not found", metricName)
}
if len(metricFamily.GetMetric()) == 0 {
return nil, fmt.Errorf("metric %q is empty", metricName)
}
values := make(map[string]float64)
for _, metric := range metricFamily.GetMetric() {
if LabelsMatch(metric, lvMap) {
if counter := metric.GetCounter(); counter != nil {
for _, labelPair := range metric.Label {
if labelPair.GetName() == labelName {
values[labelPair.GetValue()] = counter.GetValue()
}
}
}
}
}
return values, nil
}
func findMetricFamily(metricFamilies []*dto.MetricFamily, metricName string) *dto.MetricFamily {
for _, mFamily := range metricFamilies {
if mFamily.GetName() == metricName {
return mFamily
}
}
return nil
}

View File

@ -0,0 +1,151 @@
/*
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 testutil
import (
"fmt"
"io"
"strings"
"github.com/prometheus/client_golang/prometheus/testutil/promlint"
)
// exceptionMetrics is an exception list of metrics which violates promlint rules.
//
// The original entries come from the existing metrics when we introduce promlint.
// We setup this list for allow and not fail on the current violations.
// Generally speaking, you need to fix the problem for a new metric rather than add it into the list.
var exceptionMetrics = []string{
// k8s.io/apiserver/pkg/server/egressselector
"apiserver_egress_dialer_dial_failure_count", // counter metrics should have "_total" suffix
// k8s.io/apiserver/pkg/server/healthz
"apiserver_request_total", // label names should be written in 'snake_case' not 'camelCase'
// k8s.io/apiserver/pkg/endpoints/filters
"authenticated_user_requests", // counter metrics should have "_total" suffix
"authentication_attempts", // counter metrics should have "_total" suffix
// kube-apiserver
"aggregator_openapi_v2_regeneration_count",
"apiserver_admission_step_admission_duration_seconds_summary",
"apiserver_current_inflight_requests",
"apiserver_longrunning_gauge",
"get_token_count",
"get_token_fail_count",
"ssh_tunnel_open_count",
"ssh_tunnel_open_fail_count",
// kube-controller-manager
"attachdetach_controller_forced_detaches",
"authenticated_user_requests",
"authentication_attempts",
"get_token_count",
"get_token_fail_count",
"node_collector_evictions_number",
}
// A Problem is an issue detected by a Linter.
type Problem promlint.Problem
func (p *Problem) String() string {
return fmt.Sprintf("%s:%s", p.Metric, p.Text)
}
// A Linter is a Prometheus metrics linter. It identifies issues with metric
// names, types, and metadata, and reports them to the caller.
type Linter struct {
promLinter *promlint.Linter
}
// Lint performs a linting pass, returning a slice of Problems indicating any
// issues found in the metrics stream. The slice is sorted by metric name
// and issue description.
func (l *Linter) Lint() ([]Problem, error) {
promProblems, err := l.promLinter.Lint()
if err != nil {
return nil, err
}
// Ignore problems those in exception list
problems := make([]Problem, 0, len(promProblems))
for i := range promProblems {
if !l.shouldIgnore(promProblems[i].Metric) {
problems = append(problems, Problem(promProblems[i]))
}
}
return problems, nil
}
// shouldIgnore returns true if metric in the exception list, otherwise returns false.
func (l *Linter) shouldIgnore(metricName string) bool {
for i := range exceptionMetrics {
if metricName == exceptionMetrics[i] {
return true
}
}
return false
}
// NewPromLinter creates a new Linter that reads an input stream of Prometheus metrics.
// Only the text exposition format is supported.
func NewPromLinter(r io.Reader) *Linter {
return &Linter{
promLinter: promlint.New(r),
}
}
func mergeProblems(problems []Problem) string {
var problemsMsg []string
for index := range problems {
problemsMsg = append(problemsMsg, problems[index].String())
}
return strings.Join(problemsMsg, ",")
}
// shouldIgnore returns true if metric in the exception list, otherwise returns false.
func shouldIgnore(metricName string) bool {
for i := range exceptionMetrics {
if metricName == exceptionMetrics[i] {
return true
}
}
return false
}
// getLintError will ignore the metrics in exception list and converts lint problem to error.
func getLintError(problems []promlint.Problem) error {
var filteredProblems []Problem
for _, problem := range problems {
if shouldIgnore(problem.Metric) {
continue
}
filteredProblems = append(filteredProblems, Problem(problem))
}
if len(filteredProblems) == 0 {
return nil
}
return fmt.Errorf("lint error: %s", mergeProblems(filteredProblems))
}

View File

@ -0,0 +1,159 @@
/*
Copyright 2019 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 testutil
import (
"fmt"
"io"
"github.com/prometheus/client_golang/prometheus/testutil"
apimachineryversion "k8s.io/apimachinery/pkg/version"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)
type TB interface {
Logf(format string, args ...any)
Errorf(format string, args ...any)
Fatalf(format string, args ...any)
}
// CollectAndCompare registers the provided Collector with a newly created
// pedantic Registry. It then does the same as GatherAndCompare, gathering the
// metrics from the pedantic Registry.
func CollectAndCompare(c metrics.Collector, expected io.Reader, metricNames ...string) error {
lintProblems, err := testutil.CollectAndLint(c, metricNames...)
if err != nil {
return err
}
if err := getLintError(lintProblems); err != nil {
return err
}
return testutil.CollectAndCompare(c, expected, metricNames...)
}
// GatherAndCompare gathers all metrics from the provided Gatherer and compares
// it to an expected output read from the provided Reader in the Prometheus text
// exposition format. If any metricNames are provided, only metrics with those
// names are compared.
func GatherAndCompare(g metrics.Gatherer, expected io.Reader, metricNames ...string) error {
lintProblems, err := testutil.GatherAndLint(g, metricNames...)
if err != nil {
return err
}
if err := getLintError(lintProblems); err != nil {
return err
}
return testutil.GatherAndCompare(g, expected, metricNames...)
}
// CustomCollectAndCompare registers the provided StableCollector with a newly created
// registry. It then does the same as GatherAndCompare, gathering the
// metrics from the pedantic Registry.
func CustomCollectAndCompare(c metrics.StableCollector, expected io.Reader, metricNames ...string) error {
registry := metrics.NewKubeRegistry()
registry.CustomMustRegister(c)
return GatherAndCompare(registry, expected, metricNames...)
}
// ScrapeAndCompare calls a remote exporter's endpoint which is expected to return some metrics in
// plain text format. Then it compares it with the results that the `expected` would return.
// If the `metricNames` is not empty it would filter the comparison only to the given metric names.
func ScrapeAndCompare(url string, expected io.Reader, metricNames ...string) error {
return testutil.ScrapeAndCompare(url, expected, metricNames...)
}
// NewFakeKubeRegistry creates a fake `KubeRegistry` that takes the input version as `build in version`.
// It should only be used in testing scenario especially for the deprecated metrics.
// The input version format should be `major.minor.patch`, e.g. '1.18.0'.
func NewFakeKubeRegistry(ver string) metrics.KubeRegistry {
backup := metrics.BuildVersion
defer func() {
metrics.BuildVersion = backup
}()
metrics.BuildVersion = func() apimachineryversion.Info {
return apimachineryversion.Info{
GitVersion: fmt.Sprintf("v%s-alpha+1.12345", ver),
}
}
return metrics.NewKubeRegistry()
}
func AssertVectorCount(t TB, name string, labelFilter map[string]string, wantCount int) {
metrics, err := legacyregistry.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("Failed to gather metrics: %s", err)
}
counterSum := 0
for _, mf := range metrics {
if mf.GetName() != name {
continue // Ignore other metrics.
}
for _, metric := range mf.GetMetric() {
if !LabelsMatch(metric, labelFilter) {
continue
}
counterSum += int(metric.GetCounter().GetValue())
}
}
if wantCount != counterSum {
t.Errorf("Wanted count %d, got %d for metric %s with labels %#+v", wantCount, counterSum, name, labelFilter)
for _, mf := range metrics {
if mf.GetName() == name {
for _, metric := range mf.GetMetric() {
t.Logf("\tnear match: %s", metric.String())
}
}
}
}
}
func AssertHistogramTotalCount(t TB, name string, labelFilter map[string]string, wantCount int) {
metrics, err := legacyregistry.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("Failed to gather metrics: %s", err)
}
counterSum := 0
for _, mf := range metrics {
if mf.GetName() != name {
continue // Ignore other metrics.
}
for _, metric := range mf.GetMetric() {
if !LabelsMatch(metric, labelFilter) {
continue
}
counterSum += int(metric.GetHistogram().GetSampleCount())
}
}
if wantCount != counterSum {
t.Errorf("Wanted count %d, got %d for metric %s with labels %#+v", wantCount, counterSum, name, labelFilter)
for _, mf := range metrics {
if mf.GetName() == name {
for _, metric := range mf.GetMetric() {
t.Logf("\tnear match: %s\n", metric.String())
}
}
}
}
}