add support for "pid" key

solve #610
convert service.pid to Pod.Spec.HostPid
set Pod.Spec.HostPid to true if service.pid ="host", to false otherwise
update conversion.md on support for the key
This commit is contained in:
gitlawr
2017-05-31 22:39:17 +08:00
parent a6df28f98d
commit 1485ed4479
5 changed files with 90 additions and 3 deletions
+1
View File
@@ -76,6 +76,7 @@ type ServiceConfig struct {
CapAdd []string `compose:"cap_add" bundle:""`
CapDrop []string `compose:"cap_drop" bundle:""`
Expose []string `compose:"expose" bundle:""`
Pid string `compose:"pid" bundle:""`
Privileged bool `compose:"privileged" bundle:""`
Restart string `compose:"restart" bundle:""`
User string `compose:"user" bundle:"User"`
+1 -1
View File
@@ -68,7 +68,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
"MacAddress": false,
"MemSwapLimit": false,
"NetworkMode": false,
"Pid": false,
"SecurityOpt": false,
"ShmSize": false,
"StopSignal": false,
@@ -368,6 +367,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
serviceConfig.CPUQuota = int64(composeServiceConfig.CPUQuota)
serviceConfig.CapAdd = composeServiceConfig.CapAdd
serviceConfig.CapDrop = composeServiceConfig.CapDrop
serviceConfig.Pid = composeServiceConfig.Pid
serviceConfig.Expose = composeServiceConfig.Expose
serviceConfig.Privileged = composeServiceConfig.Privileged
serviceConfig.Restart = composeServiceConfig.Restart
+13 -1
View File
@@ -399,6 +399,16 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
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
securityContext := &api.SecurityContext{}
if service.Privileged == true {
@@ -423,7 +433,9 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
if *securityContext != (api.SecurityContext{}) {
template.Spec.Containers[0].SecurityContext = securityContext
}
if !reflect.DeepEqual(*podSecurityContext, api.PodSecurityContext{}) {
template.Spec.SecurityContext = podSecurityContext
}
template.Spec.Containers[0].Ports = ports
template.ObjectMeta.Labels = transformer.ConfigLabels(name)
@@ -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) {
tempPath := "/tmp/kompose_unit"
tempDir := filepath.Join(tempPath, "i_am_dir")