Skip to content

Commit

Permalink
Fix parsing group resource to allow groups with periods (#1697)
Browse files Browse the repository at this point in the history
* Fix parsing group resource to allow groups with periods

* remove GetVerb
  • Loading branch information
adel121 authored Feb 18, 2025
1 parent 0cc6abb commit 14fc1bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
34 changes: 5 additions & 29 deletions internal/controller/datadogagent/feature/enabledefault/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ package enabledefault

import (
"fmt"
"strings"

rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/DataDog/datadog-operator/internal/controller/datadogagent/common"
"github.com/DataDog/datadog-operator/pkg/constants"
Expand Down Expand Up @@ -317,27 +317,6 @@ func getDefaultClusterChecksRunnerClusterRolePolicyRules(dda metav1.Object, excl
return policyRule
}

// Used for kubernetesResourcesLabelsAsTags and kubernetesResourcesLabelsAsTags

func extractGroupAndResource(groupResource string) (group string, resource string, ok bool) {
parts := strings.Split(groupResource, ".")

switch len(parts) {
case 1:
group = ""
resource = parts[0]
ok = true
case 2:
group = parts[1]
resource = parts[0]
ok = true
default:
ok = false
}

return group, resource, ok
}

func appendGroupResource(groupResourceAccumulator map[string]map[string]struct{}, group string, resource string) map[string]map[string]struct{} {
if _, exists := groupResourceAccumulator[group]; !exists {
groupResourceAccumulator[group] = map[string]struct{}{resource: {}}
Expand All @@ -354,15 +333,13 @@ func getKubernetesResourceMetadataAsTagsPolicyRules(resourcesLabelsAsTags, resou
groupResourceAccumulator := map[string]map[string]struct{}{}

for groupResource := range resourcesLabelsAsTags {
if group, resource, ok := extractGroupAndResource(groupResource); ok {
groupResourceAccumulator = appendGroupResource(groupResourceAccumulator, group, resource)
}
gr := schema.ParseGroupResource(groupResource)
groupResourceAccumulator = appendGroupResource(groupResourceAccumulator, gr.Group, gr.Resource)
}

for groupResource := range resourcesAnnotationsAsTags {
if group, resource, ok := extractGroupAndResource(groupResource); ok {
groupResourceAccumulator = appendGroupResource(groupResourceAccumulator, group, resource)
}
gr := schema.ParseGroupResource(groupResource)
groupResourceAccumulator = appendGroupResource(groupResourceAccumulator, gr.Group, gr.Resource)
}

policyRules := make([]rbacv1.PolicyRule, 0)
Expand All @@ -373,7 +350,6 @@ func getKubernetesResourceMetadataAsTagsPolicyRules(resourcesLabelsAsTags, resou
APIGroups: []string{group},
Resources: []string{resource},
Verbs: []string{
rbac.GetVerb,
rbac.ListVerb,
rbac.WatchVerb,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func TestGetKubernetesResourceMetadataAsTagsPolicyRules(t *testing.T) {
"foo": "baz",
"bar": "bar",
},
"metrics.custom.metrics.group": {
"foo": "baz",
"bar": "bar",
},
}

annotationsAsTags := map[string]map[string]string{
Expand All @@ -41,7 +45,6 @@ func TestGetKubernetesResourceMetadataAsTagsPolicyRules(t *testing.T) {
APIGroups: []string{""},
Resources: []string{"pods"},
Verbs: []string{
rbac.GetVerb,
rbac.ListVerb,
rbac.WatchVerb,
},
Expand All @@ -50,7 +53,14 @@ func TestGetKubernetesResourceMetadataAsTagsPolicyRules(t *testing.T) {
APIGroups: []string{"apps"},
Resources: []string{"deployments"},
Verbs: []string{
rbac.GetVerb,
rbac.ListVerb,
rbac.WatchVerb,
},
},
{
APIGroups: []string{"custom.metrics.group"},
Resources: []string{"metrics"},
Verbs: []string{
rbac.ListVerb,
rbac.WatchVerb,
},
Expand Down

0 comments on commit 14fc1bb

Please sign in to comment.