createWorkerFallbackContext creates evaluators once per worker thread This ensures thread safety while reusing evaluators within the worker
(ctx context.Context, fallbackPolicy policy.Policy)
| 149 | // createWorkerFallbackContext creates evaluators once per worker thread |
| 150 | // This ensures thread safety while reusing evaluators within the worker |
| 151 | func CreateWorkerFallbackContext(ctx context.Context, fallbackPolicy policy.Policy) (*WorkerFallbackContext, error) { |
| 152 | log.Debugf("🔄 Creating worker fallback context (evaluators created once per worker)...") |
| 153 | evaluators := []evaluator.Evaluator{} |
| 154 | |
| 155 | // Create evaluators for each policy source group (same as image command) |
| 156 | for i, sourceGroup := range fallbackPolicy.Spec().Sources { |
| 157 | log.Debugf("🔄 Worker: Processing source group %d: '%s'", i, sourceGroup.Name) |
| 158 | policySources := source.PolicySourcesFrom(sourceGroup) |
| 159 | |
| 160 | log.Debugf("🔄 Worker: Found %d policy sources for group '%s'", len(policySources), sourceGroup.Name) |
| 161 | |
| 162 | var c evaluator.Evaluator |
| 163 | var err error |
| 164 | if utils.IsOpaEnabled() { |
| 165 | log.Debugf("🔄 Worker: Using OPA evaluator") |
| 166 | c, err = evaluator.NewOPAEvaluator( |
| 167 | ctx, policySources, fallbackPolicy, sourceGroup, nil) |
| 168 | } else { |
| 169 | log.Debugf("🔄 Worker: Using Conftest evaluator with filter type: include-exclude") |
| 170 | // Use the unified filtering approach with the specified filter type |
| 171 | c, err = evaluator.NewConftestEvaluatorWithFilterType( |
| 172 | ctx, policySources, fallbackPolicy, sourceGroup, "include-exclude") // Default filter type |
| 173 | } |
| 174 | |
| 175 | if err != nil { |
| 176 | log.Debugf("🔄 Worker: Failed to initialize evaluator: %v", err) |
| 177 | return nil, err |
| 178 | } |
| 179 | |
| 180 | log.Debugf("🔄 Worker: Successfully created evaluator for source group '%s'", sourceGroup.Name) |
| 181 | evaluators = append(evaluators, c) |
| 182 | } |
| 183 | |
| 184 | log.Debugf("🔄 Worker: Created %d total evaluators (reused for all components in this worker)", len(evaluators)) |
| 185 | return &WorkerFallbackContext{ |
| 186 | Evaluators: evaluators, |
| 187 | }, nil |
| 188 | } |
| 189 | |
| 190 | // getPolicyConfig resolves policy configuration (copied from validate package to avoid circular dependency) |
| 191 | func getPolicyConfig(ctx context.Context, policyConfig string) (string, error) { |
no test coverage detected