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

Commit

Permalink
Add command and entrypoint labels to containers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielhartmann committed Mar 14, 2019
1 parent 270b06c commit 44dea74
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
73 changes: 59 additions & 14 deletions executor/runtime/container.go
Expand Up @@ -2,6 +2,7 @@ package runtime

import (
"strconv"
"strings"

"github.com/Netflix/titus-executor/api/netflix/titus"
"github.com/Netflix/titus-executor/config"
Expand All @@ -10,6 +11,8 @@ import (

const (
appNameLabelKey = "com.netflix.titus.appName"
commandLabelKey = "com.netflix.titus.command"
entrypointLabelKey = "com.netflix.titus.entrypoint"
cpuLabelKey = "com.netflix.titus.cpu"
memLabelKey = "com.netflix.titus.mem"
diskLabelKey = "com.netflix.titus.disk"
Expand Down Expand Up @@ -48,25 +51,12 @@ func NewContainer(taskID string, titusInfo *titus.ContainerInfo, resources *runt
strNetwork := strconv.FormatUint(uint64(networkCfgParams.GetBandwidthLimitMbps()), 10)

env := cfg.GetNetflixEnvForTask(titusInfo, strMem, strCPU, strDisk, strNetwork)
labels[appNameLabelKey] = titusInfo.GetAppName()
labels[titusTaskInstanceIDKey] = env[titusTaskInstanceIDKey]
labels[cpuLabelKey] = strCPU
labels[memLabelKey] = strMem
labels[diskLabelKey] = strDisk
labels[networkLabelKey] = strNetwork

passthroughAttributes := titusInfo.GetPassthroughAttributes()
if passthroughAttributes != nil {
labels[ownerEmailLabelKey] = passthroughAttributes[ownerEmailPassThroughKey]
labels[jobTypeLabelKey] = passthroughAttributes[jobTypePassThroughKey]
}

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

labels[workloadTypeLabelKey] = string(workloadType)
addLabels(titusInfo, labels)

c := &runtimeTypes.Container{
TaskID: taskID,
Expand All @@ -88,3 +78,58 @@ func NewContainer(taskID string, titusInfo *titus.ContainerInfo, resources *runt

return c
}

func addLabels(containerInfo *titus.ContainerInfo, labels map[string]string) map[string]string {
labels = addContainerLabels(containerInfo, labels)
labels = addPassThroughLabels(containerInfo, labels)
labels = addProcessLabels(containerInfo, labels)
return labels
}

func addContainerLabels(containerInfo *titus.ContainerInfo, labels map[string]string) map[string]string {
labels[appNameLabelKey] = containerInfo.GetAppName()

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

labels[workloadTypeLabelKey] = string(workloadType)

return labels
}

func addPassThroughLabels(containerInfo *titus.ContainerInfo, labels map[string]string) map[string]string {
ownerEmail := ""
jobType := ""

passthroughAttributes := containerInfo.GetPassthroughAttributes()
if passthroughAttributes != nil {
ownerEmail = passthroughAttributes[ownerEmailPassThroughKey]
jobType = passthroughAttributes[jobTypePassThroughKey]
}

labels[ownerEmailLabelKey] = ownerEmail
labels[jobTypeLabelKey] = jobType

return labels
}

func addProcessLabels(containerInfo *titus.ContainerInfo, labels map[string]string) map[string]string {
process := containerInfo.GetProcess()
if process != nil {
entryPoint := process.GetEntrypoint()
if entryPoint != nil {
entryPointStr := strings.Join(entryPoint[:], " ")
labels[entrypointLabelKey] = entryPointStr
}

command := process.GetCommand()
if command != nil {
commandStr := strings.Join(entryPoint[:], " ")
labels[commandLabelKey] = commandStr
}
}

return labels
}
12 changes: 12 additions & 0 deletions executor/runtime/container_test.go
Expand Up @@ -2,6 +2,7 @@ package runtime

import (
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -105,6 +106,7 @@ func TestNewContainer(t *testing.T) {
expectedPassthroughAttributes := make(map[string]string)
expectedPassthroughAttributes[ownerEmailPassThroughKey] = expectedOwnerEmail
expectedPassthroughAttributes[jobTypePassThroughKey] = expectedJobType
expectedCommand := "cmd arg0 arg1"

containerInfo := &titus.ContainerInfo{
AppName: &expectedAppName,
Expand All @@ -121,6 +123,10 @@ func TestNewContainer(t *testing.T) {
JobGroupSequence: &expectedJobSeq,
ImageDigest: &expectedDigest,
PassthroughAttributes: expectedPassthroughAttributes,
Process: &titus.ContainerInfo_Process{
Command: strings.Split(expectedCommand, " "),
Entrypoint: strings.Split(expectedCommand, " "),
},
}

resources := &runtimeTypes.Resources{
Expand All @@ -137,6 +143,12 @@ func TestNewContainer(t *testing.T) {
actualAppName := container.Labels[appNameLabelKey]
assert.Equal(t, expectedAppName, actualAppName)

actualCommand := container.Labels[commandLabelKey]
assert.Equal(t, expectedCommand, actualCommand)

actualEntrypoint := container.Labels[entrypointLabelKey]
assert.Equal(t, expectedCommand, actualEntrypoint)

actualOwnerEmail := container.Labels[ownerEmailLabelKey]
assert.Equal(t, expectedOwnerEmail, actualOwnerEmail)

Expand Down

0 comments on commit 44dea74

Please sign in to comment.