Created
August 3, 2024 21:07
-
-
Save mateothegreat/5ff9f13598a9c2c85c22ed63e18a25af to your computer and use it in GitHub Desktop.
Wait for multiple conditions for kubernetes client-go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WaitForResourceCondition waits for a condition to be met for each resource in the list of arguments. | |
// | |
// Arguments: | |
// - args ...WaitForResourceConditionArgs: a list of arguments to wait for a condition to be met for each resource. | |
// | |
// Returns: | |
// - []error: a list of errors, one for each resource. | |
func WaitForResourceCondition(args ...WaitForResourceConditionArgs) []error { | |
var wg sync.WaitGroup | |
var mu sync.Mutex | |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) | |
defer cancel() | |
errors := make([]error, 0, len(args)) | |
for _, arg := range args { | |
wg.Add(1) | |
go func(arg WaitForResourceConditionArgs) { | |
defer wg.Done() | |
err := wait.PollUntilContextTimeout(ctx, time.Second, arg.Timeout, true, func(ctx context.Context) (bool, error) { | |
result, err := arg.Evaluator() | |
if err != nil { | |
return false, err | |
} | |
if !result { | |
return false, nil | |
} | |
return result, nil | |
}) | |
if err != nil { | |
mu.Lock() | |
errors = append(errors, fmt.Errorf("condition not met within timeout: %v", err)) | |
mu.Unlock() | |
} | |
}(arg) | |
} | |
wg.Wait() | |
return errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment