-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Webhook for member cluster #393
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package webhook | ||
|
|
||
| import ( | ||
| "go.goms.io/fleet/pkg/webhook/clusterresourceplacement" | ||
| "go.goms.io/fleet/pkg/webhook/fleetresourcehandler" | ||
| "go.goms.io/fleet/pkg/webhook/pod" | ||
| "go.goms.io/fleet/pkg/webhook/replicaset" | ||
| ) | ||
|
|
||
| func init() { | ||
| // AddToManagerFuncs is a list of functions to create webhook and add them to a manager. | ||
| AddToManagerFuncs = append(AddToManagerFuncs, fleetresourcehandler.Add) | ||
| AddToManagerFuncs = append(AddToManagerFuncs, clusterresourceplacement.Add) | ||
| AddToManagerFuncs = append(AddToManagerFuncs, pod.Add) | ||
| AddToManagerFuncs = append(AddToManagerFuncs, replicaset.Add) | ||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,43 @@ | ||
| package validation | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| authenticationv1 "k8s.io/api/authentication/v1" | ||
| "k8s.io/klog/v2" | ||
| "k8s.io/utils/strings/slices" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1" | ||
| ) | ||
|
|
||
| const ( | ||
| mastersGroup = "system:masters" | ||
| ) | ||
|
|
||
| // TODO:(Arvindthiru) Get valid usernames as flag and allow those usernames. | ||
|
|
||
| // ValidateUserForCRD checks to see if user is authenticated to make a request to modify fleet CRDs. | ||
| func ValidateUserForCRD(userInfo authenticationv1.UserInfo) bool { | ||
| return slices.Contains(userInfo.Groups, mastersGroup) | ||
| func ValidateUserForCRD(whiteListedUsers []string, userInfo authenticationv1.UserInfo) bool { | ||
| return isMasterGroupUserOrWhiteListedUser(whiteListedUsers, userInfo) | ||
| } | ||
|
|
||
| // ValidateUserForFleetCR checks to see if user is authenticated to make a request to modify Fleet CRs. | ||
| func ValidateUserForFleetCR(ctx context.Context, client client.Client, whiteListedUsers []string, userInfo authenticationv1.UserInfo) bool { | ||
| if isMasterGroupUserOrWhiteListedUser(whiteListedUsers, userInfo) { | ||
| return true | ||
| } | ||
| var memberClusterList fleetv1alpha1.MemberClusterList | ||
| if err := client.List(ctx, &memberClusterList); err != nil { | ||
| klog.V(2).ErrorS(err, "failed to list member clusters") | ||
| return false | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this list from the cache?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This client is from the manager that's constructed in hub agent's main.go where we don't pass a NewClient func, since we are not passing it looks like we get new delegating client which uses the cache on reads https://github.com/kubernetes-sigs/controller-runtime/blob/main/pkg/manager/manager.go#L141 Seems like we are using a different version of controller runtime but this seems to be true for both versions I can see why it would be concern if we read stale data from the cache and reject certain requests from the member agents, but even if the webhook rejects the request once the member agent will retry in the next reconcile and in the meantime I'm assuming the cache will be populated with new data. If that's not the case we may need to use a new client here which doesn't use the cache
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably should upgrade our controller-runtime now. |
||
| } | ||
| identities := make([]string, len(memberClusterList.Items)) | ||
| for i := range memberClusterList.Items { | ||
| identities = append(identities, memberClusterList.Items[i].Spec.Identity.Name) | ||
| } | ||
| // this ensures will allow all member agents are validated. | ||
| return slices.Contains(identities, userInfo.Username) | ||
| } | ||
|
|
||
| func isMasterGroupUserOrWhiteListedUser(whiteListedUsers []string, userInfo authenticationv1.UserInfo) bool { | ||
| return slices.Contains(whiteListedUsers, userInfo.Username) || slices.Contains(userInfo.Groups, mastersGroup) | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the whiteList only useful fro FleetCR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using whiteList for CRD check as well