-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use mdox to sync sample-adapter and doc code snippets
- Loading branch information
1 parent
95f30fd
commit a144605
Showing
6 changed files
with
716 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2022 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 main | ||
|
||
import ( | ||
"os" | ||
|
||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/component-base/logs" | ||
"k8s.io/klog/v2" | ||
|
||
basecmd "sigs.k8s.io/custom-metrics-apiserver/pkg/cmd" | ||
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider" | ||
|
||
// make this the path to the provider that you just wrote | ||
yourprov "sigs.k8s.io/custom-metrics-apiserver/docs/sample-adapter/pkg/provider" | ||
) | ||
|
||
type YourAdapter struct { | ||
basecmd.AdapterBase | ||
|
||
// the message printed on startup | ||
Message string | ||
} | ||
|
||
func main() { | ||
logs.InitLogs() | ||
defer logs.FlushLogs() | ||
|
||
// initialize the flags, with one custom flag for the message | ||
cmd := &YourAdapter{} | ||
cmd.Flags().StringVar(&cmd.Message, "msg", "starting adapter...", "startup message") | ||
logs.AddFlags(cmd.Flags()) | ||
if err := cmd.Flags().Parse(os.Args); err != nil { | ||
klog.Fatalf("unable to parse flags: %v", err) | ||
} | ||
|
||
provider := cmd.makeProviderOrDie() | ||
cmd.WithCustomMetrics(provider) | ||
// you could also set up external metrics support, | ||
// if your provider supported it: | ||
// cmd.WithExternalMetrics(provider) | ||
|
||
klog.Infof(cmd.Message) | ||
if err := cmd.Run(wait.NeverStop); err != nil { | ||
klog.Fatalf("unable to run custom metrics adapter: %v", err) | ||
} | ||
} | ||
|
||
func (a *YourAdapter) makeProviderOrDie() provider.CustomMetricsProvider { | ||
client, err := a.DynamicClient() | ||
if err != nil { | ||
klog.Fatalf("unable to construct dynamic client: %v", err) | ||
} | ||
|
||
mapper, err := a.RESTMapper() | ||
if err != nil { | ||
klog.Fatalf("unable to construct discovery REST mapper: %v", err) | ||
} | ||
|
||
return yourprov.NewProvider(client, mapper) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright 2022 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 provider | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/metrics/pkg/apis/custom_metrics" | ||
|
||
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider" | ||
"sigs.k8s.io/custom-metrics-apiserver/pkg/provider/helpers" | ||
) | ||
|
||
type yourProvider struct { | ||
client dynamic.Interface | ||
mapper apimeta.RESTMapper | ||
|
||
// just increment values when they're requested | ||
values map[provider.CustomMetricInfo]int64 | ||
} | ||
|
||
func NewProvider(client dynamic.Interface, mapper apimeta.RESTMapper) provider.CustomMetricsProvider { | ||
return &yourProvider{ | ||
client: client, | ||
mapper: mapper, | ||
values: make(map[provider.CustomMetricInfo]int64), | ||
} | ||
} | ||
|
||
// valueFor fetches a value from the fake list and increments it. | ||
func (p *yourProvider) valueFor(info provider.CustomMetricInfo) (int64, error) { | ||
// normalize the value so that you treat plural resources and singular | ||
// resources the same (e.g. pods vs pod) | ||
info, _, err := info.Normalized(p.mapper) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
value := p.values[info] | ||
value++ | ||
p.values[info] = value | ||
|
||
return value, nil | ||
} | ||
|
||
// metricFor constructs a result for a single metric value. | ||
func (p *yourProvider) metricFor(value int64, name types.NamespacedName, info provider.CustomMetricInfo) (*custom_metrics.MetricValue, error) { | ||
// construct a reference referring to the described object | ||
objRef, err := helpers.ReferenceFor(p.mapper, name, info) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &custom_metrics.MetricValue{ | ||
DescribedObject: objRef, | ||
Metric: custom_metrics.MetricIdentifier{ | ||
Name: info.Metric, | ||
}, | ||
// you'll want to use the actual timestamp in a real adapter | ||
Timestamp: metav1.Time{Time: time.Now()}, | ||
Value: *resource.NewMilliQuantity(value*100, resource.DecimalSI), | ||
}, nil | ||
} | ||
|
||
func (p *yourProvider) GetMetricByName(ctx context.Context, name types.NamespacedName, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValue, error) { | ||
value, err := p.valueFor(info) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return p.metricFor(value, name, info) | ||
} | ||
|
||
func (p *yourProvider) GetMetricBySelector(ctx context.Context, namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) { | ||
totalValue, err := p.valueFor(info) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
names, err := helpers.ListObjectNames(p.mapper, p.client, namespace, selector, info) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := make([]custom_metrics.MetricValue, len(names)) | ||
for i, name := range names { | ||
// in a real adapter, you might want to consider pre-computing the | ||
// object reference created in metricFor, instead of recomputing it | ||
// for each object. | ||
value, err := p.metricFor(100*totalValue/int64(len(res)), types.NamespacedName{Namespace: namespace, Name: name}, info) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res[i] = *value | ||
} | ||
|
||
return &custom_metrics.MetricValueList{ | ||
Items: res, | ||
}, nil | ||
} | ||
|
||
func (p *yourProvider) ListAllMetrics() []provider.CustomMetricInfo { | ||
return []provider.CustomMetricInfo{ | ||
// these are mostly arbitrary examples | ||
{ | ||
GroupResource: schema.GroupResource{Group: "", Resource: "pods"}, | ||
Metric: "packets-per-second", | ||
Namespaced: true, | ||
}, | ||
{ | ||
GroupResource: schema.GroupResource{Group: "", Resource: "services"}, | ||
Metric: "connections-per-second", | ||
Namespaced: true, | ||
}, | ||
{ | ||
GroupResource: schema.GroupResource{Group: "", Resource: "namespaces"}, | ||
Metric: "work-queue-length", | ||
Namespaced: false, | ||
}, | ||
} | ||
} |
Oops, something went wrong.