@@ -293,11 +293,17 @@ func (a *nriAPI) postUpdateContainer(ctx context.Context, criCtr *oci.Container)
293293 return a .nri .PostUpdateContainer (ctx , pod , ctr )
294294}
295295
296- func (a * nriAPI ) stopContainer (ctx context.Context , criPod * sandbox.Sandbox , criCtr * oci.Container ) error {
296+ func (a * nriAPI ) stopContainer (ctx context.Context , criPod * sandbox.Sandbox , criCtr * oci.Container , updateState bool ) error {
297297 if ! a .isEnabled () {
298298 return nil
299299 }
300300
301+ if updateState {
302+ if err := a .cri .Runtime ().UpdateContainerStatus (ctx , criCtr ); err != nil {
303+ log .Warnf (ctx , "Error updating the container status %q: %v" , criCtr .ID (), err )
304+ }
305+ }
306+
301307 ctr := & criContainer {
302308 api : a ,
303309 ctr : criCtr ,
@@ -669,21 +675,70 @@ func (c *criContainer) GetName() string {
669675 return c .GetSpec ().Annotations ["io.kubernetes.container.name" ]
670676}
671677
672- func (c * criContainer ) GetState () api.ContainerState {
673- if c .ctr != nil {
674- switch c .ctr .State ().Status {
675- case oci .ContainerStateCreated :
676- return api .ContainerState_CONTAINER_CREATED
677- case oci .ContainerStatePaused :
678- return api .ContainerState_CONTAINER_PAUSED
679- case oci .ContainerStateRunning :
680- return api .ContainerState_CONTAINER_RUNNING
681- case oci .ContainerStateStopped :
682- return api .ContainerState_CONTAINER_STOPPED
678+ func (c * criContainer ) GetStatus () * nri.ContainerStatus {
679+ const (
680+ // unknownReason is the exit reason when a container's exit code is not known
681+ unknownReason = "Unknown"
682+ // completedExitReason is the exit reason when container exits with 0.
683+ completedExitReason = "Completed"
684+ // errorExitReason is the exit reason when container exits with non-zero.
685+ errorExitReason = "Error"
686+ // oomKilledReason is the exit reason when container is killed by OOM killer.
687+ oomKilledReason = "OOMKilled"
688+ // seccompKilledReason is the exit reason when container is killed by seccomp.
689+ seccompKilledReason = "seccomp killed"
690+ )
691+
692+ status := & nri.ContainerStatus {
693+ State : api .ContainerState_CONTAINER_UNKNOWN ,
694+ Reason : unknownReason ,
695+ }
696+
697+ if c .ctr == nil {
698+ return status
699+ }
700+
701+ cState := c .ctr .State ()
702+
703+ switch cState .Status {
704+ case oci .ContainerStateCreated :
705+ status .State = api .ContainerState_CONTAINER_CREATED
706+ status .CreatedAt = c .ctr .CreatedAt ().UnixNano ()
707+ case oci .ContainerStateRunning , oci .ContainerStatePaused :
708+ status .State = api .ContainerState_CONTAINER_RUNNING
709+ status .CreatedAt = c .ctr .CreatedAt ().UnixNano ()
710+ status .StartedAt = cState .Started .UnixNano ()
711+ case oci .ContainerStateStopped :
712+ status .State = api .ContainerState_CONTAINER_STOPPED
713+ status .CreatedAt = c .ctr .CreatedAt ().UnixNano ()
714+ status .StartedAt = cState .Started .UnixNano ()
715+ status .FinishedAt = cState .Finished .UnixNano ()
716+
717+ if cState .ExitCode != nil {
718+ status .ExitCode = * cState .ExitCode
683719 }
720+
721+ switch {
722+ case cState .OOMKilled :
723+ status .Reason = oomKilledReason
724+ case cState .SeccompKilled :
725+ status .Reason = seccompKilledReason
726+ status .Message = cState .Error
727+ case cState .ExitCode != nil :
728+ if status .ExitCode == 0 {
729+ status .Reason = completedExitReason
730+ } else {
731+ status .Reason = errorExitReason
732+ status .Message = cState .Error
733+ }
734+ }
735+ }
736+
737+ if cState .InitPid > 0 {
738+ status .Pid = uint32 (cState .InitPid )
684739 }
685740
686- return api . ContainerState_CONTAINER_UNKNOWN
741+ return status
687742}
688743
689744func (c * criContainer ) GetLabels () map [string ]string {
0 commit comments