craigcondit commented on code in PR #469:
URL: https://github.com/apache/yunikorn-k8shim/pull/469#discussion_r1025812734


##########
pkg/plugin/admissioncontrollers/webhook/admission_controller.go:
##########
@@ -212,39 +230,99 @@ func (c *admissionController) processWorkload(req 
*admissionv1.AdmissionRequest)
                return admissionResponseBuilder(uid, false, err.Error(), nil)
        }
 
-       if failureResponse := c.checkUserInfoAnnotation(func() (string, bool) {
-               a, ok := annotations[userInfoAnnotation]
+       userName := req.UserInfo.Username
+       groups := req.UserInfo.Groups
+       failureResponse, userInfoSet := c.checkUserInfoAnnotation(func() 
(string, bool) {
+               a, ok := annotations[common.UserInfoAnnotation]
                return a, ok
-       }, req.UserInfo.Username, req.UserInfo.Groups, uid); failureResponse != 
nil {
+       }, userName, groups, uid)
+       if failureResponse != nil {
                return failureResponse
        }
 
+       if !userInfoSet && !c.conf.GetBypassAuth() {
+               patch, err := c.annotationHandler.GetPatchForWorkload(req, 
userName, groups)
+               if err != nil {
+                       log.Logger().Error("could not generate patch for 
workload", zap.Error(err))
+                       return admissionResponseBuilder(uid, false, 
err.Error(), nil)
+               }
+
+               patchBytes, patchErr := json.Marshal(patch)
+               if patchErr != nil {
+                       log.Logger().Error("failed to marshal patch", 
zap.Error(patchErr))
+                       return admissionResponseBuilder(uid, false, 
patchErr.Error(), nil)
+               }
+               log.Logger().Info("updating annotations on workload", 
zap.String("type", req.Kind.Kind),
+                       zap.Any("generated patch", patch))
+               return admissionResponseBuilder(uid, true, "", patchBytes)
+       }
+
        return admissionResponseBuilder(uid, true, "", nil)
 }
 
-func (c *admissionController) checkUserInfoAnnotation(getAnnotation func() 
(string, bool), userName string, groups []string, uid string) 
*admissionv1.AdmissionResponse {
-       if annotation, ok := getAnnotation(); ok && !c.conf.GetBypassAuth() {
+func (c *admissionController) processPodUpdate(req 
*admissionv1.AdmissionRequest, namespace string) *admissionv1.AdmissionResponse 
{
+       uid := string(req.UID)
+
+       var newPod v1.Pod
+       if err := json.Unmarshal(req.Object.Raw, &newPod); err != nil {
+               log.Logger().Error("unmarshal failed", zap.Error(err))
+               return admissionResponseBuilder(uid, false, err.Error(), nil)
+       }
+
+       if labelAppValue, ok := newPod.Labels[constants.LabelApp]; ok {
+               if labelAppValue == yunikornPod {
+                       log.Logger().Info("pod update - ignore yunikorn pod")
+                       return admissionResponseBuilder(uid, true, "", nil)
+               }
+       }
+
+       if !c.shouldProcessNamespace(namespace) {

Review Comment:
   I think this should be: 
     `if !c.shouldProcessNamespace(namespace) || 
!c.shouldLabelNamespace(namespace) {`
   Only labeled pods get scheduled via YuniKorn queueing, so we should exclude 
based on both process and label status.



##########
pkg/plugin/admissioncontrollers/webhook/admission_controller.go:
##########
@@ -199,11 +218,10 @@ func (c *admissionController) processPod(req 
*admissionv1.AdmissionRequest) *adm
        return admissionResponseBuilder(uid, true, "", patchBytes)
 }
 
-func (c *admissionController) processWorkload(req 
*admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
+func (c *admissionController) processWorkload(req 
*admissionv1.AdmissionRequest, namespace string) *admissionv1.AdmissionResponse 
{

Review Comment:
   The `namespace` parameter is unused here.



##########
pkg/plugin/admissioncontrollers/webhook/admission_controller.go:
##########
@@ -212,39 +230,99 @@ func (c *admissionController) processWorkload(req 
*admissionv1.AdmissionRequest)
                return admissionResponseBuilder(uid, false, err.Error(), nil)
        }
 
-       if failureResponse := c.checkUserInfoAnnotation(func() (string, bool) {
-               a, ok := annotations[userInfoAnnotation]
+       userName := req.UserInfo.Username
+       groups := req.UserInfo.Groups
+       failureResponse, userInfoSet := c.checkUserInfoAnnotation(func() 
(string, bool) {
+               a, ok := annotations[common.UserInfoAnnotation]
                return a, ok
-       }, req.UserInfo.Username, req.UserInfo.Groups, uid); failureResponse != 
nil {
+       }, userName, groups, uid)
+       if failureResponse != nil {
                return failureResponse
        }
 
+       if !userInfoSet && !c.conf.GetBypassAuth() {
+               patch, err := c.annotationHandler.GetPatchForWorkload(req, 
userName, groups)
+               if err != nil {
+                       log.Logger().Error("could not generate patch for 
workload", zap.Error(err))
+                       return admissionResponseBuilder(uid, false, 
err.Error(), nil)
+               }
+
+               patchBytes, patchErr := json.Marshal(patch)
+               if patchErr != nil {
+                       log.Logger().Error("failed to marshal patch", 
zap.Error(patchErr))
+                       return admissionResponseBuilder(uid, false, 
patchErr.Error(), nil)
+               }
+               log.Logger().Info("updating annotations on workload", 
zap.String("type", req.Kind.Kind),
+                       zap.Any("generated patch", patch))
+               return admissionResponseBuilder(uid, true, "", patchBytes)
+       }
+
        return admissionResponseBuilder(uid, true, "", nil)
 }
 
-func (c *admissionController) checkUserInfoAnnotation(getAnnotation func() 
(string, bool), userName string, groups []string, uid string) 
*admissionv1.AdmissionResponse {
-       if annotation, ok := getAnnotation(); ok && !c.conf.GetBypassAuth() {
+func (c *admissionController) processPodUpdate(req 
*admissionv1.AdmissionRequest, namespace string) *admissionv1.AdmissionResponse 
{
+       uid := string(req.UID)
+
+       var newPod v1.Pod
+       if err := json.Unmarshal(req.Object.Raw, &newPod); err != nil {
+               log.Logger().Error("unmarshal failed", zap.Error(err))
+               return admissionResponseBuilder(uid, false, err.Error(), nil)
+       }
+
+       if labelAppValue, ok := newPod.Labels[constants.LabelApp]; ok {
+               if labelAppValue == yunikornPod {
+                       log.Logger().Info("pod update - ignore yunikorn pod")
+                       return admissionResponseBuilder(uid, true, "", nil)
+               }
+       }
+
+       if !c.shouldProcessNamespace(namespace) {
+               log.Logger().Info("pod update - bypassing namespace", 
zap.String("namespace", namespace))
+               return admissionResponseBuilder(uid, true, "", nil)
+       }
+
+       pod, err := 
c.clientSet.CoreV1().Pods(newPod.Namespace).Get(ctx.Background(), newPod.Name, 
metav1.GetOptions{})

Review Comment:
   This is ugly. We're forcing a round-trip to the API server during a webhook. 
It seems as though you're only checking this to validate that the user info 
hasn't changed? If so, I'm pretty sure `AdmissionRequest` has an `OldObject` 
member that should be populated for `Update` requests.



##########
pkg/plugin/admissioncontrollers/webhook/admission_controller.go:
##########
@@ -199,11 +218,10 @@ func (c *admissionController) processPod(req 
*admissionv1.AdmissionRequest) *adm
        return admissionResponseBuilder(uid, true, "", patchBytes)
 }
 
-func (c *admissionController) processWorkload(req 
*admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
+func (c *admissionController) processWorkload(req 
*admissionv1.AdmissionRequest, namespace string) *admissionv1.AdmissionResponse 
{

Review Comment:
   Also, I think we need to exclude workloads the same way we do pods, by 
checking the namespace against (`process/bypass)Namespaces` and 
`(label/noLabel)Namespaces`. Otherwise we're affecting workloads that will not 
be scheduled by YuniKorn.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to