Skip to content

Commit 9cc7c2b

Browse files
committed
Make Go not panic on a partial container update
Right now, if you call Update with only part of the options struct added, it panics. This fixes that by only adding them if they are not nil. Signed-off-by: Astrid Gealer <[email protected]>
1 parent 37dc5fd commit 9cc7c2b

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

pkg/bindings/containers/update.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@ func Update(ctx context.Context, options *types.ContainerUpdateOptions) (string,
2626
params.Set("restartRetries", strconv.Itoa(int(*options.RestartRetries)))
2727
}
2828
}
29+
2930
updateEntities := &handlers.UpdateEntities{
30-
LinuxResources: *options.Resources,
31-
UpdateHealthCheckConfig: *options.ChangedHealthCheckConfiguration,
32-
UpdateContainerDevicesLimits: *options.DevicesLimits,
33-
Env: options.Env,
34-
UnsetEnv: options.UnsetEnv,
31+
Env: options.Env,
32+
UnsetEnv: options.UnsetEnv,
33+
}
34+
if options.Resources != nil {
35+
updateEntities.LinuxResources = *options.Resources
36+
}
37+
if options.ChangedHealthCheckConfiguration != nil {
38+
updateEntities.UpdateHealthCheckConfig = *options.ChangedHealthCheckConfiguration
3539
}
40+
if options.DevicesLimits != nil {
41+
updateEntities.UpdateContainerDevicesLimits = *options.DevicesLimits
42+
}
43+
3644
requestData, err := jsoniter.MarshalToString(updateEntities)
3745
if err != nil {
3846
return "", err

pkg/bindings/test/containers_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/containers/podman/v5/pkg/bindings"
1010
"github.com/containers/podman/v5/pkg/bindings/containers"
1111
"github.com/containers/podman/v5/pkg/domain/entities/reports"
12+
"github.com/containers/podman/v5/pkg/domain/entities/types"
1213
"github.com/containers/podman/v5/pkg/specgen"
1314
. "github.com/onsi/ginkgo/v2"
1415
. "github.com/onsi/gomega"
@@ -802,4 +803,15 @@ var _ = Describe("Podman containers ", func() {
802803
Expect(c).To(HaveLen(1))
803804
Expect(c[0].PodName).To(Equal(podName))
804805
})
806+
807+
It("Update container allows for partial updates", func() {
808+
var name = "top"
809+
_, err := bt.RunTopContainer(&name, nil)
810+
Expect(err).ToNot(HaveOccurred())
811+
812+
_, err = containers.Update(bt.conn, &types.ContainerUpdateOptions{
813+
NameOrID: name,
814+
})
815+
Expect(err).ToNot(HaveOccurred())
816+
})
805817
})

0 commit comments

Comments
 (0)