Skip to content

Commit

Permalink
Allow optional VK in CR metrics
Browse files Browse the repository at this point in the history
Allow optional VK in CR metrics, while only requiring the group to be
fixed. This would allow users to define custom metrics for:
* a specific API version: version is fixed, kind varies.
* all the API versions of a resource: version varies, kind is fixed.
* all the APIs part of an API group: version varies, kind varies.

Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
  • Loading branch information
rexagod committed Oct 7, 2022
1 parent 12402a5 commit 09bec65
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/customresourcestate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"strings"

"github.com/gobuffalo/flect"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"

"k8s.io/kube-state-metrics/v2/pkg/customresource"
Expand Down Expand Up @@ -161,6 +164,37 @@ type ConfigDecoder interface {
Decode(v interface{}) (err error)
}

// generateResources generates the set of possible resources from the varying V and, or K of the original config.
func generateResources(resources []Resource, config *rest.Config) {
d := discovery.NewDiscoveryClientForConfigOrDie(config)
var found []v1.APIResource
var got *v1.APIResourceList
var err error
for _, resource := range resources {
gvk := resource.GroupVersionKind
switch {
// - groupVersionKind:
// group: "apps"
case gvk.Version == "" && gvk.Kind == "":
// - groupVersionKind:
// group: "apps"
// kind: "Deployment"
case gvk.Version == "" && gvk.Kind != "":
// - groupVersionKind:
// group: "apps"
// version: "v1"
case gvk.Version != "" && gvk.Kind == "":
got, err = d.ServerResourcesForGroupVersion(gvk.Group + "/" + gvk.Version)
if err != nil {
klog.Errorf("Error getting resources for groupVersion %s/%s: %v", gvk.Group, gvk.Version, err)
return
}
}
found = append(found, got.APIResources...)
}
// Convert each found element into a Resource.
}

// FromConfig decodes a configuration source into a slice of customresource.RegistryFactory that are ready to use.
func FromConfig(decoder ConfigDecoder) ([]customresource.RegistryFactory, error) {
var crconfig Metrics
Expand Down

0 comments on commit 09bec65

Please sign in to comment.