Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benjaminjb/c6fd7081b32efdb61b1728c23f12f816 to your computer and use it in GitHub Desktop.
Save benjaminjb/c6fd7081b32efdb61b1728c23f12f816 to your computer and use it in GitHub Desktop.
// I was confused about what was actually going on with the reconcile loop vis-a-vis the TestReconcile function,
// so I threw some Print statements in and I have some results.
// (Note: the main reconcile loop now calls my reconcileServiceAccount func;
// and one of the first things this test does is create a PSQL parent K8s object.)
serviceaccount := &corev1.ServiceAccount{}
c.Get(context.TODO(), defaultKey, serviceaccount)
fmt.Printf("serviceaccount %+v", serviceaccount.Name)
fmt.Println("")
// Line 8 always prints the name of the serviceaccount
// because the serviceaccount always is created by this time after the parent PSQL object was created.
g.Eventually(func() error { return c.Get(context.TODO(), defaultKey, serviceaccount) }, timeout).Should(gomega.Succeed())
// Delete the ServiceAccount and expect Reconcile to be called for ServiceAccount deletion
// The following lines 17-19 will affect the final print statement.
g.Expect(c.Delete(context.TODO(), serviceaccount)).To(gomega.Succeed())
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))
g.Eventually(func() error { return c.Get(context.TODO(), defaultKey, serviceaccount) }, timeout).Should(gomega.Succeed())
// This should get the object again and print the name if the object exists. So what happens?
serviceaccount2 := &corev1.ServiceAccount{}
c.Get(context.TODO(), defaultKey, serviceaccount2)
fmt.Printf("serviceaccount2 %+v", serviceaccount2.Name)
fmt.Println("")
// If no lines are commented out, the object exists to be retrieved -- but why?
// If line 17 is commented out, then the delete never happens and the object always exists.
// If line 18 is commented out, the test will fail on line 19 -- the object is not there to be gotten within the timeout window.
// If line 19 is commented out, the object is not there to be retrieved, line 24 prints a blank.
// So do 18 + 19 tell the testing to wait (a reasonable timeout window) for the reconcile message to be recieved (18)
// and wait (a reasonable timeout window) for the reconcile to re-create the deleted object?
@benjaminjb
Copy link
Author

Wait, I think I've got that last bit mentally modeled:

Gomega's Eventually polls until either timeout or condition met, so, it runs c.Delete until it either times out or meets the condition that the service is no longer there. So the first time it runs, it deletes the service and then reruns, at which point the service no longer exists and the condition is met. If we set the timeout for something smaller than the poll time, this would fail because it would only run c.Delete once.

@benjaminjb
Copy link
Author

Oy vey, I forgot to add these K8s kinds to the watcher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment