/
config.go
441 lines (372 loc) · 13.1 KB
/
config.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2021 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"context"
"fmt"
"os"
"path"
"runtime"
"strings"
"sync"
"sync/atomic"
"unsafe"
kserveapi "github.com/kserve/kserve/pkg/apis/serving/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
EnvEtcdSecretName = "ETCD_SECRET_NAME"
DefaultEtcdSecretName = "model-serving-etcd"
ConfigType = "yaml"
MountLocation = "/etc/model-serving/config/default"
ViperKeyDelimiter = "::"
)
var (
defaultConfig *viper.Viper
configLog = ctrl.Log.WithName("config")
)
// Config holds process global configuration information
type Config struct {
// System config
EtcdSecretName string // DEPRECATED - should be removed in the future
ModelMeshEndpoint string // For dev use only
AllowAnyPVC bool
// Service config
InferenceServiceName string
InferenceServicePort uint16
TLS TLSConfig
HeadlessService bool
GrpcMaxMessageSizeBytes int
// Runtimes config
ModelMeshImage ImageConfig
ModelMeshResources ResourceRequirements
RESTProxy RESTProxyConfig
StorageHelperImage ImageConfig
StorageHelperResources ResourceRequirements
PodsPerRuntime uint16
StorageSecretName string
EnableAccessLogging bool
BuiltInServerTypes []string
PayloadProcessors []string
ServiceAccountName string
Metrics PrometheusConfig
ScaleToZero ScaleToZeroConfig
RuntimePodLabels map[string]string
RuntimePodAnnotations map[string]string
ImagePullSecrets []corev1.LocalObjectReference
// For internal use only
InternalModelMeshEnvVars EnvVarList
}
type EnvVarList []EnvVar
type EnvVar struct {
Name string `json:"name"`
Value string `json:"value"`
}
func (evl EnvVarList) ToKubernetesType() []corev1.EnvVar {
env := make([]corev1.EnvVar, len(evl))
for idx, e := range evl {
env[idx] = corev1.EnvVar{
Name: e.Name,
Value: e.Value,
}
}
return env
}
type PrometheusConfig struct {
Enabled bool
Port uint16
Scheme string
DisablePrometheusOperatorSupport bool
}
type ScaleToZeroConfig struct {
Enabled bool
// how long to wait after the last predictor assigned to a runtime is deleted
// before scaling to zero
GracePeriodSeconds uint16
}
type TLSConfig struct {
// TLS disabled if omitted
SecretName string
// Mutual TLS disabled if omitted
ClientAuth string
}
type RESTProxyConfig struct {
Enabled bool
Port uint16
Image ImageConfig
Resources ResourceRequirements
}
func (c *Config) GetEtcdSecretName() string {
secretName, found := os.LookupEnv(EnvEtcdSecretName)
if !found {
secretName = DefaultEtcdSecretName
}
// for backward compatability with the old configmap - should be removed in the future
if secretName == DefaultEtcdSecretName && c.EtcdSecretName != "" {
secretName = c.EtcdSecretName
}
return secretName
}
// ConfigProvider provides immutable snapshots of current config
type ConfigProvider struct {
config unsafe.Pointer
c sync.Cond
isReloading bool
loadedResourceVersion string
}
func NewConfigProvider(ctx context.Context, cl client.Client, name types.NamespacedName) (*ConfigProvider, error) {
// Perform initial load of the default configuration
config, err := NewMergedConfigFromString("")
if err != nil {
return nil, err
}
cp := &ConfigProvider{c: sync.Cond{L: &sync.Mutex{}}, config: (unsafe.Pointer)(config)}
return cp, cp.ReloadConfigMap(ctx, cl, name)
}
func (cp *ConfigProvider) GetConfig() *Config {
return (*Config)(atomic.LoadPointer(&cp.config))
}
// NewConfigProviderForTest is only for tests
func NewConfigProviderForTest() *ConfigProvider {
return &ConfigProvider{c: sync.Cond{L: &sync.Mutex{}}}
}
// SetConfigForTest is only for tests
func SetConfigForTest(cp *ConfigProvider, cfg *Config) {
atomic.StorePointer(&cp.config, (unsafe.Pointer)(cfg))
}
func (cp *ConfigProvider) ReloadConfigMap(ctx context.Context, c client.Client, name types.NamespacedName) error {
cp.c.L.Lock()
cp.isReloading = true
defer func() {
cp.c.L.Unlock()
cp.isReloading = false
}()
// get the user ConfigMap
configmap := corev1.ConfigMap{}
err := c.Get(ctx, name, &configmap)
// any error other than NotFound is unexpected
if err != nil && !errors.IsNotFound(err) {
return err
}
// only reload if the resource version changed from what we last loaded successfully
// if the user config does not exist, configmap.ResourceVersion is the empty string
if configmap.ResourceVersion != cp.loadedResourceVersion {
var err2 error
var newConfig *Config
// if the configmap was deleted but had been previously loaded
if configmap.ResourceVersion == "" {
configLog.Info("User configmap deleted, reverting to defaults", "ConfigMap", name)
// load from empty string to by-pass validation of the configmap
newConfig, err2 = NewMergedConfigFromString("")
} else {
configLog.Info("Reloading user config", "ConfigMap", name)
newConfig, err2 = NewMergedConfigFromConfigMap(configmap)
}
if err2 != nil {
return err2
}
// update the stored resource version to track changes
atomic.StorePointer(&cp.config, (unsafe.Pointer)(newConfig))
cp.loadedResourceVersion = configmap.ResourceVersion
}
cp.c.Broadcast()
return nil
}
// Handler used by controllers which depend on the user configuration
func ConfigWatchHandler(configMapName types.NamespacedName, f func() []reconcile.Request,
cp *ConfigProvider, kclient *client.Client) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
// Ignore ConfigMaps we don't care about
if o.GetName() == configMapName.Name && o.GetNamespace() == configMapName.Namespace {
err := cp.ReloadConfigMap(context.TODO(), *kclient, configMapName)
if err != nil {
configLog.Error(err, "Unable to reload user configuration")
}
return f()
}
return []reconcile.Request{}
})
}
func (cp *ConfigProvider) IsReloading() bool {
return cp.isReloading
}
func (cp *ConfigProvider) AwaitReload() {
cp.c.L.Lock()
defer cp.c.L.Unlock()
if cp.isReloading {
cp.c.Wait()
}
}
type ImageConfig struct {
Name string
Tag string
Digest string
Command []string
}
func (m ImageConfig) TaggedImage() string {
// Use only the digest if it is provided and ignore the tag.
// It is possible to reference an image by both tag and digest, but this is considered ambiguous and the digest would be used to pull the image anyway.
// See also https://github.com/cri-o/cri-o/pull/3060
if m.Digest != "" {
return fmt.Sprintf("%s@%s", m.Name, m.Digest)
} else if m.Tag != "" {
return fmt.Sprintf("%s:%s", m.Name, m.Tag)
}
return m.Name
}
type ResourceRequirements struct {
Requests ResourceQuantities
Limits ResourceQuantities
// used to cache the parsed resources from parseAndValidate()
parsedKubeResourceRequirements *corev1.ResourceRequirements
}
type ResourceQuantities struct {
CPU string
Memory string
}
func (rr ResourceRequirements) ToKubernetesType() *corev1.ResourceRequirements {
// assert that parseAndValidate has already been called
if rr.parsedKubeResourceRequirements == nil {
panic("ResourceRequirements: Must call parseAndValidate() before ToKubernetesType()")
}
return rr.parsedKubeResourceRequirements
}
func (rr *ResourceRequirements) parseAndValidate() error {
var err error
var limitsCPU, requestsCPU, limitsMemory, requestsMemory resource.Quantity
if limitsCPU, err = resource.ParseQuantity(rr.Limits.CPU); err != nil {
return fmt.Errorf("Cannot parse 'Limits.CPU' as a quantity: %w", err)
}
if requestsCPU, err = resource.ParseQuantity(rr.Requests.CPU); err != nil {
return fmt.Errorf("Cannot parse 'Requests.CPU' a quantity: %w", err)
}
if limitsMemory, err = resource.ParseQuantity(rr.Limits.Memory); err != nil {
return fmt.Errorf("Cannot parse 'Limits.Memory' as a quantity: %w", err)
}
if requestsMemory, err = resource.ParseQuantity(rr.Requests.Memory); err != nil {
return fmt.Errorf("Cannot parse 'Requests.Memory' a quantity: %w", err)
}
rr.parsedKubeResourceRequirements = &corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: limitsCPU,
corev1.ResourceMemory: limitsMemory,
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: requestsCPU,
corev1.ResourceMemory: requestsMemory,
},
}
return nil
}
// Sets the defaults prior to config file parsing
// Note: see also config/default/defaults.yaml for the shipped default config
func defaults(v *viper.Viper) {
v.SetDefault("InferenceServiceName", "modelmesh-serving")
v.SetDefault("InferenceServicePort", 8033)
v.SetDefault("PodsPerRuntime", 2)
v.SetDefault("StorageSecretName", "storage-config")
v.SetDefault("ServiceAccountName", "")
v.SetDefault("PayloadProcessors", []string{})
v.SetDefault(concatStringsWithDelimiter([]string{"Metrics", "Port"}), 2112)
v.SetDefault(concatStringsWithDelimiter([]string{"Metrics", "Scheme"}), "https")
v.SetDefault(concatStringsWithDelimiter([]string{"ScaleToZero", "Enabled"}), true)
v.SetDefault(concatStringsWithDelimiter([]string{"ScaleToZero", "GracePeriodSeconds"}), 60)
// default size 16MiB in bytes
v.SetDefault("GrpcMaxMessageSizeBytes", 16777216)
v.SetDefault("BuiltInServerTypes", []string{
string(kserveapi.MLServer), string(kserveapi.Triton), string(kserveapi.OVMS), "torchserve",
})
}
func concatStringsWithDelimiter(elems []string) string {
return strings.Join(elems, ViperKeyDelimiter)
}
func init() {
defaultConfig = viper.NewWithOptions(viper.KeyDelimiter(ViperKeyDelimiter))
defaults(defaultConfig)
defaultConfig.SetConfigName("config-defaults")
defaultConfig.SetConfigType(ConfigType)
defaultConfig.AddConfigPath(MountLocation)
//For dev env and tests, must get the config filename using cwd relative path
if _, filename, _, ok := runtime.Caller(0); !ok {
panic("Unable to get the caller")
} else {
filepath := path.Join(path.Dir(filename), "../../config/default")
defaultConfig.AddConfigPath(filepath)
}
if err := defaultConfig.ReadInConfig(); err != nil {
configLog.Error(err, "Unable to read the default configuration", "path", MountLocation)
}
}
func NewMergedConfigFromConfigMap(m corev1.ConfigMap) (*Config, error) {
configYaml, ok := m.Data["config.yaml"]
if !ok {
return nil, fmt.Errorf("User ConfigMap must contain a key named config.yaml")
}
return NewMergedConfigFromString(configYaml)
}
func NewMergedConfigFromString(configYaml string) (*Config, error) {
var err error
v := viper.NewWithOptions(viper.KeyDelimiter(ViperKeyDelimiter))
v.SetConfigType(ConfigType)
for _, key := range defaultConfig.AllKeys() {
v.SetDefault(key, defaultConfig.Get(key))
}
configYamlReader := strings.NewReader(configYaml)
if err = v.ReadConfig(configYamlReader); err != nil {
return nil, err
}
// Even if the default config has an image digest, a user should be able to
// override it with a tag (ignoring the default digest)
// HACK: There should be a better way to do this...
clearDigestIfTagsDiffer(v, "modelMeshImage")
clearDigestIfTagsDiffer(v, "storageHelperImage")
clearDigestIfTagsDiffer(v, "restProxy.image")
// unmarshal the config into a Config struct
var config Config
if err = v.Unmarshal(&config); err != nil {
return nil, err
}
configLog.Info("Updated model serving config", "mergedConfig", config)
// extra validations on parsed config
if err = config.ModelMeshResources.parseAndValidate(); err != nil {
return nil, fmt.Errorf("Invalid config for 'ModelMeshResources': %s", err)
}
if err = config.RESTProxy.Resources.parseAndValidate(); err != nil {
return nil, fmt.Errorf("Invalid config for 'RESTProxy.Resources': %s", err)
}
if err = config.StorageHelperResources.parseAndValidate(); err != nil {
return nil, fmt.Errorf("Invalid config for 'StorageHelperResources': %s", err)
}
// check that none of the payload processors contains a space
for _, processor := range config.PayloadProcessors {
if strings.Contains(processor, " ") {
return nil, fmt.Errorf("Error parsing payload processor '%s': endpoint must not contain spaces.", processor)
}
}
return &config, nil
}
func clearDigestIfTagsDiffer(v *viper.Viper, imageConfigField string) {
tag, digest := imageConfigField+".tag", imageConfigField+".digest"
if v.GetString(tag) != defaultConfig.GetString(tag) && v.GetString(digest) == defaultConfig.GetString(digest) {
v.Set(digest, "")
}
}