diff --git a/README.md b/README.md index 6fdebcc..9525b91 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ cluster-api-state-metrics -h Usage of ./bin/cluster-api-state-metrics: --add_dir_header If true, adds the file directory to the header of the log messages --alsologtostderr log to standard error as well as files - --apiserver string The URL of the apiserver to use + --apiserver string The URL of the apiserver to use as a master --enable-gzip-encoding Gzip responses when requested by clients via 'Accept-Encoding: gzip' header. -h, --help Print Help text --host string Host to expose metrics on. (default "::") @@ -68,17 +68,22 @@ Usage of ./bin/cluster-api-state-metrics: --metric-annotations-allowlist string Comma-separated list of Kubernetes annotations keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional annotations provide a list of resource names in their plural form and Kubernetes annotation keys you would like to allow for them (Example: '=namespaces=[kubernetes.io/team,...],pods=[kubernetes.io/team],...)'. A single '*' can be provided per resource instead to allow any annotations, but that has severe performance implications (Example: '=pods=[*]'). --metric-denylist string Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive. --metric-labels-allowlist string Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional labels provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'. A single '*' can be provided per resource instead to allow any labels, but that has severe performance implications (Example: '=pods=[*]'). + --metric-opt-in-list string Comma-separated list of metrics which are opt-in and not enabled by default. This is in addition to the metric allow- and denylists --namespaces string Comma-separated list of namespaces to be enabled. Defaults to "" --namespaces-denylist string Comma-separated list of namespaces not to be enabled. If namespaces and namespaces-denylist are both set, only namespaces that are excluded in namespaces-denylist will be used. --one_output If true, only write logs to their native severity level (vs also writing to each lower severity level) + --pod string Name of the pod that contains the kube-state-metrics container. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice. + --pod-namespace string Name of the namespace of the pod specified by --pod. When set, it is expected that --pod and --pod-namespace are both set. Most likely this should be passed via the downward API. This is used for auto-detecting sharding. If set, this has preference over statically configured sharding. This is experimental, it may be removed without notice. --port int Port to expose metrics on. (default 8080) --resources string Comma-separated list of Resources to be enabled. Defaults to "clusters,kubeadmcontrolplanes,machinedeployments,machines,machinesets" + --shard int32 The instances shard nominal (zero indexed) within the total number of shards. (default 0) --skip_headers If true, avoid header prefixes in the log messages --skip_log_headers If true, avoid headers when opening log files --stderrthreshold severity logs at or above this threshold go to stderr (default 2) --telemetry-host string Host to expose kube-state-metrics self metrics on. (default "::") --telemetry-port int Port to expose kube-state-metrics self metrics on. (default 8081) --tls-config string Path to the TLS configuration file + --total-shards int The total number of shards. Sharding is disabled when total shards is set to 1. (default 1) --use-apiserver-cache Sets resourceVersion=0 for ListWatch requests, using cached resources from the apiserver instead of an etcd quorum read. -v, --v Level number for the log level verbosity --version kube-state-metrics build version information diff --git a/go.mod b/go.mod index c88dc72..28996fc 100644 --- a/go.mod +++ b/go.mod @@ -4,32 +4,36 @@ module github.com/daimler/cluster-api-state-metrics go 1.17 require ( - github.com/oklog/run v1.1.0 github.com/pkg/errors v0.9.1 - github.com/prometheus/client_golang v1.11.0 github.com/prometheus/common v0.32.1 - github.com/prometheus/exporter-toolkit v0.7.1 - github.com/spf13/pflag v1.0.5 - k8s.io/api v0.23.0-alpha.4 - k8s.io/apiextensions-apiserver v0.23.0-alpha.4 - k8s.io/apimachinery v0.23.0-alpha.4 - k8s.io/client-go v0.23.0-alpha.4 + k8s.io/api v0.23.0 + k8s.io/apimachinery v0.23.0 + k8s.io/client-go v0.23.0 k8s.io/klog/v2 v2.30.0 - k8s.io/kube-state-metrics/v2 v2.2.2-0.20211110155727-e859b280fcc2 + k8s.io/kube-state-metrics/v2 v2.3.1-0.20220104140053-41eea36f69ef k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b sigs.k8s.io/cluster-api v1.0.0 sigs.k8s.io/controller-runtime v0.11.0-beta.0.0.20211110191610-1c34c83d69c8 ) require ( + cloud.google.com/go v0.93.3 // indirect + github.com/Azure/go-autorest v14.2.0+incompatible // indirect + github.com/Azure/go-autorest/autorest v0.11.18 // indirect + github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect + github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect + github.com/Azure/go-autorest/logger v0.2.1 // indirect + github.com/Azure/go-autorest/tracing v0.6.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/blang/semver v3.5.1+incompatible // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/coredns/caddy v1.1.0 // indirect github.com/coredns/corefile-migration v1.0.13 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgryski/go-jump v0.0.0-20211018200510-ba001c3ffce0 // indirect github.com/docker/distribution v2.7.1+incompatible // indirect github.com/evanphx/json-patch v4.12.0+incompatible // indirect + github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect github.com/go-kit/log v0.1.0 // indirect github.com/go-logfmt/logfmt v0.5.0 // indirect @@ -48,15 +52,20 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect + github.com/oklog/run v1.1.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/prometheus/client_golang v1.11.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/exporter-toolkit v0.7.1 // indirect github.com/prometheus/procfs v0.6.0 // indirect + github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect - golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect + golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect - golang.org/x/text v0.3.6 // indirect + golang.org/x/text v0.3.7 // indirect golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect google.golang.org/appengine v1.6.7 // indirect @@ -64,10 +73,11 @@ require ( gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + k8s.io/apiextensions-apiserver v0.23.0-alpha.4 // indirect k8s.io/autoscaler/vertical-pod-autoscaler v0.9.2 // indirect k8s.io/cluster-bootstrap v0.22.2 // indirect k8s.io/component-base v0.23.0-alpha.4 // indirect - k8s.io/kube-openapi v0.0.0-20210817084001-7fbd8d59e5b8 // indirect + k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect sigs.k8s.io/yaml v1.3.0 // indirect diff --git a/go.sum b/go.sum index bd79525..0d21042 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,7 @@ cloud.google.com/go v0.83.0/go.mod h1:Z7MJUsANfY0pYPdw0lbnivPx4/vhy/e2FEkSkF7vAV cloud.google.com/go v0.84.0/go.mod h1:RazrYuxIK6Kb7YrzzhPoLmCVzl7Sup4NrbKPg8KHSUM= cloud.google.com/go v0.87.0/go.mod h1:TpDYlFy7vuLzZMMZ+B6iRiELaY7z/gJPaqbMx6mlWcY= cloud.google.com/go v0.90.0/go.mod h1:kRX0mNRHe0e2rC6oNakvwQqzyDmg57xJ+SZU1eT2aDQ= +cloud.google.com/go v0.93.3 h1:wPBktZFzYBcCZVARvwVKqH1uEj+aLXofJEtrb4oOsio= cloud.google.com/go v0.93.3/go.mod h1:8utlLll2EF5XMAV15woO4lSbWQlk8rer9aLOfLh7+YI= cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE= @@ -45,22 +46,31 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Azure/go-ansiterm v0.0.0-20210608223527-2377c96fe795/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs= github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI= +github.com/Azure/go-autorest/autorest v0.11.18 h1:90Y4srNYrwOtAgVo3ndrQkTYn6kf1Eg/AjTFJ8Is2aM= github.com/Azure/go-autorest/autorest v0.11.18/go.mod h1:dSiJPy22c3u0OtOKDNttNgqpNFY/GeWa7GH/Pz56QRA= github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0= +github.com/Azure/go-autorest/autorest/adal v0.9.13 h1:Mp5hbtOePIzM8pJVRa3YLrWWmZtoxRXqUEzCfJt3+/Q= github.com/Azure/go-autorest/autorest/adal v0.9.13/go.mod h1:W/MM4U6nLxnIskrw4UwWzlHfGjwUS50aOsc/I3yuU8M= github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA= +github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw= github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74= github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0= github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0= +github.com/Azure/go-autorest/autorest/mocks v0.4.1 h1:K0laFcLE6VLTOwNgSxaGbUcLPuGXlNkbVvq4cW4nIHk= github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k= github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= +github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+ZtXWSmf4Tg= github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8= github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= +github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo= github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/Garrybest/kube-state-metrics/v2 v2.2.2-0.20211210074636-9990c3d2070d h1:TVeKHU5lxGnCVOH4Rw+ire0gmvtQU4ZYpiYlDt0xw44= +github.com/Garrybest/kube-state-metrics/v2 v2.2.2-0.20211210074636-9990c3d2070d/go.mod h1:gJ25kh7iLcFnlDqV82OvrDhzI4/quEVp//0wXgkaQVw= github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= @@ -136,6 +146,7 @@ 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/daviddengcn/go-colortext v0.0.0-20160507010035-511bcaf42ccd/go.mod h1:dv4zxwHi5C/8AeI+4gX4dCWOIvNi7I6JCSX0HvlKPgE= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +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/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug= @@ -168,12 +179,14 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= +github.com/form3tech-oss/jwt-go v3.2.3+incompatible h1:7ZaBxOI7TMoYBfyA3cQHErNNyAWIKUMIwqxEtgHOs5c= github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/fvbommel/sortorder v1.0.1/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0= +github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg= github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -316,6 +329,7 @@ github.com/googleapis/gnostic v0.5.5 h1:9fHAtK0uDfpveeqqo1hkEZJcFvYXAiCN3UutL8F9 github.com/googleapis/gnostic v0.5.5/go.mod h1:7+EbHbldMins07ALC74bsA81Ovc97DwqyJO1AENw9kA= github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo= @@ -512,8 +526,6 @@ github.com/prometheus/common v0.28.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+ github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= -github.com/prometheus/exporter-toolkit v0.7.0 h1:XtYeVeeC5daG4txbc9+mieKq+/AK4gtIBLl9Mulrjnk= -github.com/prometheus/exporter-toolkit v0.7.0/go.mod h1:ZUBIj498ePooX9t/2xtDjeQYwvRpiPP2lh5u4iblj2g= github.com/prometheus/exporter-toolkit v0.7.1 h1:c6RXaK8xBVercEeUQ4tRNL8UGWzDHfvj9dseo1FcK1Y= github.com/prometheus/exporter-toolkit v0.7.1/go.mod h1:ZUBIj498ePooX9t/2xtDjeQYwvRpiPP2lh5u4iblj2g= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= @@ -525,6 +537,7 @@ github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3x github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +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/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -589,6 +602,7 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= @@ -686,6 +700,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -738,8 +753,10 @@ golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c= golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -849,6 +866,8 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 h1:M69LAlWZCshgp0QSzyDcSsSIejIEeuaCVpmwcKwyLMk= golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= @@ -864,8 +883,9 @@ 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.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -938,7 +958,7 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.6-0.20210820212750-d4cc65f0b2ff/go.mod h1:YD9qOF0M9xpSpdWTBbzEl5e/RnCefISl8E5Noe10jFM= -golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= +golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= 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= @@ -1120,15 +1140,17 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9 honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= k8s.io/api v0.18.3/go.mod h1:UOaMwERbqJMfeeeHc8XJKawj4P9TgDRnViIqqBeH2QA= k8s.io/api v0.22.2/go.mod h1:y3ydYpLJAaDI+BbSe2xmGcqxiWHmWjkEeIbiwHvnPR8= -k8s.io/api v0.23.0-alpha.4 h1:jqhlAybNGUat3I73xXHmkKi2If1cBZbTvRUxM1fyHVY= k8s.io/api v0.23.0-alpha.4/go.mod h1:C2RqQ86jH9nM0YFGjLhKlfldBYLnBEb5sn+x50lF2zg= +k8s.io/api v0.23.0 h1:WrL1gb73VSC8obi8cuYETJGXEoFNEh3LU0Pt+Sokgro= +k8s.io/api v0.23.0/go.mod h1:8wmDdLBHBNxtOIytwLstXt5E9PddnZb0GaMcqsvDBpg= k8s.io/apiextensions-apiserver v0.22.2/go.mod h1:2E0Ve/isxNl7tWLSUDgi6+cmwHi5fQRdwGVCxbC+KFA= k8s.io/apiextensions-apiserver v0.23.0-alpha.4 h1:7mnbgfdBuuRcAd5T4Hq73t9PsaDw819b6DQPqbcEQds= k8s.io/apiextensions-apiserver v0.23.0-alpha.4/go.mod h1:kigfmoeWZRvDkUtNCLd4vEVmVHU2jhi/8ISvK2v724c= k8s.io/apimachinery v0.18.3/go.mod h1:OaXp26zu/5J7p0f92ASynJa1pZo06YlV9fG7BoWbCko= k8s.io/apimachinery v0.22.2/go.mod h1:O3oNtNadZdeOMxHFVxOreoznohCpy0z6mocxbZr7oJ0= -k8s.io/apimachinery v0.23.0-alpha.4 h1:mLx8eaYBGBm13xsVXm+mA1njW30uRm6+KKExkm4h4Rc= k8s.io/apimachinery v0.23.0-alpha.4/go.mod h1:oyH3LcOKLLooQH1NlpHlilzkWxqsiHWETyHgssntcXg= +k8s.io/apimachinery v0.23.0 h1:mIfWRMjBuMdolAWJ3Fd+aPTMv3X9z+waiARMpvvb0HQ= +k8s.io/apimachinery v0.23.0/go.mod h1:fFCTTBKvKcwTPFzjlcxp91uPFZr+JA0FubU4fLzzFYc= k8s.io/apiserver v0.22.2/go.mod h1:vrpMmbyjWrgdyOvZTSpsusQq5iigKNWv9o9KlDAbBHI= k8s.io/apiserver v0.23.0-alpha.4/go.mod h1:filg3J7fRj+AuwLTFXNcH566LHC8mLkQrD0H2zUVpJk= k8s.io/autoscaler/vertical-pod-autoscaler v0.9.2 h1:fMFkbjo6ElaL3POE3ctxLuX3aR+bEzpo2q1R5Aq2JhE= @@ -1136,13 +1158,15 @@ k8s.io/autoscaler/vertical-pod-autoscaler v0.9.2/go.mod h1:PwWTGRRCxefhAezrDbG/t k8s.io/cli-runtime v0.22.2/go.mod h1:tkm2YeORFpbgQHEK/igqttvPTRIHFRz5kATlw53zlMI= k8s.io/client-go v0.18.3/go.mod h1:4a/dpQEvzAhT1BbuWW09qvIaGw6Gbu1gZYiQZIi1DMw= k8s.io/client-go v0.22.2/go.mod h1:sAlhrkVDf50ZHx6z4K0S40wISNTarf1r800F+RlCF6U= -k8s.io/client-go v0.23.0-alpha.4 h1:tYtPY4J/fP1WBjktNAv+AAVxWzfygDFcCclENtyHzOk= k8s.io/client-go v0.23.0-alpha.4/go.mod h1:OBGvnY60bm0zXmY4unHcYUHmffR6Smg2AqJ3pzORKYk= +k8s.io/client-go v0.23.0 h1:vcsOqyPq7XV3QmQRCBH/t9BICJM9Q1M18qahjv+rebY= +k8s.io/client-go v0.23.0/go.mod h1:hrDnpnK1mSr65lHHcUuIZIXDgEbzc7/683c6hyG4jTA= k8s.io/cluster-bootstrap v0.22.2 h1:jP6Nkp3CdSfr50cAn/7WGsNS52zrwMhvr0V+E3Vkh/w= k8s.io/cluster-bootstrap v0.22.2/go.mod h1:ZkmQKprEqvrUccMnbRHISsMscA1dsQ8SffM9nHq6CgE= k8s.io/code-generator v0.18.3/go.mod h1:TgNEVx9hCyPGpdtCWA34olQYLkh3ok9ar7XfSsr8b6c= k8s.io/code-generator v0.22.2/go.mod h1:eV77Y09IopzeXOJzndrDyCI88UBok2h6WxAlBwpxa+o= k8s.io/code-generator v0.23.0-alpha.4/go.mod h1:alK4pz5+y/zKXOPBnND3TvXOC/iF2oYTBDynHO1+qlI= +k8s.io/code-generator v0.23.0/go.mod h1:vQvOhDXhuzqiVfM/YHp+dmg10WDZCchJVObc9MvowsE= k8s.io/component-base v0.18.3/go.mod h1:bp5GzGR0aGkYEfTj+eTY0AN/vXTgkJdQXjNTTVUaa3k= k8s.io/component-base v0.22.2/go.mod h1:5Br2QhI9OTe79p+TzPe9JKNQYvEKbq9rTJDWllunGug= k8s.io/component-base v0.23.0-alpha.4 h1:tM4JaTDQGXpco6l/OByn5DTil+YjtVQGmjLJ3Pr8Tew= @@ -1164,13 +1188,16 @@ k8s.io/klog/v2 v2.30.0 h1:bUO6drIvCIsvZ/XFgfxoGFQU/a4Qkh0iAlvUR7vlHJw= k8s.io/klog/v2 v2.30.0/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E= k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw= -k8s.io/kube-openapi v0.0.0-20210817084001-7fbd8d59e5b8 h1:Xxl9TLJ30BJ1pGWfGZnqbpww2rwOt3RAzbSz+omQGtg= k8s.io/kube-openapi v0.0.0-20210817084001-7fbd8d59e5b8/go.mod h1:foAE7XkrXQ1Qo2eWsW/iWksptrVdbl6t+vscSdmmGjk= -k8s.io/kube-state-metrics/v2 v2.2.2-0.20211110155727-e859b280fcc2 h1:wTQFdpQwpKHpbpqoZTtJ4J103CM74QEoJIqx3koPiaI= -k8s.io/kube-state-metrics/v2 v2.2.2-0.20211110155727-e859b280fcc2/go.mod h1:vk0Y1o51pVY7V2FOf+8T4OhsShXJKbMlpH6tiuTBhPo= +k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 h1:E3J9oCLlaobFUqsjG9DfKbP2BmgwBL2p7pn0A3dG9W4= +k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65/go.mod h1:sX9MT8g7NVZM5lVL/j8QyCCJe8YSMW30QvGZWaCIDIk= +k8s.io/kube-state-metrics/v2 v2.3.1-0.20220104140053-41eea36f69ef h1:2suX59pI7AtGvZpWgRgbiQtMPkwTrPlWrG8oQn7lyFA= +k8s.io/kube-state-metrics/v2 v2.3.1-0.20220104140053-41eea36f69ef/go.mod h1:gJ25kh7iLcFnlDqV82OvrDhzI4/quEVp//0wXgkaQVw= k8s.io/kubectl v0.22.2/go.mod h1:BApg2j0edxLArCOfO0ievI27EeTQqBDMNU9VQH734iQ= k8s.io/metrics v0.18.3/go.mod h1:TkuJE3ezDZ1ym8pYkZoEzJB7HDiFE7qxl+EmExEBoPA= k8s.io/metrics v0.22.2/go.mod h1:GUcsBtpsqQD1tKFS/2wCKu4ZBowwRncLOJH1rgWs3uw= +k8s.io/sample-controller v0.23.0 h1:Zwxi+BQxEQDg63jGzxrLUbWBp+Qkqse/web1nkpSG9g= +k8s.io/sample-controller v0.23.0/go.mod h1:8a1Cgok9A5JRa1rJgg9AQKrOF0hqwbaHt/wcndZ6fmY= k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew= k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= diff --git a/main.go b/main.go index c00d722..7bfcc3f 100644 --- a/main.go +++ b/main.go @@ -28,97 +28,32 @@ package main import ( "context" "fmt" - "net" - "net/http" - "net/http/pprof" "os" - "strconv" - "time" - "github.com/oklog/run" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/collectors" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/daimler/cluster-api-state-metrics/pkg/store" "github.com/prometheus/common/version" - "github.com/prometheus/exporter-toolkit/web" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" - clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/klog/v2" - "k8s.io/kube-state-metrics/v2/pkg/allowdenylist" - "k8s.io/kube-state-metrics/v2/pkg/util/proc" - clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" - controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/config" - - //+kubebuilder:scaffold:imports - "github.com/daimler/cluster-api-state-metrics/pkg/metricshandler" - casmoptions "github.com/daimler/cluster-api-state-metrics/pkg/options" - "github.com/daimler/cluster-api-state-metrics/pkg/store" -) -var ( - scheme = runtime.NewScheme() - - // variables set during build - buildDate string - gitBranch string - gitCommit string - user string -) - -const ( - metricsPath = "/metrics" - healthzPath = "/healthz" + "k8s.io/kube-state-metrics/v2/pkg/app" + "k8s.io/kube-state-metrics/v2/pkg/options" ) -// promLogger implements promhttp.Logger -type promLogger struct{} - -func (pl promLogger) Println(v ...interface{}) { - klog.Error(v...) -} - -// promLogger implements the Logger interface -func (pl promLogger) Log(v ...interface{}) error { - klog.Info(v...) - return nil -} - -func init() { - klog.InitFlags(nil) - - _ = clientgoscheme.AddToScheme(scheme) - _ = clusterv1.AddToScheme(scheme) - _ = controlplanev1.AddToScheme(scheme) - _ = apiextensionsv1.AddToScheme(scheme) - // +kubebuilder:scaffold:scheme -} - func main() { - opts := casmoptions.NewOptions() + options.DefaultResources = store.DefaultResources + opts := options.NewOptions() opts.AddFlags() - promLogger := promLogger{} - - ctx := context.Background() + if err := opts.Parse(); err != nil { + klog.Fatalf("Parsing flag definitions error: %v", err) + } - err := opts.Parse() - if err != nil { - klog.Fatalf("Error: %s", err) + // reset back to empty, we get the defaults duplicated at `Active resources` otherwise. + if opts.Resources.String() == "" { + options.DefaultResources = options.ResourceSet{} } if opts.Version { - version.BuildDate = buildDate - version.BuildUser = user - version.Branch = gitBranch - version.Revision = gitCommit - fmt.Printf("%s\n", version.Print("cluster-api-state-metrics")) + fmt.Printf("%s\n", version.Print("kube-state-metrics")) os.Exit(0) } @@ -126,201 +61,9 @@ func main() { opts.Usage() os.Exit(0) } - storeBuilder := store.NewBuilder() - - casmMetricsRegistry := prometheus.NewRegistry() - casmMetricsRegistry.MustRegister(version.NewCollector("cluster_api_state_metrics")) - durationVec := promauto.With(casmMetricsRegistry).NewHistogramVec( - prometheus.HistogramOpts{ - Name: "http_request_duration_seconds", - Help: "A histogram of requests for cluster-api-state-metrics metrics handler.", - Buckets: prometheus.DefBuckets, - ConstLabels: prometheus.Labels{"handler": "metrics"}, - }, []string{"method"}, - ) - storeBuilder.WithMetrics(casmMetricsRegistry) - - var resources []string - if len(opts.Resources) == 0 { - klog.Info("Using default resources") - resources = casmoptions.DefaultResources.AsSlice() - } else { - klog.Infof("Using resources %s", opts.Resources.String()) - resources = opts.Resources.AsSlice() - } - - if err := storeBuilder.WithEnabledResources(resources); err != nil { - klog.Fatalf("Failed to set up resources: %v", err) - } - - namespaces := opts.Namespaces.GetNamespaces() - nsFieldSelector := namespaces.GetExcludeNSFieldSelector(opts.NamespacesDenylist) - storeBuilder.WithNamespaces(namespaces, nsFieldSelector) - - allowDenyList, err := allowdenylist.New(opts.MetricAllowlist, opts.MetricDenylist) - if err != nil { - klog.Fatal(err) - } - - err = allowDenyList.Parse() - if err != nil { - klog.Fatalf("error initializing the allowdeny list: %v", err) - } - - klog.Infof("metric allow-denylisting: %v", allowDenyList.Status()) - storeBuilder.WithAllowDenyList(allowDenyList) - - storeBuilder.WithGenerateStoresFunc(storeBuilder.DefaultGenerateStoresFunc(), opts.UseAPIServerCache) - - proc.StartReaper() - - ctrlClient, err := getCtrlClient(opts.Kubeconfig, os.Getenv("KUBECONTEXT")) - if err != nil { - klog.Fatalf("Unable to get controller client: %v", err) - } - - storeBuilder.WithCtrlClient(ctrlClient) - storeBuilder.WithAllowAnnotations(opts.AnnotationsAllowList) - storeBuilder.WithAllowLabels(opts.LabelsAllowList) - - casmMetricsRegistry.MustRegister( - collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), - collectors.NewGoCollector(), - ) - - var g run.Group - - m := metricshandler.New( - ctx, - opts, - storeBuilder, - opts.EnableGZIPEncoding, - ) - - tlsConfig := opts.TLSConfig - - telemetryMux := buildTelemetryServer(casmMetricsRegistry) - telemetryListenAddress := net.JoinHostPort(opts.TelemetryHost, strconv.Itoa(opts.TelemetryPort)) - telemetryServer := http.Server{Handler: telemetryMux, Addr: telemetryListenAddress} - - metricsMux := buildMetricsServer(m, durationVec) - metricsServerListenAddress := net.JoinHostPort(opts.Host, strconv.Itoa(opts.Port)) - metricsServer := http.Server{Handler: metricsMux, Addr: metricsServerListenAddress} - - // Run Telemetry server - { - g.Add(func() error { - klog.Infof("Starting cluster-api-state-metrics self metrics server: %s", telemetryListenAddress) - return web.ListenAndServe(&telemetryServer, tlsConfig, promLogger) - }, func(error) { - ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second) - defer cancel() - if err := telemetryServer.Shutdown(ctxShutDown); err != nil { - klog.ErrorS(err, "shutdown telemetry server") - } - }) - } - // Run Metrics server - { - g.Add(func() error { - klog.Infof("Starting metrics server: %s", metricsServerListenAddress) - return web.ListenAndServe(&metricsServer, tlsConfig, promLogger) - }, func(error) { - ctxShutDown, cancel := context.WithTimeout(ctx, 3*time.Second) - defer cancel() - if err := metricsServer.Shutdown(ctxShutDown); err != nil { - klog.ErrorS(err, "shutdown metrics server") - } - }) - } - - if err := g.Run(); err != nil { - klog.Fatalf("RunGroup Error: %v", err) - } - klog.Info("Exiting") -} - -func buildTelemetryServer(registry prometheus.Gatherer) *http.ServeMux { - mux := http.NewServeMux() - - // Add metricsPath - mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: promLogger{}})) - // Add index - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if _, err := w.Write([]byte(` - Cluster-API-State-Metrics Metrics Server - -

Cluster-API-State-Metrics Metrics

- - - `)); err != nil { - klog.ErrorS(err, "write telemetry index data") - } - }) - return mux -} - -func buildMetricsServer(m http.Handler, durationObserver prometheus.ObserverVec) *http.ServeMux { - mux := http.NewServeMux() - - mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index)) - mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline)) - mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile)) - mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol)) - mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace)) - - mux.Handle(metricsPath, promhttp.InstrumentHandlerDuration(durationObserver, m)) - - // Add healthzPath - mux.HandleFunc(healthzPath, func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte(http.StatusText(http.StatusOK))); err != nil { - klog.ErrorS(err, "write healthz text") - } - }) - // Add index - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if _, err := w.Write([]byte(` - Cluster-API Metrics Server - -

Cluster-API Metrics

- - - `)); err != nil { - klog.ErrorS(err, "write metrics index data") - } - }) - return mux -} - -func getCtrlClient(kubeconfig, context string) (client.WithWatch, error) { - // The kubeconfig flag in "sigs.k8s.io/controller-runtime/pkg/client/config" - // is using another flagset. Because of that we use the value of the kubeconfig - // flag directly and fallback to the config.GetConfigWithContext function. - var cfg *rest.Config - var err error - if len(kubeconfig) > 0 { - cfg, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}, - &clientcmd.ConfigOverrides{ - ClusterInfo: clientcmdapi.Cluster{ - Server: "", - }, - CurrentContext: context, - }).ClientConfig() - } else { - cfg, err = config.GetConfigWithContext(os.Getenv("KUBECONTEXT")) - } - if err != nil { - return nil, err + ctx := context.Background() + if err := app.RunKubeStateMetrics(ctx, opts, store.Factories()...); err != nil { + klog.Fatalf("Failed to run kube-state-metrics: %v", err) } - return client.NewWithWatch(cfg, client.Options{ - Scheme: scheme, - }) } diff --git a/pkg/metricshandler/metrics_handler.go b/pkg/metricshandler/metrics_handler.go deleted file mode 100644 index ea42aed..0000000 --- a/pkg/metricshandler/metrics_handler.go +++ /dev/null @@ -1,99 +0,0 @@ -/* -Copyright 2019 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. - -The original file is located at [1]. -[1]: https://github.com/kubernetes/kube-state-metrics/blob/e859b280fcc2/pkg/metricshandler/metrics_handler.go - -The original source uses the kube-state-metrics's internal store package ([2]) -which makes it impossible to re-use it. Compared to the upstream package this -metrics handler does not support sharding for now. - -The original source was adjusted to: -- support the store.Builder struct instead of the upstream ksmtypes.BuilderInterface. -- remove sharding functionality to reduce complexity for the initial implementation. -*/ - -package metricshandler - -import ( - "compress/gzip" - "context" - "io" - "net/http" - "strings" - "sync" - - metricsstore "k8s.io/kube-state-metrics/v2/pkg/metrics_store" - - "github.com/daimler/cluster-api-state-metrics/pkg/options" - "github.com/daimler/cluster-api-state-metrics/pkg/store" -) - -type MetricsHandler struct { - opts *options.Options - storeBuilder *store.Builder - enableGZIPEncoding bool - - // mtx protects metricsWriters, curShard, and curTotalShards - mtx *sync.RWMutex - metricsWriters []metricsstore.MetricsWriter -} - -// New creates and returns a new MetricsHandler with the given options. -func New(ctx context.Context, opts *options.Options, storeBuilder *store.Builder, enableGZIPEncoding bool) *MetricsHandler { - m := &MetricsHandler{ - opts: opts, - storeBuilder: storeBuilder, - enableGZIPEncoding: enableGZIPEncoding, - mtx: &sync.RWMutex{}, - } - // upstream adds the context during the sharding configuration. - m.storeBuilder.WithContext(ctx) - // upstream builds the metricsWriters in its sharding configuration - m.metricsWriters = m.storeBuilder.Build() - return m -} - -func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - m.mtx.RLock() - defer m.mtx.RUnlock() - resHeader := w.Header() - var writer io.Writer = w - - resHeader.Set("Content-Type", `text/plain; version=`+"0.0.4") - - if m.enableGZIPEncoding { - // Gzip response if requested. Taken from - // github.com/prometheus/client_golang/prometheus/promhttp.decorateWriter. - reqHeader := r.Header.Get("Accept-Encoding") - parts := strings.Split(reqHeader, ",") - for _, part := range parts { - part = strings.TrimSpace(part) - if part == "gzip" || strings.HasPrefix(part, "gzip;") { - writer = gzip.NewWriter(writer) - resHeader.Set("Content-Encoding", "gzip") - } - } - } - - for _, w := range m.metricsWriters { - w.WriteAll(writer) - } - - // In case we gzipped the response, we have to close the writer. - if closer, ok := writer.(io.Closer); ok { - closer.Close() - } -} diff --git a/pkg/options/options.go b/pkg/options/options.go deleted file mode 100644 index 76c0f40..0000000 --- a/pkg/options/options.go +++ /dev/null @@ -1,120 +0,0 @@ -/* -Copyright 2018 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. - -The original file is located at [1]. -[1]: https://github.com/kubernetes/kube-state-metrics/blob/e859b280fcc2/pkg/options/options.go - -The original source was adjusted to: -- re-use types of the original package. -- remove sharding functionality to reduce complexity for the initial implementation. -*/ - -package options - -import ( - "flag" - "fmt" - "os" - - "github.com/spf13/pflag" - "k8s.io/klog/v2" - koptions "k8s.io/kube-state-metrics/v2/pkg/options" -) - -// Options are the configurable parameters for kube-state-metrics. -type Options struct { - Apiserver string - Kubeconfig string - Help bool - Port int - Host string - TelemetryPort int - TelemetryHost string - TLSConfig string - Resources koptions.ResourceSet - Namespaces koptions.NamespaceList - NamespacesDenylist koptions.NamespaceList - MetricDenylist koptions.MetricSet - MetricAllowlist koptions.MetricSet - Version bool - AnnotationsAllowList koptions.LabelsAllowList - LabelsAllowList koptions.LabelsAllowList - - EnableGZIPEncoding bool - - UseAPIServerCache bool - - flags *pflag.FlagSet -} - -// NewOptions returns a new instance of `Options`. -func NewOptions() *Options { - return &Options{ - Resources: koptions.ResourceSet{}, - MetricAllowlist: koptions.MetricSet{}, - MetricDenylist: koptions.MetricSet{}, - LabelsAllowList: koptions.LabelsAllowList{}, - } -} - -// AddFlags populated the Options struct from the command line arguments passed. -func (o *Options) AddFlags() { - o.flags = pflag.NewFlagSet("", pflag.ExitOnError) - // add klog flags - klogFlags := flag.NewFlagSet("klog", flag.ExitOnError) - klog.InitFlags(klogFlags) - o.flags.AddGoFlagSet(klogFlags) - if err := o.flags.Lookup("logtostderr").Value.Set("true"); err != nil { - klog.Fatal("error setting logtostderr default value") - } - o.flags.Lookup("logtostderr").DefValue = "true" - o.flags.Lookup("logtostderr").NoOptDefVal = "true" - - o.flags.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - o.flags.PrintDefaults() - } - - o.flags.BoolVarP(&o.UseAPIServerCache, "use-apiserver-cache", "", false, "Sets resourceVersion=0 for ListWatch requests, using cached resources from the apiserver instead of an etcd quorum read.") - o.flags.StringVar(&o.Apiserver, "apiserver", "", `The URL of the apiserver to use`) - o.flags.StringVar(&o.Kubeconfig, "kubeconfig", "", "Absolute path to the kubeconfig file") - o.flags.StringVar(&o.TLSConfig, "tls-config", "", "Path to the TLS configuration file") - o.flags.BoolVarP(&o.Help, "help", "h", false, "Print Help text") - o.flags.IntVar(&o.Port, "port", 8080, `Port to expose metrics on.`) - o.flags.StringVar(&o.Host, "host", "::", `Host to expose metrics on.`) - o.flags.IntVar(&o.TelemetryPort, "telemetry-port", 8081, `Port to expose kube-state-metrics self metrics on.`) - o.flags.StringVar(&o.TelemetryHost, "telemetry-host", "::", `Host to expose kube-state-metrics self metrics on.`) - o.flags.Var(&o.Resources, "resources", fmt.Sprintf("Comma-separated list of Resources to be enabled. Defaults to %q", &DefaultResources)) - o.flags.Var(&o.Namespaces, "namespaces", fmt.Sprintf("Comma-separated list of namespaces to be enabled. Defaults to %q", &DefaultNamespaces)) - o.flags.Var(&o.NamespacesDenylist, "namespaces-denylist", "Comma-separated list of namespaces not to be enabled. If namespaces and namespaces-denylist are both set, only namespaces that are excluded in namespaces-denylist will be used.") - o.flags.Var(&o.MetricAllowlist, "metric-allowlist", "Comma-separated list of metrics to be exposed. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.") - o.flags.Var(&o.MetricDenylist, "metric-denylist", "Comma-separated list of metrics not to be enabled. This list comprises of exact metric names and/or regex patterns. The allowlist and denylist are mutually exclusive.") - o.flags.Var(&o.AnnotationsAllowList, "metric-annotations-allowlist", "Comma-separated list of Kubernetes annotations keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional annotations provide a list of resource names in their plural form and Kubernetes annotation keys you would like to allow for them (Example: '=namespaces=[kubernetes.io/team,...],pods=[kubernetes.io/team],...)'. A single '*' can be provided per resource instead to allow any annotations, but that has severe performance implications (Example: '=pods=[*]').") - o.flags.Var(&o.LabelsAllowList, "metric-labels-allowlist", "Comma-separated list of additional Kubernetes label keys that will be used in the resource' labels metric. By default the metric contains only name and namespace labels. To include additional labels provide a list of resource names in their plural form and Kubernetes label keys you would like to allow for them (Example: '=namespaces=[k8s-label-1,k8s-label-n,...],pods=[app],...)'. A single '*' can be provided per resource instead to allow any labels, but that has severe performance implications (Example: '=pods=[*]').") - - o.flags.BoolVarP(&o.Version, "version", "", false, "kube-state-metrics build version information") - o.flags.BoolVar(&o.EnableGZIPEncoding, "enable-gzip-encoding", false, "Gzip responses when requested by clients via 'Accept-Encoding: gzip' header.") -} - -// Parse parses the flag definitions from the argument list. -func (o *Options) Parse() error { - err := o.flags.Parse(os.Args) - return err -} - -// Usage is the function called when an error occurs while parsing flags. -func (o *Options) Usage() { - o.flags.Usage() -} diff --git a/pkg/options/resources.go b/pkg/options/resources.go deleted file mode 100644 index 8d6316a..0000000 --- a/pkg/options/resources.go +++ /dev/null @@ -1,22 +0,0 @@ -// SPDX-License-Identifier: MIT - -package options - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - koptions "k8s.io/kube-state-metrics/v2/pkg/options" -) - -var ( - // DefaultNamespaces is the default namespace selector for selecting and filtering across all namespaces. - DefaultNamespaces = koptions.NamespaceList{metav1.NamespaceAll} - - // DefaultResources represents the default set of resources in cluster-api-state-metrics. - DefaultResources = koptions.ResourceSet{ - "clusters": struct{}{}, - "machines": struct{}{}, - "machinedeployments": struct{}{}, - "kubeadmcontrolplanes": struct{}{}, - "machinesets": struct{}{}, - } -) diff --git a/pkg/store/builder.go b/pkg/store/builder.go deleted file mode 100644 index 9b0261a..0000000 --- a/pkg/store/builder.go +++ /dev/null @@ -1,251 +0,0 @@ -/* -Copyright 2018 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. - -The original file is located at [1]. -[1]: https://github.com/kubernetes/kube-state-metrics/blob/e859b280fcc2/internal/store/builder.go - -The original source was adjusted to: -- use a controller-runtime client instead of the client-go interface. -- remove sharding functionality to reduce complexity for the initial implementation. -- remove unused functions. -- use custom type for BuildStoresFunc to support controller-runtime client instead - of client-go. - -The upstream package is internal which is why we are not allowed to import and -use it directly. - -Also it is not possible to re-use the wrapper at [2] because it is not flexible -enough. - -[2]: https://github.com/kubernetes/kube-state-metrics/tree/master/pkg/builder -*/ - -package store - -import ( - "context" - "reflect" - "sort" - "strings" - - "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus" - v1 "k8s.io/api/core/v1" - "k8s.io/client-go/tools/cache" - "k8s.io/klog/v2" - ksmtypes "k8s.io/kube-state-metrics/v2/pkg/builder/types" - generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator" - metricsstore "k8s.io/kube-state-metrics/v2/pkg/metrics_store" - "k8s.io/kube-state-metrics/v2/pkg/options" - "k8s.io/kube-state-metrics/v2/pkg/watch" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -// BuildStoresFunc function signature that is used to return a list of cache.Store using a WithWatch client -type BuildStoresFunc func(metricFamilies []generator.FamilyGenerator, - expectedType interface{}, - listWatchFunc func(client client.WithWatch, ns, fieldSelector string) cache.ListerWatcher, - useAPIServerCache bool, -) []cache.Store - -// Builder helps to build store. It follows the builder pattern -// (https://en.wikipedia.org/wiki/Builder_pattern). -type Builder struct { - ctrlClient client.WithWatch - namespaces options.NamespaceList - namespaceFilter string - ctx context.Context - enabledResources []string - allowDenyList ksmtypes.AllowDenyLister - listWatchMetrics *watch.ListWatchMetrics - buildStoresFunc BuildStoresFunc - allowAnnotationsList map[string][]string - allowLabelsList map[string][]string - useAPIServerCache bool -} - -// NewBuilder returns a new builder. -func NewBuilder() *Builder { - b := &Builder{} - return b -} - -// WithMetrics sets the metrics property of a Builder. -func (b *Builder) WithMetrics(r prometheus.Registerer) { - b.listWatchMetrics = watch.NewListWatchMetrics(r) -} - -// WithEnabledResources sets the enabledResources property of a Builder. -func (b *Builder) WithEnabledResources(r []string) error { - for _, col := range r { - if !resourceExists(col) { - return errors.Errorf("resource %s does not exist. Available resources: %s", col, strings.Join(availableResources(), ",")) - } - } - - var copy []string - copy = append(copy, r...) - - sort.Strings(copy) - - b.enabledResources = copy - return nil -} - -// WithNamespaces sets the namespaces property of a Builder. -func (b *Builder) WithNamespaces(n options.NamespaceList, nsFilter string) { - b.namespaces = n - b.namespaceFilter = nsFilter -} - -// WithContext sets the ctx property of a Builder. -func (b *Builder) WithContext(ctx context.Context) { - b.ctx = ctx -} - -// WithCtrlClient sets the controller-runtime client with watch as property of a Builder. -func (b *Builder) WithCtrlClient(c client.WithWatch) { - b.ctrlClient = c -} - -// WithAllowDenyList configures the allow or denylisted metric to be exposed -// by the store build by the Builder. -func (b *Builder) WithAllowDenyList(l ksmtypes.AllowDenyLister) { - b.allowDenyList = l -} - -// WithGenerateStoresFunc configures a custom generate store function -func (b *Builder) WithGenerateStoresFunc(f BuildStoresFunc, u bool) { - b.buildStoresFunc = f - b.useAPIServerCache = u -} - -// DefaultGenerateStoresFunc returns default buildStores function -func (b *Builder) DefaultGenerateStoresFunc() BuildStoresFunc { - return b.buildStores -} - -// WithAllowAnnotations configures which annotations can be returned for metrics -func (b *Builder) WithAllowAnnotations(annotations map[string][]string) { - if len(annotations) > 0 { - b.allowAnnotationsList = annotations - } -} - -// WithAllowLabels configures which labels can be returned for metrics -func (b *Builder) WithAllowLabels(labels map[string][]string) { - if len(labels) > 0 { - b.allowLabelsList = labels - } -} - -// Build initializes and registers all enabled stores. -// It returns metrics writers which can be used to write out -// metrics from the stores. -func (b *Builder) Build() []metricsstore.MetricsWriter { - if b.allowDenyList == nil { - panic("allowDenyList should not be nil") - } - - var metricsWriters []metricsstore.MetricsWriter - var activeStoreNames []string - - for _, c := range b.enabledResources { - constructor, ok := availableStores[c] - if ok { - stores := cacheStoresToMetricStores(constructor(b)) - activeStoreNames = append(activeStoreNames, c) - if len(stores) == 1 { - metricsWriters = append(metricsWriters, stores[0]) - } else { - metricsWriters = append(metricsWriters, metricsstore.NewMultiStoreMetricsWriter(stores)) - } - } - } - - klog.Infof("Active resources: %s", strings.Join(activeStoreNames, ",")) - - return metricsWriters -} - -func resourceExists(name string) bool { - _, ok := availableStores[name] - return ok -} - -func availableResources() []string { - c := []string{} - for name := range availableStores { - c = append(c, name) - } - return c -} - -func (b *Builder) buildStores( - metricFamilies []generator.FamilyGenerator, - expectedType interface{}, - listWatchFunc func(client client.WithWatch, ns string, fieldSelector string) cache.ListerWatcher, - useAPIServerCache bool, -) []cache.Store { - metricFamilies = generator.FilterMetricFamilies(b.allowDenyList, metricFamilies) - composedMetricGenFuncs := generator.ComposeMetricGenFuncs(metricFamilies) - familyHeaders := generator.ExtractMetricFamilyHeaders(metricFamilies) - - if b.namespaces.IsAllNamespaces() { - store := metricsstore.NewMetricsStore( - familyHeaders, - composedMetricGenFuncs, - ) - listWatcher := listWatchFunc(b.ctrlClient, v1.NamespaceAll, b.namespaceFilter) - b.startReflector(expectedType, store, listWatcher, useAPIServerCache) - return []cache.Store{store} - } - - stores := make([]cache.Store, 0, len(b.namespaces)) - for _, ns := range b.namespaces { - store := metricsstore.NewMetricsStore( - familyHeaders, - composedMetricGenFuncs, - ) - listWatcher := listWatchFunc(b.ctrlClient, ns, b.namespaceFilter) - b.startReflector(expectedType, store, listWatcher, useAPIServerCache) - stores = append(stores, store) - } - - return stores -} - -// startReflector starts a Kubernetes client-go reflector with the given -// listWatcher and registers it with the given store. -func (b *Builder) startReflector( - expectedType interface{}, - store cache.Store, - listWatcher cache.ListerWatcher, - useAPIServerCache bool, -) { - instrumentedListWatch := watch.NewInstrumentedListerWatcher(listWatcher, b.listWatchMetrics, reflect.TypeOf(expectedType).String(), useAPIServerCache) - reflector := cache.NewReflector(instrumentedListWatch, expectedType, store, 0) - go reflector.Run(b.ctx.Done()) -} - -// cacheStoresToMetricStores converts []cache.Store into []*metricsstore.MetricsStore -func cacheStoresToMetricStores(cStores []cache.Store) []*metricsstore.MetricsStore { - mStores := make([]*metricsstore.MetricsStore, 0, len(cStores)) - for _, store := range cStores { - mStores = append(mStores, store.(*metricsstore.MetricsStore)) - } - - return mStores -} diff --git a/pkg/store/cluster.go b/pkg/store/cluster.go index 8945b07..08308eb 100644 --- a/pkg/store/cluster.go +++ b/pkg/store/cluster.go @@ -19,7 +19,19 @@ import ( var descClusterLabelsDefaultLabels = []string{"namespace", "cluster", "uid"} -func clusterMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator { +type ClusterFactory struct { + *ControllerRuntimeClientFactory +} + +func (f *ClusterFactory) Name() string { + return "clusters" +} + +func (f *ClusterFactory) ExpectedType() interface{} { + return &clusterv1.Cluster{} +} + +func (f *ClusterFactory) MetricFamilyGenerators(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { return []generator.FamilyGenerator{ *generator.NewFamilyGenerator( "capi_cluster_labels", @@ -114,7 +126,8 @@ func clusterMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator } } -func createClusterListWatch(ctrlClient client.WithWatch, ns string, fieldSelector string) cache.ListerWatcher { +func (f *ClusterFactory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher { + ctrlClient := customResourceClient.(client.WithWatch) return &cache.ListWatch{ ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { clusterList := clusterv1.ClusterList{} diff --git a/pkg/store/cluster_test.go b/pkg/store/cluster_test.go index df26e84..f7186d4 100644 --- a/pkg/store/cluster_test.go +++ b/pkg/store/cluster_test.go @@ -106,8 +106,9 @@ func TestClusterStore(t *testing.T) { }, } for i, c := range cases { - c.Func = generator.ComposeMetricGenFuncs(clusterMetricFamilies(nil)) - c.Headers = generator.ExtractMetricFamilyHeaders(clusterMetricFamilies(nil)) + f := ClusterFactory{} + c.Func = generator.ComposeMetricGenFuncs(f.MetricFamilyGenerators(nil, nil)) + c.Headers = generator.ExtractMetricFamilyHeaders(f.MetricFamilyGenerators(nil, nil)) if err := c.run(); err != nil { t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) } diff --git a/pkg/store/factory.go b/pkg/store/factory.go new file mode 100644 index 0000000..f61a556 --- /dev/null +++ b/pkg/store/factory.go @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MIT + +package store + +import ( + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/rest" + "k8s.io/kube-state-metrics/v2/pkg/customresource" + "k8s.io/kube-state-metrics/v2/pkg/options" + clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" + controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var scheme = runtime.NewScheme() + +func init() { + _ = clusterv1.AddToScheme(scheme) + _ = controlplanev1.AddToScheme(scheme) + // +kubebuilder:scaffold:scheme +} + +type ControllerRuntimeClientFactory struct{} + +func Factories() []customresource.RegistryFactory { + return []customresource.RegistryFactory{ + &ClusterFactory{}, + &KubeadmControlPlaneFactory{}, + &MachineDeploymentFactory{}, + &MachineSetFactory{}, + &MachineFactory{}, + } +} + +func (f *ControllerRuntimeClientFactory) CreateClient(cfg *rest.Config) (interface{}, error) { + return client.NewWithWatch(cfg, client.Options{ + Scheme: scheme, + }) +} + +var DefaultResources = options.ResourceSet{} + +func init() { + if len(DefaultResources) == 0 { + for _, factory := range Factories() { + DefaultResources[factory.Name()] = struct{}{} + } + } +} diff --git a/pkg/store/kubeadmcontrolplane.go b/pkg/store/kubeadmcontrolplane.go index ed27a01..e777ed5 100644 --- a/pkg/store/kubeadmcontrolplane.go +++ b/pkg/store/kubeadmcontrolplane.go @@ -20,7 +20,19 @@ import ( var descKubeadmControlPlaneLabelsDefaultLabels = []string{"namespace", "kubeadmcontrolplane", "uid"} -func kubeadmControlPlaneMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator { +type KubeadmControlPlaneFactory struct { + *ControllerRuntimeClientFactory +} + +func (f *KubeadmControlPlaneFactory) Name() string { + return "kubeadmcontrolplanes" +} + +func (f *KubeadmControlPlaneFactory) ExpectedType() interface{} { + return &controlplanev1.KubeadmControlPlane{} +} + +func (f *KubeadmControlPlaneFactory) MetricFamilyGenerators(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { return []generator.FamilyGenerator{ *generator.NewFamilyGenerator( "capi_kubeadmcontrolplane_labels", @@ -201,7 +213,8 @@ func kubeadmControlPlaneMetricFamilies(allowLabelsList []string) []generator.Fam } } -func createKubeadmControlPlaneListWatch(ctrlClient client.WithWatch, ns string, fieldSelector string) cache.ListerWatcher { +func (f *KubeadmControlPlaneFactory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher { + ctrlClient := customResourceClient.(client.WithWatch) return &cache.ListWatch{ ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { kubeadmControlPlaneList := controlplanev1.KubeadmControlPlaneList{} diff --git a/pkg/store/kubeadmcontrolplane_test.go b/pkg/store/kubeadmcontrolplane_test.go index e6860d2..4ad7a93 100644 --- a/pkg/store/kubeadmcontrolplane_test.go +++ b/pkg/store/kubeadmcontrolplane_test.go @@ -126,8 +126,9 @@ func TestKubeadmControlPlaneStore(t *testing.T) { }, } for i, c := range cases { - c.Func = generator.ComposeMetricGenFuncs(kubeadmControlPlaneMetricFamilies(nil)) - c.Headers = generator.ExtractMetricFamilyHeaders(kubeadmControlPlaneMetricFamilies(nil)) + f := KubeadmControlPlaneFactory{} + c.Func = generator.ComposeMetricGenFuncs(f.MetricFamilyGenerators(nil, nil)) + c.Headers = generator.ExtractMetricFamilyHeaders(f.MetricFamilyGenerators(nil, nil)) if err := c.run(); err != nil { t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) } diff --git a/pkg/store/machine.go b/pkg/store/machine.go index cad98f7..b5b1a98 100644 --- a/pkg/store/machine.go +++ b/pkg/store/machine.go @@ -19,7 +19,19 @@ import ( var descMachineLabelsDefaultLabels = []string{"namespace", "machine", "uid"} -func machineMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator { +type MachineFactory struct { + *ControllerRuntimeClientFactory +} + +func (f *MachineFactory) Name() string { + return "machines" +} + +func (f *MachineFactory) ExpectedType() interface{} { + return &clusterv1.Machine{} +} + +func (f *MachineFactory) MetricFamilyGenerators(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { return []generator.FamilyGenerator{ *generator.NewFamilyGenerator( "capi_machine_labels", @@ -194,7 +206,8 @@ func machineMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator } } -func createMachineListWatch(ctrlClient client.WithWatch, ns string, fieldSelector string) cache.ListerWatcher { +func (f *MachineFactory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher { + ctrlClient := customResourceClient.(client.WithWatch) return &cache.ListWatch{ ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { machineList := clusterv1.MachineList{} diff --git a/pkg/store/machine_test.go b/pkg/store/machine_test.go index 8c7e209..0111bb5 100644 --- a/pkg/store/machine_test.go +++ b/pkg/store/machine_test.go @@ -172,8 +172,9 @@ func TestMachineStore(t *testing.T) { }, } for i, c := range cases { - c.Func = generator.ComposeMetricGenFuncs(machineMetricFamilies(nil)) - c.Headers = generator.ExtractMetricFamilyHeaders(machineMetricFamilies(nil)) + f := MachineFactory{} + c.Func = generator.ComposeMetricGenFuncs(f.MetricFamilyGenerators(nil, nil)) + c.Headers = generator.ExtractMetricFamilyHeaders(f.MetricFamilyGenerators(nil, nil)) if err := c.run(); err != nil { t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) } diff --git a/pkg/store/machinedeployment.go b/pkg/store/machinedeployment.go index c6e576b..52bdee6 100644 --- a/pkg/store/machinedeployment.go +++ b/pkg/store/machinedeployment.go @@ -20,7 +20,19 @@ import ( var descMachineDeploymentLabelsDefaultLabels = []string{"namespace", "machinedeployment", "uid"} -func machineDeploymentMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator { +type MachineDeploymentFactory struct { + *ControllerRuntimeClientFactory +} + +func (f *MachineDeploymentFactory) Name() string { + return "machinedeployments" +} + +func (f *MachineDeploymentFactory) ExpectedType() interface{} { + return &clusterv1.MachineDeployment{} +} + +func (f *MachineDeploymentFactory) MetricFamilyGenerators(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { return []generator.FamilyGenerator{ *generator.NewFamilyGenerator( "capi_machinedeployment_labels", @@ -240,7 +252,8 @@ func machineDeploymentMetricFamilies(allowLabelsList []string) []generator.Famil } } -func createMachineDeploymentListWatch(ctrlClient client.WithWatch, ns string, fieldSelector string) cache.ListerWatcher { +func (f *MachineDeploymentFactory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher { + ctrlClient := customResourceClient.(client.WithWatch) return &cache.ListWatch{ ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { machineDeploymentList := clusterv1.MachineDeploymentList{} diff --git a/pkg/store/machinedeployment_test.go b/pkg/store/machinedeployment_test.go index 6f8a11b..21b6184 100644 --- a/pkg/store/machinedeployment_test.go +++ b/pkg/store/machinedeployment_test.go @@ -142,8 +142,9 @@ func TestMachineDeploymentStore(t *testing.T) { }, } for i, c := range cases { - c.Func = generator.ComposeMetricGenFuncs(machineDeploymentMetricFamilies(nil)) - c.Headers = generator.ExtractMetricFamilyHeaders(machineDeploymentMetricFamilies(nil)) + f := MachineDeploymentFactory{} + c.Func = generator.ComposeMetricGenFuncs(f.MetricFamilyGenerators(nil, nil)) + c.Headers = generator.ExtractMetricFamilyHeaders(f.MetricFamilyGenerators(nil, nil)) if err := c.run(); err != nil { t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) } diff --git a/pkg/store/machineset.go b/pkg/store/machineset.go index da64310..98aca2b 100644 --- a/pkg/store/machineset.go +++ b/pkg/store/machineset.go @@ -19,7 +19,19 @@ import ( var descMachineSetLabelsDefaultLabels = []string{"namespace", "machineset", "uid"} -func machineSetMetricFamilies(allowLabelsList []string) []generator.FamilyGenerator { +type MachineSetFactory struct { + *ControllerRuntimeClientFactory +} + +func (f *MachineSetFactory) Name() string { + return "machinesets" +} + +func (f *MachineSetFactory) ExpectedType() interface{} { + return &clusterv1.MachineSet{} +} + +func (f *MachineSetFactory) MetricFamilyGenerators(allowAnnotationsList, allowLabelsList []string) []generator.FamilyGenerator { return []generator.FamilyGenerator{ *generator.NewFamilyGenerator( "capi_machineset_labels", @@ -151,7 +163,8 @@ func machineSetMetricFamilies(allowLabelsList []string) []generator.FamilyGenera } } -func createMachineSetListWatch(ctrlClient client.WithWatch, ns string, fieldSelector string) cache.ListerWatcher { +func (f *MachineSetFactory) ListWatch(customResourceClient interface{}, ns string, fieldSelector string) cache.ListerWatcher { + ctrlClient := customResourceClient.(client.WithWatch) return &cache.ListWatch{ ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) { machineSetList := clusterv1.MachineSetList{} diff --git a/pkg/store/machineset_test.go b/pkg/store/machineset_test.go index 997abae..34c753b 100644 --- a/pkg/store/machineset_test.go +++ b/pkg/store/machineset_test.go @@ -87,8 +87,9 @@ func TestMachineSetStore(t *testing.T) { }, } for i, c := range cases { - c.Func = generator.ComposeMetricGenFuncs(machineSetMetricFamilies(nil)) - c.Headers = generator.ExtractMetricFamilyHeaders(machineSetMetricFamilies(nil)) + f := MachineSetFactory{} + c.Func = generator.ComposeMetricGenFuncs(f.MetricFamilyGenerators(nil, nil)) + c.Headers = generator.ExtractMetricFamilyHeaders(f.MetricFamilyGenerators(nil, nil)) if err := c.run(); err != nil { t.Errorf("unexpected collecting result in %vth run:\n%s", i, err) } diff --git a/pkg/store/store.go b/pkg/store/store.go deleted file mode 100644 index 8b287ec..0000000 --- a/pkg/store/store.go +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-License-Identifier: MIT - -package store - -import ( - "k8s.io/client-go/tools/cache" - clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" - controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4" -) - -var availableStores = map[string]func(f *Builder) []cache.Store{ - "clusters": func(b *Builder) []cache.Store { return b.buildClusterStore() }, - "machines": func(b *Builder) []cache.Store { return b.buildMachineStore() }, - "machinedeployments": func(b *Builder) []cache.Store { return b.buildMachineDeploymentStore() }, - "kubeadmcontrolplanes": func(b *Builder) []cache.Store { return b.buildKubeadmControlPlaneStore() }, - "machinesets": func(b *Builder) []cache.Store { return b.buildMachineSetStore() }, -} - -func (b *Builder) buildClusterStore() []cache.Store { - return b.buildStoresFunc(clusterMetricFamilies(b.allowLabelsList["clusters"]), &clusterv1.Cluster{}, createClusterListWatch, b.useAPIServerCache) -} - -func (b *Builder) buildMachineStore() []cache.Store { - return b.buildStoresFunc(machineMetricFamilies(b.allowLabelsList["machine"]), &clusterv1.Machine{}, createMachineListWatch, b.useAPIServerCache) -} - -func (b *Builder) buildMachineDeploymentStore() []cache.Store { - return b.buildStoresFunc(machineDeploymentMetricFamilies(b.allowLabelsList["machinedeployment"]), &clusterv1.MachineDeployment{}, createMachineDeploymentListWatch, b.useAPIServerCache) -} - -func (b *Builder) buildKubeadmControlPlaneStore() []cache.Store { - return b.buildStoresFunc(kubeadmControlPlaneMetricFamilies(b.allowLabelsList["kubeadmcontrolplane"]), &controlplanev1.KubeadmControlPlane{}, createKubeadmControlPlaneListWatch, b.useAPIServerCache) -} - -func (b *Builder) buildMachineSetStore() []cache.Store { - return b.buildStoresFunc(machineSetMetricFamilies(b.allowLabelsList["machineset"]), &clusterv1.MachineSet{}, createMachineSetListWatch, b.useAPIServerCache) -}