forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Role, RoleBinding, ClusterRole, ClusterRoleBinding and ServiceA…
…ccount modules (#45) * ENG-947 & ENG-1241: Removed Persistent-Volume from transformObject flow * ENG-947: Added Persistent-Volume-Claim flow * ENG-1948, ENG-1949 & ENG-1955: Added Role, RoleBinding, ClusterRole, ClusterRoleBinding and ServiceAccount modules * ENG-1948, ENG-1949 & ENG-1955: Added Role, RoleBinding, ClusterRole, ClusterRoleBinding and ServiceAccount modules
- Loading branch information
1 parent
db49257
commit 9d719b2
Showing
22 changed files
with
2,888 additions
and
381 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
98 changes: 98 additions & 0 deletions
98
receiver/k8sclusterreceiver/internal/clusterrole/clusterrole.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,98 @@ | ||
package clusterrole | ||
|
||
import ( | ||
"fmt" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/maps" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
"strings" | ||
"time" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata" | ||
imetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata" | ||
) | ||
|
||
const ( | ||
// Keys for clusterrole metadata. | ||
AttributeK8SClusterRoleUID = "k8s.clusterrole.uid" | ||
AttributeK8SClusterRoleName = "k8s.clusterrole.name" | ||
ClusterRoleCreationTime = "clusterrole.creation_timestamp" | ||
) | ||
|
||
// Transform transforms the clusterrole to remove the fields. | ||
// IMPORTANT: Make sure to update this function before using new clusterrole fields. | ||
func Transform(r *rbacv1.ClusterRole) *rbacv1.ClusterRole { | ||
newCR := &rbacv1.ClusterRole{ | ||
ObjectMeta: metadata.TransformObjectMeta(r.ObjectMeta), | ||
} | ||
return newCR | ||
} | ||
|
||
func RecordMetrics(mb *imetadata.MetricsBuilder, cr *rbacv1.ClusterRole, ts pcommon.Timestamp) { | ||
mb.RecordK8sClusterroleRuleCountDataPoint(ts, int64(len(cr.Rules))) | ||
|
||
rb := mb.NewResourceBuilder() | ||
rb.SetK8sClusterroleUID(string(cr.GetUID())) | ||
rb.SetK8sClusterroleName(cr.GetName()) | ||
rb.SetK8sClusterName("unknown") | ||
rb.SetK8sClusterroleType("ClusterRole") | ||
rb.SetK8sClusterroleStartTime(cr.GetCreationTimestamp().String()) | ||
rb.SetK8sClusterroleLabels(mapToString(cr.GetLabels(), "&")) | ||
rb.SetK8sClusterroleAnnotations(mapToString(cr.GetAnnotations(), "&")) | ||
rb.SetK8sClusterroleRules(convertRulesToString(cr.Rules)) | ||
mb.EmitForResource(metadata.WithResource(rb.Emit())) | ||
} | ||
|
||
func mapToString(m map[string]string, seperator string) string { | ||
var res []string | ||
for k, v := range m { | ||
res = append(res, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return strings.Join(res, seperator) | ||
} | ||
|
||
func convertRulesToString(rules []rbacv1.PolicyRule) string { | ||
var result strings.Builder | ||
|
||
for i, rule := range rules { | ||
if i > 0 { | ||
result.WriteString(";") | ||
} | ||
|
||
result.WriteString("verbs=") | ||
result.WriteString(strings.Join(rule.Verbs, ",")) | ||
|
||
result.WriteString("&apiGroups=") | ||
result.WriteString(strings.Join(rule.APIGroups, ",")) | ||
|
||
result.WriteString("&resources=") | ||
result.WriteString(strings.Join(rule.Resources, ",")) | ||
|
||
result.WriteString("&resourceNames=") | ||
result.WriteString(strings.Join(rule.ResourceNames, ",")) | ||
|
||
result.WriteString("&nonResourceURLs=") | ||
result.WriteString(strings.Join(rule.NonResourceURLs, ",")) | ||
|
||
} | ||
|
||
return result.String() | ||
} | ||
|
||
func GetMetadata(r *rbacv1.ClusterRole) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata { | ||
meta := maps.MergeStringMaps(map[string]string{}, r.Labels) | ||
|
||
meta[AttributeK8SClusterRoleName] = r.Name | ||
meta[ClusterRoleCreationTime] = r.GetCreationTimestamp().Format(time.RFC3339) | ||
|
||
rID := experimentalmetricmetadata.ResourceID(r.UID) | ||
return map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{ | ||
rID: { | ||
EntityType: "k8s.clusterrole", | ||
ResourceIDKey: AttributeK8SClusterRoleUID, | ||
ResourceID: rID, | ||
Metadata: meta, | ||
}, | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
receiver/k8sclusterreceiver/internal/clusterrole/clusterrole_test.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,24 @@ | ||
package clusterrole | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"testing" | ||
) | ||
|
||
func TestTransform(t *testing.T) { | ||
originalCR := &rbacv1.ClusterRole{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cr", | ||
UID: "my-cr-uid", | ||
}, | ||
} | ||
wantCR := &rbacv1.ClusterRole{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-cr", | ||
UID: "my-cr-uid", | ||
}, | ||
} | ||
assert.Equal(t, wantCR, Transform(originalCR)) | ||
} |
95 changes: 95 additions & 0 deletions
95
receiver/k8sclusterreceiver/internal/clusterrolebinding/clusterrolebinding.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,95 @@ | ||
package clusterrolebinding | ||
|
||
import ( | ||
"fmt" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/maps" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
"strings" | ||
"time" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata" | ||
imetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata" | ||
) | ||
|
||
const ( | ||
// Keys for clusterrolebinding metadata. | ||
AttributeK8SClusterRoleBindingUID = "k8s.clusterrolebinding.uid" | ||
AttributeK8SClusterRoleBindingName = "k8s.clusterrolebinding.name" | ||
ClusterRoleBindingCreationTime = "clusterrolebinding.creation_timestamp" | ||
) | ||
|
||
// Transform transforms the clusterrolebinding to remove the fields. | ||
// IMPORTANT: Make sure to update this function before using new clusterrolebinding fields. | ||
func Transform(rb *rbacv1.ClusterRoleBinding) *rbacv1.ClusterRoleBinding { | ||
newCRB := &rbacv1.ClusterRoleBinding{ | ||
ObjectMeta: metadata.TransformObjectMeta(rb.ObjectMeta), | ||
} | ||
return newCRB | ||
} | ||
|
||
func RecordMetrics(mb *imetadata.MetricsBuilder, crbind *rbacv1.ClusterRoleBinding, ts pcommon.Timestamp) { | ||
mb.RecordK8sClusterrolebindingSubjectCountDataPoint(ts, int64(len(crbind.Subjects))) | ||
|
||
rb := mb.NewResourceBuilder() | ||
rb.SetK8sClusterrolebindingUID(string(crbind.GetUID())) | ||
rb.SetK8sClusterrolebindingName(crbind.GetName()) | ||
rb.SetK8sClusterName("unknown") | ||
rb.SetK8sClusterrolebindingLabels(mapToString(crbind.GetLabels(), "&")) | ||
rb.SetK8sClusterrolebindingAnnotations(mapToString(crbind.GetAnnotations(), "&")) | ||
rb.SetK8sClusterrolebindingStartTime(crbind.GetCreationTimestamp().String()) | ||
rb.SetK8sClusterrolebindingType("ClusterRoleBinding") | ||
rb.SetK8sClusterrolebindingSubjects(convertSubjectsToString(crbind.Subjects)) | ||
rb.SetK8sClusterrolebindingRoleRef(fmt.Sprintf("apiGroup=%s&kind=%s&name=%s", | ||
crbind.RoleRef.APIGroup, | ||
crbind.RoleRef.Kind, | ||
crbind.RoleRef.Name)) | ||
mb.EmitForResource(metadata.WithResource(rb.Emit())) | ||
} | ||
|
||
func mapToString(m map[string]string, seperator string) string { | ||
var res []string | ||
for k, v := range m { | ||
res = append(res, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return strings.Join(res, seperator) | ||
} | ||
|
||
func convertSubjectsToString(subjects []rbacv1.Subject) string { | ||
var result strings.Builder | ||
|
||
for i, subject := range subjects { | ||
if i > 0 { | ||
result.WriteString(";") | ||
} | ||
|
||
result.WriteString("kind=") | ||
result.WriteString(subject.Kind) | ||
|
||
result.WriteString("&name=") | ||
result.WriteString(subject.Name) | ||
|
||
result.WriteString("&namespace=") | ||
result.WriteString(subject.Namespace) | ||
} | ||
|
||
return result.String() | ||
} | ||
|
||
func GetMetadata(crb *rbacv1.ClusterRoleBinding) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata { | ||
meta := maps.MergeStringMaps(map[string]string{}, crb.Labels) | ||
|
||
meta[AttributeK8SClusterRoleBindingName] = crb.Name | ||
meta[ClusterRoleBindingCreationTime] = crb.GetCreationTimestamp().Format(time.RFC3339) | ||
|
||
crbID := experimentalmetricmetadata.ResourceID(crb.UID) | ||
return map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{ | ||
crbID: { | ||
EntityType: "k8s.clusterrolebinding", | ||
ResourceIDKey: AttributeK8SClusterRoleBindingUID, | ||
ResourceID: crbID, | ||
Metadata: meta, | ||
}, | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
receiver/k8sclusterreceiver/internal/clusterrolebinding/clusterrolebinding_test.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,24 @@ | ||
package clusterrolebinding | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"testing" | ||
) | ||
|
||
func TestTransform(t *testing.T) { | ||
originalCRB := &rbacv1.ClusterRoleBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-crb", | ||
UID: "my-crb-uid", | ||
}, | ||
} | ||
wantCRB := &rbacv1.ClusterRoleBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-crb", | ||
UID: "my-crb-uid", | ||
}, | ||
} | ||
assert.Equal(t, wantCRB, Transform(originalCRB)) | ||
} |
Oops, something went wrong.