Skip to content

Commit b31bdfc

Browse files
author
Mrunal Patel
authored
Merge pull request opencontainers#1558 from hqhq/update_state
Update state after update
2 parents eb464f7 + e6e1c34 commit b31bdfc

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

libcontainer/container_linux.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,17 @@ func (c *linuxContainer) Set(config configs.Config) error {
186186
if status == Stopped {
187187
return newGenericError(fmt.Errorf("container not running"), ContainerNotRunning)
188188
}
189+
if err := c.cgroupManager.Set(&config); err != nil {
190+
// Set configs back
191+
if err2 := c.cgroupManager.Set(c.config); err2 != nil {
192+
logrus.Warnf("Setting back cgroup configs failed due to error: %v, your state.json and actual configs might be inconsistent.", err2)
193+
}
194+
return err
195+
}
196+
// After config setting succeed, update config and states
189197
c.config = &config
190-
return c.cgroupManager.Set(c.config)
198+
_, err = c.updateState(nil)
199+
return err
191200
}
192201

193202
func (c *linuxContainer) Start(process *Process) error {
@@ -1388,7 +1397,9 @@ func (c *linuxContainer) criuNotifications(resp *criurpc.CriuResp, process *Proc
13881397
}
13891398

13901399
func (c *linuxContainer) updateState(process parentProcess) (*State, error) {
1391-
c.initProcess = process
1400+
if process != nil {
1401+
c.initProcess = process
1402+
}
13921403
state, err := c.currentState()
13931404
if err != nil {
13941405
return nil, err

libcontainer/container_linux_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/opencontainers/runc/libcontainer/cgroups"
1111
"github.com/opencontainers/runc/libcontainer/configs"
12+
"github.com/opencontainers/runc/libcontainer/system"
1213
)
1314

1415
type mockCgroupManager struct {
@@ -216,3 +217,65 @@ func TestGetContainerState(t *testing.T) {
216217
}
217218
}
218219
}
220+
221+
func TestGetContainerStateAfterUpdate(t *testing.T) {
222+
var (
223+
pid = os.Getpid()
224+
)
225+
stat, err := system.Stat(pid)
226+
if err != nil {
227+
t.Fatal(err)
228+
}
229+
container := &linuxContainer{
230+
id: "myid",
231+
config: &configs.Config{
232+
Namespaces: []configs.Namespace{
233+
{Type: configs.NEWPID},
234+
{Type: configs.NEWNS},
235+
{Type: configs.NEWNET},
236+
{Type: configs.NEWUTS},
237+
{Type: configs.NEWIPC},
238+
},
239+
Cgroups: &configs.Cgroup{
240+
Resources: &configs.Resources{
241+
Memory: 1024,
242+
},
243+
},
244+
},
245+
initProcess: &mockProcess{
246+
_pid: pid,
247+
started: stat.StartTime,
248+
},
249+
cgroupManager: &mockCgroupManager{},
250+
}
251+
container.state = &createdState{c: container}
252+
state, err := container.State()
253+
if err != nil {
254+
t.Fatal(err)
255+
}
256+
if state.InitProcessPid != pid {
257+
t.Fatalf("expected pid %d but received %d", pid, state.InitProcessPid)
258+
}
259+
if state.InitProcessStartTime != stat.StartTime {
260+
t.Fatalf("expected process start time %d but received %d", stat.StartTime, state.InitProcessStartTime)
261+
}
262+
if state.Config.Cgroups.Resources.Memory != 1024 {
263+
t.Fatalf("expected Memory to be 1024 but received %q", state.Config.Cgroups.Memory)
264+
}
265+
266+
// Set initProcessStartTime so we fake to be running
267+
container.initProcessStartTime = state.InitProcessStartTime
268+
container.state = &runningState{c: container}
269+
newConfig := container.Config()
270+
newConfig.Cgroups.Resources.Memory = 2048
271+
if err := container.Set(newConfig); err != nil {
272+
t.Fatal(err)
273+
}
274+
state, err = container.State()
275+
if err != nil {
276+
t.Fatal(err)
277+
}
278+
if state.Config.Cgroups.Resources.Memory != 2048 {
279+
t.Fatalf("expected Memory to be 2048 but received %q", state.Config.Cgroups.Memory)
280+
}
281+
}

0 commit comments

Comments
 (0)