set strategy to Recreate if vols, add tests,docs

When volumes are specified in the Docker Compose
files, then in case of Kubernetes, Deployment's
Spec.Strategy.Type and in case of OpenShift,
DeploymentConfig's Spec.Strategy.Type are set to
"Recreate". This is a safer deployment strategy
when Volumes are getting used.

This commit also adds unit tests for Kubernetes
as well as OpenShift, and fixes the failing
functional tests (tests.sh) due to the change.

No functional tests have been added since
the functionality is already covered when the
volume mounts are being tested earlier in the
file.

This fixes #264
This commit is contained in:
Shubham Minglani 2017-01-13 05:21:52 +05:30
parent fb042c7b20
commit b73efa54f2
13 changed files with 926 additions and 823 deletions

View File

@ -394,3 +394,6 @@ services:
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restart: "on-failure"
```
#### Notes:
- If the Docker Compose file has a volume specified for a service, the Deployment (Kubernetes) or DeploymentConfig (OpenShift) strategy is changed to "Recreate" instead of "RollingUpdate" (default). This is done to avoid multiple instances of a service from accessing a volume at the same time.

View File

@ -384,6 +384,15 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
// update supported controller
for _, obj := range *objects {
k.UpdateController(obj, fillTemplate, fillObjectMeta)
if len(service.Volumes) > 0 {
switch objType := obj.(type) {
case *extensions.Deployment:
objType.Spec.Strategy.Type = extensions.RecreateDeploymentStrategyType
case *deployapi.DeploymentConfig:
objType.Spec.Strategy.Type = deployapi.DeploymentStrategyTypeRecreate
}
}
}
}

View File

@ -182,3 +182,29 @@ func TestServiceWithoutPort(t *testing.T) {
}
}
// Tests if deployment strategy is being set to Recreate when volumes are
// present
func TestRecreateStrategyWithVolumesPresent(t *testing.T) {
service := kobject.ServiceConfig{
ContainerName: "name",
Image: "image",
Volumes: []string{"/tmp/volume"},
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
}
k := Kubernetes{}
objects := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 1})
for _, obj := range objects {
if deployment, ok := obj.(*extensions.Deployment); ok {
if deployment.Spec.Strategy.Type != extensions.RecreateDeploymentStrategyType {
t.Errorf("Expected %v as Strategy Type, got %v",
extensions.RecreateDeploymentStrategyType,
deployment.Spec.Strategy.Type)
}
}
}
}

View File

@ -380,3 +380,29 @@ func TestRestartOnFailure(t *testing.T) {
}
t.Fatalf("Process ran with err %v, want exit status 1", err)
}
// Tests if deployment strategy is being set to Recreate when volumes are
// present
func TestRecreateStrategyWithVolumesPresent(t *testing.T) {
service := kobject.ServiceConfig{
ContainerName: "name",
Image: "image",
Volumes: []string{"/tmp/volume"},
}
komposeObject := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
}
o := OpenShift{Kubernetes: kubernetes.Kubernetes{}}
objects := o.Transform(komposeObject, kobject.ConvertOptions{CreateDeploymentConfig: true, Replicas: 1})
for _, obj := range objects {
if deploymentConfig, ok := obj.(*deployapi.DeploymentConfig); ok {
if deploymentConfig.Spec.Strategy.Type != deployapi.DeploymentStrategyTypeRecreate {
t.Errorf("Expected %v as Strategy Type, got %v",
deployapi.DeploymentStrategyTypeRecreate,
deploymentConfig.Spec.Strategy.Type)
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -177,7 +177,9 @@
"restartPolicy": "Always"
}
},
"strategy": {}
"strategy": {
"type": "Recreate"
}
},
"status": {}
},

View File

@ -177,7 +177,8 @@
},
"spec": {
"strategy": {
"resources": {}
"resources": {},
"type": "Recreate"
},
"triggers": [
{

View File

@ -203,7 +203,9 @@
"restartPolicy": "Always"
}
},
"strategy": {}
"strategy": {
"type": "Recreate"
}
},
"status": {}
},

View File

@ -190,6 +190,7 @@
},
"spec": {
"strategy": {
"type": "Recreate",
"resources": {}
},
"triggers": [

View File

@ -75,7 +75,9 @@
"restartPolicy": "Always"
}
},
"strategy": {}
"strategy": {
"type": "Recreate"
}
},
"status": {}
},

View File

@ -41,7 +41,8 @@
},
"spec": {
"strategy": {
"resources": {}
"resources": {},
"type": "Recreate"
},
"triggers": [
{

View File

@ -106,7 +106,9 @@
"restartPolicy": "Always"
}
},
"strategy": {}
"strategy": {
"type": "Recreate"
}
},
"status": {}
},
@ -185,7 +187,9 @@
"restartPolicy": "Always"
}
},
"strategy": {}
"strategy": {
"type": "Recreate"
}
},
"status": {}
},

View File

@ -67,7 +67,8 @@
},
"spec": {
"strategy": {
"resources": {}
"resources": {},
"type": "Recreate"
},
"triggers": [
{
@ -198,7 +199,8 @@
},
"spec": {
"strategy": {
"resources": {}
"resources": {},
"type": "Recreate"
},
"triggers": [
{