github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/util/qos.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"encoding/json"
    21  	"sync"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/klog/v2"
    25  
    26  	"github.com/kubewharf/katalyst-api/pkg/consts"
    27  	"github.com/kubewharf/katalyst-core/pkg/config/generic"
    28  )
    29  
    30  var (
    31  	qosConfig        *generic.QoSConfiguration
    32  	qosConfigSetOnce sync.Once
    33  )
    34  
    35  func SetQoSConfig(config *generic.QoSConfiguration) {
    36  	qosConfigSetOnce.Do(func() {
    37  		qosConfig = config
    38  	})
    39  }
    40  
    41  func IsReclaimedPod(pod *v1.Pod) bool {
    42  	ok, _ := qosConfig.CheckReclaimedQoSForPod(pod)
    43  	return ok
    44  }
    45  
    46  func IsDedicatedPod(pod *v1.Pod) bool {
    47  	ok, _ := qosConfig.CheckDedicatedQoSForPod(pod)
    48  	return ok
    49  }
    50  
    51  func IsNumaBinding(pod *v1.Pod) bool {
    52  	if pod == nil {
    53  		return false
    54  	}
    55  
    56  	enhancementKey, ok := pod.Annotations[consts.PodAnnotationMemoryEnhancementKey]
    57  	if !ok {
    58  		return false
    59  	}
    60  	return memoryEnhancement(
    61  		enhancementKey,
    62  		consts.PodAnnotationMemoryEnhancementNumaBinding,
    63  		consts.PodAnnotationMemoryEnhancementNumaBindingEnable)
    64  }
    65  
    66  func IsExclusive(pod *v1.Pod) bool {
    67  	if pod == nil {
    68  		return false
    69  	}
    70  
    71  	enhancementKey, ok := pod.Annotations[consts.PodAnnotationMemoryEnhancementKey]
    72  	if !ok {
    73  		return false
    74  	}
    75  	return memoryEnhancement(
    76  		enhancementKey,
    77  		consts.PodAnnotationMemoryEnhancementNumaExclusive,
    78  		consts.PodAnnotationMemoryEnhancementNumaExclusiveEnable)
    79  }
    80  
    81  func memoryEnhancement(enhancementStr string, key, value string) bool {
    82  	if key == "" {
    83  		return true
    84  	}
    85  	if enhancementStr == "" {
    86  		return false
    87  	}
    88  
    89  	var enhancement map[string]string
    90  	if err := json.Unmarshal([]byte(enhancementStr), &enhancement); err != nil {
    91  		klog.Errorf("failed to unmarshal %s: %v", enhancementStr, err)
    92  		return false
    93  	}
    94  
    95  	enable, ok := enhancement[key]
    96  	if !ok {
    97  		return false
    98  	}
    99  
   100  	return enable == value
   101  }