-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcomponents.go
More file actions
389 lines (326 loc) · 13.3 KB
/
components.go
File metadata and controls
389 lines (326 loc) · 13.3 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
// 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 recipe
import (
"fmt"
"slices"
"sync"
"github.com/NVIDIA/aicr/pkg/errors"
"gopkg.in/yaml.v3"
)
// ComponentRegistry holds the declarative configuration for all components.
// This is loaded from embedded recipe data (recipes/registry.yaml) at startup.
type ComponentRegistry struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Components []ComponentConfig `yaml:"components"`
// Index for fast lookup by name (populated after loading)
byName map[string]*ComponentConfig
}
// ComponentConfig defines the bundler configuration for a component.
// This replaces the per-component Go packages with declarative YAML.
type ComponentConfig struct {
// Name is the component identifier used in recipes (e.g., "gpu-operator").
Name string `yaml:"name"`
// DisplayName is the human-readable name used in templates and output.
DisplayName string `yaml:"displayName"`
// ValueOverrideKeys are alternative keys for --set flag matching.
// Example: ["gpuoperator"] allows --set gpuoperator:key=value
ValueOverrideKeys []string `yaml:"valueOverrideKeys,omitempty"`
// Helm contains default Helm chart settings.
Helm HelmConfig `yaml:"helm,omitempty"`
// Kustomize contains default Kustomize settings.
Kustomize KustomizeConfig `yaml:"kustomize,omitempty"`
// NodeScheduling defines paths for injecting node selectors and tolerations.
NodeScheduling NodeSchedulingConfig `yaml:"nodeScheduling,omitempty"`
PodScheduling PodSchedulingConfig `yaml:"podScheduling,omitempty"`
// Validations defines component-specific validation checks.
Validations []ComponentValidationConfig `yaml:"validations,omitempty"`
// HealthCheck defines custom health check configuration for this component.
HealthCheck HealthCheckConfig `yaml:"healthCheck,omitempty"`
}
// HealthCheckConfig defines custom health check settings for a component.
type HealthCheckConfig struct {
// AssertFile is the path to a Chainsaw-style assert YAML file (relative to data directory).
// When set, the expected-resources check uses Chainsaw CLI to evaluate assertions
// instead of the default auto-discovery + typed replica checks.
AssertFile string `yaml:"assertFile,omitempty"`
}
// HelmConfig contains default Helm chart settings for a component.
type HelmConfig struct {
// DefaultRepository is the default Helm repository URL.
DefaultRepository string `yaml:"defaultRepository,omitempty"`
// DefaultChart is the chart name (e.g., "nvidia/gpu-operator").
DefaultChart string `yaml:"defaultChart,omitempty"`
// DefaultVersion is the default chart version if not specified in recipe.
DefaultVersion string `yaml:"defaultVersion,omitempty"`
// DefaultNamespace is the Kubernetes namespace for deploying this component.
DefaultNamespace string `yaml:"defaultNamespace,omitempty"`
}
// KustomizeConfig contains default Kustomize settings for a component.
type KustomizeConfig struct {
// DefaultSource is the default Git repository or OCI reference.
DefaultSource string `yaml:"defaultSource,omitempty"`
// DefaultPath is the path within the repository to the kustomization.
DefaultPath string `yaml:"defaultPath,omitempty"`
// DefaultTag is the default Git tag, branch, or commit.
DefaultTag string `yaml:"defaultTag,omitempty"`
}
// NodeSchedulingConfig defines paths for node scheduling injection.
type NodeSchedulingConfig struct {
// System defines paths for system component scheduling.
System SchedulingPaths `yaml:"system,omitempty"`
// Accelerated defines paths for GPU/accelerated node scheduling.
Accelerated SchedulingPaths `yaml:"accelerated,omitempty"`
// NodeCountPaths are Helm value paths where the bundle-time node count is injected (e.g. estimatedNodeCount for skyhook-operator).
NodeCountPaths []string `yaml:"nodeCountPaths,omitempty"`
}
// SchedulingPaths holds the Helm value paths for node scheduling.
type SchedulingPaths struct {
// NodeSelectorPaths are paths where node selectors are injected.
NodeSelectorPaths []string `yaml:"nodeSelectorPaths,omitempty"`
// TolerationPaths are paths where tolerations are injected.
TolerationPaths []string `yaml:"tolerationPaths,omitempty"`
// TaintPaths are paths where taints are injected as structured objects.
// Intended to be used instea of TaintStrPaths for components that need to set specific parts of taints
// and can't process the string format.
TaintPaths []string `yaml:"taintPaths,omitempty"`
// TaintStrPaths are paths where taints are injected as strings (format: key=value:effect or key:effect).
TaintStrPaths []string `yaml:"taintStrPaths,omitempty"`
}
// PodSchedulingConfig defines paths for pod scheduling injection.
type PodSchedulingConfig struct {
// Workload defines paths for workload pod scheduling.
Workload WorkloadSchedulingPaths `yaml:"workload,omitempty"`
}
// WorkloadSchedulingPaths holds the Helm value paths for workload scheduling.
type WorkloadSchedulingPaths struct {
// WorkloadSelectorPaths are paths where workload selectors are injected.
WorkloadSelectorPaths []string `yaml:"workloadSelectorPaths,omitempty"`
}
// ComponentValidationConfig defines a component-specific validation check.
type ComponentValidationConfig struct {
// Function is the name of the validation function to execute (e.g., "CheckWorkloadSelectorMissing").
Function string `yaml:"function"`
// Severity determines whether failures are warnings or errors ("warning" or "error").
Severity string `yaml:"severity"`
// Conditions are optional conditions that must be met for the validation to run.
// Values are arrays of strings for OR matching (single element arrays are equivalent to single values).
// Example: {"intent": ["training"]} or {"intent": ["training", "inference"]}
Conditions map[string][]string `yaml:"conditions,omitempty"`
// Message is an optional detail message to append to validation failures/warnings.
Message string `yaml:"message,omitempty"`
}
// Global component registry (loaded once, thread-safe access)
var (
globalRegistry *ComponentRegistry
globalRegistryOnce sync.Once
globalRegistryErr error
)
// GetComponentRegistry returns the global component registry.
// The registry is loaded once from embedded data and cached.
// Returns an error if the registry file cannot be loaded or parsed.
func GetComponentRegistry() (*ComponentRegistry, error) {
globalRegistryOnce.Do(func() {
globalRegistry, globalRegistryErr = loadComponentRegistry()
})
return globalRegistry, globalRegistryErr
}
// ResetComponentRegistryForTesting resets the singleton registry so it will be
// reloaded from the current DataProvider on the next call to GetComponentRegistry.
// This must only be called from tests.
func ResetComponentRegistryForTesting() {
globalRegistry = nil
globalRegistryErr = nil
globalRegistryOnce = sync.Once{}
}
// loadComponentRegistry loads the component registry from the data provider.
func loadComponentRegistry() (*ComponentRegistry, error) {
provider := GetDataProvider()
data, err := provider.ReadFile("registry.yaml")
if err != nil {
return nil, errors.Wrap(errors.ErrCodeInternal, "failed to read registry.yaml", err)
}
var registry ComponentRegistry
if err := yaml.Unmarshal(data, ®istry); err != nil {
return nil, errors.Wrap(errors.ErrCodeInternal, "failed to parse registry.yaml", err)
}
// Build index for fast lookup
registry.byName = make(map[string]*ComponentConfig, len(registry.Components))
for i := range registry.Components {
comp := ®istry.Components[i]
registry.byName[comp.Name] = comp
}
return ®istry, nil
}
// Get returns the component configuration by name.
// Returns nil if the component is not found.
func (r *ComponentRegistry) Get(name string) *ComponentConfig {
if r == nil || r.byName == nil {
return nil
}
return r.byName[name]
}
// GetByOverrideKey returns the component configuration by value override key.
// This is used for matching --set flags like --set gpuoperator:key=value.
// Returns nil if no component matches the key.
func (r *ComponentRegistry) GetByOverrideKey(key string) *ComponentConfig {
if r == nil {
return nil
}
for i := range r.Components {
comp := &r.Components[i]
// Check the component name first
if comp.Name == key {
return comp
}
// Check alternative override keys
if slices.Contains(comp.ValueOverrideKeys, key) {
return comp
}
}
return nil
}
// Names returns all component names in the registry.
func (r *ComponentRegistry) Names() []string {
if r == nil {
return nil
}
names := make([]string, len(r.Components))
for i, comp := range r.Components {
names[i] = comp.Name
}
return names
}
// Count returns the number of components in the registry.
func (r *ComponentRegistry) Count() int {
if r == nil {
return 0
}
return len(r.Components)
}
// Validate checks the component registry for errors.
// Returns a slice of validation errors (empty if valid).
func (r *ComponentRegistry) Validate() []error {
if r == nil {
return []error{errors.New(errors.ErrCodeInvalidRequest, "registry is nil")}
}
var errs []error
// Check for required fields
for i, comp := range r.Components {
if comp.Name == "" {
errs = append(errs, errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("component[%d]: name is required", i)))
}
if comp.DisplayName == "" {
errs = append(errs, errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("component[%d] (%s): displayName is required", i, comp.Name)))
}
}
// Check for duplicate names
seen := make(map[string]bool)
for _, comp := range r.Components {
if comp.Name != "" {
if seen[comp.Name] {
errs = append(errs, errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("duplicate component name: %s", comp.Name)))
}
seen[comp.Name] = true
}
}
// Check for duplicate override keys
overrideKeys := make(map[string]string) // key -> component name
for _, comp := range r.Components {
for _, key := range comp.ValueOverrideKeys {
if existing, ok := overrideKeys[key]; ok {
errs = append(errs, errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("duplicate valueOverrideKey %q: used by both %s and %s", key, existing, comp.Name)))
}
overrideKeys[key] = comp.Name
}
}
// Check for mutually exclusive helm/kustomize configuration
for i, comp := range r.Components {
hasHelm := comp.Helm.DefaultRepository != "" || comp.Helm.DefaultChart != ""
hasKustomize := comp.Kustomize.DefaultSource != ""
if hasHelm && hasKustomize {
errs = append(errs, errors.New(errors.ErrCodeInvalidRequest, fmt.Sprintf("component[%d] (%s): cannot have both helm and kustomize configuration", i, comp.Name)))
}
}
return errs
}
// GetSystemNodeSelectorPaths returns all system node selector paths for a component.
func (c *ComponentConfig) GetSystemNodeSelectorPaths() []string {
if c == nil {
return nil
}
return c.NodeScheduling.System.NodeSelectorPaths
}
// GetSystemTolerationPaths returns all system toleration paths for a component.
func (c *ComponentConfig) GetSystemTolerationPaths() []string {
if c == nil {
return nil
}
return c.NodeScheduling.System.TolerationPaths
}
// GetAcceleratedNodeSelectorPaths returns all accelerated node selector paths for a component.
func (c *ComponentConfig) GetAcceleratedNodeSelectorPaths() []string {
if c == nil {
return nil
}
return c.NodeScheduling.Accelerated.NodeSelectorPaths
}
// GetAcceleratedTolerationPaths returns all accelerated toleration paths for a component.
func (c *ComponentConfig) GetAcceleratedTolerationPaths() []string {
if c == nil {
return nil
}
return c.NodeScheduling.Accelerated.TolerationPaths
}
// GetWorkloadSelectorPaths returns all workload selector paths for a component.
func (c *ComponentConfig) GetWorkloadSelectorPaths() []string {
if c == nil {
return nil
}
return c.PodScheduling.Workload.WorkloadSelectorPaths
}
// GetAcceleratedTaintStrPaths returns all accelerated taint string paths for a component.
func (c *ComponentConfig) GetAcceleratedTaintStrPaths() []string {
if c == nil {
return nil
}
return c.NodeScheduling.Accelerated.TaintStrPaths
}
// GetNodeCountPaths returns Helm value paths where the node count is injected.
func (c *ComponentConfig) GetNodeCountPaths() []string {
if c == nil {
return nil
}
return c.NodeScheduling.NodeCountPaths
}
// GetValidations returns all validation configurations for a component.
func (c *ComponentConfig) GetValidations() []ComponentValidationConfig {
if c == nil {
return nil
}
return c.Validations
}
// GetType returns the component deployment type based on which config is present.
// Returns ComponentTypeKustomize if Kustomize.DefaultSource is set,
// otherwise returns ComponentTypeHelm (the default).
func (c *ComponentConfig) GetType() ComponentType {
if c == nil {
return ComponentTypeHelm
}
if c.Kustomize.DefaultSource != "" {
return ComponentTypeKustomize
}
return ComponentTypeHelm
}