Merge pull request #617 from gitlawr/add_pid

add support for "pid" key. Fixes #610
This commit is contained in:
Suraj Deshmukh 2017-05-31 21:36:54 +05:30 committed by GitHub
commit a35d5965b5
5 changed files with 90 additions and 3 deletions

View File

@ -32,7 +32,7 @@ This document outlines all possible conversion details regarding `docker-compose
| logging | N | | | | logging | N | | |
| network_mode | N | | | | network_mode | N | | |
| networks | N | | | | networks | N | | |
| pid | N | | | | pid | Y | Pod.Spec.HostPID | |
| ports | Y | Service.Spec.Ports | | | ports | Y | Service.Spec.Ports | |
| security_opt | N | | | | security_opt | N | | |
| stop_grace_period | Y | Pod.Spec.TerminationGracePeriodSeconds | | | stop_grace_period | Y | Pod.Spec.TerminationGracePeriodSeconds | |

View File

@ -76,6 +76,7 @@ type ServiceConfig struct {
CapAdd []string `compose:"cap_add" bundle:""` CapAdd []string `compose:"cap_add" bundle:""`
CapDrop []string `compose:"cap_drop" bundle:""` CapDrop []string `compose:"cap_drop" bundle:""`
Expose []string `compose:"expose" bundle:""` Expose []string `compose:"expose" bundle:""`
Pid string `compose:"pid" bundle:""`
Privileged bool `compose:"privileged" bundle:""` Privileged bool `compose:"privileged" bundle:""`
Restart string `compose:"restart" bundle:""` Restart string `compose:"restart" bundle:""`
User string `compose:"user" bundle:"User"` User string `compose:"user" bundle:"User"`

View File

@ -68,7 +68,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
"MacAddress": false, "MacAddress": false,
"MemSwapLimit": false, "MemSwapLimit": false,
"NetworkMode": false, "NetworkMode": false,
"Pid": false,
"SecurityOpt": false, "SecurityOpt": false,
"ShmSize": false, "ShmSize": false,
"StopSignal": false, "StopSignal": false,
@ -368,6 +367,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
serviceConfig.CPUQuota = int64(composeServiceConfig.CPUQuota) serviceConfig.CPUQuota = int64(composeServiceConfig.CPUQuota)
serviceConfig.CapAdd = composeServiceConfig.CapAdd serviceConfig.CapAdd = composeServiceConfig.CapAdd
serviceConfig.CapDrop = composeServiceConfig.CapDrop serviceConfig.CapDrop = composeServiceConfig.CapDrop
serviceConfig.Pid = composeServiceConfig.Pid
serviceConfig.Expose = composeServiceConfig.Expose serviceConfig.Expose = composeServiceConfig.Expose
serviceConfig.Privileged = composeServiceConfig.Privileged serviceConfig.Privileged = composeServiceConfig.Privileged
serviceConfig.Restart = composeServiceConfig.Restart serviceConfig.Restart = composeServiceConfig.Restart

View File

@ -399,6 +399,16 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
template.Spec.Containers[0].Resources.Limits = memoryResourceList template.Spec.Containers[0].Resources.Limits = memoryResourceList
} }
podSecurityContext := &api.PodSecurityContext{}
//set pid namespace mode
if service.Pid != "" {
if service.Pid == "host" {
podSecurityContext.HostPID = true
} else {
log.Warningf("Ignoring PID key for service \"%v\". Invalid value \"%v\".", name, service.Pid)
}
}
// Setup security context // Setup security context
securityContext := &api.SecurityContext{} securityContext := &api.SecurityContext{}
if service.Privileged == true { if service.Privileged == true {
@ -423,7 +433,9 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
if *securityContext != (api.SecurityContext{}) { if *securityContext != (api.SecurityContext{}) {
template.Spec.Containers[0].SecurityContext = securityContext template.Spec.Containers[0].SecurityContext = securityContext
} }
if !reflect.DeepEqual(*podSecurityContext, api.PodSecurityContext{}) {
template.Spec.SecurityContext = podSecurityContext
}
template.Spec.Containers[0].Ports = ports template.Spec.Containers[0].Ports = ports
template.ObjectMeta.Labels = transformer.ConfigLabels(name) template.ObjectMeta.Labels = transformer.ConfigLabels(name)

View File

@ -174,6 +174,80 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
} }
func TestTransformWithPid(t *testing.T) {
// An example service
service := kobject.ServiceConfig{
ContainerName: "name",
Image: "image",
Environment: []kobject.EnvVar{kobject.EnvVar{Name: "env", Value: "value"}},
Port: []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: api.ProtocolTCP}},
Command: []string{"cmd"},
WorkingDir: "dir",
Args: []string{"arg1", "arg2"},
Volumes: []string{"/tmp/volume"},
Network: []string{"network1", "network2"},
Restart: "always",
Pid: "host",
}
// An example object generated via k8s runtime.Objects()
komposeObject := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
}
k := Kubernetes{}
objects, err := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 3})
if err != nil {
t.Error(errors.Wrap(err, "k.Transform failed"))
}
for _, obj := range objects {
if deploy, ok := obj.(*extensions.Deployment); ok {
hostPid := deploy.Spec.Template.Spec.SecurityContext.HostPID
if hostPid != true {
t.Errorf("Pid in ServiceConfig is not matching HostPID in PodSpec")
}
}
}
}
func TestTransformWithInvaildPid(t *testing.T) {
// An example service
service := kobject.ServiceConfig{
ContainerName: "name",
Image: "image",
Environment: []kobject.EnvVar{kobject.EnvVar{Name: "env", Value: "value"}},
Port: []kobject.Ports{kobject.Ports{HostPort: 123, ContainerPort: 456, Protocol: api.ProtocolTCP}},
Command: []string{"cmd"},
WorkingDir: "dir",
Args: []string{"arg1", "arg2"},
Volumes: []string{"/tmp/volume"},
Network: []string{"network1", "network2"},
Restart: "always",
Pid: "badvalue",
}
// An example object generated via k8s runtime.Objects()
komposeObject := kobject.KomposeObject{
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
}
k := Kubernetes{}
objects, err := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 3})
if err != nil {
t.Error(errors.Wrap(err, "k.Transform failed"))
}
for _, obj := range objects {
if deploy, ok := obj.(*extensions.Deployment); ok {
if deploy.Spec.Template.Spec.SecurityContext != nil {
hostPid := deploy.Spec.Template.Spec.SecurityContext.HostPID
if hostPid != false {
t.Errorf("Pid in ServiceConfig is not matching HostPID in PodSpec")
}
}
}
}
}
func TestIsDir(t *testing.T) { func TestIsDir(t *testing.T) {
tempPath := "/tmp/kompose_unit" tempPath := "/tmp/kompose_unit"
tempDir := filepath.Join(tempPath, "i_am_dir") tempDir := filepath.Join(tempPath, "i_am_dir")