Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Add label setting test
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielhartmann committed Oct 23, 2018
1 parent 14a3936 commit 4b0a232
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
3 changes: 2 additions & 1 deletion config/config.go
Expand Up @@ -19,7 +19,8 @@ const (
)

// Config contains the executor configuration
type Config struct { // nolint: maligned
type Config struct {
// nolint: maligned
// MetatronEnabled returns if Metatron is enabled
MetatronEnabled bool
// PrivilegedContainersEnabled returns whether to give tasks CAP_SYS_ADMIN
Expand Down
52 changes: 42 additions & 10 deletions executor/runtime/container.go
Expand Up @@ -8,26 +8,58 @@ import (
runtimeTypes "github.com/Netflix/titus-executor/executor/runtime/types"
)

const (
cpuLabelKey = "com.netflix.titus.cpu"
memLabelKey = "com.netflix.titus.mem"
diskLabelKey = "com.netflix.titus.disk"
networkLabelKey = "com.netflix.titus.network"
workloadTypeLabelKey = "com.netflix.titus.workload.type"
titusTaskInstanceIDKey = "TITUS_TASK_INSTANCE_ID"
)

// WorkloadType classifies isolation behaviors on resources (e.g. CPU). The exact implementation details of the
// isolation mechanism are determine by an isolation service (e.g. titus-isolate).
type WorkloadType string

// Regardless of isolation mechanism:
//
// "static" workloads are provided resources which to the greatest degree possible are isolated from other workloads
// on a given host. In return they opt out of the opportunity to consume unused resources opportunistically.
//
// "burst" workloads opt in to consumption of unused resources on a host at the cost of accepting the possibility of
// more resource interference from other workloads.
const (
StaticWorkloadType WorkloadType = "static"
BurstWorkloadType WorkloadType = "burst"
)

// NewContainer allocates and initializes a new container struct object
func NewContainer(taskID string, titusInfo *titus.ContainerInfo, constraints *runtimeTypes.Resources, labels map[string]string, cfg config.Config) *runtimeTypes.Container {
func NewContainer(taskID string, titusInfo *titus.ContainerInfo, resources *runtimeTypes.Resources, labels map[string]string, cfg config.Config) *runtimeTypes.Container {
networkCfgParams := titusInfo.GetNetworkConfigInfo()

strCPU := strconv.FormatInt(constraints.CPU, 10)
strMem := strconv.FormatInt(constraints.Mem, 10)
strDisk := strconv.FormatUint(constraints.Disk, 10)
strCPU := strconv.FormatInt(resources.CPU, 10)
strMem := strconv.FormatInt(resources.Mem, 10)
strDisk := strconv.FormatUint(resources.Disk, 10)
strNetwork := strconv.FormatUint(uint64(networkCfgParams.GetBandwidthLimitMbps()), 10)

env := cfg.GetNetflixEnvForTask(titusInfo, strMem, strCPU, strDisk, strNetwork)
labels["TITUS_TASK_INSTANCE_ID"] = env["TITUS_TASK_INSTANCE_ID"]
labels["com.netflix.titus.cpu"] = strCPU
labels["com.netflix.titus.mem"] = strMem
labels["com.netflix.titus.disk"] = strDisk
labels["com.netflix.titus.network"] = strNetwork
labels[titusTaskInstanceIDKey] = env[titusTaskInstanceIDKey]
labels[cpuLabelKey] = strCPU
labels[memLabelKey] = strMem
labels[diskLabelKey] = strDisk
labels[networkLabelKey] = strNetwork

workloadType := StaticWorkloadType
if titusInfo.GetAllowCpuBursting() {
workloadType = BurstWorkloadType
}

labels[workloadTypeLabelKey] = string(workloadType)

c := &runtimeTypes.Container{
TaskID: taskID,
TitusInfo: titusInfo,
Resources: constraints,
Resources: resources,
Env: env,
Labels: labels,
SecurityGroupIDs: networkCfgParams.GetSecurityGroups(),
Expand Down
46 changes: 46 additions & 0 deletions executor/runtime/container_test.go
@@ -1,6 +1,7 @@
package runtime

import (
"strconv"
"testing"

"github.com/Netflix/titus-executor/api/netflix/titus"
Expand Down Expand Up @@ -78,3 +79,48 @@ func TestImageByDigestIgnoresTag(t *testing.T) {
t.Fatalf("Expected %s, got %s", expected, got)
}
}

func TestNewContainer(t *testing.T) {
taskID := "task-id"
expectedCPU := int64(2)
expectedMem := int64(1024)
expectedDisk := uint64(15000)
expectedNetwork := uint32(256)
expectedWorkloadType := BurstWorkloadType
batch := true

containerInfo := &titus.ContainerInfo{
ImageName: protobuf.String("titusoss/alpine"),
Version: protobuf.String("latest"),
NetworkConfigInfo: &titus.ContainerInfo_NetworkConfigInfo{
BandwidthLimitMbps: &expectedNetwork,
},
AllowCpuBursting: &batch,
}

resources := &runtimeTypes.Resources{
CPU: expectedCPU,
Mem: expectedMem,
Disk: expectedDisk,
}

labels := make(map[string]string)
config := config.Config{}

container := NewContainer(taskID, containerInfo, resources, labels, config)

actualCPU, _ := strconv.ParseInt(container.Labels[cpuLabelKey], 10, 64)
assert.Equal(t, expectedCPU, actualCPU)

actualMem, _ := strconv.ParseInt(container.Labels[memLabelKey], 10, 64)
assert.Equal(t, expectedMem, actualMem)

actualDisk, _ := strconv.ParseUint(container.Labels[diskLabelKey], 10, 64)
assert.Equal(t, expectedDisk, actualDisk)

actualNetwork, _ := strconv.ParseUint(container.Labels[networkLabelKey], 10, 32)
assert.Equal(t, expectedNetwork, uint32(actualNetwork))

actualWorkloadType := container.Labels[workloadTypeLabelKey]
assert.Equal(t, expectedWorkloadType, WorkloadType(actualWorkloadType))
}

0 comments on commit 4b0a232

Please sign in to comment.