-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CONTP-376] expose kubernetes_resources_labels/annotations_as_tags #1379
Merged
fanny-jiang
merged 6 commits into
main
from
adelhajhassan/expose_kubernetes_labels_as_tags_and_annotations_as_tags
Oct 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a67116
expose kubernetes_resources_labels/annotations_as_tags
adel121 d8c717b
Merge branch 'main' into adelhajhassan/expose_kubernetes_labels_as_ta…
adel121 e6633ec
fix lint
adel121 e775384
Merge remote-tracking branch 'refs/remotes/origin/main' into adelhajh…
fanny-jiang 7936fef
Merge branch 'main' into adelhajhassan/expose_kubernetes_labels_as_ta…
fanny-jiang b602673
rename clusterRole
fanny-jiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||
} | ||
|
||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Code Quality Violation
avoid bare return (...read more)
The "Avoid bare returns" rule in Go static analysis is designed to increase clarity and readability in your code. A bare return is when a function that has named return values returns those values implicitly, without explicitly stating what is being returned.
While Go's allowance for bare returns can make code more concise, it can also make it more difficult to understand and debug, especially in larger functions. Implicitly relying on the state of named return values can lead to unexpected behavior if those values are modified elsewhere in the function.
To adhere to this rule and promote better coding practices, always explicitly return values in your functions. This makes it clear what values are being returned and in what state, reducing the chance of bugs and making your code easier to understand. For example, instead of writing
return
in a function that returns anint
and abool
, writereturn 0, false
.Learn More