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?
@riddhi89
Copy link

riddhi89 commented May 16, 2019

// 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?

That is my understanding as well.

// If no lines are commented out, the object exists to be retrieved -- but why?

Do you mean, object fails to be retrieved here? If yes, it is an interesting observation 🤔

@benjaminjb
Copy link
Author

Heh, my confusion on line 27 is more like, "this works -- and the object exists -- but why?"

I'm now under the believe that line 17 deletes the service, line 18 waits for the reconcile loop to be called, and line 19 waits for the object to exist.

(And the object will exist because the controller will notice during one of its loops that the desired state has deviated from the actual state, calling the reconcile func, and once that func gets called, it'll call the helper func to create that missing object.)

Hmmm, but if that's true I'm confused about the final block of tests, where we test

g.Eventually(func() error { return c.Delete(context.TODO(), masterService) }, timeout).
		Should(gomega.MatchError("services \"pgsql-foo\" not found"))

which looks like it calls for deleting the service and expects an error because the service no longer exists. But why doesn't the service exist at this point in the test?

@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