From 0ae286d617896152541dc14311298b4cb323257a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 31 May 2020 12:42:10 -0700 Subject: [PATCH] Correctly handle empty values from the API requests; mergo by default thinks these "empty" values should be skipped --- server/environment_docker.go | 4 +++- server/update.go | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/server/environment_docker.go b/server/environment_docker.go index afccf29..00324ac 100644 --- a/server/environment_docker.go +++ b/server/environment_docker.go @@ -122,11 +122,13 @@ func (d *DockerEnvironment) InSituUpdate() error { return errors.WithStack(err) } + ctx, _ := context.WithTimeout(context.Background(), time.Second * 10) u := container.UpdateConfig{ Resources: d.getResourcesForServer(), } - if _, err := d.Client.ContainerUpdate(context.Background(), d.Server.Uuid, u); err != nil { + d.Server.Log().WithField("limits", fmt.Sprintf("%+v", u.Resources)).Debug("updating server container on-the-fly with passed limits") + if _, err := d.Client.ContainerUpdate(ctx, d.Server.Uuid, u); err != nil { return errors.WithStack(err) } diff --git a/server/update.go b/server/update.go index 741bcf8..f62c89f 100644 --- a/server/update.go +++ b/server/update.go @@ -34,6 +34,16 @@ func (s *Server) UpdateDataStructure(data []byte, background bool) error { return errors.WithStack(err) } + // Don't explode if we're setting CPU limits to 0. Mergo sees that as an empty value + // so it won't override the value we've passed through in the API call. However, we can + // safely assume that we're passing through valid data structures here. I foresee this + // backfiring at some point, but until then... + // + // We'll go ahead and do this with swap as well. + s.Build.CpuLimit = src.Build.CpuLimit + s.Build.Swap = src.Build.Swap + s.Build.DiskSpace = src.Build.DiskSpace + // Mergo can't quite handle this boolean value correctly, so for now we'll just // handle this edge case manually since none of the other data passed through in this // request is going to be boolean. Allegedly. @@ -81,12 +91,9 @@ func (s *Server) runBackgroundActions() { // Update the environment in place, allowing memory and CPU usage to be adjusted // on the fly without the user needing to reboot (theoretically). go func(server *Server) { + server.Log().Info("performing server limit modification on-the-fly") if err := server.Environment.InSituUpdate(); err != nil { - zap.S().Warnw( - "failed to perform in-situ update of server environment", - zap.String("server", server.Uuid), - zap.Error(err), - ) + server.Log().WithField("error", err).Warn("failed to perform on-the-fly update of the server environment") } }(s)