Skip to content

Commit

Permalink
spd: get qos Level by pod and its annotations
Browse files Browse the repository at this point in the history
Signed-off-by: linzhecheng <linzhecheng@bytedance.com>
  • Loading branch information
cheney-lin committed Mar 26, 2024
1 parent 58177dd commit f85c091
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/controller/spd/spd.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,21 @@ func (sc *SPDController) getWorkload(gvr schema.GroupVersionResource, namespace,
}

// defaultBaselinePercent returns default baseline ratio based on the qos level of workload,
// and if the configured data cannot be found, we will return 1.0,
// and if the configured data cannot be found, we will return 100,
// which signifies that the resources of this workload cannot be reclaimed to reclaimed_cores.
func (sc *SPDController) defaultBaselinePercent(workload *unstructured.Unstructured) *int32 {
annotations, err := native.GetUnstructuredTemplateAnnotations(workload)
podTemplateSpec, err := native.GetUnstructuredPodTemplateSpec(workload)
if err != nil {
general.ErrorS(err, "failed to GetUnstructuredTemplateAnnotations")
general.ErrorS(err, "failed to GetUnstructuredPodTemplate")
return pointer.Int32(100)
}
qosLevel, err := sc.qosConfig.GetQoSLevel(nil, annotations)

pod := &core.Pod{
ObjectMeta: podTemplateSpec.ObjectMeta,
Spec: podTemplateSpec.Spec,
}

qosLevel, err := sc.qosConfig.GetQoSLevel(pod, podTemplateSpec.Annotations)
if err != nil {
general.ErrorS(err, "failed to GetQoSLevel")
return pointer.Int32(100)
Expand Down
16 changes: 16 additions & 0 deletions pkg/controller/spd/spd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func TestSPDController_Run(t *testing.T) {
"workload": "sts1",
},
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"katalyst.kubewharf.io/qos_level": "dedicated_cores",
},
},
Spec: v1.PodSpec{},
},
},
},
spd: nil,
Expand All @@ -178,6 +186,14 @@ func TestSPDController_Run(t *testing.T) {
"workload": "sts1",
},
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"katalyst.kubewharf.io/qos_level": "dedicated_cores",
},
},
Spec: v1.PodSpec{},
},
},
},
wantSPD: &apiworkload.ServiceProfileDescriptor{
Expand Down
18 changes: 18 additions & 0 deletions pkg/util/native/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

v1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -35,6 +36,7 @@ import (
var (
objectFieldsForLabelSelector = []string{"spec", "selector"}
objectFieldsForTemplateAnnotations = []string{"spec", "template", "metadata", "annotations"}
objectFieldsForPodTemplate = []string{"spec", "template"}
)

// GenerateUniqObjectUIDKey generate a uniq key (including UID) for the given object.
Expand Down Expand Up @@ -189,6 +191,22 @@ func GetUnstructuredTemplateAnnotations(object *unstructured.Unstructured) (map[
return annotations, nil
}

func GetUnstructuredPodTemplateSpec(object *unstructured.Unstructured) (*v1.PodTemplateSpec, error) {
val, ok, err := unstructured.NestedFieldCopy(object.UnstructuredContent(), objectFieldsForPodTemplate...)
if err != nil {
return nil, err
} else if !ok || val == nil {
return nil, fmt.Errorf("%v doesn't exist", objectFieldsForPodTemplate)
}

podTemplateSpec := &v1.PodTemplateSpec{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(val.(map[string]interface{}), podTemplateSpec); err != nil {
return nil, err
}

return podTemplateSpec, nil
}

// VisitUnstructuredAncestors is to walk through all the ancestors of the given object,
// during this process, we will try to handle each ancestor with the given util function.
// if the handleFunc returns true, it means that we should continue the walking process
Expand Down

0 comments on commit f85c091

Please sign in to comment.