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:
+30
-26
@@ -16,7 +16,10 @@ limitations under the License.
|
||||
|
||||
package kobject
|
||||
|
||||
import "k8s.io/kubernetes/pkg/api"
|
||||
import (
|
||||
"github.com/docker/libcompose/yaml"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
// KomposeObject holds the generic struct of Kompose transformation
|
||||
type KomposeObject struct {
|
||||
@@ -54,31 +57,32 @@ type ConvertOptions struct {
|
||||
type ServiceConfig struct {
|
||||
// use tags to mark from what element this value comes
|
||||
ContainerName string
|
||||
Image string `compose:"image",bundle:"Image"`
|
||||
Environment []EnvVar `compose:"environment",bundle:"Env"`
|
||||
Port []Ports `compose:"ports",bundle:"Ports"`
|
||||
Command []string `compose:"command",bundle:"Command"`
|
||||
WorkingDir string `compose:"",bundle:"WorkingDir"`
|
||||
Args []string `compose:"args",bundle:"Args"`
|
||||
Volumes []string `compose:"volumes",bundle:"Volumes"`
|
||||
Network []string `compose:"network",bundle:"Networks"`
|
||||
Labels map[string]string `compose:"labels",bundle:"Labels"`
|
||||
Annotations map[string]string `compose:"",bundle:""`
|
||||
CPUSet string `compose:"cpuset",bundle:""`
|
||||
CPUShares int64 `compose:"cpu_shares",bundle:""`
|
||||
CPUQuota int64 `compose:"cpu_quota",bundle:""`
|
||||
CapAdd []string `compose:"cap_add",bundle:""`
|
||||
CapDrop []string `compose:"cap_drop",bundle:""`
|
||||
Expose []string `compose:"expose",bundle:""`
|
||||
Privileged bool `compose:"privileged",bundle:""`
|
||||
Restart string `compose:"restart",bundle:""`
|
||||
User string `compose:"user",bundle:"User"`
|
||||
VolumesFrom []string `compose:"volumes_from",bundle:""`
|
||||
ServiceType string `compose:"kompose.service.type",bundle:""`
|
||||
Build string `compose:"build",bundle:""`
|
||||
ExposeService string `compose:"kompose.service.expose",bundle:""`
|
||||
Stdin bool `compose:"stdin_open",bundle:""`
|
||||
Tty bool `compose:"tty",bundle:""`
|
||||
Image string `compose:"image",bundle:"Image"`
|
||||
Environment []EnvVar `compose:"environment",bundle:"Env"`
|
||||
Port []Ports `compose:"ports",bundle:"Ports"`
|
||||
Command []string `compose:"command",bundle:"Command"`
|
||||
WorkingDir string `compose:"",bundle:"WorkingDir"`
|
||||
Args []string `compose:"args",bundle:"Args"`
|
||||
Volumes []string `compose:"volumes",bundle:"Volumes"`
|
||||
Network []string `compose:"network",bundle:"Networks"`
|
||||
Labels map[string]string `compose:"labels",bundle:"Labels"`
|
||||
Annotations map[string]string `compose:"",bundle:""`
|
||||
CPUSet string `compose:"cpuset",bundle:""`
|
||||
CPUShares int64 `compose:"cpu_shares",bundle:""`
|
||||
CPUQuota int64 `compose:"cpu_quota",bundle:""`
|
||||
CapAdd []string `compose:"cap_add",bundle:""`
|
||||
CapDrop []string `compose:"cap_drop",bundle:""`
|
||||
Expose []string `compose:"expose",bundle:""`
|
||||
Privileged bool `compose:"privileged",bundle:""`
|
||||
Restart string `compose:"restart",bundle:""`
|
||||
User string `compose:"user",bundle:"User"`
|
||||
VolumesFrom []string `compose:"volumes_from",bundle:""`
|
||||
ServiceType string `compose:"kompose.service.type",bundle:""`
|
||||
Build string `compose:"build",bundle:""`
|
||||
ExposeService string `compose:"kompose.service.expose",bundle:""`
|
||||
Stdin bool `compose:"stdin_open",bundle:""`
|
||||
Tty bool `compose:"tty",bundle:""`
|
||||
MemLimit yaml.MemStringorInt `compose:"mem_limit",bundle:""`
|
||||
}
|
||||
|
||||
// EnvVar holds the environment variable struct of a container
|
||||
|
||||
@@ -64,7 +64,6 @@ func checkUnsupportedKey(composeProject *project.Project) []string {
|
||||
"Ipc": false,
|
||||
"Logging": false,
|
||||
"MacAddress": false,
|
||||
"MemLimit": false,
|
||||
"MemSwapLimit": false,
|
||||
"NetworkMode": false,
|
||||
"Pid": false,
|
||||
@@ -366,6 +365,7 @@ func (c *Compose) LoadFile(files []string) kobject.KomposeObject {
|
||||
serviceConfig.VolumesFrom = composeServiceConfig.VolumesFrom
|
||||
serviceConfig.Stdin = composeServiceConfig.StdinOpen
|
||||
serviceConfig.Tty = composeServiceConfig.Tty
|
||||
serviceConfig.MemLimit = composeServiceConfig.MemLimit
|
||||
|
||||
komposeObject.ServiceConfigs[name] = serviceConfig
|
||||
}
|
||||
|
||||
@@ -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