Adds mem_limit support for conversion
This commit adds mem_limit support. Taking the value from docker-compose.yaml and converting it to it's associative value in Kubernetes artifacts. Closes (half) of https://github.com/kubernetes-incubator/kompose/issues/267
This commit is contained in:
@@ -39,6 +39,7 @@ import (
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
|
||||
deployapi "github.com/openshift/origin/pkg/deploy/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -343,6 +344,15 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
template.Spec.Containers[0].TTY = service.Tty
|
||||
template.Spec.Volumes = volumes
|
||||
|
||||
// Configure the resource limits
|
||||
if service.MemLimit != 0 {
|
||||
memoryResourceList := api.ResourceList{
|
||||
api.ResourceMemory: *resource.NewQuantity(
|
||||
int64(service.MemLimit), "RandomStringForFormat")}
|
||||
template.Spec.Containers[0].Resources.Limits = memoryResourceList
|
||||
}
|
||||
|
||||
// Setup security context
|
||||
securityContext := &api.SecurityContext{}
|
||||
if service.Privileged == true {
|
||||
securityContext.Privileged = &service.Privileged
|
||||
@@ -356,6 +366,7 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// update template only if securityContext is not empty
|
||||
if *securityContext != (api.SecurityContext{}) {
|
||||
template.Spec.Containers[0].SecurityContext = securityContext
|
||||
@@ -363,6 +374,7 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
|
||||
template.Spec.Containers[0].Ports = ports
|
||||
template.ObjectMeta.Labels = transformer.ConfigLabels(name)
|
||||
|
||||
// Configure the container restart policy.
|
||||
switch service.Restart {
|
||||
case "", "always":
|
||||
|
||||
@@ -67,11 +67,59 @@ func TestCreateService(t *testing.T) {
|
||||
|
||||
// Test the creation of the service
|
||||
svc := k.CreateService("foo", service, objects)
|
||||
|
||||
if svc.Spec.Ports[0].Port != 123 {
|
||||
t.Errorf("Expected port 123 upon conversion, actual %d", svc.Spec.Ports[0].Port)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Test the creation of a service with a memory limit
|
||||
*/
|
||||
func TestCreateServiceWithMemLimit(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"}, // not supported
|
||||
Labels: nil,
|
||||
Annotations: map[string]string{"abc": "def"},
|
||||
CPUSet: "cpu_set", // not supported
|
||||
CPUShares: 1, // not supported
|
||||
CPUQuota: 1, // not supported
|
||||
CapAdd: []string{"cap_add"}, // not supported
|
||||
CapDrop: []string{"cap_drop"}, // not supported
|
||||
Expose: []string{"expose"}, // not supported
|
||||
Privileged: true,
|
||||
Restart: "always",
|
||||
MemLimit: 1337,
|
||||
}
|
||||
|
||||
// An example object generated via k8s runtime.Objects()
|
||||
komposeObject := kobject.KomposeObject{
|
||||
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
|
||||
}
|
||||
k := Kubernetes{}
|
||||
objects := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 3})
|
||||
|
||||
// Retrieve the deployment object and test that it matches the MemLimit value
|
||||
for _, obj := range objects {
|
||||
if deploy, ok := obj.(*extensions.Deployment); ok {
|
||||
memTest, _ := deploy.Spec.Template.Spec.Containers[0].Resources.Limits.Memory().AsInt64()
|
||||
if memTest != 1337 {
|
||||
t.Errorf("Expected 1337 for mem_limit check, got %v", memTest)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Test the creation of a service with a specified user.
|
||||
The expected result is that Kompose will set user in PodSpec
|
||||
|
||||
Reference in New Issue
Block a user