Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions components/processors/observek8sattributesprocessor/podactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ const (
OwnerKindStatefulSet = "StatefulSet"
)

// Helper Fns
func hasPodReadyCondition(conditions []v1.PodCondition) bool {
for _, condition := range conditions {
if condition.Type == v1.PodReady && condition.Status == v1.ConditionTrue {
return true
}
}
return false
}

func isPodInitializedConditionTrue(status *v1.PodStatus) bool {
for _, condition := range status.Conditions {
if condition.Type != v1.PodInitialized {
continue
}

return condition.Status == v1.ConditionTrue
Comment on lines +48 to +52
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe change this to

Suggested change
if condition.Type != v1.PodInitialized {
continue
}
return condition.Status == v1.ConditionTrue
if condition.Type == v1.PodInitialized && condition.Status == v1.ConditionTrue {
return true
}

to match hasPodReadyCondition

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumping this one

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually just helper code I copied from the same kubectl code, if its semantically the same i'd prefer to leave it in case that helper code changes in the future as well it'll be easier to copy/compare

}
return false
}

func isRestartableInitContainer(initContainer *v1.Container) bool {
if initContainer == nil || initContainer.RestartPolicy == nil {
return false
}
return *initContainer.RestartPolicy == v1.ContainerRestartPolicyAlways
}

func isPodPhaseTerminal(phase v1.PodPhase) bool {
return phase == v1.PodFailed || phase == v1.PodSucceeded
}

// ---------------------------------- Pod "status" ----------------------------------

type PodStatusAction struct{}
Expand All @@ -43,18 +75,46 @@ func NewPodStatusAction() PodStatusAction {

// Generates the Pod "status" facet.
func (PodStatusAction) ComputeAttributes(pod v1.Pod) (attributes, error) {
// based on https://github.com/kubernetes/kubernetes/blob/0d3b859af81e6a5f869a7766c8d45afd1c600b04/pkg/printers/internalversion/printers.go#L901
reason := string(pod.Status.Phase)
// based on https://github.com/kubernetes/kubernetes/blob/cd3b5c57668a0a6e32057ef82dfab40e9b0bec5b/pkg/printers/internalversion/printers.go#L881
restarts := 0
restartableInitContainerRestarts := 0
totalContainers := len(pod.Spec.Containers)
readyContainers := 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still looks like these four variables are unused.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so here's the logic in datadog agent https://github.com/DataDog/datadog-agent/blob/7d47d25899c025c8b3e5ba91e488fa0f97f7a275/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod.go#L220 they also only use status but kept the rest of the logic so that they can just copy paste it over

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think the argument for taking it out is that its unnecessary computation / more efficient which i agree with but its a slight preference vs. way better imo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I'm happy with where you landed!

If we need to change it again, I think it could be more clear to have a helper that returns all values and is very explicitly full copy/paste. Then we could call that and extra only what we want


podPhase := pod.Status.Phase
reason := string(podPhase)
if pod.Status.Reason != "" {
reason = pod.Status.Reason
}

// If the Pod carries {type:PodScheduled, reason:SchedulingGated}, set reason to 'SchedulingGated'.
for _, condition := range pod.Status.Conditions {
if condition.Type == v1.PodScheduled && condition.Reason == v1.PodReasonSchedulingGated {
reason = v1.PodReasonSchedulingGated
}
}

initContainers := make(map[string]*v1.Container)
for i := range pod.Spec.InitContainers {
initContainers[pod.Spec.InitContainers[i].Name] = &pod.Spec.InitContainers[i]
if isRestartableInitContainer(&pod.Spec.InitContainers[i]) {
totalContainers++
}
}

initializing := false
for i := range pod.Status.InitContainerStatuses {
container := pod.Status.InitContainerStatuses[i]
restarts += int(container.RestartCount)
switch {
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0:
continue
case isRestartableInitContainer(initContainers[container.Name]) &&
container.Started != nil && *container.Started:
if container.Ready {
readyContainers++
}
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in the switch statement? It could skip the default case and miss setting the reason there.

case container.State.Terminated != nil:
// initialization is failed
if len(container.State.Terminated.Reason) == 0 {
Expand All @@ -76,11 +136,13 @@ func (PodStatusAction) ComputeAttributes(pod v1.Pod) (attributes, error) {
}
break
}
if !initializing {

if !initializing || isPodInitializedConditionTrue(&pod.Status) {
restarts = restartableInitContainerRestarts
hasRunning := false
for i := len(pod.Status.ContainerStatuses) - 1; i >= 0; i-- {
container := pod.Status.ContainerStatuses[i]

restarts += int(container.RestartCount)
if container.State.Waiting != nil && container.State.Waiting.Reason != "" {
reason = container.State.Waiting.Reason
} else if container.State.Terminated != nil && container.State.Terminated.Reason != "" {
Expand All @@ -93,12 +155,17 @@ func (PodStatusAction) ComputeAttributes(pod v1.Pod) (attributes, error) {
}
} else if container.Ready && container.State.Running != nil {
hasRunning = true
readyContainers++
}
}

// change pod status back to "Running" if there is at least one container still reporting as "Running" status
if reason == "Completed" && hasRunning {
reason = "Running"
if hasPodReadyCondition(pod.Status.Conditions) {
reason = "Running"
} else {
reason = "NotReady"
}
}
}

Expand Down
240 changes: 120 additions & 120 deletions components/processors/observek8sattributesprocessor/podactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,133 +12,133 @@ func TestPodActions(t *testing.T) {
testBodyFilepath: "./testdata/podObjectEvent.json",
},
),
expectedResults: []queryWithResult{
{
path: "observe_transform.facets.status",
expResult: "Terminating",
},
},
},
{ // Tests that we don't override/drop other facets computed in OTTL
name: "existingObserveTransformAttributes",
inLogs: createResourceLogs(
logWithResource{
testBodyFilepath: "./testdata/podObjectEvent.json",
recordAttributes: map[string]any{
"observe_transform": map[string]interface{}{
"facets": map[string]interface{}{
"other_key": "test",
},
},
"name": "existingObserveTransformAttributes",
},
},
),
expectedResults: []queryWithResult{
{
path: "observe_transform.facets.status",
expResult: "Terminating",
},
{
path: "observe_transform.facets.other_key",
expResult: "test",
},
},
},
{
name: "Pod container counts",
inLogs: createResourceLogs(
logWithResource{
testBodyFilepath: "./testdata/podObjectEvent.json",
},
),
expectedResults: []queryWithResult{
{
path: "observe_transform.facets.restarts",
expResult: int64(5),
},
{
path: "observe_transform.facets.total_containers",
expResult: int64(4),
},
{
path: "observe_transform.facets.ready_containers",
expResult: int64(3),
},
},
},
{
name: "Pod readiness gates",
inLogs: createResourceLogs(
logWithResource{
testBodyFilepath: "./testdata/podObjectEventWithReadinessGates.json",
},
),
expectedResults: []queryWithResult{
{
path: "observe_transform.facets.readinessGatesReady",
expResult: int64(1),
},
{
path: "observe_transform.facets.readinessGatesTotal",
expResult: int64(2),
},
},
},
{
name: "Pod conditions",
inLogs: createResourceLogs(
logWithResource{
testBodyFilepath: "./testdata/podObjectEvent.json",
},
),
expectedResults: []queryWithResult{
// Conditions must be a map with 5 elements
{
path: "observe_transform.facets.conditions | length(@)",
expResult: float64(6),
},
{
path: "observe_transform.facets.conditions.PodReadyToStartContainers",
expResult: "False",
},
{
path: "observe_transform.facets.conditions.Initialized",
expResult: "True",
},
{
path: "observe_transform.facets.conditions.Ready",
expResult: "False",
},
{
path: "observe_transform.facets.conditions.ContainersReady",
expResult: "False",
},
{
path: "observe_transform.facets.conditions.PodScheduled",
expResult: "True",
},
{
path: "observe_transform.facets.conditions.TestCondition",
expResult: "Unknown",
},
},
},
{
name: "Pod conditions",
inLogs: createResourceLogs(
logWithResource{
testBodyFilepath: "./testdata/podTestEvent.json",
},
),
expectedResults: []queryWithResult{
// Conditions must be a map with 5 elements
{
path: "observe_transform.facets.conditions | length(@)",
expResult: float64(5),
},
},
},
// { // Tests that we don't override/drop other facets computed in OTTL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace these deleted tests with new ones?

// name: "existingObserveTransformAttributes",
// inLogs: createResourceLogs(
// logWithResource{
// testBodyFilepath: "./testdata/podObjectEvent.json",
// recordAttributes: map[string]any{
// "observe_transform": map[string]interface{}{
// "facets": map[string]interface{}{
// "other_key": "test",
// },
// },
// "name": "existingObserveTransformAttributes",
// },
// },
// ),
// expectedResults: []queryWithResult{
// {
// path: "observe_transform.facets.status",
// expResult: "Terminating",
// },
// {
// path: "observe_transform.facets.other_key",
// expResult: "test",
// },
// },
// },
// {
// name: "Pod container counts",
// inLogs: createResourceLogs(
// logWithResource{
// testBodyFilepath: "./testdata/podObjectEvent.json",
// },
// ),
// expectedResults: []queryWithResult{
// {
// path: "observe_transform.facets.restarts",
// expResult: int64(5),
// },
// {
// path: "observe_transform.facets.total_containers",
// expResult: int64(4),
// },
// {
// path: "observe_transform.facets.ready_containers",
// expResult: int64(3),
// },
// },
// },
// {
// name: "Pod readiness gates",
// inLogs: createResourceLogs(
// logWithResource{
// testBodyFilepath: "./testdata/podObjectEventWithReadinessGates.json",
// },
// ),
// expectedResults: []queryWithResult{
// {
// path: "observe_transform.facets.readinessGatesReady",
// expResult: int64(1),
// },
// {
// path: "observe_transform.facets.readinessGatesTotal",
// expResult: int64(2),
// },
// },
// },
// {
// name: "Pod conditions",
// inLogs: createResourceLogs(
// logWithResource{
// testBodyFilepath: "./testdata/podObjectEvent.json",
// },
// ),
// expectedResults: []queryWithResult{
// // Conditions must be a map with 5 elements
// {
// path: "observe_transform.facets.conditions | length(@)",
// expResult: float64(6),
// },
// {
// path: "observe_transform.facets.conditions.PodReadyToStartContainers",
// expResult: "False",
// },
// {
// path: "observe_transform.facets.conditions.Initialized",
// expResult: "True",
// },
// {
// path: "observe_transform.facets.conditions.Ready",
// expResult: "False",
// },
// {
// path: "observe_transform.facets.conditions.ContainersReady",
// expResult: "False",
// },
// {
// path: "observe_transform.facets.conditions.PodScheduled",
// expResult: "True",
// },
// {
// path: "observe_transform.facets.conditions.TestCondition",
// expResult: "Unknown",
// },
// },
// },
// {
// name: "Pod conditions",
// inLogs: createResourceLogs(
// logWithResource{
// testBodyFilepath: "./testdata/podTestEvent.json",
// },
// ),
// expectedResults: []queryWithResult{
// // Conditions must be a map with 5 elements
// {
// path: "observe_transform.facets.conditions | length(@)",
// expResult: float64(5),
// },
// },
// },
} {
runTest(t, test)
}
Expand Down
Loading