-
Notifications
You must be signed in to change notification settings - Fork 568
/
defaults.go
246 lines (221 loc) · 7.76 KB
/
defaults.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package v2_8_0
import (
"context"
"fmt"
"os"
"strings"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/pps"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/migrations"
"github.com/pachyderm/pachyderm/v2/src/internal/ppsdb"
"github.com/pachyderm/pachyderm/v2/src/internal/ppsutil"
"github.com/pachyderm/pachyderm/v2/src/internal/uuid"
)
func synthesizeClusterDefaults(ctx context.Context, env migrations.Env) error {
if err := env.LockTables(ctx, "collections.cluster_defaults"); err != nil {
return errors.EnsureStack(err)
}
envMap, err := envMap(os.Environ())
if err != nil {
return errors.Wrap(err, "could not convert os.Environ to map")
}
cd, err := defaultsFromEnv(envMap)
if err != nil {
return err
}
js, err := protojson.Marshal(cd)
if err != nil {
return errors.Wrap(err, "could not marshal cluster defaults to JSON")
}
wrapper := &ppsdb.ClusterDefaultsWrapper{Json: string(js)}
if err := ppsdb.CollectionsV2_7_0()[0].ReadWrite(env.Tx).Create(ctx, "", wrapper); err != nil {
return errors.Wrap(err, "could not create cluster defaults")
}
return nil
}
func defaultsFromEnv(envMap map[string]string) (*pps.ClusterDefaults, error) {
var (
cd = &pps.ClusterDefaults{
CreatePipelineRequest: &pps.CreatePipelineRequest{
ResourceRequests: &pps.ResourceSpec{},
SidecarResourceRequests: &pps.ResourceSpec{},
},
}
err error
)
if cd.CreatePipelineRequest.ResourceRequests.Memory, err = getQuantityStringFromEnv(envMap, "PIPELINE_DEFAULT_MEMORY_REQUEST", "256Mi"); err != nil {
return nil, errors.Wrap(err, "could not synthesize pipeline default memory request")
}
if cd.CreatePipelineRequest.ResourceRequests.Cpu, err = getFloat32FromEnv(envMap, "PIPELINE_DEFAULT_CPU_REQUEST", 1); err != nil {
return nil, errors.Wrap(err, "could not synthesize pipeline default CPU request")
}
if cd.CreatePipelineRequest.ResourceRequests.Disk, err = getQuantityStringFromEnv(envMap, "PIPELINE_DEFAULT_STORAGE_REQUEST", "1Gi"); err != nil {
return nil, errors.Wrap(err, "could not synthesize pipeline default disk request")
}
if cd.CreatePipelineRequest.SidecarResourceRequests.Memory, err = getQuantityStringFromEnv(envMap, "SIDECAR_DEFAULT_MEMORY_REQUEST", "256Mi"); err != nil {
return nil, errors.Wrap(err, "could not synthesize sidecar default memory request")
}
if cd.CreatePipelineRequest.SidecarResourceRequests.Cpu, err = getFloat32FromEnv(envMap, "SIDECAR_DEFAULT_CPU_REQUEST", 1); err != nil {
return nil, errors.Wrap(err, "could not synthesize sidecar default CPU request")
}
if cd.CreatePipelineRequest.SidecarResourceRequests.Disk, err = getQuantityStringFromEnv(envMap, "SIDECAR_DEFAULT_STORAGE_REQUEST", "1Gi"); err != nil {
return nil, errors.Wrap(err, "could not synthesize sidecar default disk request")
}
return cd, nil
}
var errEmptyVariable = errors.New("empty environment variable")
func getQuantityFromEnv(env map[string]string, name, defaultValue string) (resource.Quantity, error) {
var (
q resource.Quantity
val, ok = env[name]
err error
)
if val == "" {
if ok {
return q, errors.Wrap(errEmptyVariable, name)
}
val = defaultValue
}
if q, err = resource.ParseQuantity(val); err != nil {
return q, errors.Wrapf(err, "could not parse %s (%q) as quantity", name, val)
}
if q.Sign() < 0 {
return q, errors.Errorf("negative quantity %v for %s (%s)", q, name, val)
}
return q, nil
}
func getQuantityStringFromEnv(env map[string]string, name, defaultValue string) (string, error) {
q, err := getQuantityFromEnv(env, name, defaultValue)
if err != nil {
return "", errors.Wrap(err, "could not get quantity")
}
return q.String(), nil
}
func getFloat32FromEnv(env map[string]string, name string, defaultValue float32) (float32, error) {
q, err := getQuantityFromEnv(env, name, resource.NewMilliQuantity(int64(defaultValue*1000), resource.DecimalSI).String())
if err != nil {
return 0, errors.Wrap(err, "could not get quantity")
}
return float32(q.AsApproximateFloat64()), nil
}
// envMap converts a list of NAME=VAL pairs (as returned by os.Environ) to a map
// from names to values. Later values overwrite earlier ones.
func envMap(environ []string) (map[string]string, error) {
var env = make(map[string]string)
for _, e := range environ {
ee := strings.SplitN(e, "=", 2)
if len(ee) == 1 {
return nil, errors.Errorf("invalid environment member %s", e)
}
env[ee[0]] = ee[1]
}
return env, nil
}
func versionKey(p *pps.Pipeline, version uint64) string {
// zero pad in case we want to sort
return fmt.Sprintf("%s@%08d", p, version)
}
var pipelinesVersionIndex = &index{
Name: "version",
Extract: func(val proto.Message) string {
info := val.(*pps.PipelineInfo)
return versionKey(info.Pipeline, info.Version)
},
}
func pipelinesNameKey(p *pps.Pipeline) string {
return p.String()
}
var pipelinesNameIndex = &index{
Name: "name",
Extract: func(val proto.Message) string {
return pipelinesNameKey(val.(*pps.PipelineInfo).Pipeline)
},
}
var pipelinesIndexes = []*index{
pipelinesVersionIndex,
pipelinesNameIndex,
}
func pipelineCommitKey(commit *pfs.Commit) (string, error) {
if commit.Repo.Type != pfs.SpecRepoType {
return "", errors.Errorf("commit %s is not from a spec repo", commit)
}
if projectName := commit.Repo.Project.GetName(); projectName != "" {
return fmt.Sprintf("%s/%s@%s", projectName, commit.Repo.Name, commit.Id), nil
}
return fmt.Sprintf("%s@%s", commit.Repo.Name, commit.Id), nil
}
func withKeyGen(gen func(interface{}) (string, error)) colOption {
return func(c *postgresCollection) {
c.keyGen = gen
}
}
func withKeyCheck(check func(string) error) colOption {
return func(c *postgresCollection) {
c.keyCheck = check
}
}
func parsePipelineKey(key string) (projectName, pipelineName, id string, err error) {
parts := strings.Split(key, "@")
if len(parts) != 2 || !uuid.IsUUIDWithoutDashes(parts[1]) {
return "", "", "", errors.Errorf("key %s is not of form [<project>/]<pipeline>@<id>", key)
}
id = parts[1]
parts = strings.Split(parts[0], "/")
if len(parts) == 0 {
return "", "", "", errors.Errorf("key %s is not of form [<project>/]<pipeline>@<id>")
}
pipelineName = parts[len(parts)-1]
if len(parts) == 1 {
return
}
projectName = strings.Join(parts[0:len(parts)-1], "/")
return
}
func synthesizeSpec(pi *pps.PipelineInfo) error {
if pi == nil {
return errors.New("nil PipelineInfo")
}
// create an initial user and effective spec equal to what would have been previously used
spec := ppsutil.PipelineReqFromInfo(pi)
js, err := protojson.Marshal(spec)
if err != nil {
return errors.Wrapf(err, "could not marshal CreatePipelineRequest as JSON")
}
if pi.EffectiveSpecJson == "" {
pi.EffectiveSpecJson = string(js)
}
if pi.UserSpecJson == "" {
pi.UserSpecJson = string(js)
}
return nil
}
func synthesizeSpecs(ctx context.Context, env migrations.Env) error {
var pipelineInfo = new(pps.PipelineInfo)
if err := migratePostgreSQLCollection(ctx, env.Tx, "pipelines", pipelinesIndexes, pipelineInfo, func(oldKey string) (newKey string, newVal proto.Message, err error) {
if err = synthesizeSpec(pipelineInfo); err != nil {
return "", nil, err
}
if newKey, err = pipelineCommitKey(pipelineInfo.SpecCommit); err != nil {
return
}
return newKey, pipelineInfo, nil
},
withKeyGen(func(key interface{}) (string, error) {
if commit, ok := key.(*pfs.Commit); ok {
return pipelineCommitKey(commit)
}
return "", errors.New("must provide a spec commit")
}),
withKeyCheck(func(key string) error {
_, _, _, err := parsePipelineKey(key)
return err
}),
); err != nil {
return errors.Wrap(err, "could not migrate jobs")
}
return nil
}