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 <astrid@gealer.email>
This commit is contained in:
Astrid Gealer 2025-04-08 13:17:11 +01:00
parent 37dc5fdf89
commit 9cc7c2b4ab
2 changed files with 25 additions and 5 deletions

View File

@ -26,13 +26,21 @@ func Update(ctx context.Context, options *types.ContainerUpdateOptions) (string,
params.Set("restartRetries", strconv.Itoa(int(*options.RestartRetries)))
}
}
updateEntities := &handlers.UpdateEntities{
LinuxResources: *options.Resources,
UpdateHealthCheckConfig: *options.ChangedHealthCheckConfiguration,
UpdateContainerDevicesLimits: *options.DevicesLimits,
Env: options.Env,
UnsetEnv: options.UnsetEnv,
Env: options.Env,
UnsetEnv: options.UnsetEnv,
}
if options.Resources != nil {
updateEntities.LinuxResources = *options.Resources
}
if options.ChangedHealthCheckConfiguration != nil {
updateEntities.UpdateHealthCheckConfig = *options.ChangedHealthCheckConfiguration
}
if options.DevicesLimits != nil {
updateEntities.UpdateContainerDevicesLimits = *options.DevicesLimits
}
requestData, err := jsoniter.MarshalToString(updateEntities)
if err != nil {
return "", err

View File

@ -9,6 +9,7 @@ import (
"github.com/containers/podman/v5/pkg/bindings"
"github.com/containers/podman/v5/pkg/bindings/containers"
"github.com/containers/podman/v5/pkg/domain/entities/reports"
"github.com/containers/podman/v5/pkg/domain/entities/types"
"github.com/containers/podman/v5/pkg/specgen"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
@ -802,4 +803,15 @@ var _ = Describe("Podman containers ", func() {
Expect(c).To(HaveLen(1))
Expect(c[0].PodName).To(Equal(podName))
})
It("Update container allows for partial updates", func() {
var name = "top"
_, err := bt.RunTopContainer(&name, nil)
Expect(err).ToNot(HaveOccurred())
_, err = containers.Update(bt.conn, &types.ContainerUpdateOptions{
NameOrID: name,
})
Expect(err).ToNot(HaveOccurred())
})
})