Skip to content

Commit

Permalink
✨ Report on OOMkilled status
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Nov 8, 2023
1 parent 6756963 commit 353c288
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion controllers/scanapi/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
corev1 "k8s.io/api/core/v1"
)

func updateScanAPIConditions(config *mondoov1alpha2.MondooAuditConfig, degradedStatus bool, conditions []appsv1.DeploymentCondition) {
func updateScanAPIConditions(config *mondoov1alpha2.MondooAuditConfig, degradedStatus bool, conditions []appsv1.DeploymentCondition, pods *corev1.PodList) {
msg := "ScanAPI controller is available"
reason := "ScanAPIAvailable"
status := corev1.ConditionFalse
Expand All @@ -33,6 +33,15 @@ func updateScanAPIConditions(config *mondoov1alpha2.MondooAuditConfig, degradedS
}
}

for _, pod := range pods.Items {
for _, status := range pod.Status.ContainerStatuses {
if status.LastTerminationState.Terminated != nil && status.LastTerminationState.Terminated.ExitCode == 137 {
// TODO: double check container name?
msg = "ScanAPI controller is unavailable due to OOM"
}
}
}

reason = "ScanAPIUnvailable"
status = corev1.ConditionTrue
}
Expand Down
18 changes: 16 additions & 2 deletions controllers/scanapi/deployment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (n *DeploymentHandler) down(ctx context.Context) error {
}

// Make sure to clear any degraded status
updateScanAPIConditions(n.Mondoo, false, []appsv1.DeploymentCondition{})
updateScanAPIConditions(n.Mondoo, false, []appsv1.DeploymentCondition{}, &corev1.PodList{})

return nil
}
Expand Down Expand Up @@ -143,7 +143,21 @@ func (n *DeploymentHandler) syncDeployment(ctx context.Context) error {
return nil
}

updateScanAPIConditions(n.Mondoo, existingDeployment.Status.UnavailableReplicas != 0, existingDeployment.Status.Conditions)
// TODO: refactor to func?
// Get Pods for this deployment
selector, _ := metav1.LabelSelectorAsSelector(existingDeployment.Spec.Selector)
opts := []client.ListOption{
client.InNamespace(existingDeployment.Namespace),
client.MatchingLabelsSelector{Selector: selector},
}
pods := &corev1.PodList{}
err = n.KubeClient.List(ctx, pods, opts...)
if err != nil {
logger.Error(err, "Failed to list Pods for scan API")
return err
}

updateScanAPIConditions(n.Mondoo, existingDeployment.Status.UnavailableReplicas != 0, existingDeployment.Status.Conditions, pods)

if !k8s.AreDeploymentsEqual(*deployment, existingDeployment) {
logger.Info("Update needed for scan API Deployment")
Expand Down

0 comments on commit 353c288

Please sign in to comment.