-
Notifications
You must be signed in to change notification settings - Fork 152
/
executor_input_compiler.go
184 lines (151 loc) · 5.59 KB
/
executor_input_compiler.go
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
173
174
175
176
177
178
179
180
181
182
183
184
package service
import (
"context"
"strings"
"time"
"github.com/raystack/salt/log"
"github.com/raystack/optimus/core/scheduler"
"github.com/raystack/optimus/core/tenant"
"github.com/raystack/optimus/internal/compiler"
"github.com/raystack/optimus/internal/utils"
)
const (
// taskConfigPrefix will be used to prefix all the config variables of
// transformation instance, i.e. task
taskConfigPrefix = "TASK__"
// projectConfigPrefix will be used to prefix all the config variables of
// a project, i.e. registered entities
projectConfigPrefix = "GLOBAL__"
contextProject = "proj"
contextSecret = "secret"
contextSystemDefined = "inst"
contextTask = "task"
SecretsStringToMatch = ".secret."
TimeISOFormat = time.RFC3339
// Configuration for system defined variables
configDstart = "DSTART"
configDend = "DEND"
configExecutionTime = "EXECUTION_TIME"
configDestination = "JOB_DESTINATION"
)
type TenantService interface {
GetDetails(ctx context.Context, tnnt tenant.Tenant) (*tenant.WithDetails, error)
GetSecrets(ctx context.Context, tnnt tenant.Tenant) ([]*tenant.PlainTextSecret, error)
}
type TemplateCompiler interface {
Compile(templateMap map[string]string, context map[string]any) (map[string]string, error)
}
type AssetCompiler interface {
CompileJobRunAssets(ctx context.Context, job *scheduler.Job, systemEnvVars map[string]string, scheduledAt time.Time, contextForTask map[string]interface{}) (map[string]string, error)
}
type InputCompiler struct {
tenantService TenantService
compiler TemplateCompiler
assetCompiler AssetCompiler
logger log.Logger
}
func (i InputCompiler) Compile(ctx context.Context, job *scheduler.Job, config scheduler.RunConfig, executedAt time.Time) (*scheduler.ExecutorInput, error) {
tenantDetails, err := i.tenantService.GetDetails(ctx, job.Tenant)
if err != nil {
i.logger.Error("error getting tenant details: %s", err)
return nil, err
}
systemDefinedVars, err := getSystemDefinedConfigs(job, config, executedAt)
if err != nil {
i.logger.Error("error getting config for job [%s]: %s", job.Name.String(), err)
return nil, err
}
// Prepare template context and compile task config
taskContext := compiler.PrepareContext(
compiler.From(tenantDetails.GetConfigs()).WithName(contextProject).WithKeyPrefix(projectConfigPrefix),
compiler.From(tenantDetails.SecretsMap()).WithName(contextSecret),
compiler.From(systemDefinedVars).WithName(contextSystemDefined).AddToContext(),
)
// Compile asset files
fileMap, err := i.assetCompiler.CompileJobRunAssets(ctx, job, systemDefinedVars, config.ScheduledAt, taskContext)
if err != nil {
i.logger.Error("error compiling job run assets: %s", err)
return nil, err
}
confs, secretConfs, err := i.compileConfigs(job.Task.Config, taskContext)
if err != nil {
i.logger.Error("error compiling task config: %s", err)
return nil, err
}
if config.Executor.Type == scheduler.ExecutorTask {
return &scheduler.ExecutorInput{
Configs: utils.MergeMaps(confs, systemDefinedVars),
Secrets: secretConfs,
Files: fileMap,
}, nil
}
// If request for hook, add task configs to templateContext
hookContext := compiler.PrepareContext(
compiler.From(confs, secretConfs).WithName(contextTask).WithKeyPrefix(taskConfigPrefix),
)
mergedContext := utils.MergeAnyMaps(taskContext, hookContext)
hook, err := job.GetHook(config.Executor.Name)
if err != nil {
i.logger.Error("error getting hook [%s]: %s", config.Executor.Name, err)
return nil, err
}
hookConfs, hookSecrets, err := i.compileConfigs(hook.Config, mergedContext)
if err != nil {
i.logger.Error("error compiling configs for hook [%s]: %s", hook.Name, err)
return nil, err
}
return &scheduler.ExecutorInput{
Configs: utils.MergeMaps(hookConfs, systemDefinedVars),
Secrets: hookSecrets,
Files: fileMap,
}, nil
}
func (i InputCompiler) compileConfigs(configs map[string]string, templateCtx map[string]any) (map[string]string, map[string]string, error) {
conf, secretsConfig := splitConfigWithSecrets(configs)
var err error
if conf, err = i.compiler.Compile(conf, templateCtx); err != nil {
i.logger.Error("error compiling template with config: %s", err)
return nil, nil, err
}
if secretsConfig, err = i.compiler.Compile(secretsConfig, templateCtx); err != nil {
i.logger.Error("error compiling template with secret: %s", err)
return nil, nil, err
}
return conf, secretsConfig, nil
}
func getSystemDefinedConfigs(job *scheduler.Job, runConfig scheduler.RunConfig, executedAt time.Time) (map[string]string, error) {
startTime, err := job.Window.GetStartTime(runConfig.ScheduledAt)
if err != nil {
return nil, err
}
endTime, err := job.Window.GetEndTime(runConfig.ScheduledAt)
if err != nil {
return nil, err
}
return map[string]string{
configDstart: startTime.Format(TimeISOFormat),
configDend: endTime.Format(TimeISOFormat),
configExecutionTime: executedAt.Format(TimeISOFormat),
configDestination: job.Destination,
}, nil
}
func splitConfigWithSecrets(conf map[string]string) (map[string]string, map[string]string) {
configs := map[string]string{}
configWithSecrets := map[string]string{}
for name, val := range conf {
if strings.Contains(val, SecretsStringToMatch) {
configWithSecrets[name] = val
continue
}
configs[name] = val
}
return configs, configWithSecrets
}
func NewJobInputCompiler(tenantService TenantService, compiler TemplateCompiler, assetCompiler AssetCompiler, logger log.Logger) *InputCompiler {
return &InputCompiler{
tenantService: tenantService,
compiler: compiler,
assetCompiler: assetCompiler,
logger: logger,
}
}