-
Notifications
You must be signed in to change notification settings - Fork 113
Draft: GPU Mutating Webhook #326
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
Draft
guptaNswati
wants to merge
4
commits into
NVIDIA:main
Choose a base branch
from
guptaNswati:mutating-webhook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /** | ||
| # Copyright 2024 NVIDIA CORPORATION | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| **/ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "log" | ||
| "net/http" | ||
|
|
||
| admissionv1 "k8s.io/api/admission/v1" | ||
|
Check failure on line 27 in cmd/gpu-mutating-webhook/admission_controller.go
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/serializer" | ||
| ) | ||
|
|
||
| const ( | ||
| jsonContentType = `application/json` | ||
| ) | ||
|
|
||
| var ( | ||
| universalDeserializer = serializer.NewCodecFactory(runtime.NewScheme()).UniversalDeserializer() | ||
| ) | ||
|
|
||
| type patchOperation struct { | ||
| Op string `json:"op"` | ||
| Path string `json:"path"` | ||
| Value interface{} `json:"value,omitempty"` | ||
| } | ||
|
|
||
| type admitFunc func(*admissionv1.AdmissionRequest) ([]patchOperation, error) | ||
|
|
||
| // Swati: skip nvidia-dra-driver-gpu ns as well | ||
| func isKubeNamespace(ns string) bool { | ||
| return (ns == metav1.NamespacePublic || ns == metav1.NamespaceSystem) | ||
| } | ||
|
|
||
| func doServeAdmitFunc(w http.ResponseWriter, r *http.Request, admit admitFunc) ([]byte, error) { | ||
| // Request validation. Only handle POST requests with a body and json content type. | ||
| if r.Method != http.MethodPost { | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| return nil, fmt.Errorf("invalid method %s, only POST is allowed", r.Method) | ||
| } | ||
|
|
||
| body, err := ioutil.ReadAll(r.Body) | ||
| if err != nil { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return nil, fmt.Errorf("could not read request body: %v", err) | ||
| } | ||
|
|
||
| if ct := r.Header.Get("Content-Type"); ct != jsonContentType { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return nil, fmt.Errorf("unsupported content type %s, only %s is supported", ct, jsonContentType) | ||
| } | ||
|
|
||
| // Parse the AdmissionReview request. | ||
| var admissionReviewReq admissionv1.AdmissionReview | ||
| if _, _, err := universalDeserializer.Decode(body, nil, &admissionReviewReq); err != nil { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return nil, fmt.Errorf("could not deserialize AdmissionReview: %v", err) | ||
| } else if admissionReviewReq.Request == nil { | ||
| w.WriteHeader(http.StatusBadRequest) | ||
| return nil, errors.New("malformed admission review: Request is nil") | ||
| } | ||
|
|
||
| // Build the response | ||
| admissionReviewResp := admissionv1.AdmissionReview{ | ||
| TypeMeta: admissionReviewReq.TypeMeta, | ||
| Response: &admissionv1.AdmissionResponse{ | ||
| UID: admissionReviewReq.Request.UID, | ||
| }, | ||
| } | ||
|
|
||
| // Skip k8s namespaces | ||
| var patchOps []patchOperation | ||
| if !isKubeNamespace(admissionReviewReq.Request.Namespace) { | ||
| patchOps, err = admit(admissionReviewReq.Request) | ||
| } | ||
|
|
||
| if err != nil { | ||
| admissionReviewResp.Response.Allowed = false | ||
| admissionReviewResp.Response.Result = &metav1.Status{ | ||
| Message: err.Error(), | ||
| } | ||
| } else { | ||
| patchBytes, err := json.Marshal(patchOps) | ||
| if err != nil { | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| return nil, fmt.Errorf("could not marshal JSON patch: %v", err) | ||
| } | ||
| admissionReviewResp.Response.Allowed = true | ||
| admissionReviewResp.Response.Patch = patchBytes | ||
|
|
||
| pt := admissionv1.PatchTypeJSONPatch | ||
| admissionReviewResp.Response.PatchType = &pt | ||
| } | ||
|
|
||
| respBytes, err := json.Marshal(admissionReviewResp) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not marshal AdmissionReview response: %v", err) | ||
| } | ||
| return respBytes, nil | ||
| } | ||
|
|
||
| // serveAdmitFunc is a wrapper that handles HTTP, calls doServeAdmitFunc, and writes the result. | ||
| func serveAdmitFunc(w http.ResponseWriter, r *http.Request, admit admitFunc) { | ||
| log.Print("Handling webhook request ...") | ||
|
|
||
| respBytes, err := doServeAdmitFunc(w, r, admit) | ||
| if err != nil { | ||
| log.Printf("Error handling webhook request: %v", err) | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| _, _ = w.Write([]byte(err.Error())) | ||
| return | ||
| } | ||
|
|
||
| log.Print("Webhook request handled successfully") | ||
| _, writeErr := w.Write(respBytes) | ||
| if writeErr != nil { | ||
| log.Printf("Could not write response: %v", writeErr) | ||
| } | ||
| } | ||
|
|
||
| // admitFuncHandler converts an admitFunc into an http.Handler | ||
| func admitFuncHandler(admit admitFunc) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| serveAdmitFunc(w, r, admit) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| /** | ||
| # Copyright 2025 NVIDIA CORPORATION | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| **/ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| admissionv1 "k8s.io/api/admission/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| tlsDir = `/etc/webhook/tls` | ||
| tlsCertFile = `tls.crt` | ||
| tlsKeyFile = `tls.key` | ||
| gpuResourceName = "nvidia.com/gpu" | ||
| gpuClaimName = "nvidia-gpu-resourceclaim" | ||
| gpuTemplateName = "nvidia-gpu-resourceclaim-template" | ||
| ) | ||
|
|
||
| var ( | ||
| podResource = metav1.GroupVersionResource{Version: "v1", Resource: "pods"} | ||
| ) | ||
|
|
||
| func applyGPUMutation(req *admissionv1.AdmissionRequest) ([]patchOperation, error) { | ||
| // Only mutate Pod CREATE | ||
| // Swati: may be add UPDATE | ||
| if req.Resource != podResource || req.Operation != admissionv1.Create { | ||
| klog.Infof("skip mutation for %v/%v", req.Resource, req.Operation) | ||
| return nil, nil | ||
| } | ||
|
|
||
| var pod corev1.Pod | ||
| if _, _, err := universalDeserializer.Decode(req.Object.Raw, nil, &pod); err != nil { | ||
| klog.Errorf("failed to decode Pod: %v", err) | ||
| return nil, fmt.Errorf("could not deserialize pod: %w", err) | ||
| } | ||
|
|
||
| key := escapeJSONPointer(gpuResourceName) | ||
| var patches []patchOperation | ||
| var ctrGPUResourceClaims []string | ||
|
|
||
| // Iterate on all containers and check for "nvidia.com/gpu" limits | ||
| // using the logic described here for prefering limits over requests | ||
| // GPUs are only supposed to be specified in the limits section, meaning | ||
| // - can specify GPU limits without specifying requests. limit will be used as request value by default | ||
| // - can specify GPU in both limits and requests but they must be equal | ||
| // - cannot specify GPU requests without specifying limits | ||
| // refer: https://kubernetes.io/docs/tasks/manage-gpus/scheduling-gpus/#using-device-plugins | ||
| for ci, ctr := range pod.Spec.Containers { | ||
| ctrName := ctr.Name | ||
| limitCount, limitOk := ctr.Resources.Limits[gpuResourceName] | ||
|
|
||
| // skip if no GPUs in limits | ||
| if !limitOk || limitCount.Value() < 1 { | ||
| continue | ||
| } | ||
| gpuCount := limitCount.Value() | ||
|
|
||
| // check any GPUs in requests | ||
| // it must be equal to limits | ||
| if reqCount, reqOK := ctr.Resources.Requests[gpuResourceName]; reqOK { | ||
| if reqCount.Value() != gpuCount { | ||
| klog.Warningf("container[%q]: gpu request (%d) != limit (%d), skipping mutation", ctrName, reqCount.Value(), gpuCount) | ||
| continue | ||
| } | ||
| reqPatch := removeResourceRequest(ci, "requests", key) | ||
| patches = append(patches, reqPatch) | ||
| klog.Infof("removed container[%q].Resources.Requests: %v", ctrName, reqPatch) | ||
| } | ||
| limitPatch := removeResourceRequest(ci, "limits", key) | ||
| patches = append(patches, limitPatch) | ||
| klog.Infof("removed container[%q].Resources.Limits: %v", ctrName, limitPatch) | ||
|
|
||
| // ensure container-claims slice exists | ||
| // this is JSON way to first creating the field if it does not exist and append later with "-" | ||
| if len(ctr.Resources.Claims) == 0 { | ||
| createPatch := createClaimPatch(fmt.Sprintf("/spec/containers/%d/resources/claims", ci)) | ||
| patches = append(patches, createPatch) | ||
| klog.Infof("created container[%q] empty claims array: %v", ctrName, createPatch) | ||
| } | ||
|
|
||
| // append one claim per GPU | ||
| for i := int64(0); i < gpuCount; i++ { | ||
| claimName := fmt.Sprintf("%s-%d", gpuClaimName, i) | ||
| ctrGPUResourceClaims = append(ctrGPUResourceClaims, claimName) | ||
| appendPatch := appendClaimPatch( | ||
| fmt.Sprintf("/spec/containers/%d/resources/claims", ci), | ||
| map[string]string{"name": claimName}, | ||
| ) | ||
| patches = append(patches, appendPatch) | ||
| klog.Infof("added to container[%q].Resources.Claims: %v", ctrName, appendPatch) | ||
| } | ||
| } | ||
|
|
||
| // Add claims pod-level | ||
| podName := pod.Name | ||
| if len(ctrGPUResourceClaims) > 0 { | ||
| // ensure pod-claims slice exists | ||
| if len(pod.Spec.ResourceClaims) == 0 { | ||
| createPatch := createClaimPatch("/spec/resourceClaims") | ||
| patches = append(patches, createPatch) | ||
| klog.Infof("created pod[%q] empty claims array: %v", podName, createPatch) | ||
| } | ||
|
|
||
| // append each container GPU claim at pod-level | ||
| for _, name := range ctrGPUResourceClaims { | ||
| appendPatch := appendClaimPatch( | ||
| "/spec/resourceClaims", | ||
| map[string]string{ | ||
| "name": name, | ||
| "resourceClaimTemplateName": gpuTemplateName, | ||
| }, | ||
| ) | ||
| patches = append(patches, appendPatch) | ||
| klog.Infof("added ResourceClaim %q (template=%q) to %q: %v", name, gpuTemplateName, podName, appendPatch) | ||
| } | ||
| } | ||
|
|
||
| return patches, nil | ||
| } | ||
|
|
||
| // escapeJSONPointer replace "/" with "~1" | ||
| // refer: https://github.com/json-patch/json-patch-tests/issues/42 | ||
| // needed for "nvidia.com/gpu". otherwise JSON will treat "/" as a path delimiter and treat "gpu" as new field | ||
| func escapeJSONPointer(s string) string { | ||
| return strings.ReplaceAll(s, "/", "~1") | ||
| } | ||
|
|
||
| // removeResourceRequest removes either .resources.requests or .resources.limits | ||
| func removeResourceRequest(ci int, field, key string) patchOperation { | ||
| return patchOperation{ | ||
| Op: "remove", | ||
| Path: fmt.Sprintf("/spec/containers/%d/resources/%s/%s", ci, field, key), | ||
| } | ||
| } | ||
|
|
||
| // createClaimPatch creates an empty slice at the given path | ||
| func createClaimPatch(path string) patchOperation { | ||
| return patchOperation{ | ||
| Op: "add", | ||
| Path: path, | ||
| Value: []map[string]string{}, | ||
| } | ||
| } | ||
|
|
||
| // appendClaimPatch appends to the slice at path | ||
| // "-" is JSON way to inserting at the end of the array when no index is specified. | ||
| // refer: https://datatracker.ietf.org/doc/html/rfc6902 | ||
| func appendClaimPatch(path string, entry map[string]string) patchOperation { | ||
| return patchOperation{ | ||
| Op: "add", | ||
| Path: path + "/-", | ||
| Value: entry, | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| certPath := filepath.Join(tlsDir, tlsCertFile) | ||
| keyPath := filepath.Join(tlsDir, tlsKeyFile) | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.Handle("/mutate", admitFuncHandler(applyGPUMutation)) | ||
|
|
||
| server := &http.Server{ | ||
| Addr: ":8443", | ||
| Handler: mux, | ||
| } | ||
|
|
||
| if err := server.ListenAndServeTLS(certPath, keyPath); err != nil { | ||
| // Swati: need better error handling here | ||
| log.Fatalf("Failed to start server: %v", err) | ||
| } | ||
| klog.Infof("Started gpu-mutating-webhook server at %s", server.Addr) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Updated code test:
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.
Could we construct unit tests that exercise the same logic?