-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoverrides.go
More file actions
664 lines (568 loc) · 21.4 KB
/
overrides.go
File metadata and controls
664 lines (568 loc) · 21.4 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
//
// 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 component
import (
"fmt"
"reflect"
"strconv"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
corev1 "k8s.io/api/core/v1"
"github.com/NVIDIA/aicr/pkg/errors"
)
// titleCaser is cached to avoid per-call allocation.
var titleCaser = cases.Title(language.English)
// String constants for override values.
const (
strVersion = "version"
strDriver = "driver"
strEnabled = "enabled"
)
// ApplyValueOverrides applies overrides to a struct using reflection.
// Supports dot-notation paths (e.g., "gds.enabled", "driver.version").
// Automatically handles type conversion for strings, bools, ints, and nested structs.
// Returns an error containing all failed overrides instead of stopping at the first failure.
func ApplyValueOverrides(target any, overrides map[string]string) error {
if len(overrides) == 0 {
return nil
}
targetValue := reflect.ValueOf(target)
if targetValue.Kind() != reflect.Ptr {
return errors.New(errors.ErrCodeInvalidRequest, "target must be a pointer to a struct")
}
targetValue = targetValue.Elem()
if targetValue.Kind() != reflect.Struct {
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("target must be a pointer to a struct, got %s", targetValue.Kind()))
}
// Collect all errors instead of failing on first error
var errs []string
for path, value := range overrides {
if err := setFieldByPath(targetValue, path, value); err != nil {
errs = append(errs, fmt.Sprintf("%s=%s: %v", path, value, err))
}
}
if len(errs) > 0 {
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("failed to apply overrides: %s", strings.Join(errs, "; ")))
}
return nil
}
// ApplyMapOverrides applies overrides to a map[string]any using dot-notation paths.
// Handles nested maps by traversing the path segments and creating nested maps as needed.
// Useful for applying --set flag overrides to values.yaml content.
func ApplyMapOverrides(target map[string]any, overrides map[string]string) error {
if target == nil {
return errors.New(errors.ErrCodeInvalidRequest, "target map cannot be nil")
}
if len(overrides) == 0 {
return nil
}
var errs []string
for path, value := range overrides {
if err := setMapValueByPath(target, path, value); err != nil {
errs = append(errs, fmt.Sprintf("%s=%s: %v", path, value, err))
}
}
if len(errs) > 0 {
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("failed to apply map overrides: %s", strings.Join(errs, "; ")))
}
return nil
}
// getOrCreateNestedMap traverses a dot-separated path in a nested map,
// creating intermediate maps as needed, and returns the parent map
// and the final key. When strict is true, returns an error if an
// intermediate path segment exists but is not a map. When strict is
// false, non-map values are silently replaced with new maps.
func getOrCreateNestedMap(m map[string]any, path string, strict bool) (map[string]any, string, error) {
parts := strings.Split(path, ".")
current := m
for _, part := range parts[:len(parts)-1] {
if next, ok := current[part]; ok {
if nextMap, ok := next.(map[string]any); ok {
current = nextMap
} else if strict {
return nil, "", errors.New(errors.ErrCodeInvalidRequest,
fmt.Sprintf("path segment %q exists but is not a map (type: %T)", part, next))
} else {
newMap := make(map[string]any)
current[part] = newMap
current = newMap
}
} else {
newMap := make(map[string]any)
current[part] = newMap
current = newMap
}
}
return current, parts[len(parts)-1], nil
}
// setMapValueByPath sets a value in a nested map using dot-notation path.
// Creates nested maps as needed. Converts string values to bools when appropriate.
func setMapValueByPath(target map[string]any, path, value string) error {
parent, key, err := getOrCreateNestedMap(target, path, true)
if err != nil {
return errors.Wrap(errors.ErrCodeInvalidRequest, "failed to resolve override path", err)
}
parent[key] = convertMapValue(value)
return nil
}
// convertMapValue converts a string value to an appropriate Go type.
// Handles bools ("true"/"false") and numbers.
func convertMapValue(value string) any {
// Try bool conversion
if value == StrTrue {
return true
}
if value == StrFalse {
return false
}
// Try integer conversion
if i, err := strconv.ParseInt(value, 10, 64); err == nil {
return i
}
// Try float conversion
if f, err := strconv.ParseFloat(value, 64); err == nil {
return f
}
// Return as string
return value
}
// setFieldByPath sets a field value using dot-notation path.
// Supports both flat fields (EnableGDS for "gds.enabled") and nested structs (Driver.Version for "driver.version").
func setFieldByPath(structValue reflect.Value, path, value string) error {
parts := strings.Split(path, ".")
// Special case: Handle multi-segment paths (e.g., "manager.resources.cpu.limit" -> "ManagerCPULimit")
if len(parts) == 4 {
flatFieldName := deriveMultiSegmentFieldName(parts)
if flatFieldName != "" {
if flatField, found := findField(structValue, flatFieldName, path); found {
return setFieldValue(flatField, value)
}
}
}
// Special case: Try to find a flat field that matches the full path pattern first
// This handles cases like "gds.enabled" -> "EnableGDS", "mig.strategy" -> "MIGStrategy"
if len(parts) == 2 {
flatFieldName := deriveFlatFieldName(parts[0], parts[1])
if flatField, found := findField(structValue, flatFieldName, path); found {
return setFieldValue(flatField, value)
}
}
// Standard traversal for nested structs
currentValue := structValue
// Traverse to the target field
for i, part := range parts {
isLast := i == len(parts)-1
// Convert path segment to field name (e.g., "gds" -> "GDS", "enabled" -> "Enabled")
fieldName := pathToFieldName(part)
// Find field (case-insensitive search)
field, found := findField(currentValue, fieldName, part)
if !found {
return errors.New(errors.ErrCodeNotFound, fmt.Sprintf("field not found: %s (searching for %s in %s)", path, part, currentValue.Type()))
}
if !field.IsValid() {
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("invalid field: %s", path))
}
if !field.CanSet() {
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("cannot set field: %s (field is not settable)", path))
}
if isLast {
// Set the final field value
return setFieldValue(field, value)
}
// Navigate to nested struct
switch field.Kind() {
case reflect.Struct:
currentValue = field
case reflect.Ptr:
// Handle pointer to struct
if field.IsNil() {
// Create new struct instance
field.Set(reflect.New(field.Type().Elem()))
}
currentValue = field.Elem()
case reflect.Invalid, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128,
reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
reflect.Slice, reflect.String, reflect.UnsafePointer:
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("cannot traverse non-struct field: %s (type: %s)", part, field.Type()))
}
}
return nil
}
// deriveMultiSegmentFieldName handles paths with 4 segments.
// Examples:
// - ["manager", "resources", "cpu", "limit"] -> "ManagerCPULimit"
// - ["manager", "resources", "memory", "limit"] -> "ManagerMemoryLimit"
// - ["manager", "resources", "cpu", "request"] -> "ManagerCPURequest"
// - ["manager", "resources", "memory", "request"] -> "ManagerMemoryRequest"
func deriveMultiSegmentFieldName(parts []string) string {
if len(parts) != 4 {
return ""
}
// Handle Skyhook manager resource paths: manager.resources.{cpu|memory}.{limit|request}
if strings.ToLower(parts[0]) == "manager" && strings.ToLower(parts[1]) == "resources" {
resourceType := strings.ToUpper(parts[2]) // "cpu" -> "CPU", "memory" -> "MEMORY"
if strings.ToLower(parts[2]) == "memory" {
resourceType = "Memory"
}
actionType := pathToFieldName(parts[3]) // "limit" -> "Limit", "request" -> "Request"
return "Manager" + resourceType + actionType
}
return ""
}
// deriveFlatFieldName creates a flat field name from a dotted path.
// Examples:
// - ("gds", "enabled") -> "EnableGDS"
// - ("mig", "strategy") -> "MIGStrategy"
// - ("driver", "version") -> "DriverVersion"
// - ("operator", "version") -> "GPUOperatorVersion"
// - ("toolkit", "version") -> "NvidiaContainerToolkitVersion"
// - ("driver", "repository") -> "DriverRegistry"
// - ("sandboxWorkloads", "enabled") -> "EnableSecureBoot"
// - ("driver", "useOpenKernelModules") -> "UseOpenKernelModule"
func deriveFlatFieldName(prefix, suffix string) string {
prefixTitle := pathToFieldName(prefix)
suffixTitle := pathToFieldName(suffix)
// Handle special mappings for common patterns
prefixLower := strings.ToLower(prefix)
suffixLower := strings.ToLower(suffix)
// Special case mappings based on prefix and suffix combinations
switch {
// operator.version -> GPUOperatorVersion
case prefixLower == "operator" && suffixLower == strVersion:
return "GPUOperatorVersion"
// toolkit.version -> NvidiaContainerToolkitVersion
case prefixLower == "toolkit" && suffixLower == strVersion:
return "NvidiaContainerToolkitVersion"
// driver.repository -> DriverRegistry
case prefixLower == strDriver && suffixLower == "repository":
return "DriverRegistry"
// driver.registry -> DriverRegistry (Network Operator)
case prefixLower == strDriver && suffixLower == "registry":
return "DriverRegistry"
// ofed.version -> OFEDVersion
case prefixLower == "ofed" && suffixLower == strVersion:
return "OFEDVersion"
// ofed.deploy -> DeployOFED
case prefixLower == "ofed" && suffixLower == "deploy":
return "DeployOFED"
// nic.type -> NicType
case prefixLower == "nic" && suffixLower == "type":
return "NicType"
// containerRuntime.socket -> ContainerRuntimeSocket
case prefixLower == "containerruntime" && suffixLower == "socket":
return "ContainerRuntimeSocket"
// hostDevice.enabled -> EnableHostDevice
case prefixLower == "hostdevice" && suffixLower == strEnabled:
return "EnableHostDevice"
// operator.registry -> OperatorRegistry (Skyhook)
case prefixLower == "operator" && suffixLower == "registry":
return "OperatorRegistry"
// kubeRbacProxy.version -> KubeRbacProxyVersion
case prefixLower == "kuberbacproxy" && suffixLower == strVersion:
return "KubeRbacProxyVersion"
// agent.image -> SkyhookAgentImage
case prefixLower == "agent" && suffixLower == "image":
return "SkyhookAgentImage"
}
// Special case: tolerations.key -> TolerationKey
if prefixLower == "tolerations" && suffixLower == "key" {
return "TolerationKey"
}
// Special case: tolerations.value -> TolerationValue
if prefixLower == "tolerations" && suffixLower == "value" {
return "TolerationValue"
}
// Special case: sandboxWorkloads.enabled -> EnableSecureBoot
if prefixLower == "sandboxworkloads" && suffixLower == strEnabled {
return "EnableSecureBoot"
}
// Special case: driver.useOpenKernelModules -> UseOpenKernelModule (singular)
if prefixLower == strDriver && (suffixLower == "useopenkernelmodules" || suffixLower == "useopenkernelmodule") {
return "UseOpenKernelModule"
}
// Handle "enabled" suffix specially - often becomes "Enable<Prefix>"
if suffixLower == strEnabled {
return "Enable" + prefixTitle
}
// Otherwise concatenate: Driver + Version = DriverVersion
return prefixTitle + suffixTitle
}
// findField searches for a field by name (case-insensitive) or by matching the original path segment.
func findField(structValue reflect.Value, fieldName, pathSegment string) (reflect.Value, bool) {
structType := structValue.Type()
// Try exact match first
field := structValue.FieldByName(fieldName)
if field.IsValid() {
return field, true
}
// Try case-insensitive search
for i := 0; i < structValue.NumField(); i++ {
f := structType.Field(i)
if strings.EqualFold(f.Name, fieldName) || strings.EqualFold(f.Name, pathSegment) {
return structValue.Field(i), true
}
// Also try matching common patterns (e.g., "EnableGDS" for "gds.enabled")
if matchesPattern(f.Name, pathSegment) {
return structValue.Field(i), true
}
}
return reflect.Value{}, false
}
// matchesPattern checks if a field name matches a path segment using common patterns.
// Examples:
// - "EnableGDS" matches "gds" (Enable + acronym pattern)
// - "DriverVersion" matches both "driver" and "version"
// - "MIGStrategy" matches both "mig" and "strategy"
// - "GPUOperatorVersion" matches "operator" or "gpu-operator"
func matchesPattern(fieldName, pathSegment string) bool {
fieldLower := strings.ToLower(fieldName)
segmentLower := strings.ToLower(pathSegment)
// Check if field name contains the segment
if strings.Contains(fieldLower, segmentLower) {
return true
}
// Check for "Enable" prefix pattern (EnableGDS matches "gds")
if strings.HasPrefix(fieldLower, "enable") {
withoutEnable := strings.TrimPrefix(fieldLower, "enable")
if withoutEnable == segmentLower {
return true
}
}
// Check for compound words (DriverVersion matches "driver", GPUOperatorVersion matches "operator")
// Look for the segment at the start of the field name
if strings.HasPrefix(fieldLower, segmentLower) {
return true
}
// Handle dash-separated segments (gpu-operator matches GPUOperator)
if strings.Contains(segmentLower, "-") {
dashless := strings.ReplaceAll(segmentLower, "-", "")
if strings.Contains(fieldLower, dashless) {
return true
}
}
return false
}
// pathToFieldName converts a path segment to a potential field name.
// Examples:
// - "gds" -> "GDS"
// - "enabled" -> "Enabled"
// - "mig_strategy" -> "MIGStrategy"
func pathToFieldName(segment string) string {
// Handle common acronyms that should stay uppercase
acronyms := map[string]string{
"gds": "GDS",
"gpu": "GPU",
"mig": "MIG",
"dcgm": "DCGM",
"cpu": "CPU",
"api": "API",
"cdi": "CDI",
"gdr": "GDR",
"rdma": "RDMA",
"sriov": "SRIOV",
"vfio": "VFIO",
"vgpu": "VGPU",
"ofed": "OFED",
"crds": "CRDs",
"rbac": "RBAC",
"tls": "TLS",
"nfd": "NFD",
"gfd": "GFD",
}
segmentLower := strings.ToLower(segment)
// Check if it's a known acronym
if acronym, found := acronyms[segmentLower]; found {
return acronym
}
// Handle underscore-separated words (e.g., "mig_strategy" -> "MIGStrategy")
if strings.Contains(segment, "_") {
parts := strings.Split(segment, "_")
var result strings.Builder
for _, part := range parts {
if acronym, found := acronyms[strings.ToLower(part)]; found {
result.WriteString(acronym)
} else {
result.WriteString(titleCaser.String(part))
}
}
return result.String()
}
// Handle dash-separated words (e.g., "gpu-operator" -> "GPUOperator")
if strings.Contains(segment, "-") {
parts := strings.Split(segment, "-")
var result strings.Builder
for _, part := range parts {
if acronym, found := acronyms[strings.ToLower(part)]; found {
result.WriteString(acronym)
} else {
result.WriteString(titleCaser.String(part))
}
}
return result.String()
}
// Simple title case
return titleCaser.String(segment)
}
// setFieldValue sets a reflect.Value with automatic type conversion.
func setFieldValue(field reflect.Value, value string) error {
fieldType := field.Type()
switch fieldType.Kind() {
case reflect.String:
field.SetString(value)
return nil
case reflect.Bool:
boolVal, err := parseBool(value)
if err != nil {
return errors.Wrap(errors.ErrCodeInvalidRequest, fmt.Sprintf("invalid boolean value %q", value), err)
}
field.SetBool(boolVal)
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intVal, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return errors.Wrap(errors.ErrCodeInvalidRequest, fmt.Sprintf("invalid integer value %q", value), err)
}
field.SetInt(intVal)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uintVal, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return errors.Wrap(errors.ErrCodeInvalidRequest, fmt.Sprintf("invalid unsigned integer value %q", value), err)
}
field.SetUint(uintVal)
return nil
case reflect.Float32, reflect.Float64:
floatVal, err := strconv.ParseFloat(value, 64)
if err != nil {
return errors.Wrap(errors.ErrCodeInvalidRequest, fmt.Sprintf("invalid float value %q", value), err)
}
field.SetFloat(floatVal)
return nil
case reflect.Invalid, reflect.Uintptr, reflect.Complex64, reflect.Complex128,
reflect.Array, reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.Struct, reflect.UnsafePointer:
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("unsupported field type: %s", fieldType))
}
return errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("unsupported field type: %s", fieldType))
}
// parseBool parses boolean values with support for various formats.
func parseBool(value string) (bool, error) {
switch strings.ToLower(value) {
case StrTrue, "yes", "1", "on", strEnabled:
return true, nil
case StrFalse, "no", "0", "off", "disabled":
return false, nil
default:
return false, errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("cannot parse %q as boolean", value))
}
}
// ApplyNodeSelectorOverrides applies node selector overrides to a values map.
// If nodeSelector is non-empty, it sets or merges with the existing nodeSelector field.
// The function applies to the specified paths in the values map (e.g., "nodeSelector", "webhook.nodeSelector").
func ApplyNodeSelectorOverrides(values map[string]any, nodeSelector map[string]string, paths ...string) {
if len(nodeSelector) == 0 || values == nil {
return
}
// Default to top-level "nodeSelector" if no paths specified
if len(paths) == 0 {
paths = []string{"nodeSelector"}
}
for _, path := range paths {
setNodeSelectorAtPath(values, nodeSelector, path)
}
}
// setNodeSelectorAtPath sets the node selector at the specified dot-notation path.
func setNodeSelectorAtPath(values map[string]any, nodeSelector map[string]string, path string) {
parent, key, _ := getOrCreateNestedMap(values, path, false)
// Set the node selector - convert map[string]string to map[string]any
nsMap := make(map[string]any, len(nodeSelector))
for k, v := range nodeSelector {
nsMap[k] = v
}
parent[key] = nsMap
}
// ApplyTolerationsOverrides applies toleration overrides to a values map.
// If tolerations is non-empty, it sets or replaces the existing tolerations field.
// The function applies to the specified paths in the values map (e.g., "tolerations", "webhook.tolerations").
func ApplyTolerationsOverrides(values map[string]any, tolerations []corev1.Toleration, paths ...string) {
if len(tolerations) == 0 || values == nil {
return
}
// Default to top-level "tolerations" if no paths specified
if len(paths) == 0 {
paths = []string{"tolerations"}
}
// Convert tolerations to YAML-friendly format
tolList := TolerationsToPodSpec(tolerations)
for _, path := range paths {
setTolerationsAtPath(values, tolList, path)
}
}
// setTolerationsAtPath sets the tolerations at the specified dot-notation path.
func setTolerationsAtPath(values map[string]any, tolerations []map[string]any, path string) {
parent, key, _ := getOrCreateNestedMap(values, path, false)
// Convert to []any for proper YAML serialization
tolInterface := make([]any, len(tolerations))
for i, t := range tolerations {
tolInterface[i] = t
}
parent[key] = tolInterface
}
// TolerationsToPodSpec converts a slice of corev1.Toleration to a YAML-friendly format.
// This format matches what Kubernetes expects in pod specs and Helm values.
func TolerationsToPodSpec(tolerations []corev1.Toleration) []map[string]any {
result := make([]map[string]any, 0, len(tolerations))
for _, t := range tolerations {
tolMap := make(map[string]any)
// Only include non-empty fields to keep YAML clean
if t.Key != "" {
tolMap["key"] = t.Key
}
if t.Operator != "" {
tolMap["operator"] = string(t.Operator)
}
if t.Value != "" {
tolMap["value"] = t.Value
}
if t.Effect != "" {
tolMap["effect"] = string(t.Effect)
}
if t.TolerationSeconds != nil {
tolMap["tolerationSeconds"] = *t.TolerationSeconds
}
result = append(result, tolMap)
}
return result
}
// nodeSelectorToMatchExpressions converts a map of node selectors to matchExpressions format.
// This format is used by some CRDs like Skyhook that use label selector syntax.
// Each key=value pair becomes a matchExpression with operator "In" and single value.
func nodeSelectorToMatchExpressions(nodeSelector map[string]string) []map[string]any {
if len(nodeSelector) == 0 {
return nil
}
result := make([]map[string]any, 0, len(nodeSelector))
for key, value := range nodeSelector {
expr := map[string]any{
"key": key,
"operator": "In",
"values": []string{value},
}
result = append(result, expr)
}
return result
}