This is an automated email from the ASF dual-hosted git repository.

zhuqi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/yunikorn-k8shim.git


The following commit(s) were added to refs/heads/master by this push:
     new ecb651e6 [YUNIKORN-1697] [shim] Make namespace annotation to support 
max applications update. (#885)
ecb651e6 is described below

commit ecb651e665f6830eea6df35905b09a9cf00b0fdf
Author: qzhu <[email protected]>
AuthorDate: Fri Aug 2 11:09:08 2024 +0800

    [YUNIKORN-1697] [shim] Make namespace annotation to support max 
applications update. (#885)
    
    fix lint
    Address comments
    Fix golint
    
    Closes: #885
    
    Signed-off-by: qzhu <[email protected]>
---
 pkg/cache/context.go              |  8 +++++++
 pkg/cache/context_test.go         |  7 ++++++
 pkg/common/constants/constants.go |  3 +++
 pkg/common/utils/utils.go         | 21 +++++++++++++++++
 pkg/common/utils/utils_test.go    | 47 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 86 insertions(+)

diff --git a/pkg/cache/context.go b/pkg/cache/context.go
index 244b7856..c608b98e 100644
--- a/pkg/cache/context.go
+++ b/pkg/cache/context.go
@@ -934,6 +934,8 @@ func (ctx *Context) notifyTaskComplete(appID, taskID 
string) {
 // adds the following tags to the request based on annotations (if exist):
 //   - namespace.resourcequota
 //   - namespace.parentqueue
+//   - namespace.resourceguaranteed
+//   - namespace.resourcemaxapps
 func (ctx *Context) updateApplicationTags(request *AddApplicationRequest, 
namespace string) {
        namespaceObj := ctx.getNamespaceObject(namespace)
        if namespaceObj == nil {
@@ -955,6 +957,12 @@ func (ctx *Context) updateApplicationTags(request 
*AddApplicationRequest, namesp
                }
        }
 
+       // add maxApps resource info as an app tag
+       maxApps := utils.GetNamespaceMaxAppsFromAnnotation(namespaceObj)
+       if maxApps != "" {
+               request.Metadata.Tags[siCommon.AppTagNamespaceResourceMaxApps] 
= maxApps
+       }
+
        // add parent queue info as an app tag
        parentQueue := utils.GetNameSpaceAnnotationValue(namespaceObj, 
constants.AnnotationParentQueue)
        if parentQueue != "" {
diff --git a/pkg/cache/context_test.go b/pkg/cache/context_test.go
index afc44701..c4802d4a 100644
--- a/pkg/cache/context_test.go
+++ b/pkg/cache/context_test.go
@@ -1522,6 +1522,7 @@ func TestAddApplicationsWithTags(t *testing.T) {
                                constants.NamespaceQuota:                 
"{\"cpu\": \"1\", \"memory\": \"256M\", \"nvidia.com/gpu\": \"1\"}",
                                constants.DomainYuniKorn + "parentqueue": 
"root.test",
                                constants.NamespaceGuaranteed:            
"{\"cpu\": \"1\", \"memory\": \"256M\", \"nvidia.com/gpu\": \"1\"}",
+                               constants.NamespaceMaxApps:               
"1000",
                        },
                },
        }
@@ -1610,6 +1611,12 @@ func TestAddApplicationsWithTags(t *testing.T) {
                t.Fatalf("resource parsing failed")
        }
 
+       maxApps, ok := 
request.Metadata.Tags[siCommon.AppTagNamespaceResourceMaxApps]
+       if !ok {
+               t.Fatalf("max apps tag is not updated from the namespace")
+       }
+       assert.Equal(t, maxApps, "1000")
+
        parentQueue, ok := 
request.Metadata.Tags[constants.AppTagNamespaceParentQueue]
        if !ok {
                t.Fatalf("parent queue tag is not updated from the namespace")
diff --git a/pkg/common/constants/constants.go 
b/pkg/common/constants/constants.go
index a6968e58..0e6f00ba 100644
--- a/pkg/common/constants/constants.go
+++ b/pkg/common/constants/constants.go
@@ -97,6 +97,9 @@ const NamespaceQuota = DomainYuniKorn + "namespace.quota"
 // NamespaceGuaranteed Namespace Guaranteed
 const NamespaceGuaranteed = DomainYuniKorn + "namespace.guaranteed"
 
+// NamespaceMaxApps Namespace Max Apps
+const NamespaceMaxApps = DomainYuniKorn + "namespace.maxApps"
+
 // AnnotationAllowPreemption set on PriorityClass, opt out of preemption for 
pods with this priority class
 const AnnotationAllowPreemption = DomainYuniKorn + "allow-preemption"
 
diff --git a/pkg/common/utils/utils.go b/pkg/common/utils/utils.go
index eb9f7120..28118997 100644
--- a/pkg/common/utils/utils.go
+++ b/pkg/common/utils/utils.go
@@ -214,6 +214,27 @@ func GetNamespaceGuaranteedFromAnnotation(namespaceObj 
*v1.Namespace) *si.Resour
        return nil
 }
 
+// get namespace max apps from namespace annotation
+func GetNamespaceMaxAppsFromAnnotation(namespaceObj *v1.Namespace) string {
+       if maxApps := GetNameSpaceAnnotationValue(namespaceObj, 
constants.NamespaceMaxApps); maxApps != "" {
+               numMaxApp, err := strconv.Atoi(maxApps)
+               if err != nil {
+                       log.Log(log.ShimUtils).Warn("Unable to process 
namespace.maxApps annotation",
+                               zap.String("namespace", namespaceObj.Name),
+                               zap.String("namespace.maxApps is", maxApps))
+                       return ""
+               }
+               if numMaxApp < 0 {
+                       log.Log(log.ShimUtils).Warn("Invalid value for 
namespace.maxApps annotation",
+                               zap.String("namespace", namespaceObj.Name),
+                               zap.String("namespace.maxApps is", maxApps))
+                       return ""
+               }
+               return maxApps
+       }
+       return ""
+}
+
 func GetNamespaceQuotaFromAnnotation(namespaceObj *v1.Namespace) *si.Resource {
        // retrieve resource quota info from annotations
        cpuQuota := GetNameSpaceAnnotationValue(namespaceObj, 
constants.CPUQuota)
diff --git a/pkg/common/utils/utils_test.go b/pkg/common/utils/utils_test.go
index adb07214..0b20f1a9 100644
--- a/pkg/common/utils/utils_test.go
+++ b/pkg/common/utils/utils_test.go
@@ -331,6 +331,53 @@ func TestGetNamespaceGuaranteedFromAnnotation(t 
*testing.T) {
        }
 }
 
+func TestGetNamespaceMaxAppsFromAnnotation(t *testing.T) {
+       testCases := []struct {
+               namespace      *v1.Namespace
+               expectedMaxApp string
+       }{
+               {&v1.Namespace{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "test",
+                               Namespace: "test",
+                       },
+               }, ""},
+               {&v1.Namespace{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "test",
+                               Namespace: "test",
+                               Annotations: map[string]string{
+                                       constants.NamespaceMaxApps: "5",
+                               },
+                       },
+               }, "5"},
+               {&v1.Namespace{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "test",
+                               Namespace: "test",
+                               Annotations: map[string]string{
+                                       constants.NamespaceMaxApps: "-5",
+                               },
+                       },
+               }, ""},
+               {&v1.Namespace{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "test",
+                               Namespace: "test",
+                               Annotations: map[string]string{
+                                       constants.NamespaceMaxApps: "error",
+                               },
+                       },
+               }, ""},
+       }
+       for _, tc := range testCases {
+               t.Run(fmt.Sprintf("namespace: %v", tc.namespace), func(t 
*testing.T) {
+                       maxApp := 
GetNamespaceMaxAppsFromAnnotation(tc.namespace)
+                       assert.Equal(t, maxApp, tc.expectedMaxApp)
+               })
+       }
+}
+
 func TestGetNamespaceQuotaFromAnnotationUsingNewAndOldAnnotations(t 
*testing.T) {
        testCases := []struct {
                namespace        *v1.Namespace


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to