github.com/kubewharf/katalyst-core@v0.5.3/pkg/custom-metric/provider/provider_dummy.go (about)

     1  /*
     2  Copyright 2022 The Katalyst 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 provider
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/api/resource"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/labels"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/types"
    28  	"k8s.io/metrics/pkg/apis/custom_metrics"
    29  	"k8s.io/metrics/pkg/apis/external_metrics"
    30  	"sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
    31  )
    32  
    33  type DummyMetricQuery struct{}
    34  
    35  var _ MetricProvider = DummyMetricQuery{}
    36  
    37  func (d DummyMetricQuery) GetMetricByName(_ context.Context, _ types.NamespacedName,
    38  	_ provider.CustomMetricInfo, _ labels.Selector,
    39  ) (*custom_metrics.MetricValue, error) {
    40  	return &custom_metrics.MetricValue{
    41  		DescribedObject: custom_metrics.ObjectReference{
    42  			Kind:      "pod",
    43  			Namespace: "my-namespace",
    44  			Name:      "my-pod",
    45  		},
    46  		Metric: custom_metrics.MetricIdentifier{
    47  			Name: "cpu-usage",
    48  		},
    49  		Timestamp: metav1.Time{Time: time.Now()},
    50  		Value:     *resource.NewQuantity(1, resource.DecimalSI),
    51  	}, nil
    52  }
    53  
    54  func (d DummyMetricQuery) GetMetricBySelector(_ context.Context, _ string, _ labels.Selector,
    55  	_ provider.CustomMetricInfo, _ labels.Selector,
    56  ) (*custom_metrics.MetricValueList, error) {
    57  	return &custom_metrics.MetricValueList{
    58  		Items: []custom_metrics.MetricValue{
    59  			{
    60  				DescribedObject: custom_metrics.ObjectReference{
    61  					Kind:      "pod",
    62  					Namespace: "my-namespace",
    63  					Name:      "my-pod",
    64  				},
    65  				Metric: custom_metrics.MetricIdentifier{
    66  					Name: "cpu-usage",
    67  				},
    68  				Timestamp: metav1.Time{Time: time.Now()},
    69  				Value:     *resource.NewQuantity(1, resource.DecimalSI),
    70  			},
    71  		},
    72  	}, nil
    73  }
    74  
    75  func (d DummyMetricQuery) ListAllMetrics() []provider.CustomMetricInfo {
    76  	return []provider.CustomMetricInfo{
    77  		{
    78  			GroupResource: schema.GroupResource{Resource: "pod"},
    79  			Namespaced:    true,
    80  			Metric:        "cpu-usage",
    81  		},
    82  	}
    83  }
    84  
    85  func (d DummyMetricQuery) GetExternalMetric(_ context.Context, _ string, _ labels.Selector,
    86  	_ provider.ExternalMetricInfo,
    87  ) (*external_metrics.ExternalMetricValueList, error) {
    88  	return &external_metrics.ExternalMetricValueList{
    89  		Items: []external_metrics.ExternalMetricValue{
    90  			{
    91  				MetricName: "my-external-metric",
    92  				MetricLabels: map[string]string{
    93  					"foo": "bar",
    94  				},
    95  				Value: *resource.NewQuantity(42, resource.DecimalSI),
    96  			},
    97  		},
    98  	}, nil
    99  }
   100  
   101  func (d DummyMetricQuery) ListAllExternalMetrics() []provider.ExternalMetricInfo {
   102  	return []provider.ExternalMetricInfo{
   103  		{
   104  			Metric: "my-external-metric",
   105  		},
   106  	}
   107  }