Skip to content

Commit

Permalink
[eclipse-kanto#196] Starting of constantly restarting container fails
Browse files Browse the repository at this point in the history
 - Do not release container resources when restart manager is canceled, they are already released upon exit.
 - Apply restart policy when it is updated.

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov3@bosch.com>
  • Loading branch information
dimitar-dimitrow committed Nov 23, 2023
1 parent 27df4f0 commit cd2d32f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 7 additions & 2 deletions containerm/mgr/mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (mgr *containerMgr) Stop(ctx context.Context, id string, stopOpts *types.St
}
container.Lock()
defer container.Unlock()
go mgr.applyRestartPolicy(context.Background(), container)
mgr.applyRestartPolicy(context.Background(), container)
return nil
}

Expand Down Expand Up @@ -312,10 +312,11 @@ func (mgr *containerMgr) Update(ctx context.Context, id string, updateOpts *type
changesMade = true
}

var rpChanged bool
if updateOpts.RestartPolicy != nil && !reflect.DeepEqual(updateOpts.RestartPolicy, container.HostConfig.RestartPolicy) {
mgr.resetContainerRestartManager(container, false)
container.HostConfig.RestartPolicy = updateOpts.RestartPolicy
changesMade = true
changesMade, rpChanged = true, true
}

if changesMade {
Expand All @@ -327,6 +328,10 @@ func (mgr *containerMgr) Update(ctx context.Context, id string, updateOpts *type
log.ErrorErr(errMeta, failedConfigStoringErrorMsg)
}
}

if rpChanged && (container.State.Exited || container.State.Status == types.Stopped) {
mgr.applyRestartPolicy(context.Background(), container)
}
return nil
}

Expand Down
13 changes: 8 additions & 5 deletions containerm/mgr/mgr_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (mgr *containerMgr) applyRestartPolicy(ctx context.Context, container *type
}
}
if err != nil {
mgr.updateConfigToStopped(ctx, container, -1, err)
mgr.updateConfigToStopped(ctx, container, -1, err, false)
}
}()
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (mgr *containerMgr) containersToArray() []*types.Container {
}
return ctrs
}
func (mgr *containerMgr) updateConfigToStopped(ctx context.Context, c *types.Container, exitCode int64, err error) error {
func (mgr *containerMgr) updateConfigToStopped(ctx context.Context, c *types.Container, exitCode int64, err error, releaseContainerResources bool) error {
var (
code int64
errMsg string
Expand All @@ -155,7 +155,10 @@ func (mgr *containerMgr) updateConfigToStopped(ctx context.Context, c *types.Con
}
}()

return mgr.releaseContainerResources(c)
if releaseContainerResources {
return mgr.releaseContainerResources(c)
}
return nil
}

func (mgr *containerMgr) updateConfigToExited(ctx context.Context, c *types.Container, exitCode int64, err error, oomKilled bool) error {
Expand Down Expand Up @@ -298,7 +301,7 @@ func (mgr *containerMgr) processStartContainer(ctx context.Context, id string, r

pid, err = mgr.ctrClient.StartContainer(ctx, container, "")
if err != nil {
_ = mgr.updateConfigToStopped(ctx, container, -1, err)
_ = mgr.updateConfigToStopped(ctx, container, -1, err, true)
return err
}

Expand Down Expand Up @@ -361,7 +364,7 @@ func (mgr *containerMgr) stopContainer(ctx context.Context, container *types.Con
container.ManuallyStopped = false
return exitErr
}
return mgr.updateConfigToStopped(ctx, container, exitCode, exitErr)
return mgr.updateConfigToStopped(ctx, container, exitCode, exitErr, true)
}

func (mgr *containerMgr) stopManagerService(ctx context.Context) error {
Expand Down

0 comments on commit cd2d32f

Please sign in to comment.