Skip to content

Commit

Permalink
fixup! fixup! fixup! Allow optional VK in CR metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
rexagod committed Nov 4, 2022
1 parent ed9d193 commit f973b82
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/customresourcestate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func FromConfig(decoder ConfigDecoder, opts *options.Options) ([]customresource.
return nil, fmt.Errorf("failed to create metrics factory for %s: %w", resource.GroupVersionKind, err)
}
// factory.Name() == kind, and will fail if no kind is specified.
if _, ok := factoriesIndex[factory.Name()]; ok {
if _, ok := factoriesIndex[factory.Name()]; ok && factory.Name() != "" {
return nil, fmt.Errorf("found multiple custom resource configurations for the same resource %s", factory.Name())
}
factoriesIndex[factory.Name()] = true
Expand Down
64 changes: 46 additions & 18 deletions pkg/customresourcestate/custom_resource_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import (
// interface which provides metrics for custom resources defined in a configuration file.
type customResourceMetrics struct {
MetricNamePrefix string
GroupVersionKind schema.GroupVersionKind
ResourceName string
Families []compiledFamily
GroupVersionKind []schema.GroupVersionKind
ResourceName []string
Families [][]compiledFamily
}

var _ customresource.RegistryFactory = &customResourceMetrics{}
Expand All @@ -55,18 +55,19 @@ func NewCustomResourceMetrics(resource Resource) (customresource.RegistryFactory
gvk := schema.GroupVersionKind(resource.GroupVersionKind)
return &customResourceMetrics{
MetricNamePrefix: resource.GetMetricNamePrefix(),
GroupVersionKind: gvk,
Families: compiled,
ResourceName: resource.GetResourceName(),
GroupVersionKind: []schema.GroupVersionKind{gvk},
Families: [][]compiledFamily{compiled},
ResourceName: []string{resource.GetResourceName()},
}, nil
}

func (s customResourceMetrics) Name() string {
return s.ResourceName
// Assuming VK are not missing.
return s.ResourceName[0]
}

// fillGVR fills the missing Version or Resource fields in the provided GroupVersionResource.
func fillGVR(gotGVR schema.GroupVersionResource, d *discovery.DiscoveryClient) (wantGVRs []schema.GroupVersionResource) {
func fillGVR(gotGVR schema.GroupVersionResource, d *discovery.DiscoveryClient) (wantGVRs []schema.GroupVersionResource, wantGVKs []schema.GroupVersionKind) {

// Fetch groups and their resources.
_, discoveredResources, err := d.ServerGroupsAndResources()
Expand All @@ -75,7 +76,7 @@ func fillGVR(gotGVR schema.GroupVersionResource, d *discovery.DiscoveryClient) (
}

// Collect resources in a map to remove duplicates.
t := map[schema.GroupVersionResource]bool{}
t := map[schema.GroupVersionResource]schema.GroupVersionKind{}

// Allows resetting the modified GVR to the original one.
originalGotGVR := gotGVR
Expand Down Expand Up @@ -122,18 +123,27 @@ func fillGVR(gotGVR schema.GroupVersionResource, d *discovery.DiscoveryClient) (
}
// Collect all possible GVKs for the current config.
gotGVR.Version = discoveredVersion
t[gotGVR] = true
t[gotGVR] = schema.GroupVersionKind{
Group: discoveredGroup,
Version: discoveredVersion,
Kind: discoveredAPIResource.Kind,
}
gotGVR = originalGotGVR
}
}
}
}

// Convert map[GVR]bool to []GVR, so that it can be ingested further.
// Convert map[GVR]GVK to []GVR, so that it can be ingested further.
for gvr := range t {
wantGVRs = append(wantGVRs, gvr)
}

// Convert map[GVR]GVK to []GVK, so that it can be ingested further.
for _, gvk := range t {
wantGVKs = append(wantGVKs, gvk)
}

return
}

Expand All @@ -143,30 +153,47 @@ func (s customResourceMetrics) CreateClient(cfg *rest.Config) ([]interface{}, er
return nil, err
}
gotGVR := schema.GroupVersionResource{
Group: s.GroupVersionKind.Group,
Version: s.GroupVersionKind.Version,
Resource: s.ResourceName,
Group: s.GroupVersionKind[0].Group,
Version: s.GroupVersionKind[0].Version,
Resource: s.ResourceName[0],
}
wantGVRs := fillGVR(gotGVR, discovery.NewDiscoveryClientForConfigOrDie(cfg))
wantGVRs, wantGVKs := fillGVR(gotGVR, discovery.NewDiscoveryClientForConfigOrDie(cfg))
var out []interface{}
for _, wantGVR := range wantGVRs {
out = append(out, c.Resource(wantGVR))
}
s.GroupVersionKind = wantGVKs

// Populate s.ResourceName with the resource names.
var gvrSet []string
for _, gvr := range wantGVRs {
gvrSet = append(gvrSet, gvr.Resource)
}
s.ResourceName = gvrSet

// Populate s.Families.Labels with the label names.
//for i := range s.Families {
// if s.Families[i].Labels["kind"] == nil {
// s.Families[i].Labels["kind"] =
// }
//}
return out, nil
}

func (s customResourceMetrics) MetricFamilyGenerators(_, _ []string) (result []generator.FamilyGenerator) {
klog.InfoS("Custom resource state added metrics", "familyNames", s.names())
for _, f := range s.Families {
result = append(result, famGen(f))
// Assume that the VK is not missing.
result = append(result, famGen(f[0]))
}

return result
}

func (s customResourceMetrics) ExpectedType() interface{} {
u := unstructured.Unstructured{}
u.SetGroupVersionKind(s.GroupVersionKind)
// Assume that VK are not missing.
u.SetGroupVersionKind(s.GroupVersionKind[0])
return &u
}

Expand All @@ -187,7 +214,8 @@ func (s customResourceMetrics) ListWatch(customResourceClient interface{}, ns st

func (s customResourceMetrics) names() (names []string) {
for _, family := range s.Families {
names = append(names, family.Name)
// Assume that the VK is not missing.
names = append(names, family[0].Name)
}
return names
}

0 comments on commit f973b82

Please sign in to comment.