generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Dynamic module discovery in kymastatsreceiver (#80)
- Loading branch information
Showing
13 changed files
with
361 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
receiver/kymastatsreceiver/internal/modulediscovery/client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package modulediscovery | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"go.uber.org/zap" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/discovery" | ||
) | ||
|
||
type Client struct { | ||
discovery discovery.DiscoveryInterface | ||
logger *zap.Logger | ||
moduleGroups []string | ||
} | ||
|
||
func New(discovery discovery.DiscoveryInterface, logger *zap.Logger, moduleGroups []string) *Client { | ||
return &Client{ | ||
discovery: discovery, | ||
logger: logger, | ||
moduleGroups: moduleGroups, | ||
} | ||
} | ||
|
||
func (c *Client) Discover() ([]schema.GroupVersionResource, error) { | ||
// ServerPreferredResources returns API resources/groups of the preferred (usually, stored) API version. | ||
// It guarantees that only version per resource/group is returned. | ||
resourceLists, err := c.discovery.ServerPreferredResources() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to discover preferred resources: %w", err) | ||
} | ||
|
||
var gvrs []schema.GroupVersionResource | ||
for _, resourceList := range resourceLists { | ||
groupVersion, err := schema.ParseGroupVersion(resourceList.GroupVersion) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse groupVersion %s: %w", resourceList.GroupVersion, err) | ||
} | ||
|
||
if !slices.Contains(c.moduleGroups, groupVersion.Group) { | ||
continue | ||
} | ||
|
||
c.logger.Debug("Discovered module group", zap.Any("groupVersion", groupVersion)) | ||
|
||
for _, resource := range resourceList.APIResources { | ||
gvr := groupVersion.WithResource(resource.Name) | ||
if isSubresource(resource.Name) { | ||
c.logger.Debug("Skipping subresource", zap.Any("groupVersionResource", gvr)) | ||
continue | ||
} | ||
|
||
gvrs = append(gvrs, gvr) | ||
|
||
c.logger.Debug("Discovered module resource", zap.Any("groupVersionResource", gvr)) | ||
} | ||
} | ||
|
||
return gvrs, nil | ||
} | ||
|
||
func isSubresource(resourceName string) bool { | ||
return strings.Contains(resourceName, "/") | ||
} |
Oops, something went wrong.