This repository was archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathk8s_resource_adds.go
More file actions
executable file
·172 lines (150 loc) · 4.39 KB
/
k8s_resource_adds.go
File metadata and controls
executable file
·172 lines (150 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package flytek8s
import (
"context"
"os"
"strconv"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/flytek8s/config"
"github.com/flyteorg/flytestdlib/contextutils"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
pluginsCore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
)
const (
taskGeneratedNameMinLength = 10
taskGeneratedNameMaxLength = 100
)
func GetContextEnvVars(ownerCtx context.Context) []v1.EnvVar {
var envVars []v1.EnvVar
if ownerCtx == nil {
return envVars
}
// Injecting useful env vars from the context
if wfName := contextutils.Value(ownerCtx, contextutils.WorkflowIDKey); wfName != "" {
envVars = append(envVars,
v1.EnvVar{
Name: "FLYTE_INTERNAL_EXECUTION_WORKFLOW",
Value: wfName,
},
)
}
return envVars
}
func GetExecutionEnvVars(id pluginsCore.TaskExecutionID) []v1.EnvVar {
if id == nil || id.GetID().NodeExecutionId == nil || id.GetID().NodeExecutionId.ExecutionId == nil {
return []v1.EnvVar{}
}
// Execution level env variables.
nodeExecutionID := id.GetID().NodeExecutionId.ExecutionId
attemptNumber := strconv.Itoa(int(id.GetID().RetryAttempt))
envVars := []v1.EnvVar{
{
Name: "FLYTE_INTERNAL_EXECUTION_ID",
Value: nodeExecutionID.Name,
},
{
Name: "FLYTE_INTERNAL_EXECUTION_PROJECT",
Value: nodeExecutionID.Project,
},
{
Name: "FLYTE_INTERNAL_EXECUTION_DOMAIN",
Value: nodeExecutionID.Domain,
},
{
Name: "FLYTE_ATTEMPT_NUMBER",
Value: attemptNumber,
},
// TODO: Fill in these
// {
// Name: "FLYTE_INTERNAL_EXECUTION_WORKFLOW",
// Value: "",
// },
// {
// Name: "FLYTE_INTERNAL_EXECUTION_LAUNCHPLAN",
// Value: "",
// },
}
// Task definition Level env variables.
if id.GetID().TaskId != nil {
taskID := id.GetID().TaskId
// TODO: what's an idiomatic way of handling this error given that we don't propagate errors up in GetExecutionEnvVars?
taskGeneratedName, _ := id.GetGeneratedNameWith(taskGeneratedNameMinLength, taskGeneratedNameMaxLength)
envVars = append(envVars,
v1.EnvVar{
Name: "FLYTE_INTERNAL_TASK_PROJECT",
Value: taskID.Project,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_TASK_DOMAIN",
Value: taskID.Domain,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_TASK_NAME",
Value: taskID.Name,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_TASK_VERSION",
Value: taskID.Version,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_TASK_GENERATED_NAME",
Value: taskGeneratedName,
},
// Historic Task Definition Level env variables.
// Remove these once SDK is migrated to use the new ones.
v1.EnvVar{
Name: "FLYTE_INTERNAL_PROJECT",
Value: taskID.Project,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_DOMAIN",
Value: taskID.Domain,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_NAME",
Value: taskID.Name,
},
v1.EnvVar{
Name: "FLYTE_INTERNAL_VERSION",
Value: taskID.Version,
})
}
return envVars
}
func DecorateEnvVars(ctx context.Context, envVars []v1.EnvVar, id pluginsCore.TaskExecutionID) []v1.EnvVar {
envVars = append(envVars, GetContextEnvVars(ctx)...)
envVars = append(envVars, GetExecutionEnvVars(id)...)
for k, v := range config.GetK8sPluginConfig().DefaultEnvVars {
envVars = append(envVars, v1.EnvVar{Name: k, Value: v})
}
for k, envVarName := range config.GetK8sPluginConfig().DefaultEnvVarsFromEnv {
value := os.Getenv(envVarName)
envVars = append(envVars, v1.EnvVar{Name: k, Value: value})
}
return envVars
}
func GetPodTolerations(interruptible bool, resourceRequirements ...v1.ResourceRequirements) []v1.Toleration {
// 1. Get the tolerations for the resources requested
var tolerations []v1.Toleration
resourceNames := sets.NewString()
for _, resources := range resourceRequirements {
for r := range resources.Limits {
resourceNames.Insert(r.String())
}
for r := range resources.Requests {
resourceNames.Insert(r.String())
}
}
resourceTols := config.GetK8sPluginConfig().ResourceTolerations
for _, r := range resourceNames.UnsortedList() {
if v, ok := resourceTols[v1.ResourceName(r)]; ok {
tolerations = append(tolerations, v...)
}
}
// 2. Get the tolerations for interruptible pods
if interruptible {
tolerations = append(tolerations, config.GetK8sPluginConfig().InterruptibleTolerations...)
}
// 3. Add default tolerations
tolerations = append(tolerations, config.GetK8sPluginConfig().DefaultTolerations...)
return tolerations
}