Skip to content

Commit c9ef9e9

Browse files
CopilotJaydipGabani
andcommitted
Address review feedback: use IsAssigned directly, add nil checks
- Remove helper functions from operations.go, use IsAssigned directly - Replace all usages in main.go with direct IsAssigned calls - Add nil checks in audit.AddToManager and webhook.AddToManager - Remove unnecessary comments per feedback Co-authored-by: JaydipGabani <[email protected]>
1 parent 0400d6e commit c9ef9e9

File tree

5 files changed

+11
-177
lines changed

5 files changed

+11
-177
lines changed

main.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,10 @@ blockingLoop:
380380
}
381381

382382
func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.Tracker, setupFinished chan struct{}) error {
383-
// Block until the setup (certificate generation) finishes.
384383
<-setupFinished
385384

386-
// Determine which dependencies need to be created based on enabled operations
387-
needsOPAClient := operations.HasValidationOperations() || *externaldata.ExternalDataEnabled
388-
needsMutationSystem := mutation.Enabled() || *expansion.ExpansionEnabled
385+
needsOPAClient := operations.IsAssigned(operations.Audit) || operations.IsAssigned(operations.Webhook) || operations.IsAssigned(operations.Status) || *externaldata.ExternalDataEnabled
386+
needsMutationSystem := operations.IsAssigned(operations.MutationWebhook) || operations.IsAssigned(operations.MutationController) || operations.IsAssigned(operations.MutationStatus) || *expansion.ExpansionEnabled
389387
needsExpansionSystem := *expansion.ExpansionEnabled
390388
needsProviderCache := *externaldata.ExternalDataEnabled
391389

@@ -395,12 +393,9 @@ func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.
395393
var expansionSystem *expansion.System
396394
var certWatcher *certwatcher.CertWatcher
397395

398-
// Setup external data provider cache if needed
399396
if needsProviderCache {
400-
setupLog.Info("setting up external data provider cache")
401397
providerCache = frameworksexternaldata.NewCache()
402398

403-
// Set up cert watcher for external data (shared between OPA and mutation)
404399
certFile := filepath.Join(*certDir, certName)
405400
keyFile := filepath.Join(*certDir, keyName)
406401

@@ -411,16 +406,13 @@ func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.
411406
return err
412407
}
413408

414-
setupLog.Info("setting up client cert watcher")
415409
if err := mgr.Add(certWatcher); err != nil {
416410
setupLog.Error(err, "unable to register client cert watcher")
417411
return err
418412
}
419413
}
420414

421-
// Setup OPA client if needed for validation operations or external data
422415
if needsOPAClient {
423-
setupLog.Info("setting up OPA client")
424416
args := []rego.Arg{rego.Tracing(false), rego.DisableBuiltins(disabledBuiltins.ToSlice()...)}
425417

426418
if needsProviderCache {
@@ -438,7 +430,6 @@ func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.
438430
return err
439431
}
440432

441-
// Register the client cert watcher to the driver
442433
args = append(args, rego.EnableExternalDataClientAuth(), rego.AddExternalDataClientCertWatcher(certWatcher))
443434
}
444435

@@ -483,9 +474,7 @@ func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.
483474
}
484475
}
485476

486-
// Setup mutation system if needed
487477
if needsMutationSystem {
488-
setupLog.Info("setting up mutation system")
489478
mutationOpts := mutation.SystemOpts{Reporter: mutation.NewStatsReporter()}
490479

491480
if needsProviderCache {
@@ -496,13 +485,10 @@ func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.
496485
mutationSystem = mutation.NewSystem(mutationOpts)
497486
}
498487

499-
// Setup expansion system if needed (depends on mutation system)
500488
if needsExpansionSystem {
501-
setupLog.Info("setting up expansion system")
502489
expansionSystem = expansion.NewSystem(mutationSystem)
503490
}
504491

505-
// Export system is always created
506492
exportSystem := export.NewSystem()
507493

508494
c := mgr.GetCache()
@@ -529,18 +515,11 @@ func setupControllers(ctx context.Context, mgr ctrl.Manager, tracker *readiness.
529515
return err
530516
}
531517

532-
// processExcluder is used for namespace exclusion for specified processes in config
533518
processExcluder := process.Get()
534519

535-
// Setup all Controllers
536-
setupLog.Info("setting up controllers")
537-
538-
// Cache manager is only needed for validation operations
539520
var cm *cachemanager.CacheManager
540521
var events chan event.GenericEvent
541-
if operations.HasValidationOperations() {
542-
// Events ch will be used to receive events from dynamic watches registered
543-
// via the registrar below.
522+
if operations.IsAssigned(operations.Audit) || operations.IsAssigned(operations.Webhook) || operations.IsAssigned(operations.Status) {
544523
events = make(chan event.GenericEvent, 1024)
545524
reg, err := wm.NewRegistrar(
546525
cachemanager.RegistrarName,

pkg/audit/controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func AddToManager(m manager.Manager, deps *Dependencies) error {
3838
log.Info("auditing is disabled")
3939
return nil
4040
}
41+
if deps.Client == nil {
42+
log.Info("audit requires OPA client, skipping")
43+
return nil
44+
}
4145
am, err := New(m, deps)
4246
if err != nil {
4347
return err

pkg/operations/operations.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,3 @@ func AssignedStringList() []string {
131131
func HasValidationOperations() bool {
132132
return IsAssigned(Audit) || IsAssigned(Status) || IsAssigned(Webhook)
133133
}
134-
135-
// HasMutationOperations returns `true` if there
136-
// are any operations that would require mutation controllers.
137-
func HasMutationOperations() bool {
138-
return IsAssigned(MutationController) || IsAssigned(MutationStatus) || IsAssigned(MutationWebhook)
139-
}
140-
141-
// HasStatusOperation returns `true` if the Status operation is assigned.
142-
func HasStatusOperation() bool {
143-
return IsAssigned(Status)
144-
}
145-
146-
// HasGenerateOperation returns `true` if the Generate operation is assigned.
147-
func HasGenerateOperation() bool {
148-
return IsAssigned(Generate)
149-
}
150-

pkg/operations/operations_test.go

Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -48,139 +48,3 @@ func Test_Flags(t *testing.T) {
4848
})
4949
}
5050
}
51-
52-
// Test_HelperFunctions validates the helper functions for checking operation types.
53-
func Test_HelperFunctions(t *testing.T) {
54-
tests := []struct {
55-
name string
56-
operations []string
57-
expectValidation bool
58-
expectMutation bool
59-
expectStatus bool
60-
expectGenerate bool
61-
}{
62-
{
63-
name: "audit only",
64-
operations: []string{"audit"},
65-
expectValidation: true,
66-
expectMutation: false,
67-
expectStatus: false,
68-
expectGenerate: false,
69-
},
70-
{
71-
name: "webhook only",
72-
operations: []string{"webhook"},
73-
expectValidation: true,
74-
expectMutation: false,
75-
expectStatus: false,
76-
expectGenerate: false,
77-
},
78-
{
79-
name: "status only",
80-
operations: []string{"status"},
81-
expectValidation: true,
82-
expectMutation: false,
83-
expectStatus: true,
84-
expectGenerate: false,
85-
},
86-
{
87-
name: "mutation-webhook only",
88-
operations: []string{"mutation-webhook"},
89-
expectValidation: false,
90-
expectMutation: true,
91-
expectStatus: false,
92-
expectGenerate: false,
93-
},
94-
{
95-
name: "mutation-controller only",
96-
operations: []string{"mutation-controller"},
97-
expectValidation: false,
98-
expectMutation: true,
99-
expectStatus: false,
100-
expectGenerate: false,
101-
},
102-
{
103-
name: "mutation-status only",
104-
operations: []string{"mutation-status"},
105-
expectValidation: false,
106-
expectMutation: true,
107-
expectStatus: false,
108-
expectGenerate: false,
109-
},
110-
{
111-
name: "generate only",
112-
operations: []string{"generate"},
113-
expectValidation: false,
114-
expectMutation: false,
115-
expectStatus: false,
116-
expectGenerate: true,
117-
},
118-
{
119-
name: "audit and mutation-webhook",
120-
operations: []string{"audit", "mutation-webhook"},
121-
expectValidation: true,
122-
expectMutation: true,
123-
expectStatus: false,
124-
expectGenerate: false,
125-
},
126-
{
127-
name: "status and generate",
128-
operations: []string{"status", "generate"},
129-
expectValidation: true,
130-
expectMutation: false,
131-
expectStatus: true,
132-
expectGenerate: true,
133-
},
134-
{
135-
name: "all operations",
136-
operations: []string{"audit", "webhook", "status", "mutation-webhook", "mutation-controller", "mutation-status", "generate"},
137-
expectValidation: true,
138-
expectMutation: true,
139-
expectStatus: true,
140-
expectGenerate: true,
141-
},
142-
}
143-
144-
for _, tt := range tests {
145-
t.Run(tt.name, func(t *testing.T) {
146-
// Save current state
147-
operationsMtx.Lock()
148-
oldOps := operations
149-
operations = newOperationSet()
150-
operationsMtx.Unlock()
151-
152-
// Set up test operations
153-
flagSet := flag.NewFlagSet("test", flag.ContinueOnError)
154-
flagSet.Var(operations, "operation", "test")
155-
156-
args := []string{}
157-
for _, op := range tt.operations {
158-
args = append(args, "-operation", op)
159-
}
160-
161-
if err := flagSet.Parse(args); err != nil {
162-
t.Fatalf("failed to parse flags: %v", err)
163-
}
164-
165-
// Test helper functions
166-
if got := HasValidationOperations(); got != tt.expectValidation {
167-
t.Errorf("HasValidationOperations() = %v, want %v", got, tt.expectValidation)
168-
}
169-
if got := HasMutationOperations(); got != tt.expectMutation {
170-
t.Errorf("HasMutationOperations() = %v, want %v", got, tt.expectMutation)
171-
}
172-
if got := HasStatusOperation(); got != tt.expectStatus {
173-
t.Errorf("HasStatusOperation() = %v, want %v", got, tt.expectStatus)
174-
}
175-
if got := HasGenerateOperation(); got != tt.expectGenerate {
176-
t.Errorf("HasGenerateOperation() = %v, want %v", got, tt.expectGenerate)
177-
}
178-
179-
// Restore state
180-
operationsMtx.Lock()
181-
operations = oldOps
182-
operationsMtx.Unlock()
183-
})
184-
}
185-
}
186-

pkg/webhook/webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ var AddToManagerFuncs []func(manager.Manager, Dependencies) error
4242

4343
// AddToManager adds all Controllers to the Manager.
4444
func AddToManager(m manager.Manager, deps Dependencies) error {
45+
if deps.OpaClient == nil && deps.MutationSystem == nil {
46+
log.Info("webhook requires OPA client or mutation system, skipping")
47+
return nil
48+
}
4549
for _, f := range AddToManagerFuncs {
4650
if err := f(m, deps); err != nil {
4751
return err

0 commit comments

Comments
 (0)