From 4099dcf8810654a15ae5bfd0b521e43e58e2f28a Mon Sep 17 00:00:00 2001 From: Christian Schlotter Date: Mon, 30 Jan 2023 16:35:07 +0100 Subject: [PATCH] Implement metric-gen tool Implements the metric-gen tool which could get used to create custom resource configurations directly from code, similar to what controller-gen does. --- exp/metric-gen/go.mod | 74 +++++ exp/metric-gen/go.sum | 204 ++++++++++++++ exp/metric-gen/main.go | 123 ++++++++ exp/metric-gen/metric/generator.go | 132 +++++++++ exp/metric-gen/metric/marker_gauge.go | 105 +++++++ exp/metric-gen/metric/marker_info.go | 98 +++++++ exp/metric-gen/metric/marker_stateset.go | 97 +++++++ exp/metric-gen/metric/markers.go | 184 ++++++++++++ exp/metric-gen/metric/parser.go | 262 ++++++++++++++++++ pkg/customresourcestate/config.go | 12 +- .../config_metrics_types.go | 2 +- 11 files changed, 1286 insertions(+), 7 deletions(-) create mode 100644 exp/metric-gen/go.mod create mode 100644 exp/metric-gen/go.sum create mode 100644 exp/metric-gen/main.go create mode 100644 exp/metric-gen/metric/generator.go create mode 100644 exp/metric-gen/metric/marker_gauge.go create mode 100644 exp/metric-gen/metric/marker_info.go create mode 100644 exp/metric-gen/metric/marker_stateset.go create mode 100644 exp/metric-gen/metric/markers.go create mode 100644 exp/metric-gen/metric/parser.go diff --git a/exp/metric-gen/go.mod b/exp/metric-gen/go.mod new file mode 100644 index 0000000000..11c0d0fe3f --- /dev/null +++ b/exp/metric-gen/go.mod @@ -0,0 +1,74 @@ +module k8s.io/kube-state-metrics/exp/metric-gen + +go 1.19 + +replace k8s.io/kube-state-metrics/v2 => ../.. + +require ( + github.com/spf13/pflag v1.0.5 + k8s.io/apimachinery v0.28.0 + k8s.io/client-go v0.28.0 + k8s.io/klog/v2 v2.100.1 + k8s.io/kube-state-metrics/v2 v2.0.0-00010101000000-000000000000 + k8s.io/utils v0.0.0-20230711102312-30195339c3c7 + sigs.k8s.io/controller-tools v0.13.0 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/blang/semver/v4 v4.0.0 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 // indirect + github.com/emicklei/go-restful/v3 v3.9.0 // indirect + github.com/fatih/color v1.15.0 // indirect + github.com/go-logr/logr v1.2.4 // indirect + github.com/go-openapi/jsonpointer v0.19.6 // indirect + github.com/go-openapi/jsonreference v0.20.2 // indirect + github.com/go-openapi/swag v0.22.3 // indirect + github.com/gobuffalo/flect v1.0.2 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/google/gofuzz v1.2.0 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/imdario/mergo v0.3.6 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/prometheus/client_golang v1.16.0 // indirect + github.com/prometheus/client_model v0.4.0 // indirect + github.com/prometheus/common v0.44.0 // indirect + github.com/prometheus/procfs v0.10.1 // indirect + github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/spf13/cobra v1.7.0 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.14.0 // indirect + golang.org/x/oauth2 v0.8.0 // indirect + golang.org/x/sys v0.11.0 // indirect + golang.org/x/term v0.11.0 // indirect + golang.org/x/text v0.12.0 // indirect + golang.org/x/time v0.3.0 // indirect + golang.org/x/tools v0.12.0 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/protobuf v1.30.0 // indirect + gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + k8s.io/api v0.28.0 // indirect + k8s.io/apiextensions-apiserver v0.28.0 // indirect + k8s.io/component-base v0.28.0 // indirect + k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect + k8s.io/sample-controller v0.27.4 // indirect + sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect + sigs.k8s.io/yaml v1.3.0 // indirect +) diff --git a/exp/metric-gen/go.sum b/exp/metric-gen/go.sum new file mode 100644 index 0000000000..47c8283659 --- /dev/null +++ b/exp/metric-gen/go.sum @@ -0,0 +1,204 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= +github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 h1:0wH6nO9QEa02Qx8sIQGw6ieKdz+BXjpccSOo9vXNl4U= +github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0/go.mod h1:4hKCXuwrJoYvHZxJ86+bRVTOMyJ0Ej+RqfSm8mHi6KA= +github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= +github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= +github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= +github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE= +github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= +github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE= +github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k= +github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= +github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA= +github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I= +github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28= +github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= +github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE= +github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= +github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= +github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= +github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= +github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY= +github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= +github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= +github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= +golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8= +golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0= +golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= +golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss= +golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= +gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +k8s.io/api v0.28.0 h1:3j3VPWmN9tTDI68NETBWlDiA9qOiGJ7sdKeufehBYsM= +k8s.io/api v0.28.0/go.mod h1:0l8NZJzB0i/etuWnIXcwfIv+xnDOhL3lLW919AWYDuY= +k8s.io/apiextensions-apiserver v0.28.0 h1:CszgmBL8CizEnj4sj7/PtLGey6Na3YgWyGCPONv7E9E= +k8s.io/apiextensions-apiserver v0.28.0/go.mod h1:uRdYiwIuu0SyqJKriKmqEN2jThIJPhVmOWETm8ud1VE= +k8s.io/apimachinery v0.28.0 h1:ScHS2AG16UlYWk63r46oU3D5y54T53cVI5mMJwwqFNA= +k8s.io/apimachinery v0.28.0/go.mod h1:X0xh/chESs2hP9koe+SdIAcXWcQ+RM5hy0ZynB+yEvw= +k8s.io/client-go v0.28.0 h1:ebcPRDZsCjpj62+cMk1eGNX1QkMdRmQ6lmz5BLoFWeM= +k8s.io/client-go v0.28.0/go.mod h1:0Asy9Xt3U98RypWJmU1ZrRAGKhP6NqDPmptlAzK2kMc= +k8s.io/component-base v0.28.0 h1:HQKy1enJrOeJlTlN4a6dU09wtmXaUvThC0irImfqyxI= +k8s.io/component-base v0.28.0/go.mod h1:Yyf3+ZypLfMydVzuLBqJ5V7Kx6WwDr/5cN+dFjw1FNk= +k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= +k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= +k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ= +k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM= +k8s.io/sample-controller v0.27.4 h1:oeKVVOWw+fVaZVWCw3l9myrAoNUqR8A4BcA91fyyZaU= +k8s.io/sample-controller v0.27.4/go.mod h1:bfGT2cEyzWjfCqVSrWPbNsym9MkgKz7Z7Bwl0tjysOo= +k8s.io/utils v0.0.0-20230711102312-30195339c3c7 h1:ZgnF1KZsYxWIifwSNZFZgNtWE89WI5yiP5WwlfDoIyc= +k8s.io/utils v0.0.0-20230711102312-30195339c3c7/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +sigs.k8s.io/controller-tools v0.13.0 h1:NfrvuZ4bxyolhDBt/rCZhDnx3M2hzlhgo5n3Iv2RykI= +sigs.k8s.io/controller-tools v0.13.0/go.mod h1:5vw3En2NazbejQGCeWKRrE7q4P+CW8/klfVqP8QZkgA= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= +sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= +sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE= +sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= +sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8= diff --git a/exp/metric-gen/main.go b/exp/metric-gen/main.go new file mode 100644 index 0000000000..ec6264d922 --- /dev/null +++ b/exp/metric-gen/main.go @@ -0,0 +1,123 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 ( + "fmt" + "os" + + "github.com/spf13/pflag" + "k8s.io/kube-state-metrics/exp/metric-gen/metric" + "sigs.k8s.io/controller-tools/pkg/genall" + "sigs.k8s.io/controller-tools/pkg/genall/help" + prettyhelp "sigs.k8s.io/controller-tools/pkg/genall/help/pretty" + "sigs.k8s.io/controller-tools/pkg/loader" + "sigs.k8s.io/controller-tools/pkg/markers" +) + +const ( + generatorName = "metric" +) + +var ( + // optionsRegistry contains all the marker definitions used to process command line options + optionsRegistry = &markers.Registry{} +) + +func main() { + var whichMarkersFlag bool + + pflag.CommandLine.BoolVarP(&whichMarkersFlag, "which-markers", "w", false, "print out all markers available with the requested generators") + + pflag.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0]) + fmt.Fprintf(os.Stderr, " metric-gen [flags] /path/to/package [/path/to/package]\n\n") + fmt.Fprintf(os.Stderr, "Flags:\n") + pflag.PrintDefaults() + fmt.Fprintf(os.Stderr, "\n") + } + + pflag.Parse() + + // Register the metric generator itself as marker so genall.FromOptions is able to initialize the runtime properly. + // This also registers the markers inside the optionsRegistry so its available to print the marker docs. + metricGenerator := metric.Generator{} + defn := markers.Must(markers.MakeDefinition(generatorName, markers.DescribesPackage, metricGenerator)) + if err := optionsRegistry.Register(defn); err != nil { + panic(err) + } + + if whichMarkersFlag { + printMarkerDocs() + return + } + + // Check if package paths got passed as input parameters. + if len(os.Args[1:]) == 0 { + fmt.Fprint(os.Stderr, "error: Please provide package paths as parameters\n\n") + pflag.Usage() + os.Exit(1) + } + + // Load the passed packages as roots. + roots, err := loader.LoadRoots(os.Args[1:]...) + if err != nil { + fmt.Fprint(os.Stderr, fmt.Sprintf("error: loading packages %v\n", err)) + os.Exit(1) + } + + // Set up the generator runtime using controller-tools and passing our optionsRegistry. + rt, err := genall.FromOptions(optionsRegistry, []string{generatorName}) + if err != nil { + fmt.Fprint(os.Stderr, fmt.Sprintf("error: %v\n", err)) + os.Exit(1) + } + + // Setup the generation context with the loaded roots. + rt.GenerationContext.Roots = roots + // Setup the runtime to output to stdout. + rt.OutputRules = genall.OutputRules{Default: genall.OutputToStdout} + + // Run the generator using the runtime. + if hadErrs := rt.Run(); hadErrs { + fmt.Fprint(os.Stderr, "generator did not run successfully\n") + os.Exit(1) + } +} + +// printMarkerDocs prints out marker help for the given generators specified in +// the rawOptions +func printMarkerDocs() error { + // just grab a registry so we don't lag while trying to load roots + // (like we'd do if we just constructed the full runtime). + reg, err := genall.RegistryFromOptions(optionsRegistry, []string{generatorName}) + if err != nil { + return err + } + + helpInfo := help.ByCategory(reg, help.SortByCategory) + + for _, cat := range helpInfo { + if cat.Category == "" { + continue + } + contents := prettyhelp.MarkersDetails(false, cat.Category, cat.Markers) + if err := contents.WriteTo(os.Stderr); err != nil { + return err + } + } + return nil +} diff --git a/exp/metric-gen/metric/generator.go b/exp/metric-gen/metric/generator.go new file mode 100644 index 0000000000..2d438aa9e3 --- /dev/null +++ b/exp/metric-gen/metric/generator.go @@ -0,0 +1,132 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 metric + +import ( + "fmt" + "sort" + + "k8s.io/klog/v2" + "sigs.k8s.io/controller-tools/pkg/crd" + "sigs.k8s.io/controller-tools/pkg/genall" + "sigs.k8s.io/controller-tools/pkg/loader" + "sigs.k8s.io/controller-tools/pkg/markers" + + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" +) + +type Generator struct{} + +func (Generator) CheckFilter() loader.NodeFilter { + // Re-use controller-tools filter to filter out unrelated nodes that aren't used + // in CRD generation, like interfaces and struct fields without JSON tag. + return crd.Generator{}.CheckFilter() +} + +func (g Generator) Generate(ctx *genall.GenerationContext) error { + // Create the parser which is specific to the metric generator. + parser := newParser( + &crd.Parser{ + Collector: ctx.Collector, + Checker: ctx.Checker, + }, + ) + + // Loop over all passed packages. + for _, root := range ctx.Roots { + // skip packages which don't import metav1 because they can't define a CRD without meta v1. + metav1 := root.Imports()["k8s.io/apimachinery/pkg/apis/meta/v1"] + if metav1 == nil { + continue + } + + // parse the given package to feed crd.FindKubeKinds to find CRD objects. + parser.NeedPackage(root) + kubeKinds := crd.FindKubeKinds(parser.Parser, metav1) + if len(kubeKinds) == 0 { + klog.Fatalf("no objects in the roots") + } + + for _, gv := range kubeKinds { + // Create customresourcestate.Resource for each CRD which contains all metric + // definitions for the CRD. + parser.NeedResourceFor(gv) + } + } + + // Build customresourcestate configuration file from generated data. + metrics := customresourcestate.Metrics{ + Spec: customresourcestate.MetricsSpec{ + Resources: []customresourcestate.Resource{}, + }, + } + + // Sort the resources to get a deterministic output. + + for _, resource := range parser.CustomResourceStates { + if len(resource.Metrics) > 0 { + // sort the metrics + sort.Slice(resource.Metrics, func(i, j int) bool { + return resource.Metrics[i].Name < resource.Metrics[j].Name + }) + + metrics.Spec.Resources = append(metrics.Spec.Resources, resource) + } + } + + sort.Slice(metrics.Spec.Resources, func(i, j int) bool { + if metrics.Spec.Resources[i].MetricNamePrefix == nil && metrics.Spec.Resources[j].MetricNamePrefix == nil { + a := metrics.Spec.Resources[i].GroupVersionKind.Group + "/" + metrics.Spec.Resources[i].GroupVersionKind.Version + "/" + metrics.Spec.Resources[i].GroupVersionKind.Kind + b := metrics.Spec.Resources[j].GroupVersionKind.Group + "/" + metrics.Spec.Resources[j].GroupVersionKind.Version + "/" + metrics.Spec.Resources[j].GroupVersionKind.Kind + return a < b + } + + // Either a or b will not be the empty string, so we can compare them. + var a, b string + if metrics.Spec.Resources[i].MetricNamePrefix == nil { + a = *metrics.Spec.Resources[i].MetricNamePrefix + } + if metrics.Spec.Resources[j].MetricNamePrefix != nil { + b = *metrics.Spec.Resources[j].MetricNamePrefix + } + return a < b + }) + + // Write the rendered yaml to the context which will result in stdout. + filePath := "metrics.yaml" + if err := ctx.WriteYAML(filePath, "", []interface{}{metrics}, genall.WithTransform(addCustomResourceStateKind)); err != nil { + return fmt.Errorf("WriteYAML to %s: %w", filePath, err) + } + + return nil +} + +// addCustomResourceStateKind adds the correct kind because we don't have a correct +// kubernetes-style object as configuration definition. +func addCustomResourceStateKind(obj map[string]interface{}) error { + obj["kind"] = "CustomResourceStateMetrics" + return nil +} + +func (g Generator) RegisterMarkers(into *markers.Registry) error { + for _, m := range markerDefinitions { + if err := m.Register(into); err != nil { + return err + } + } + + return nil +} diff --git a/exp/metric-gen/metric/marker_gauge.go b/exp/metric-gen/metric/marker_gauge.go new file mode 100644 index 0000000000..6cb7f3675a --- /dev/null +++ b/exp/metric-gen/metric/marker_gauge.go @@ -0,0 +1,105 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 metric + +import ( + "sigs.k8s.io/controller-tools/pkg/markers" + + "k8s.io/klog/v2" + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" +) + +const ( + // GaugeMarkerName is a marker for defining metric definitions. + GaugeMarkerName = "Metrics:gauge" +) + +func init() { + markerDefinitions = append( + markerDefinitions, + must(markers.MakeDefinition(GaugeMarkerName, markers.DescribesField, GaugeMarker{})). + help(GaugeMarker{}.help()), + must(markers.MakeDefinition(GaugeMarkerName, markers.DescribesType, GaugeMarker{})). + help(GaugeMarker{}.help()), + ) +} + +type GaugeMarker struct { + Name string + Help string `marker:"help,optional"` + JSONPath JSONPath `marker:"JSONPath,optional"` + LabelFromKey string `marker:"labelFromKey,optional"` + LabelsFromPath map[string]JSONPath `marker:"labelsFromPath,optional"` + NilIsZero bool `marker:"nilIsZero,optional"` + ValueFrom *JSONPath `marker:"valueFrom,optional"` +} + +func (GaugeMarker) help() *markers.DefinitionHelp { + return &markers.DefinitionHelp{ + Category: "Metrics", + DetailedHelp: markers.DetailedHelp{ + Summary: "Defines a Gauge metric and uses the implicit path to the field joined by the provided JSONPath as path for the metric configuration.", + Details: "", + }, + FieldHelp: map[string]markers.DetailedHelp{}, + } +} + +func (g GaugeMarker) ToGenerator(basePath ...string) *customresourcestate.Generator { + additionalPath, err := g.JSONPath.Parse() + if err != nil { + klog.Fatal(err) + } + var valueFrom []string + if g.ValueFrom != nil { + valueFrom, err = g.ValueFrom.Parse() + if err != nil { + klog.Fatal(err) + } + } + + labelsFromPath := map[string][]string{} + for k, v := range g.LabelsFromPath { + path := []string{} + var err error + if v != "." { + path, err = v.Parse() + if err != nil { + klog.Fatal(err) + } + } + labelsFromPath[k] = path + } + + path := append(basePath, additionalPath...) + + return &customresourcestate.Generator{ + Name: g.Name, + Help: g.Help, + Each: customresourcestate.Metric{ + Type: customresourcestate.MetricTypeGauge, + Gauge: &customresourcestate.MetricGauge{ + NilIsZero: g.NilIsZero, + MetricMeta: customresourcestate.MetricMeta{ + Path: path, + LabelsFromPath: labelsFromPath, + }, + LabelFromKey: g.LabelFromKey, + ValueFrom: valueFrom, + }, + }, + } +} diff --git a/exp/metric-gen/metric/marker_info.go b/exp/metric-gen/metric/marker_info.go new file mode 100644 index 0000000000..f302e488e1 --- /dev/null +++ b/exp/metric-gen/metric/marker_info.go @@ -0,0 +1,98 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 metric + +import ( + "sigs.k8s.io/controller-tools/pkg/markers" + + "k8s.io/klog/v2" + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" +) + +const ( + // InfoMarkerName is a marker for defining metric definitions. + InfoMarkerName = "Metrics:info" +) + +func init() { + markerDefinitions = append( + markerDefinitions, + must(markers.MakeDefinition(InfoMarkerName, markers.DescribesField, InfoMarker{})). + help(InfoMarker{}.help()), + must(markers.MakeDefinition(InfoMarkerName, markers.DescribesType, InfoMarker{})). + help(InfoMarker{}.help()), + ) +} + +type InfoMarker struct { + Name string + Help string `marker:"help,optional"` + LabelsFromPath map[string]JSONPath `marker:"labelsFromPath,optional"` + JSONPath JSONPath `marker:"JSONPath,optional"` + LabelFromKey string `marker:"labelFromKey,optional"` +} + +func (InfoMarker) help() *markers.DefinitionHelp { + return &markers.DefinitionHelp{ + Category: "Metrics", + DetailedHelp: markers.DetailedHelp{ + Summary: "Defines a Info metric and uses the implicit path to the field as path for the metric configuration.", + Details: "", + }, + FieldHelp: map[string]markers.DetailedHelp{}, + } +} + +func (i InfoMarker) ToGenerator(basePath ...string) *customresourcestate.Generator { + path := basePath + if i.JSONPath != "" { + valueFrom, err := i.JSONPath.Parse() + if err != nil { + klog.Fatal(err) + } + if len(valueFrom) > 0 { + path = append(path, valueFrom...) + } + } + + labelsFromPath := map[string][]string{} + for k, v := range i.LabelsFromPath { + path := []string{} + var err error + if v != "." { + path, err = v.Parse() + if err != nil { + klog.Fatal(err) + } + } + labelsFromPath[k] = path + } + + return &customresourcestate.Generator{ + Name: i.Name, + Help: i.Help, + Each: customresourcestate.Metric{ + Type: customresourcestate.MetricTypeInfo, + Info: &customresourcestate.MetricInfo{ + MetricMeta: customresourcestate.MetricMeta{ + Path: path, + LabelsFromPath: labelsFromPath, + }, + LabelFromKey: i.LabelFromKey, + }, + }, + } +} diff --git a/exp/metric-gen/metric/marker_stateset.go b/exp/metric-gen/metric/marker_stateset.go new file mode 100644 index 0000000000..b1110a378b --- /dev/null +++ b/exp/metric-gen/metric/marker_stateset.go @@ -0,0 +1,97 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 metric + +import ( + "sigs.k8s.io/controller-tools/pkg/markers" + + "k8s.io/klog/v2" + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" +) + +const ( + // StateSetMarkerName is a marker for defining metric definitions. + StateSetMarkerName = "Metrics:stateset" +) + +func init() { + markerDefinitions = append( + markerDefinitions, + must(markers.MakeDefinition(StateSetMarkerName, markers.DescribesField, StateSetMarker{})). + help(StateSetMarker{}.help()), + must(markers.MakeDefinition(StateSetMarkerName, markers.DescribesType, StateSetMarker{})). + help(StateSetMarker{}.help()), + ) +} + +type StateSetMarker struct { + Name string + Help string `marker:"help,optional"` + LabelsFromPath map[string]JSONPath `marker:"labelsFromPath,optional"` + JSONPath *JSONPath `marker:"JSONPath,optional"` + LabelName string `marker:"labelName,optional"` + List []string `marker:"list"` +} + +func (StateSetMarker) help() *markers.DefinitionHelp { + return &markers.DefinitionHelp{ + Category: "Metrics", + DetailedHelp: markers.DetailedHelp{ + Summary: "Defines a StateSet metric and uses the implicit path to the field as path for the metric configuration.", + Details: "", + }, + FieldHelp: map[string]markers.DetailedHelp{}, + } +} + +func (s StateSetMarker) ToGenerator(basePath ...string) *customresourcestate.Generator { + path := basePath + + var valueFrom []string + var err error + if s.JSONPath != nil { + valueFrom, err = s.JSONPath.Parse() + if err != nil { + klog.Fatal(err) + } + } + + labelsFromPath := map[string][]string{} + for k, v := range s.LabelsFromPath { + path, err := v.Parse() + if err != nil { + klog.Fatal(err) + } + labelsFromPath[k] = path + } + + return &customresourcestate.Generator{ + Name: s.Name, + Help: s.Help, + Each: customresourcestate.Metric{ + Type: customresourcestate.MetricTypeStateSet, + StateSet: &customresourcestate.MetricStateSet{ + MetricMeta: customresourcestate.MetricMeta{ + Path: path, + LabelsFromPath: labelsFromPath, + }, + List: s.List, + LabelName: s.LabelName, + ValueFrom: valueFrom, + }, + }, + } +} diff --git a/exp/metric-gen/metric/markers.go b/exp/metric-gen/metric/markers.go new file mode 100644 index 0000000000..84f7cf03a1 --- /dev/null +++ b/exp/metric-gen/metric/markers.go @@ -0,0 +1,184 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 metric + +import ( + "fmt" + + "k8s.io/client-go/util/jsonpath" + "k8s.io/utils/pointer" + "sigs.k8s.io/controller-tools/pkg/markers" + + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" +) + +const ( + // NameMarkerName is a marker for defining metric definitions. + NameMarkerName = "Metrics:namePrefix" + // LabelFromPathMarkerName is a marker for defining labels for paths. + LabelFromPathMarkerName = "Metrics:labelFromPath" +) + +var ( + markerDefinitions = []*definitionWithHelp{ + must(markers.MakeDefinition(NameMarkerName, markers.DescribesType, NamePrefixMarker(""))). + help(NamePrefixMarker("").Help()), + must(markers.MakeDefinition(LabelFromPathMarkerName, markers.DescribesType, LabelFromPathMarker{})). + help(LabelFromPathMarker{}.Help()), + must(markers.MakeDefinition(LabelFromPathMarkerName, markers.DescribesField, LabelFromPathMarker{})). + help(LabelFromPathMarker{}.Help()), + // GroupName is a marker copied from controller-runtime to identify the API Group. + // It needs to get added as marker so the parser will be able to read the API + // which is Group set for a package. + must(markers.MakeDefinition("groupName", markers.DescribesPackage, "")), + } +) + +// +controllertools:marker:generateHelp:category=CRD + +// ResourceMarker is a marker that knows how to apply itself to a particular +// version in a CRD Spec. +type ResourceMarker interface { + // ApplyToCRD applies this marker to the given CRD, in the given version + // within that CRD. It's called after everything else in the CRD is populated. + ApplyToResource(resource *customresourcestate.Resource) error +} + +// GeneratorMarker is a marker that knows how to create a generator from itself. +type GeneratorMarker interface { + // ApplyToCRD applies this marker to the given CRD, in the given version + // within that CRD. It's called after everything else in the CRD is populated. + ToGenerator(basePath ...string) *customresourcestate.Generator +} + +type NamePrefixMarker string + +func (NamePrefixMarker) Help() *markers.DefinitionHelp { + return &markers.DefinitionHelp{ + Category: "Metrics", + DetailedHelp: markers.DetailedHelp{ + Summary: "enables the creation of a customresourcestate Resource for the CRD and uses the given prefix for the metrics.", + Details: "", + }, + FieldHelp: map[string]markers.DetailedHelp{}, + } +} + +func (n NamePrefixMarker) ApplyToResource(resource *customresourcestate.Resource) error { + resource.MetricNamePrefix = pointer.String(string(n)) + return nil +} + +type LabelFromPathMarker struct { + // +Metrics:labelFromPath:name=,JSONPath= on API type struct + Name string + JSONPath JSONPath `marker:"JSONPath"` +} + +type JSONPath string + +func (j JSONPath) Parse() ([]string, error) { + ret := []string{} + + jp, err := jsonpath.Parse("foo", `{`+string(j)+`}`) + if err != nil { + return nil, fmt.Errorf("parse JSONPath: %w", err) + } + + if len(jp.Root.Nodes) > 1 { + return nil, fmt.Errorf("expected a single JSONPath, got %d", len(jp.Root.Nodes)) + } + + switch jp.Root.Nodes[0].Type() { + case jsonpath.NodeList: + list, ok := jp.Root.Nodes[0].(*jsonpath.ListNode) + if !ok { + return nil, fmt.Errorf("unable to typecast to jsonpath.ListNode") + } + for _, n := range list.Nodes { + nf, ok := n.(*jsonpath.FieldNode) + if !ok { + return nil, fmt.Errorf("unable to typecast to jsonpath.NodeField") + } + ret = append(ret, nf.Value) + } + default: + return nil, fmt.Errorf("unexcepted jsonpath node type: %q", jp.Root.Nodes[0].Type()) + } + + return ret, nil +} + +func (LabelFromPathMarker) Help() *markers.DefinitionHelp { + return &markers.DefinitionHelp{ + Category: "Metrics", + DetailedHelp: markers.DetailedHelp{ + Summary: "adds an additional label to all metrics of this field or type with a value from the given JSONPath.", + Details: "", + }, + FieldHelp: map[string]markers.DetailedHelp{}, + } +} + +func (n LabelFromPathMarker) ApplyToResource(resource *customresourcestate.Resource) error { + if resource.LabelsFromPath == nil { + resource.LabelsFromPath = map[string][]string{} + } + jsonPathElems, err := n.JSONPath.Parse() + if err != nil { + return err + } + + if jsonPath, labelExists := resource.LabelsFromPath[n.Name]; labelExists { + if len(jsonPathElems) != len(jsonPath) { + return fmt.Errorf("duplicate definition for label %q", n.Name) + } + for i, v := range jsonPath { + if v != jsonPathElems[i] { + return fmt.Errorf("duplicate definition for label %q", n.Name) + } + } + } + + resource.LabelsFromPath[n.Name] = jsonPathElems + return nil +} + +type definitionWithHelp struct { + *markers.Definition + Help *markers.DefinitionHelp +} + +func must(def *markers.Definition, err error) *definitionWithHelp { + return &definitionWithHelp{ + Definition: markers.Must(def, err), + } +} + +func (d *definitionWithHelp) help(help *markers.DefinitionHelp) *definitionWithHelp { + d.Help = help + return d +} + +func (d *definitionWithHelp) Register(reg *markers.Registry) error { + if err := reg.Register(d.Definition); err != nil { + return err + } + if d.Help != nil { + reg.AddHelp(d.Definition, d.Help) + } + return nil +} diff --git a/exp/metric-gen/metric/parser.go b/exp/metric-gen/metric/parser.go new file mode 100644 index 0000000000..4c03f6592e --- /dev/null +++ b/exp/metric-gen/metric/parser.go @@ -0,0 +1,262 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +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 metric + +import ( + "fmt" + "go/ast" + "go/types" + "sort" + "strings" + + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/klog/v2" + "sigs.k8s.io/controller-tools/pkg/crd" + "sigs.k8s.io/controller-tools/pkg/loader" + "sigs.k8s.io/controller-tools/pkg/markers" + + "k8s.io/kube-state-metrics/v2/pkg/customresourcestate" +) + +type Parser struct { + *crd.Parser + + CustomResourceStates map[schema.GroupKind]customresourcestate.Resource + FlattenedMetrics map[crd.TypeIdent][]customresourcestate.Metric +} + +func newParser(parser *crd.Parser) *Parser { + return &Parser{ + Parser: parser, + CustomResourceStates: make(map[schema.GroupKind]customresourcestate.Resource), + FlattenedMetrics: make(map[crd.TypeIdent][]customresourcestate.Metric), + } +} + +func (p *Parser) NeedResourceFor(groupKind schema.GroupKind) { + if _, exists := p.CustomResourceStates[groupKind]; exists { + return + } + + var packages []*loader.Package + for pkg, gv := range p.GroupVersions { + if gv.Group != groupKind.Group { + continue + } + packages = append(packages, pkg) + } + + resource := customresourcestate.Resource{ + GroupVersionKind: customresourcestate.GroupVersionKind{ + Group: groupKind.Group, + Kind: groupKind.Kind, + Version: "", // TODO + }, + } + + for _, pkg := range packages { + typeIdent := crd.TypeIdent{Package: pkg, Name: groupKind.Kind} + typeInfo := p.Types[typeIdent] + if typeInfo == nil { + continue + } + + // Skip if namePrefix marker is not set to not create configuration for CRs used in other CRs. + // e.g. to not create configuration for KubeadmControlPlaneTemplate. + if m := typeInfo.Markers.Get(NameMarkerName); m == nil { + continue + } + + resource.Metrics = p.NeedMetricsGeneratorFor(typeIdent) + + sort.Slice(resource.Metrics, func(i, j int) bool { + return resource.Metrics[i].Name < resource.Metrics[j].Name + }) + + if resource.GroupVersionKind.Version != "" { + klog.Fatal("GroupVersionKind.Version is already set", "resource", resource) + } + resource.GroupVersionKind.Version = p.GroupVersions[pkg].Version + } + + for _, pkg := range packages { + typeIdent := crd.TypeIdent{Package: pkg, Name: groupKind.Kind} + typeInfo := p.Types[typeIdent] + if typeInfo == nil { + continue + } + + for _, markerVals := range typeInfo.Markers { + for _, val := range markerVals { + if resourceMarker, isResourceMarker := val.(ResourceMarker); isResourceMarker { + if err := resourceMarker.ApplyToResource(&resource); err != nil { + pkg.AddError(loader.ErrFromNode(err /* an okay guess */, typeInfo.RawSpec)) + } + } + } + } + } + + p.CustomResourceStates[groupKind] = resource +} + +type generatorRequester interface { + NeedMetricsGeneratorFor(typ crd.TypeIdent) []customresourcestate.Generator +} + +// generatorContext stores and provides information across a hierarchy of metric generators generation. +type generatorContext struct { + pkg *loader.Package + info *markers.TypeInfo + generatorRequester generatorRequester + + PackageMarkers markers.MarkerValues +} + +func newGeneratorContext(pkg *loader.Package, req generatorRequester) *generatorContext { + pkg.NeedTypesInfo() + return &generatorContext{ + pkg: pkg, + generatorRequester: req, + } +} + +// requestGenerator asks for the generator for a type in the package with the +// given import path. +func (c *generatorContext) requestGenerator(pkgPath, typeName string) []customresourcestate.Generator { + pkg := c.pkg + if pkgPath != "" { + pkg = c.pkg.Imports()[pkgPath] + } + return c.generatorRequester.NeedMetricsGeneratorFor(crd.TypeIdent{ + Package: pkg, + Name: typeName, + }) +} + +func generatorsFromMarkers(m markers.MarkerValues, basePath ...string) []customresourcestate.Generator { + generators := []customresourcestate.Generator{} + + for _, markerVals := range m { + for _, val := range markerVals { + if generatorMarker, isGeneratorMarker := val.(GeneratorMarker); isGeneratorMarker { + if g := generatorMarker.ToGenerator(basePath...); g != nil { + generators = append(generators, *g) + } + } + } + } + + return generators +} + +func (p *Parser) NeedMetricsGeneratorFor(typ crd.TypeIdent) []customresourcestate.Generator { + if _, knownMetrics := p.FlattenedMetrics[typ]; knownMetrics { + return nil + } + + info, gotInfo := p.Types[typ] + if !gotInfo { + klog.Fatal("expected to get info for %v but does not exist", typ) + } + + generators := generatorsFromMarkers(info.Markers) + for _, f := range info.Fields { + jsonTag, hasTag := f.Tag.Lookup("json") + if !hasTag { + // if the field doesn't have a JSON tag, it doesn't belong in output (and shouldn't exist in a serialized type) + continue + } + jsonOpts := strings.Split(jsonTag, ",") + if len(jsonOpts) == 1 && jsonOpts[0] == "-" { + // skipped fields have the tag "-" (note that "-," means the field is named "-") + continue + } + + generators = append(generators, generatorsFromMarkers(f.Markers, jsonOpts[0])...) + + generatorCtx := newGeneratorContext(typ.Package, p) + for _, generator := range generatorsFor(generatorCtx, f.RawField.Type) { + generators = append(generators, prependPathOnGenerator(generator, jsonOpts[0])) + } + } + + return generators +} + +func prependPathOnGenerator(generator customresourcestate.Generator, pathPrefix string) customresourcestate.Generator { + switch generator.Each.Type { + case customresourcestate.MetricTypeGauge: + generator.Each.Gauge.MetricMeta.Path = append([]string{pathPrefix}, generator.Each.Gauge.MetricMeta.Path...) + case customresourcestate.MetricTypeStateSet: + generator.Each.StateSet.MetricMeta.Path = append([]string{pathPrefix}, generator.Each.StateSet.MetricMeta.Path...) + case customresourcestate.MetricTypeInfo: + generator.Each.Info.MetricMeta.Path = append([]string{pathPrefix}, generator.Each.Info.MetricMeta.Path...) + } + + return generator +} + +func generatorsFor(ctx *generatorContext, rawType ast.Expr) []customresourcestate.Generator { + switch expr := rawType.(type) { + case *ast.Ident: + return localNamedToGenerators(ctx, expr) + case *ast.SelectorExpr: + // Results in using transitive markers from external packages. + return generatorsFor(ctx, expr.X) + case *ast.ArrayType: + // The current configuration does not allow creating metric configurations inside arrays + return nil + case *ast.MapType: + // The current configuration does not allow creating metric configurations inside maps + return nil + case *ast.StarExpr: + return generatorsFor(ctx, expr.X) + case *ast.StructType: + klog.Fatal(loader.ErrFromNode(fmt.Errorf("unsupported AST kind %T", expr), rawType)) + default: + klog.Fatal(loader.ErrFromNode(fmt.Errorf("unsupported AST kind %T", expr), rawType)) + // NB(directxman12): we explicitly don't handle interfaces + return nil + } + + return nil +} + +func localNamedToGenerators(ctx *generatorContext, ident *ast.Ident) []customresourcestate.Generator { + typeInfo := ctx.pkg.TypesInfo.TypeOf(ident) + if typeInfo == types.Typ[types.Invalid] { + // Expected to hit this error for types from not loaded packages + // TODO(chrischdi): verify + // klog.Warningf("Skipping unknown type: %v", loader.ErrFromNode(fmt.Errorf("unknown type %s", ident.Name), ident)) + return nil + } + + if _, isBasic := typeInfo.(*types.Basic); isBasic { + // There can't be markers for basic go types for this generator. + return nil + } + + // NB(directxman12): if there are dot imports, this might be an external reference, + // so use typechecking info to get the actual object + typeNameInfo := typeInfo.(*types.Named).Obj() + pkg := typeNameInfo.Pkg() + pkgPath := loader.NonVendorPath(pkg.Path()) + if pkg == ctx.pkg.Types { + pkgPath = "" + } + return ctx.requestGenerator(pkgPath, typeNameInfo.Name()) +} diff --git a/pkg/customresourcestate/config.go b/pkg/customresourcestate/config.go index 385858b8ba..f9c25ea8ac 100644 --- a/pkg/customresourcestate/config.go +++ b/pkg/customresourcestate/config.go @@ -100,9 +100,9 @@ func (gvk GroupVersionKind) String() string { // Labels is common configuration of labels to add to metrics. type Labels struct { // CommonLabels are added to all metrics. - CommonLabels map[string]string `yaml:"commonLabels" json:"commonLabels"` + CommonLabels map[string]string `yaml:"commonLabels" json:"commonLabels,omitempty"` // LabelsFromPath adds additional labels where the value is taken from a field in the resource. - LabelsFromPath map[string][]string `yaml:"labelsFromPath" json:"labelsFromPath"` + LabelsFromPath map[string][]string `yaml:"labelsFromPath" json:"labelsFromPath,omitempty"` } // Merge combines the labels from two configs, returning a new config. The other Labels will overwrite keys in this Labels. @@ -140,7 +140,7 @@ type Generator struct { // Labels are added to all metrics. Labels from Each will overwrite these if using the same key. Labels `yaml:",inline" json:",inline"` // json will inline because it is already tagged // ErrorLogV defines the verbosity threshold for errors logged for this metric. Must be non-zero to override the resource setting. - ErrorLogV klog.Level `yaml:"errorLogV" json:"errorLogV"` + ErrorLogV klog.Level `yaml:"errorLogV" json:"errorLogV,omitempty"` } // Metric defines a metric to expose. @@ -152,13 +152,13 @@ type Metric struct { // Gauge defines a gauge metric. // +optional - Gauge *MetricGauge `yaml:"gauge" json:"gauge"` + Gauge *MetricGauge `yaml:"gauge,omitempty" json:"gauge,omitempty"` // StateSet defines a state set metric. // +optional - StateSet *MetricStateSet `yaml:"stateSet" json:"stateSet"` + StateSet *MetricStateSet `yaml:"stateSet,omitempty" json:"stateSet,omitempty"` // Info defines an info metric. // +optional - Info *MetricInfo `yaml:"info" json:"info"` + Info *MetricInfo `yaml:"info,omitempty" json:"info,omitempty"` } // ConfigDecoder is for use with FromConfig. diff --git a/pkg/customresourcestate/config_metrics_types.go b/pkg/customresourcestate/config_metrics_types.go index 6e8e9167cd..12d0dac8ba 100644 --- a/pkg/customresourcestate/config_metrics_types.go +++ b/pkg/customresourcestate/config_metrics_types.go @@ -29,7 +29,7 @@ const ( // MetricMeta are variables which may used for any metric type. type MetricMeta struct { // LabelsFromPath adds additional labels where the value of the label is taken from a field under Path. - LabelsFromPath map[string][]string `yaml:"labelsFromPath" json:"labelsFromPath"` + LabelsFromPath map[string][]string `yaml:"labelsFromPath,omitempty" json:"labelsFromPath,omitempty"` // Path is the path to to generate metric(s) for. Path []string `yaml:"path" json:"path"` }