Add --restart flag to pod create

Add --restart flag to pod create to allow users to set the
restart policy for the pod, which applies to all the containers
in the pod. This reuses the restart policy already there for
containers and has the same restart policy options.
Add "never" to the restart policy options to match k8s syntax.
It is a synonym for "no" and does the exact same thing where the
containers are not restarted once exited.
Only the containers that have exited will be restarted based on the
restart policy, running containers will not be restarted when an exited
container is restarted in the same pod (same as is done in k8s).

Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
This commit is contained in:
Urvashi Mohnani
2023-03-19 20:07:19 -04:00
parent 867639e84d
commit edbeee5238
18 changed files with 264 additions and 33 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/containers/image/v5/types"
encconfig "github.com/containers/ocicrypt/config"
enchelpers "github.com/containers/ocicrypt/helpers"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/errorhandling"
"github.com/containers/podman/v4/pkg/namespaces"
"github.com/containers/podman/v4/pkg/rootless"
@ -665,3 +666,37 @@ func DecryptConfig(decryptionKeys []string) (*encconfig.DecryptConfig, error) {
return decryptConfig, nil
}
// ParseRestartPolicy parses the value given to the --restart flag and returns the policy
// and restart retries value
func ParseRestartPolicy(policy string) (string, uint, error) {
var (
retriesUint uint
policyType string
)
splitRestart := strings.Split(policy, ":")
switch len(splitRestart) {
case 1:
// No retries specified
policyType = splitRestart[0]
if strings.ToLower(splitRestart[0]) == "never" {
policyType = define.RestartPolicyNo
}
case 2:
if strings.ToLower(splitRestart[0]) != "on-failure" {
return "", 0, errors.New("restart policy retries can only be specified with on-failure restart policy")
}
retries, err := strconv.Atoi(splitRestart[1])
if err != nil {
return "", 0, fmt.Errorf("parsing restart policy retry count: %w", err)
}
if retries < 0 {
return "", 0, errors.New("must specify restart policy retry count as a number greater than 0")
}
retriesUint = uint(retries)
policyType = splitRestart[0]
default:
return "", 0, errors.New("invalid restart policy: may specify retries at most once")
}
return policyType, retriesUint, nil
}