@@ -39,7 +39,7 @@ Put your provider in the `pkg/provider` directory in your repository.
39
39
40
40
<summary >To get started, you' ; ll need some imports:</summary >
41
41
42
- ``` go
42
+ ``` go mdox-exec="go run ./hack/snippets -d package,import docs/sample-adapter/pkg/provider/provider.go"
43
43
package provider
44
44
45
45
import (
@@ -84,7 +84,7 @@ For this walkthrough, you can just return a few statically-named metrics,
84
84
two that are namespaced, and one that's on namespaces themselves, and thus
85
85
root-scoped:
86
86
87
- ``` go
87
+ ``` go mdox-exec="go run ./hack/snippets -d func=*yourProvider.ListAllMetrics docs/sample-adapter/pkg/provider/provider.go"
88
88
func (p *yourProvider ) ListAllMetrics () []provider .CustomMetricInfo {
89
89
return []provider.CustomMetricInfo {
90
90
// these are mostly arbitrary examples
@@ -188,7 +188,7 @@ You'll need a handle to a RESTMapper (to map between resources and kinds)
188
188
and dynamic client to fetch lists of objects in the cluster, if you don't
189
189
already have sufficient information in your metrics pipeline:
190
190
191
- ``` go
191
+ ``` go mdox-exec="go run ./hack/snippets -d type=yourProvider,func=NewProvider docs/sample-adapter/pkg/provider/provider.go"
192
192
type yourProvider struct {
193
193
client dynamic.Interface
194
194
mapper apimeta.RESTMapper
@@ -214,7 +214,7 @@ backend in these methods.
214
214
First, a couple of helpers, which support doing the fake "fetch"
215
215
operation, and constructing a result object:
216
216
217
- ``` go
217
+ ``` go mdox-exec="go run ./hack/snippets -d func=*yourProvider.valueFor,func=*yourProvider.metricFor docs/sample-adapter/pkg/provider/provider.go"
218
218
// valueFor fetches a value from the fake list and increments it.
219
219
func (p *yourProvider ) valueFor (info provider .CustomMetricInfo ) (int64 , error ) {
220
220
// normalize the value so that you treat plural resources and singular
@@ -225,7 +225,7 @@ func (p *yourProvider) valueFor(info provider.CustomMetricInfo) (int64, error) {
225
225
}
226
226
227
227
value := p.values [info]
228
- value += 1
228
+ value++
229
229
p.values [info] = value
230
230
231
231
return value, nil
@@ -245,7 +245,7 @@ func (p *yourProvider) metricFor(value int64, name types.NamespacedName, info pr
245
245
Name: info.Metric ,
246
246
},
247
247
// you'll want to use the actual timestamp in a real adapter
248
- Timestamp: metav1.Time {time.Now ()},
248
+ Timestamp: metav1.Time {Time: time.Now ()},
249
249
Value: *resource.NewMilliQuantity (value*100 , resource.DecimalSI ),
250
250
}, nil
251
251
}
@@ -255,7 +255,7 @@ Then, you'll need to implement the two main methods. The first fetches
255
255
a single metric value for one object (for example, for the ` object ` metric
256
256
type in the HorizontalPodAutoscaler):
257
257
258
- ``` go
258
+ ``` go mdox-exec="go run ./hack/snippets -d func=*yourProvider.GetMetricByName docs/sample-adapter/pkg/provider/provider.go"
259
259
func (p *yourProvider ) GetMetricByName (ctx context .Context , name types .NamespacedName , info provider .CustomMetricInfo , metricSelector labels .Selector ) (*custom_metrics .MetricValue , error ) {
260
260
value , err := p.valueFor (info)
261
261
if err != nil {
@@ -268,7 +268,7 @@ func (p *yourProvider) GetMetricByName(ctx context.Context, name types.Namespace
268
268
The second fetches multiple metric values, one for each object in a set
269
269
(for example, for the ` pods ` metric type in the HorizontalPodAutoscaler).
270
270
271
- ``` go
271
+ ``` go mdox-exec="go run ./hack/snippets -d func=*yourProvider.GetMetricBySelector docs/sample-adapter/pkg/provider/provider.go"
272
272
func (p *yourProvider ) GetMetricBySelector (ctx context .Context , namespace string , selector labels .Selector , info provider .CustomMetricInfo , metricSelector labels .Selector ) (*custom_metrics .MetricValueList , error ) {
273
273
totalValue , err := p.valueFor (info)
274
274
if err != nil {
@@ -309,11 +309,10 @@ the metrics provided by your provider.
309
309
310
310
<summary >First, you' ; ll need a few imports:</summary >
311
311
312
- ``` go
312
+ ``` go mdox-exec="go run ./hack/snippets -d package,import docs/sample-adapter/main.go"
313
313
package main
314
314
315
315
import (
316
- " flag"
317
316
" os"
318
317
319
318
" k8s.io/apimachinery/pkg/util/wait"
@@ -324,7 +323,7 @@ import (
324
323
" sigs.k8s.io/custom-metrics-apiserver/pkg/provider"
325
324
326
325
// make this the path to the provider that you just wrote
327
- yourprov " example.com/youradapter /pkg/provider"
326
+ yourprov " sigs.k8s.io/custom-metrics-apiserver/docs/sample-adapter /pkg/provider"
328
327
)
329
328
```
330
329
@@ -333,7 +332,7 @@ import (
333
332
With those out of the way, you can make use of the ` basecmd.AdapterBase `
334
333
struct to help set up the API server:
335
334
336
- ``` go
335
+ ``` go mdox-exec="go run ./hack/snippets -d type=YourAdapter,func=main docs/sample-adapter/main.go"
337
336
type YourAdapter struct {
338
337
basecmd.AdapterBase
339
338
@@ -348,10 +347,10 @@ func main() {
348
347
// initialize the flags, with one custom flag for the message
349
348
cmd := &YourAdapter{}
350
349
cmd.Flags ().StringVar (&cmd.Message , " msg" , " starting adapter..." , " startup message" )
351
- // make sure you get the klog flags
352
- logs. AddGoFlags (flag. CommandLine )
353
- cmd. Flags (). AddGoFlagSet (flag. CommandLine )
354
- cmd. Flags (). Parse (os. Args )
350
+ logs. AddFlags (cmd. Flags ())
351
+ if err := cmd. Flags (). Parse (os. Args ); err != nil {
352
+ klog. Fatalf ( " unable to parse flags: %v " , err )
353
+ }
355
354
356
355
provider := cmd.makeProviderOrDie ()
357
356
cmd.WithCustomMetrics (provider)
@@ -372,7 +371,7 @@ you might need to pass configuration for connecting to the backing metrics
372
371
solution, extra credentials, or advanced configuration. For the provider
373
372
you wrote above, the setup code looks something like this:
374
373
375
- ``` go
374
+ ``` go mdox-exec="go run ./hack/snippets -d func=*YourAdapter.makeProviderOrDie docs/sample-adapter/main.go"
376
375
func (a *YourAdapter ) makeProviderOrDie () provider .CustomMetricsProvider {
377
376
client , err := a.DynamicClient ()
378
377
if err != nil {
0 commit comments