Skip to content

Commit 9160ce0

Browse files
authored
Add replicaset resources by default when using the k8sattributesprocessor (#2832)
* Add replicaset resources by default Signed-off-by: Israel Blancas <[email protected]> * Add missing changelog Signed-off-by: Israel Blancas <[email protected]> * Fix changelog Signed-off-by: Israel Blancas <[email protected]> * Remove not needed permissions Signed-off-by: Israel Blancas <[email protected]> --------- Signed-off-by: Israel Blancas <[email protected]>
1 parent b58917e commit 9160ce0

File tree

3 files changed

+33
-48
lines changed

3 files changed

+33
-48
lines changed

.chloggen/bug_2823.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Create automatically the RBAC permissions to manage replicasets when using the k8sattributesprocessor"
9+
10+
# One or more tracking issues related to the change
11+
issues: [2823]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

internal/manifests/collector/parser/processor/processor_k8sattributes.go

+13-22
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ func (o *K8sAttributesParser) ParserName() string {
5050
}
5151

5252
func (o *K8sAttributesParser) GetRBACRules() []rbacv1.PolicyRule {
53-
var prs []rbacv1.PolicyRule
54-
55-
// This one needs to be added always
56-
policy := rbacv1.PolicyRule{
57-
APIGroups: []string{""},
58-
Resources: []string{"pods", "namespaces"},
59-
Verbs: []string{"get", "watch", "list"},
53+
// These policies need to be added always
54+
var prs []rbacv1.PolicyRule = []rbacv1.PolicyRule{
55+
{
56+
APIGroups: []string{""},
57+
Resources: []string{"pods", "namespaces"},
58+
Verbs: []string{"get", "watch", "list"},
59+
},
60+
{
61+
APIGroups: []string{"apps"},
62+
Resources: []string{"replicasets"},
63+
Verbs: []string{"get", "watch", "list"},
64+
},
6065
}
61-
prs = append(prs, policy)
6266

6367
extractCfg, ok := o.config["extract"]
6468
if !ok {
@@ -77,20 +81,7 @@ func (o *K8sAttributesParser) GetRBACRules() []rbacv1.PolicyRule {
7781

7882
for _, m := range metadata {
7983
metadataField := fmt.Sprint(m)
80-
if metadataField == "k8s.deployment.uid" || metadataField == "k8s.deployment.name" {
81-
prs = append(prs,
82-
rbacv1.PolicyRule{
83-
APIGroups: []string{"apps"},
84-
Resources: []string{"replicasets"},
85-
Verbs: []string{"get", "watch", "list"},
86-
},
87-
rbacv1.PolicyRule{
88-
APIGroups: []string{"extensions"},
89-
Resources: []string{"replicasets"},
90-
Verbs: []string{"get", "watch", "list"},
91-
},
92-
)
93-
} else if strings.Contains(metadataField, "k8s.node") {
84+
if strings.Contains(metadataField, "k8s.node") {
9485
prs = append(prs,
9586
rbacv1.PolicyRule{
9687
APIGroups: []string{""},

internal/manifests/collector/parser/processor/processor_k8sattributes_test.go

+4-26
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ func TestK8sAttributesRBAC(t *testing.T) {
3434
{
3535
name: "no extra parameters",
3636
config: nil,
37-
expectedRules: []rbacv1.PolicyRule{
38-
{
39-
APIGroups: []string{""},
40-
Resources: []string{"pods", "namespaces"},
41-
Verbs: []string{"get", "watch", "list"},
42-
},
43-
},
44-
},
45-
{
46-
name: "extract k8s.deployment.uid",
47-
config: map[interface{}]interface{}{
48-
"extract": map[interface{}]interface{}{
49-
"metadata": []interface{}{
50-
"k8s.deployment.uid",
51-
},
52-
},
53-
},
5437
expectedRules: []rbacv1.PolicyRule{
5538
{
5639
APIGroups: []string{""},
@@ -62,19 +45,14 @@ func TestK8sAttributesRBAC(t *testing.T) {
6245
Resources: []string{"replicasets"},
6346
Verbs: []string{"get", "watch", "list"},
6447
},
65-
{
66-
APIGroups: []string{"extensions"},
67-
Resources: []string{"replicasets"},
68-
Verbs: []string{"get", "watch", "list"},
69-
},
7048
},
7149
},
7250
{
73-
name: "extract k8s.deployment.name",
51+
name: "extract k8s.node",
7452
config: map[interface{}]interface{}{
7553
"extract": map[interface{}]interface{}{
7654
"metadata": []interface{}{
77-
"k8s.deployment.name",
55+
"k8s.node",
7856
},
7957
},
8058
},
@@ -90,8 +68,8 @@ func TestK8sAttributesRBAC(t *testing.T) {
9068
Verbs: []string{"get", "watch", "list"},
9169
},
9270
{
93-
APIGroups: []string{"extensions"},
94-
Resources: []string{"replicasets"},
71+
APIGroups: []string{""},
72+
Resources: []string{"nodes"},
9573
Verbs: []string{"get", "watch", "list"},
9674
},
9775
},

0 commit comments

Comments
 (0)