|
| 1 | +// Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package evidence |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "embed" |
| 20 | + "io/fs" |
| 21 | + "log/slog" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/NVIDIA/aicr/pkg/errors" |
| 27 | +) |
| 28 | + |
| 29 | +//go:embed scripts/collect-evidence.sh |
| 30 | +var collectScript []byte |
| 31 | + |
| 32 | +//go:embed scripts/manifests |
| 33 | +var manifestsFS embed.FS |
| 34 | + |
| 35 | +// ValidFeatures lists all supported evidence collection features. |
| 36 | +var ValidFeatures = []string{ |
| 37 | + "dra", |
| 38 | + "gang", |
| 39 | + "secure", |
| 40 | + "metrics", |
| 41 | + "gateway", |
| 42 | + "operator", |
| 43 | + "hpa", |
| 44 | + "cluster-autoscaling", |
| 45 | +} |
| 46 | + |
| 47 | +// FeatureDescriptions maps feature names to human-readable descriptions. |
| 48 | +var FeatureDescriptions = map[string]string{ |
| 49 | + "dra": "DRA GPU allocation test", |
| 50 | + "gang": "Gang scheduling co-scheduling test", |
| 51 | + "secure": "Secure accelerator access verification", |
| 52 | + "metrics": "Accelerator & AI service metrics", |
| 53 | + "gateway": "Inference API gateway conditions", |
| 54 | + "operator": "Robust AI operator + webhook test", |
| 55 | + "hpa": "HPA pod autoscaling (scale-up + scale-down)", |
| 56 | + "cluster-autoscaling": "Cluster autoscaling (ASG configuration)", |
| 57 | +} |
| 58 | + |
| 59 | +// CollectorOption configures the Collector. |
| 60 | +type CollectorOption func(*Collector) |
| 61 | + |
| 62 | +// Collector orchestrates behavioral evidence collection by invoking the |
| 63 | +// embedded collect-evidence.sh script against a live Kubernetes cluster. |
| 64 | +type Collector struct { |
| 65 | + outputDir string |
| 66 | + features []string |
| 67 | + noCleanup bool |
| 68 | +} |
| 69 | + |
| 70 | +// NewCollector creates a new evidence Collector. |
| 71 | +func NewCollector(outputDir string, opts ...CollectorOption) *Collector { |
| 72 | + c := &Collector{ |
| 73 | + outputDir: outputDir, |
| 74 | + } |
| 75 | + for _, opt := range opts { |
| 76 | + opt(c) |
| 77 | + } |
| 78 | + return c |
| 79 | +} |
| 80 | + |
| 81 | +// WithFeatures sets which features to collect evidence for. |
| 82 | +// If empty, all features are collected. |
| 83 | +func WithFeatures(features []string) CollectorOption { |
| 84 | + return func(c *Collector) { |
| 85 | + c.features = features |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// WithNoCleanup skips test namespace cleanup after collection. |
| 90 | +func WithNoCleanup(noCleanup bool) CollectorOption { |
| 91 | + return func(c *Collector) { |
| 92 | + c.noCleanup = noCleanup |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Run executes evidence collection for the configured features. |
| 97 | +func (c *Collector) Run(ctx context.Context) error { |
| 98 | + // Write embedded script and manifests to temp directory. |
| 99 | + tmpDir, err := os.MkdirTemp("", "aicr-evidence-") |
| 100 | + if err != nil { |
| 101 | + return errors.Wrap(errors.ErrCodeInternal, "failed to create temp directory", err) |
| 102 | + } |
| 103 | + defer os.RemoveAll(tmpDir) |
| 104 | + |
| 105 | + scriptPath := filepath.Join(tmpDir, "collect-evidence.sh") |
| 106 | + if err := os.WriteFile(scriptPath, collectScript, 0o700); err != nil { //nolint:gosec // script needs execute permission |
| 107 | + return errors.Wrap(errors.ErrCodeInternal, "failed to write evidence script", err) |
| 108 | + } |
| 109 | + |
| 110 | + manifestDir := filepath.Join(tmpDir, "manifests") |
| 111 | + if err := writeEmbeddedManifests(manifestDir); err != nil { |
| 112 | + return errors.Wrap(errors.ErrCodeInternal, "failed to write manifests", err) |
| 113 | + } |
| 114 | + |
| 115 | + // Create output directory. |
| 116 | + if err := os.MkdirAll(c.outputDir, 0o755); err != nil { |
| 117 | + return errors.Wrap(errors.ErrCodeInternal, "failed to create output directory", err) |
| 118 | + } |
| 119 | + |
| 120 | + // Determine sections to run. "all" or empty means run everything. |
| 121 | + sections := c.features |
| 122 | + if len(sections) == 0 { |
| 123 | + sections = []string{"all"} |
| 124 | + } |
| 125 | + for _, s := range sections { |
| 126 | + if s == "all" { |
| 127 | + sections = []string{"all"} |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Run each feature. |
| 133 | + var lastErr error |
| 134 | + for _, section := range sections { |
| 135 | + slog.Info("collecting evidence", "feature", section) |
| 136 | + if err := c.runSection(ctx, scriptPath, tmpDir, section); err != nil { |
| 137 | + slog.Warn("evidence collection failed for feature", |
| 138 | + "feature", section, "error", err) |
| 139 | + lastErr = err |
| 140 | + // Continue with remaining features. |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if lastErr != nil { |
| 145 | + return errors.Wrap(errors.ErrCodeInternal, |
| 146 | + "one or more evidence sections failed", lastErr) |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +// runSection executes the evidence script for a single section. |
| 152 | +func (c *Collector) runSection(ctx context.Context, scriptPath, scriptDir, section string) error { |
| 153 | + cmd := exec.CommandContext(ctx, "bash", scriptPath, section) |
| 154 | + cmd.Dir = scriptDir |
| 155 | + cmd.Env = append(os.Environ(), |
| 156 | + "EVIDENCE_DIR="+c.outputDir, |
| 157 | + "SCRIPT_DIR="+scriptDir, |
| 158 | + ) |
| 159 | + if c.noCleanup { |
| 160 | + cmd.Env = append(cmd.Env, "NO_CLEANUP=true") |
| 161 | + } |
| 162 | + cmd.Stdout = os.Stdout |
| 163 | + cmd.Stderr = os.Stderr |
| 164 | + return cmd.Run() |
| 165 | +} |
| 166 | + |
| 167 | +// writeEmbeddedManifests extracts the embedded manifests to the target directory. |
| 168 | +func writeEmbeddedManifests(targetDir string) error { |
| 169 | + return fs.WalkDir(manifestsFS, "scripts/manifests", func(path string, d fs.DirEntry, err error) error { |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + // Compute relative path from "scripts/manifests" prefix. |
| 175 | + relPath, _ := filepath.Rel("scripts/manifests", path) |
| 176 | + targetPath := filepath.Join(targetDir, relPath) |
| 177 | + |
| 178 | + if d.IsDir() { |
| 179 | + return os.MkdirAll(targetPath, 0o755) |
| 180 | + } |
| 181 | + |
| 182 | + data, err := manifestsFS.ReadFile(path) |
| 183 | + if err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + return os.WriteFile(targetPath, data, 0o600) |
| 187 | + }) |
| 188 | +} |
0 commit comments