forked from LaconicNetwork/kompose
Fixes #575
This commit Add support for cap_add & cap_drop which maps to Pod.Spec.Container.SecurityContext.Capabilities.Add/Drop Added unit tests for ConfigCapabilities function Updated conversion.md on support for these keys
This commit is contained in:
@@ -361,6 +361,9 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
// Configure the container ports.
|
||||
ports := k.ConfigPorts(name, service)
|
||||
|
||||
// Configure capabilities
|
||||
capabilities := k.ConfigCapabilities(service)
|
||||
|
||||
// Configure annotations
|
||||
annotations := transformer.ConfigAnnotations(service)
|
||||
|
||||
@@ -401,6 +404,11 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
|
||||
}
|
||||
|
||||
//set capabilities if it is not empty
|
||||
if len(capabilities.Add) > 0 || len(capabilities.Drop) > 0 {
|
||||
securityContext.Capabilities = capabilities
|
||||
}
|
||||
|
||||
// update template only if securityContext is not empty
|
||||
if *securityContext != (api.SecurityContext{}) {
|
||||
template.Spec.Containers[0].SecurityContext = securityContext
|
||||
|
||||
@@ -331,6 +331,22 @@ func (k *Kubernetes) ConfigServicePorts(name string, service kobject.ServiceConf
|
||||
return servicePorts
|
||||
}
|
||||
|
||||
//ConfigCapabilities configure POSIX capabilities that can be added or removed to a container
|
||||
func (k *Kubernetes) ConfigCapabilities(service kobject.ServiceConfig) *api.Capabilities {
|
||||
capsAdd := []api.Capability{}
|
||||
capsDrop := []api.Capability{}
|
||||
for _, capAdd := range service.CapAdd {
|
||||
capsAdd = append(capsAdd, api.Capability(capAdd))
|
||||
}
|
||||
for _, capDrop := range service.CapDrop {
|
||||
capsDrop = append(capsDrop, api.Capability(capDrop))
|
||||
}
|
||||
return &api.Capabilities{
|
||||
Add: capsAdd,
|
||||
Drop: capsDrop,
|
||||
}
|
||||
}
|
||||
|
||||
// ConfigTmpfs configure the tmpfs.
|
||||
func (k *Kubernetes) ConfigTmpfs(name string, service kobject.ServiceConfig) ([]api.VolumeMount, []api.Volume) {
|
||||
//initializing volumemounts and volumes
|
||||
|
||||
@@ -45,10 +45,10 @@ func newServiceConfig() kobject.ServiceConfig {
|
||||
Network: []string{"network1", "network2"}, // not supported
|
||||
Labels: nil,
|
||||
Annotations: map[string]string{"abc": "def"},
|
||||
CPUQuota: 1, // not supported
|
||||
CapAdd: []string{"cap_add"}, // not supported
|
||||
CapDrop: []string{"cap_drop"}, // not supported
|
||||
Expose: []string{"expose"}, // not supported
|
||||
CPUQuota: 1, // not supported
|
||||
CapAdd: []string{"cap_add"},
|
||||
CapDrop: []string{"cap_drop"},
|
||||
Expose: []string{"expose"}, // not supported
|
||||
Privileged: true,
|
||||
Restart: "always",
|
||||
Stdin: true,
|
||||
@@ -509,3 +509,22 @@ func TestConfigTmpfs(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestConfigCapabilities(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
service kobject.ServiceConfig
|
||||
result api.Capabilities
|
||||
}{
|
||||
"ConfigCapsWithAddDrop": {kobject.ServiceConfig{CapAdd: []string{"CHOWN", "SETUID"}, CapDrop: []string{"KILL"}}, api.Capabilities{Add: []api.Capability{api.Capability("CHOWN"), api.Capability("SETUID")}, Drop: []api.Capability{api.Capability("KILL")}}},
|
||||
"ConfigCapsNoAddDrop": {kobject.ServiceConfig{CapAdd: nil, CapDrop: nil}, api.Capabilities{Add: []api.Capability{}, Drop: []api.Capability{}}},
|
||||
}
|
||||
|
||||
k := Kubernetes{}
|
||||
for name, test := range testCases {
|
||||
t.Log("Test case:", name)
|
||||
result := k.ConfigCapabilities(test.service)
|
||||
if !reflect.DeepEqual(result.Add, test.result.Add) || !reflect.DeepEqual(result.Drop, test.result.Drop) {
|
||||
t.Errorf("Not expected result for ConfigCapabilities")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user