Skip to content

Commit

Permalink
feat(api): improve pod container status logic and add simplified stat…
Browse files Browse the repository at this point in the history
…us to pod details (#8913)

* feat(api): improve pod container status logic and add simplified status to pod details

* regenerate schema
  • Loading branch information
floreks authored Apr 15, 2024
1 parent 848c714 commit 577dd58
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 46 deletions.
5 changes: 5 additions & 0 deletions modules/api/pkg/resource/pod/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
Waiting ContainerState = "Waiting"
Running ContainerState = "Running"
Terminated ContainerState = "Terminated"
Failed ContainerState = "Failed"
Unknown ContainerState = "Unknown"
)

Expand All @@ -52,6 +53,10 @@ func toContainerState(state v1.ContainerState) ContainerState {
}

if state.Terminated != nil {
if state.Terminated.ExitCode > 0 {
return Failed
}

return Terminated
}

Expand Down
23 changes: 17 additions & 6 deletions modules/api/pkg/resource/pod/detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ type Container struct {
// Status of a pod container
Status *v1.ContainerStatus `json:"status"`

// State is a simplified status.
// One of:
// - Waiting
// - Running
// - Terminated
// - Failed
// - Unknown
State ContainerState `json:"state"`

// Probes
LivenessProbe *v1.Probe `json:"livenessProbe"`
ReadinessProbe *v1.Probe `json:"readinessProbe"`
Expand Down Expand Up @@ -259,17 +268,19 @@ func extractContainerInfo(containerList []v1.Container, pod *v1.Pod, configMaps
}
vars = append(vars, evalEnvFrom(container, configMaps, secrets)...)

volume_mounts := extractContainerMounts(container, pod)
volumeMounts := extractContainerMounts(container, pod)
status, state := extractContainerStatus(pod, &container)

containers = append(containers, Container{
Name: container.Name,
Image: container.Image,
Env: vars,
Commands: container.Command,
Args: container.Args,
VolumeMounts: volume_mounts,
VolumeMounts: volumeMounts,
SecurityContext: container.SecurityContext,
Status: extractContainerStatus(pod, &container),
Status: status,
State: toContainerState(state),
LivenessProbe: container.LivenessProbe,
ReadinessProbe: container.ReadinessProbe,
StartupProbe: container.StartupProbe,
Expand Down Expand Up @@ -439,14 +450,14 @@ func extractContainerResourceValue(fs *v1.ResourceFieldSelector, container *v1.C
return "", fmt.Errorf("Unsupported container resource : %v", fs.Resource)
}

func extractContainerStatus(pod *v1.Pod, container *v1.Container) *v1.ContainerStatus {
func extractContainerStatus(pod *v1.Pod, container *v1.Container) (*v1.ContainerStatus, v1.ContainerState) {
statuses := append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...)

for _, status := range statuses {
if status.Name == container.Name {
return &status
return &status, status.State
}
}

return nil
return nil, v1.ContainerState{}
}
46 changes: 25 additions & 21 deletions modules/api/schema/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9600,8 +9600,8 @@
},
"clusterrolebinding.ClusterRoleBindingDetail": {
"required": [
"typeMeta",
"objectMeta",
"typeMeta",
"roleRef",
"errors"
],
Expand Down Expand Up @@ -10034,13 +10034,13 @@
},
"cronjob.CronJobDetail": {
"required": [
"suspend",
"active",
"lastSchedule",
"containerImages",
"objectMeta",
"typeMeta",
"schedule",
"suspend",
"concurrencyPolicy",
"startingDeadlineSeconds",
"errors"
Expand Down Expand Up @@ -10158,11 +10158,11 @@
},
"daemonset.DaemonSetDetail": {
"required": [
"podInfo",
"containerImages",
"initContainerImages",
"objectMeta",
"typeMeta",
"podInfo",
"errors"
],
"properties": {
Expand Down Expand Up @@ -10702,13 +10702,13 @@
},
"horizontalpodautoscaler.HorizontalPodAutoscalerDetail": {
"required": [
"minReplicas",
"maxReplicas",
"currentCPUUtilizationPercentage",
"targetCPUUtilizationPercentage",
"objectMeta",
"typeMeta",
"scaleTargetRef",
"minReplicas",
"currentReplicas",
"desiredReplicas",
"lastScaleTime"
Expand Down Expand Up @@ -10985,13 +10985,13 @@
},
"job.JobDetail": {
"required": [
"objectMeta",
"typeMeta",
"podInfo",
"containerImages",
"initContainerImages",
"parallelism",
"jobStatus",
"objectMeta",
"typeMeta",
"completions",
"errors"
],
Expand Down Expand Up @@ -11485,10 +11485,10 @@
},
"node.NodeDetail": {
"required": [
"objectMeta",
"typeMeta",
"ready",
"allocatedResources",
"objectMeta",
"phase",
"podCIDR",
"providerID",
Expand Down Expand Up @@ -11663,13 +11663,13 @@
"required": [
"objectMeta",
"reclaimPolicy",
"storageClass",
"reason",
"status",
"claim",
"reason",
"typeMeta",
"capacity",
"accessModes",
"storageClass",
"mountOptions",
"message",
"persistentVolumeSource"
Expand Down Expand Up @@ -11788,13 +11788,13 @@
},
"persistentvolumeclaim.PersistentVolumeClaimDetail": {
"required": [
"capacity",
"accessModes",
"storageClass",
"objectMeta",
"typeMeta",
"status",
"volume"
"volume",
"capacity",
"accessModes",
"storageClass"
],
"properties": {
"accessModes": {
Expand Down Expand Up @@ -11860,6 +11860,7 @@
"volumeMounts",
"securityContext",
"status",
"state",
"livenessProbe",
"readinessProbe",
"startupProbe"
Expand Down Expand Up @@ -11904,6 +11905,9 @@
"startupProbe": {
"$ref": "#/definitions/v1.Probe"
},
"state": {
"type": "string"
},
"status": {
"$ref": "#/definitions/v1.ContainerStatus"
},
Expand Down Expand Up @@ -12223,11 +12227,11 @@
},
"replicaset.ReplicaSetDetail": {
"required": [
"initContainerImages",
"objectMeta",
"typeMeta",
"podInfo",
"containerImages",
"initContainerImages",
"selector",
"horizontalPodAutoscalerList",
"errors"
Expand Down Expand Up @@ -12337,11 +12341,11 @@
},
"replicationcontroller.ReplicationControllerDetail": {
"required": [
"initContainerImages",
"objectMeta",
"typeMeta",
"podInfo",
"containerImages",
"initContainerImages",
"labelSelector",
"errors"
],
Expand Down Expand Up @@ -12544,8 +12548,8 @@
},
"role.RoleDetail": {
"required": [
"typeMeta",
"objectMeta",
"typeMeta",
"rules",
"errors"
],
Expand Down Expand Up @@ -12717,9 +12721,9 @@
},
"secret.SecretDetail": {
"required": [
"type",
"objectMeta",
"typeMeta",
"type",
"data"
],
"properties": {
Expand Down Expand Up @@ -12806,13 +12810,13 @@
},
"service.ServiceDetail": {
"required": [
"internalEndpoint",
"externalEndpoints",
"selector",
"type",
"clusterIP",
"objectMeta",
"typeMeta",
"internalEndpoint",
"externalEndpoints",
"endpointList",
"sessionAffinity",
"errors"
Expand Down Expand Up @@ -13132,12 +13136,12 @@
},
"types.CustomResourceDefinitionDetail": {
"required": [
"objectMeta",
"typeMeta",
"group",
"scope",
"names",
"established",
"objectMeta",
"conditions",
"objects",
"subresources",
Expand Down Expand Up @@ -13284,8 +13288,8 @@
},
"types.CustomResourceObjectDetail": {
"required": [
"objectMeta",
"typeMeta",
"objectMeta",
"errors"
],
"properties": {
Expand Down
22 changes: 22 additions & 0 deletions modules/web/.meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ sources:
transforms:
- replaceField:
typeDefs: |
enum ContainerState {
Waiting
Running
Terminated
Failed
Unknown
}
type Map {
map: ObjMap!
}
type StateWrapper {
state: ContainerState!
}
replacements:
- from:
type: persistentvolume_PersistentVolume
Expand Down Expand Up @@ -84,3 +94,15 @@ sources:
to:
type: Map
field: map
- from:
type: pod_ContainerStatus
field: state
to:
type: StateWrapper
field: state
- from:
type: pod_Container
field: state
to:
type: StateWrapper
field: state
Loading

0 comments on commit 577dd58

Please sign in to comment.