wilfred-s commented on code in PR #900:
URL: https://github.com/apache/yunikorn-k8shim/pull/900#discussion_r1732088469


##########
pkg/common/utils/utils.go:
##########
@@ -213,6 +213,66 @@ func GetApplicationIDFromPod(pod *v1.Pod) string {
        return GenerateApplicationID(pod.Namespace, 
conf.GetSchedulerConf().GenerateUniqueAppIds, string(pod.UID))
 }
 
+func CheckAppIdInPod(pod *v1.Pod) error {
+       appIdLabelKeys := []string{
+               constants.CanonicalLabelApplicationID,
+               constants.SparkLabelAppID,
+               constants.LabelApplicationID,
+       }
+       appIdAnnotationKeys := []string{
+               constants.AnnotationApplicationID,
+       }
+       if err := ValidatePodLabelAnnotation(pod, appIdLabelKeys, 
appIdAnnotationKeys); err != nil {
+               return fmt.Errorf("pod has inconsistent application ID in 
labels and annotations. %w", err)
+       }
+       return nil
+}
+
+func CheckQueueNameInPod(pod *v1.Pod) error {
+       queueLabelKeys := []string{
+               constants.CanonicalLabelQueueName,
+               constants.LabelQueueName,
+       }
+       queueAnnotationKeys := []string{
+               constants.AnnotationQueueName,
+       }

Review Comment:
   same for these two as above



##########
pkg/common/utils/utils_test.go:
##########
@@ -723,6 +724,212 @@ func TestGetApplicationIDFromPod(t *testing.T) {
        }
 }
 
+func TestCheckAppIdInPod(t *testing.T) {
+       mismatchError := errors.New("pod has inconsistent application ID")
+       tests := []struct {
+               name     string
+               pod      *v1.Pod
+               expected error
+       }{
+               {
+                       name: "consistent app ID",
+                       pod: &v1.Pod{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Labels: map[string]string{
+                                               
constants.CanonicalLabelApplicationID: "app-123",
+                                               constants.SparkLabelAppID:      
       "app-123",
+                                               constants.LabelApplicationID:   
       "app-123",
+                                       },
+                                       Annotations: map[string]string{
+                                               
constants.AnnotationApplicationID: "app-123",
+                                       },
+                               },
+                       },
+                       expected: nil,
+               },
+               {
+                       name: "inconsistent app ID in labels",
+                       pod: &v1.Pod{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Labels: map[string]string{
+                                               
constants.CanonicalLabelApplicationID: "app-123",
+                                               constants.SparkLabelAppID:      
       "app-456",
+                                       },
+                               },
+                       },
+                       expected: mismatchError,
+               },
+               {
+                       name: "inconsistent app ID between label and 
annotation",
+                       pod: &v1.Pod{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Labels: map[string]string{
+                                               
constants.CanonicalLabelApplicationID: "app-123",
+                                       },
+                                       Annotations: map[string]string{
+                                               
constants.AnnotationApplicationID: "app-456",
+                                       },
+                               },
+                       },
+                       expected: mismatchError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       err := CheckAppIdInPod(tt.pod)
+                       if err != nil {
+                               assert.ErrorContains(t, err, 
tt.expected.Error())
+                       } else {
+                               assert.NilError(t, err)
+                       }
+               })
+       }
+}
+
+func TestCheckQueueNameInPod(t *testing.T) {
+       mismatchError := errors.New("pod has inconsistent queue name")
+       tests := []struct {
+               name     string
+               pod      *v1.Pod
+               expected error
+       }{
+               {
+                       name: "consistent queue name",
+                       pod: &v1.Pod{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Labels: map[string]string{
+                                               
constants.CanonicalLabelQueueName: "root.a",
+                                               constants.LabelQueueName:       
   "root.a",
+                                       },
+                                       Annotations: map[string]string{
+                                               constants.AnnotationQueueName: 
"root.a",
+                                       },
+                               },
+                       },
+                       expected: nil,
+               },
+               {
+                       name: "inconsistent app ID in labels",
+                       pod: &v1.Pod{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Labels: map[string]string{
+                                               
constants.CanonicalLabelQueueName: "root.a",
+                                               constants.LabelQueueName:       
   "root.b",
+                                       },
+                               },
+                       },
+                       expected: mismatchError,
+               },
+               {
+                       name: "inconsistent app ID between label and 
annotation",
+                       pod: &v1.Pod{
+                               ObjectMeta: metav1.ObjectMeta{
+                                       Labels: map[string]string{
+                                               
constants.CanonicalLabelQueueName: "root.a",
+                                       },
+                                       Annotations: map[string]string{
+                                               constants.AnnotationQueueName: 
"root.b",
+                                       },
+                               },
+                       },
+                       expected: mismatchError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       err := CheckAppIdInPod(tt.pod)

Review Comment:
   I think this needs to be `CheckQueueNameInPod()` that would also fix the 
coverage issue



##########
pkg/common/utils/utils.go:
##########
@@ -213,6 +213,66 @@ func GetApplicationIDFromPod(pod *v1.Pod) string {
        return GenerateApplicationID(pod.Namespace, 
conf.GetSchedulerConf().GenerateUniqueAppIds, string(pod.UID))
 }
 
+func CheckAppIdInPod(pod *v1.Pod) error {
+       appIdLabelKeys := []string{
+               constants.CanonicalLabelApplicationID,
+               constants.SparkLabelAppID,
+               constants.LabelApplicationID,
+       }
+       appIdAnnotationKeys := []string{
+               constants.AnnotationApplicationID,
+       }

Review Comment:
   These should be treated as immutable and always the same. The way we declare 
them now creates new objects and drops them on every call. We should pull them 
outside of the function so we have only one and document them .



-- 
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