Implement hns validation webhook #602
/assign @adrianludwin
/assign @sophieliu15
cc @adrianchung
Maybe use the full name instead of acronym? Or jut use "subnamespaces" as the first sentance.
Do we need to log the message here? IIUC, it will be logged in line 56?
+1
When did we make "baz" a child of "foo"? Or is that not necessary in the test? If not, maybe comment why we can delete baz from foo when it's not a child of foo.
It would be more useful to give a full list here
lgtm after a few minor comments
Ah right, good catch!
SG, yes since the creation of hns is masked by k hns create, users may not aware they are actually creating hns instances. Will update, thanks!
Thanks for pointing this one out! I was going to test deleting an hns as long as the namespace in the 'cnm' field doesn't have owner annotation and allowsCD.
After another look, I think I shouldn't use this case since this request would never get to handle(), it should return bad request in Handle() already. I will set a real hns relationship for them at the top.
sure, will update!
Is this function used in this PR? I'd expect to see a test for something like this, but don't see anything associated with it -- either that or as a part of an interface definition. Am I missing some understanding here?
You mean InjectDecoder? It's part of the boilerplate code that's called by controller-runtime, I'm pretty sure nothing will compile if it's not there.
This might be a bit easier to read if you had more early exits:
if req.op != v1beta1.Delete {
return allow("") // we only care about deletions
}
if ns.AllowsCascadingDelete {
return allow("") // go ahead, make my day
}
// Check to see if there are any HNSes that can't be deleted
...
```
Shouldn't this be if cns.IsOwned && !cns.AllowsCascadingDelete()? If so, maybe change hnses to cantDelete or something like that, to make it clear why things get added to that list.
If I'm correct, and this is a bug, maybe add a unit test for this too?
Maybe Please set allowCascadingDelete first either in the parent namespace or in all the owned children?
FYI updated in line 21 by createOwnedNamespace().
The documentation of the interface DecoderInjector is here: https://godoc.org/sigs.k8s.io/controller-runtime/pkg/webhook/admission#DecoderInjector. It only says it will be used by ControllerManager to inject decoder though.
Yes good catch it's a bug. Say the owner doesn't allowCD but all its owned NS allows, then it should return allow(). Good catch on this corner case, will add the unit test on that and manual test it too.
I tried just deleting InjectDecoder and InjectClient methods out of .pkg/validators/hierarchy.go as a test and the binary still compiles and deploys. I'm not one to argue with the compiler. As an experiment can you see what'd happen if we took this out and see if there's any side effects? I don't want to block the review or anything -- you're just likely closest to trying this out!
Hmm. Are you sure the webhook is running correctly after this? Without InjectDecoder, I'm not sure how the decoder field is set, which means it should cause a nil pointer exception the first time we try to decode something. Perhaps it just looks fine because you never created a HierarchicalNamespace object, but as soon as you do, HNC will crash?
I would have _thought_ that InjectDecoder was checked at compile time, but perhaps it's only checked at runtime with a type switch.
why the update is needed? In your Handle method, it only handles create and delete operations.
Nice catch! We should probably reject any updates to HNSes (it doesn't have any real fields anyway), or else take update out of this list.
Thanks for bringing this up! I am also curious so I tested with removing the InjectDecoder in namespace webhook and make deploy. I got errors when trying to delete a namespace. The errors was the same as when I had dereference of an nil pointer similar to #603 .
Done, I put first two ifs into one.
Added new unit tests and manually tested with setting allowCascadingDeletes (the webhook and cascading deletion all worked as expected).
Good catch, thanks! Removed.
Done, I also added a list of owned namespaces with allowCascadingDelete unset.
I manually tested with update removed. Seems to reject all update requests, we need to have it listed here and in Handle reject all update requests.
How did you try to update it? You mean with labels and stuff? Yeah, perhaps we should allow those.
I'm surprised that an action that we _didn't_ list as part of the validator would be blocked. What was the error message with update removed from the webhook config?
PTAL, I updated the namespace webhook to check if all owned namespaces allowCD and added the new unit tests. I also updated the HNS webhook to deny all update requests and added one unit test case. @adrianludwin , @jiachengxu
Note that update is still present here.
Maybe restructure this as a switch statement, with any unexpected op resulting in log.Error? We should let unexpected ops through though, since it's probably just a webhook misconfiguration.
I'm thinking we _should_ allow Update though in case people want to put labels or annotations onto them. You can't change the namespace or name, which are the only properties we care about anyway.
update verb is removed in HNS webhook and the update request handling and the unit test are all removed. @adrianludwin
/lgtm
/approve
/hold
/assign @rjbez17
/lgtm
/approve
/hold cancel
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: <a href="https://github.com/kubernetes-sigs/multi-tenancy/pull/602#issuecomment-609936784" title="Approved">adrianludwin</a>, <a href="https://github.com/kubernetes-sigs/multi-tenancy/pull/602#issuecomment-610023772" title="Approved">rjbez17</a>, <a href="https://github.com/kubernetes-sigs/multi-tenancy/pull/602#" title="Author self-approved">yiqigao217</a>
The full list of commands accepted by this bot can be found here.
The pull request process is described here
<details >
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":[]} -->
sophieliu15
yiqigao217
Add HierarchicalNamespace and Namespace webhooks. They will allow all
requests from HNC SA.
HNS webhook will deny:
1) creating hns in a forbidden (excluded) namespace;
2) creating hns with an existing namespace name;
3) deleting hns if the owned namespace doesn't allow cascading deletion.
NS webhook will deny:
1) deleting ns if it's owned, no matter if it allows cascading deletion,
since users should delete the hns instance in the owner instead;
2) deleting ns if it has owned ns that cannot be cascading deleted.
Tested with new unit tests and manually tested on GKE cluster to see if
it works as expected and no conflict with cascading deletion implemented
earlier.
Fixes #458
Part of #457