Skip to content

Instantly share code, notes, and snippets.

@calebamiles
Last active January 10, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebamiles/8e62c22a7f82c915f0bb5779240b16e8 to your computer and use it in GitHub Desktop.
Save calebamiles/8e62c22a7f82c915f0bb5779240b16e8 to your computer and use it in GitHub Desktop.
a possible updated test loop
// somewhere earlier we define
// var numberPodsEvicted *uint32
for i := 0; i < numOfEvictions; i++ {
wg.Add(1)
go func(id int, errCh chan error) {
defer wg.Done()
podName := fmt.Sprintf(podNameFormat, id)
eviction := newEviction(ns.Name, podName, deleteOption)
err := wait.PollImmediate(5*time.Second, 60*time.Second, func() (bool, error) {
e := clientSet.Policy().Evictions(ns.Name).Evict(eviction)
switch {
case errors.IsTooManyRequests(e):
return false, nil
case errors.IsConflict(e):
// the important test case
return false, fmt.Errorf("Unexpected Conflict (409) error caused by failing to handle concurrent PDB updates: %v", e)
case e == nil:
return true, nil
default:
return false, e
}
})
if err != nil {
errCh <- err
// don't return here otherwise we would leak the pod
}
_, err = clientSet.Core().Pods(ns.Name).Get(podName, metav1.GetOptions{})
switch {
case errors.IsNotFound(err):
atomic.AddUint32(numberPodsEvicted, 1)
// pod was evicted and deleted so return from goroutine immediately
return
case err == nil:
// this shouldn't happen if the pod was evicted successfully
errCh <- fmt.Errorf("Pod %q is expected to be evicted", podName)
default:
errCh <- err
}
// delete pod which still exists due to error
e := clientSet.Core().Pods(ns.Name).Delete(podName, deleteOption)
if e != nil {
errCh <- e
}
}(i, errCh)
}
// when checking the correct number of pods have been evicted we can either look at numberPodsEvicted directly
// because wg.Done() has returned or we can do
//
// if atomic.LoadUint32(numberPodsEvicted) != numOfEvictions {
// t.Fatalf("fewer number of successful evictions than expected : %d", atomic.LoadUint32(numberPodsEvicted))
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment