Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ownerRefernce to external check creation #378

Merged
merged 9 commits into from Apr 20, 2020

Conversation

NissesSenap
Copy link
Contributor

This doesn't work. Need input.
This is my initial stab at #366

@NissesSenap
Copy link
Contributor Author

Can someone point me in the right direction?
My go skills is still low and so i get lost within channels and interfaces.

I wanted to send the the UID together with name etc. that is needed for the ownerReference meta data to the checks created but from the kuberhealthy I can't manage to find how to get the data from the CRD and then push it towards the objects straight away.

Instead I tried to solve it by pulling the data from the CRD as seen in my PR.
First of all it doesn't work due to missing info (could do some strange for loops to make a educated guess) and it would give unneeded access to the SA and when i write this comment I realize updating the final object from itself sounds like a dumb idea... :)

Any way if someone can point me in the correct direction I think i should be able to solve it.

@integrii integrii self-requested a review March 26, 2020 21:38
@integrii
Copy link
Collaborator

Hey @NissesSenap - Thank you for hacking on this! Unfortunately, I think the current approach may not be quite right.

I think the right place to do this is around here. Basically we want to go into the p variable here and set the right Owner metadata information. According to the docs, the metadata should be at p.ownerReferences (or something similar) and should be of type OwnerReference.

Ultimately, I think that when a pod is programmatically created (anywhere) we should include this owner information. It's really good stuff and I am happy you brought it up. Making this one change would implement this owner information for all checker pods created by Kuberhealthy... Which is awesome.

The next step would be to go through each checker pod and ensure that anytime it creates a pod or daemonset or whatever, it always specifies the right OwnerReference object that points back to the checker pod.

Added crdUID to Checker struct to save the UID created with the CRD.
@NissesSenap NissesSenap changed the title Add ownerRefernce to daemonset-check Add ownerRefernce to external check creation Mar 27, 2020
@NissesSenap
Copy link
Contributor Author

Man I don't know how many hours i was looking for something like createPod() but ofc i should have looked in the pkg and not in the cmd/kuberhealthy...
I did a force push so i removed the old code from my branch so i didn't have to recreated the PR.

I'm not sure I found the correct path this time ether but it's definitely closer.
Now i can see a ownerRefernece in the created objects but they point back to there own controller which makes sense but i guess i hoped it would append to the list and be able to still refer back to the kuberhealthy deployment.

Or I still haven't been able to wrap my head around it ^^
Anyway need to do some other things now but please have a look and I will try to get some more time for this soon.

@integrii
Copy link
Collaborator

Thanks @NissesSenap ! I will take a look at this one tomorrow... I didn't get enough time to investigate your PR today.

@integrii
Copy link
Collaborator

integrii commented Apr 2, 2020

Have you by any chance built/tested this one yet on one of your own clusters? Our tests are almost non-existent due to how hard it is to spin up, tear down, and instrument a cluster for testing.

If you have tested it, what version is that cluster?

@NissesSenap
Copy link
Contributor Author

Yes I have tried it but it dosen't do the trick the ownerRef gets overwritten by the deployment, I will need to take a look at it again, but currently i need to prioritize other things.
If you or someone else have time to look at it please go for it.

I run Kubernetes Version: v1.16.2 (Openshift version 4.3.0)

@@ -119,6 +123,7 @@ func New(client *kubernetes.Clientset, checkConfig *khcheckcrd.KuberhealthyCheck
OriginalPodSpec: checkConfig.Spec.PodSpec,
PodSpec: checkConfig.Spec.PodSpec,
KubeClient: client,
crdUID: checkConfig.GetObjectMeta().GetUID(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like instead of the khcheck crdUID, you want to reference the kuberhealthy deployment UID? im not sure we have something in the code that gets the kuberhealthy deployment UID... @integrii ?

may need to add a function somewhere that finds the kuberhealthy deployment and gets the UID from it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think that the external checker pods that get spun up would have an owner reference that points to the kuberhealthy pod that spawned them (not the deployment or khcheck). This way, if the kuberhealthy instance that is orchestrating the check disappears for some reason, the checker pods it was orchestrating (and had stateful data for) are also removed.

I think this means we can probably drop all the crdUID property stuff and just simply put in the hostname of the kuberhealthy instance that spawns the pod in the owner reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I will finally take a third stab at this and I will do as you say just point back at the kuberhealthy pod as the owner. I hopefully have something to push later today.

Don't try to point to the CRD:s, instead point the owenerRefernece to
the operator. Don't take in to account that the operator pod can be
removed.
p.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Pod",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to find a way to not hardcode the APIVersion and Kind but I was unable to do so.
I'm sure there is a nice way through the k8s core api, if you know it please point it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried with apiv1.Pod{}.APIVersion but this just returns an empty string

@NissesSenap
Copy link
Contributor Author

I cleaned the CRD stuff as pointed out and put it towards the kuberhealthy master pod.
I seen it work in my own setup, if you want to give it a try you can use: quay.io/nissessenap/kuberhealthy:2.1.3

@NissesSenap
Copy link
Contributor Author

Was thinking about remaking my function a bit and make it global so the other cmd could use it to reference to them self thus getting a ownerReference on the DS/deployment etc that they create.

But the none of the checks currently just imports comcast/kuberhealthy/v2/pkg/checks/external
so it dosen't make sense. Not that the same function is hard to write but boring to have the exact same code in all the checks.
Could add it in kubeClient but it dosen't feel super natural.
Could create a util or something like that?
All we need is a KubeClient and namespace and those two things is something the checks always have.
Do you have a good idea on where to put a globally scope function like this?

@joshulyne
Copy link
Collaborator

Hmm I've been looking through that too, where would be a good place for this. So I know all checks import the external/checkclient package since it provides functions for external checks to report back to Kuberhealthy. I'm thinking that could be a spot, but its definitely not perfect. I would say there for now, unless others have an issue with that. @jonnydawg? @integrii is on paternal leave 🎉 rn so he may be slower to respond :P

Both @jonnydawg and I can try importing that function in the daemonset and deployment check once you have it implemented so we can start adding ownerReferences to those checks!

}
return kHealthyPod, nil
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if this could all be combined to a single function addOwnerReference where it takes in the kube client and namespace (like you mentioned with making this more of a global function) and returns an ownerReference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got an issue with circular imports so i created a separate package that i called utils and made it too two functions where ownerRef calls on the get pod one. Please have a look at the latest commit

This to make it reachable from the clients.
external/client needs the external packages.
For now I have the exact same code in both files.
This way it still works and the external checkers can still get ownerRef
in a easy way.
This way we only need 1 function to get ownerRef
@NissesSenap
Copy link
Contributor Author

Due to circular imports I created a new package that i called uitls, maybe not the best name.

It won't be any issue for you to call this package from the different checks.
If you don't like this solution we can ofc have the same code base in two places.

Hopefully you think this looks okay. I can also have a mock fest and try to write some test for my very small package.

} else {
// Sets OwnerRefernces on all checker pods
p.OwnerReferences = ownerRef
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm after @jonnydawg and I look through this a bit, I believe we want to return the error instead of just logging it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought was that it isn't worth not creating the pod just because you can't find the ownerRef so that's why I did a log instead of a return.

But I have updated the code to return the error and removed the else.

@joshulyne
Copy link
Collaborator

joshulyne commented Apr 15, 2020

Overall, I think this looks good! I like how its in a separate util package. I think as long as we bubble up the error we should be good to go! @jonnydawg and I should be able to import this package to the ds and deployment checks 👍 thanks so much!

go.mod Outdated Show resolved Hide resolved
@joshulyne
Copy link
Collaborator

Also, tested this locally, and it worked well! When I delete the kuberhealthy instance responsible for all the external checks, all the completed external checks were deleted -- super helpful for cleanup purposes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants