Allow controller flag to override restart policy (#1340)

This commit is contained in:
Hang Yan 2020-11-02 18:17:12 +08:00 committed by GitHub
parent c1280a4f55
commit 2dc6037cce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 8 deletions

1
go.mod
View File

@ -21,7 +21,6 @@ require (
github.com/docker/cli v0.0.0-20190711175710-5b38d82aa076
github.com/docker/libcompose v0.4.0
github.com/fatih/structs v1.1.0
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/fsouza/go-dockerclient v1.6.5
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect

View File

@ -78,6 +78,11 @@ type ConvertOptions struct {
WithKomposeAnnotation bool
}
// IsPodController indicate if the user want to use a controller
func (opt *ConvertOptions) IsPodController() bool {
return opt.IsDeploymentFlag || opt.IsDaemonSetFlag || opt.IsReplicationControllerFlag || opt.Controller != ""
}
// ServiceConfig holds the basic struct of a container
type ServiceConfig struct {
ContainerName string

View File

@ -1154,11 +1154,8 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
}
// Generate pod only and nothing more
if service.Restart == "no" || service.Restart == "on-failure" {
// Error out if Controller Object is specified with restart: 'on-failure'
if opt.IsDeploymentFlag || opt.IsDaemonSetFlag || opt.IsReplicationControllerFlag {
return nil, errors.New("Controller object cannot be specified with restart: 'on-failure'")
}
if (service.Restart == "no" || service.Restart == "on-failure") && !opt.IsPodController() {
log.Infof("Create kubernetes pod instead of pod controller due to restart policy: %s", service.Restart)
pod := k.InitPod(name, service)
objects = append(objects, pod)
} else {

View File

@ -518,8 +518,8 @@ func TestRestartOnFailure(t *testing.T) {
t.Log("Test case:", name)
k := Kubernetes{}
_, err := k.Transform(test.komposeObject, test.opt)
if err == nil {
t.Errorf("Expected an error, got %v instead", err)
if err != nil {
t.Errorf("Expected nil error, got %v instead", err)
}
}
}