Skip to content

Commit fdcb159

Browse files
Add Deployment "selector" facet
1 parent ce31647 commit fdcb159

File tree

7 files changed

+596
-7
lines changed

7 files changed

+596
-7
lines changed

components/processors/observek8sattributesprocessor/daemonsetactions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import (
66
)
77

88
const (
9-
DaemonsetSelectorAttributeKey = "selector"
9+
DaemonSetSelectorAttributeKey = "selector"
1010
)
1111

1212
type DaemonSetSelectorAction struct{}
1313

14-
func NewDaemonsetSelectorAction() DaemonSetSelectorAction {
14+
func NewDaemonSetSelectorAction() DaemonSetSelectorAction {
1515
return DaemonSetSelectorAction{}
1616
}
1717

1818
// ---------------------------------- DaemonSet "selector" ----------------------------------
1919

20-
// Generates the Daemonset "selector" facet.
20+
// Generates the DaemonSet "selector" facet.
2121
func (DaemonSetSelectorAction) ComputeAttributes(daemonset appsv1.DaemonSet) (attributes, error) {
22-
selectorString := metav1.FormatLabelSelector(daemonset.Spec.Selector)
23-
return attributes{DaemonsetSelectorAttributeKey: selectorString}, nil
22+
selecotString := metav1.FormatLabelSelector(daemonset.Spec.Selector)
23+
return attributes{DaemonSetSelectorAttributeKey: selecotString}, nil
2424
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package observek8sattributesprocessor
2+
3+
import (
4+
appsv1 "k8s.io/api/apps/v1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
const (
9+
DeploymentSelectorAttributeKey = "selector"
10+
)
11+
12+
type DeploymentSelectorAction struct{}
13+
14+
func NewDeploymentSelectorAction() DeploymentSelectorAction {
15+
return DeploymentSelectorAction{}
16+
}
17+
18+
// ---------------------------------- Deployment "selector" ----------------------------------
19+
20+
// Generates the Deployment "selector" facet.
21+
func (DeploymentSelectorAction) ComputeAttributes(deployment appsv1.Deployment) (attributes, error) {
22+
selecotString := metav1.FormatLabelSelector(deployment.Spec.Selector)
23+
return attributes{DeploymentSelectorAttributeKey: selecotString}, nil
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package observek8sattributesprocessor
2+
3+
import "testing"
4+
5+
func TestDeploymentActions(t *testing.T) {
6+
for _, testCase := range []k8sEventProcessorTest{
7+
{
8+
name: "Pretty print of a DaemonSet's selector",
9+
inLogs: resourceLogsFromSingleJsonEvent("./testdata/deploymentEvent.json"),
10+
expectedResults: []queryWithResult{
11+
{"observe_transform.facets.selector", "app.kubernetes.io/instance=observe-agent,app.kubernetes.io/name=deployment-cluster-events,component=standalone-collector"},
12+
},
13+
},
14+
} {
15+
runTest(t, testCase)
16+
}
17+
}

components/processors/observek8sattributesprocessor/processor.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
// APPS
2828
EventKindStatefulSet = "StatefulSet"
2929
EventKindDaemonSet = "DaemonSet"
30+
EventKindDeployment = "Deployment"
3031
// WORKLOAD
3132
EventKindJob = "Job"
3233
EventKindCronJob = "CronJob"
@@ -53,6 +54,7 @@ type K8sEventsProcessor struct {
5354

5455
daemonSetActions []daemonSetAction
5556
statefulSetActions []statefulSetAction
57+
deploymentActions []deploymentAction
5658

5759
persistentVolumeActions []persistentVolumeAction
5860
persistentVolumeClaimActions []persistentVolumeClaimAction
@@ -90,11 +92,14 @@ func newK8sEventsProcessor(logger *zap.Logger, cfg component.Config) *K8sEventsP
9092
NewCronJobActiveAction(),
9193
},
9294

95+
deploymentActions: []deploymentAction{
96+
NewDeploymentSelectorAction(),
97+
},
9398
statefulSetActions: []statefulSetAction{
9499
NewStatefulsetSelectorAction(),
95100
},
96101
daemonSetActions: []daemonSetAction{
97-
NewDaemonsetSelectorAction(),
102+
NewDaemonSetSelectorAction(),
98103
},
99104

100105
persistentVolumeActions: []persistentVolumeAction{
@@ -179,6 +184,14 @@ func (kep *K8sEventsProcessor) unmarshalEvent(lr plog.LogRecord) metav1.Object {
179184
return nil
180185
}
181186
return &configMap
187+
case EventKindDeployment:
188+
var deployment appsv1.Deployment
189+
err := json.Unmarshal([]byte(lr.Body().AsString()), &deployment)
190+
if err != nil {
191+
kep.logger.Error("failed to unmarshal Deployment event %v", zap.Error(err), zap.String("event", lr.Body().AsString()))
192+
return nil
193+
}
194+
return &deployment
182195
case EventKindStatefulSet:
183196
var statefulSet appsv1.StatefulSet
184197
err := json.Unmarshal([]byte(lr.Body().AsString()), &statefulSet)

components/processors/observek8sattributesprocessor/serviceactions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func NewServiceSelectorAction() ServiceSelectorAction {
7676
// Generates the Service "selector" facet.
7777
func (ServiceSelectorAction) ComputeAttributes(service corev1.Service) (attributes, error) {
7878
selectorString := FormatLabels(service.Spec.Selector)
79-
return attributes{DaemonsetSelectorAttributeKey: selectorString}, nil
79+
return attributes{DaemonSetSelectorAttributeKey: selectorString}, nil
8080

8181
}
8282

0 commit comments

Comments
 (0)