Files
Matt Heon 482ef7bfcf Add support for updating restart policy
This is something Docker does, and we did not do until now. Most
difficult/annoying part was the REST API, where I did not really
want to modify the struct being sent, so I made the new restart
policy parameters query parameters instead.

Testing was also a bit annoying, because testing restart policy
always is.

Signed-off-by: Matt Heon <mheon@redhat.com>
2024-04-17 08:23:51 -04:00

42 lines
1.0 KiB
Go

package containers
import (
"context"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/containers/podman/v5/pkg/bindings"
"github.com/containers/podman/v5/pkg/domain/entities/types"
jsoniter "github.com/json-iterator/go"
)
func Update(ctx context.Context, options *types.ContainerUpdateOptions) (string, error) {
conn, err := bindings.GetClient(ctx)
if err != nil {
return "", err
}
params := url.Values{}
if options.Specgen.RestartPolicy != "" {
params.Set("restartPolicy", options.Specgen.RestartPolicy)
if options.Specgen.RestartRetries != nil {
params.Set("restartRetries", strconv.Itoa(int(*options.Specgen.RestartRetries)))
}
}
resources, err := jsoniter.MarshalToString(options.Specgen.ResourceLimits)
if err != nil {
return "", err
}
stringReader := strings.NewReader(resources)
response, err := conn.DoRequest(ctx, stringReader, http.MethodPost, "/containers/%s/update", params, nil, options.NameOrID)
if err != nil {
return "", err
}
defer response.Body.Close()
return options.NameOrID, response.Process(nil)
}