@@ -74,48 +74,37 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
7474 },
7575 }
7676
77- // Initialize checkers in parallel and collect all checks .
78- checkCh := make ( chan Check )
79- wg := & sync. WaitGroup {}
77+ // Collect all checks in parallel.
78+ checkerWG := & sync. WaitGroup {}
79+ resultCh := make ( chan CheckResult )
8080 for _ , checker := range h .checkers {
81- wg .Add (1 )
82- go func (ctx context.Context , checker Checker ) {
81+ checkerWG .Add (1 )
82+
83+ go func (ctx context.Context , checker Checker , resultCh chan CheckResult ) {
84+ // Initialize the checker.
8385 checks := checker .Init (ctx , h .client , cluster )
86+
87+ // Run its checks in parallel.
88+ checksWG := & sync.WaitGroup {}
8489 for _ , check := range checks {
85- checkCh <- check
90+ checksWG .Add (1 )
91+ go func (ctx context.Context , check Check , resultCh chan CheckResult ) {
92+ result := check (ctx )
93+ resultCh <- result
94+ checksWG .Done ()
95+ }(ctx , check , resultCh )
8696 }
87- wg .Done ()
88- }(ctx , checker )
89- }
97+ checksWG .Wait ()
9098
91- // Close the channel when all checkers are done.
92- go func (wg * sync.WaitGroup , checkCh chan Check ) {
93- wg .Wait ()
94- close (checkCh )
95- }(wg , checkCh )
96-
97- // Collect all checks from the channel.
98- checks := []Check {}
99- for check := range checkCh {
100- checks = append (checks , check )
99+ checkerWG .Done ()
100+ }(ctx , checker , resultCh )
101101 }
102102
103- // Run all checks in parallel.
104- resultCh := make (chan CheckResult )
105- for _ , check := range checks {
106- wg .Add (1 )
107- go func (ctx context.Context , check Check ) {
108- result := check (ctx )
109- resultCh <- result
110- wg .Done ()
111- }(ctx , check )
112- }
113-
114- // Close the channel when all checks are done.
103+ // Close the channel when all checkers are done.
115104 go func (wg * sync.WaitGroup , resultCh chan CheckResult ) {
116105 wg .Wait ()
117106 close (resultCh )
118- }(wg , resultCh )
107+ }(checkerWG , resultCh )
119108
120109 // Collect all check results from the channel.
121110 internalError := false
0 commit comments