-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose kubernetes_resources_labels/annotations_as_tags (#1379)
* expose kubernetes_resources_labels/annotations_as_tags * fix lint * rename clusterRole --------- Co-authored-by: Fanny Jiang <fanny.jiang@datadoghq.com>
- Loading branch information
1 parent
fe0188f
commit 26068ba
Showing
10 changed files
with
267 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package override | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/DataDog/datadog-operator/pkg/kubernetes/rbac" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
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: {}} | ||
} else { | ||
groupResourceAccumulator[group][resource] = struct{}{} | ||
} | ||
|
||
return groupResourceAccumulator | ||
} | ||
|
||
func getKubernetesResourceMetadataAsTagsPolicyRules(resourcesLabelsAsTags, resourcesAnnotationsAsTags map[string]map[string]string) []rbacv1.PolicyRule { | ||
// maps group to resource set | ||
// using map to avoid duplicates | ||
groupResourceAccumulator := map[string]map[string]struct{}{} | ||
|
||
for groupResource := range resourcesLabelsAsTags { | ||
if group, resource, ok := extractGroupAndResource(groupResource); ok { | ||
groupResourceAccumulator = appendGroupResource(groupResourceAccumulator, group, resource) | ||
} | ||
} | ||
|
||
for groupResource := range resourcesAnnotationsAsTags { | ||
if group, resource, ok := extractGroupAndResource(groupResource); ok { | ||
groupResourceAccumulator = appendGroupResource(groupResourceAccumulator, group, resource) | ||
} | ||
} | ||
|
||
policyRules := make([]rbacv1.PolicyRule, 0) | ||
|
||
for group, resources := range groupResourceAccumulator { | ||
for resource := range resources { | ||
policyRules = append(policyRules, rbacv1.PolicyRule{ | ||
APIGroups: []string{group}, | ||
Resources: []string{resource}, | ||
Verbs: []string{ | ||
rbac.GetVerb, | ||
rbac.ListVerb, | ||
rbac.WatchVerb, | ||
}, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
return policyRules | ||
} |
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,63 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
package override | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/DataDog/datadog-operator/pkg/kubernetes/rbac" | ||
assert "github.com/stretchr/testify/require" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
func TestGetKubernetesResourceMetadataAsTagsPolicyRules(t *testing.T) { | ||
labelsAsTags := map[string]map[string]string{ | ||
"pods": { | ||
"foo": "bar", | ||
"bar": "bar", | ||
}, | ||
"deployments.apps": { | ||
"foo": "baz", | ||
"bar": "bar", | ||
}, | ||
} | ||
|
||
annotationsAsTags := map[string]map[string]string{ | ||
"pods": { | ||
"foo": "bar", | ||
"bar": "bar", | ||
}, | ||
"deployments.apps": { | ||
"foo": "baz", | ||
"bar": "bar", | ||
}, | ||
} | ||
|
||
expectedRules := []rbacv1.PolicyRule{ | ||
{ | ||
APIGroups: []string{""}, | ||
Resources: []string{"pods"}, | ||
Verbs: []string{ | ||
rbac.GetVerb, | ||
rbac.ListVerb, | ||
rbac.WatchVerb, | ||
}, | ||
}, | ||
{ | ||
APIGroups: []string{"apps"}, | ||
Resources: []string{"deployments"}, | ||
Verbs: []string{ | ||
rbac.GetVerb, | ||
rbac.ListVerb, | ||
rbac.WatchVerb, | ||
}, | ||
}, | ||
} | ||
|
||
rules := getKubernetesResourceMetadataAsTagsPolicyRules(labelsAsTags, annotationsAsTags) | ||
|
||
assert.ElementsMatch(t, expectedRules, rules) | ||
} |
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