Skip to content

Commit

Permalink
webhook: improve node affinity for MultiQuotaTree
Browse files Browse the repository at this point in the history
Signed-off-by: chuanyun.lcy <chuanyun.lcy@alibaba-inc.com>
  • Loading branch information
chuanyun.lcy committed Jan 29, 2024
1 parent 1158040 commit cb3703b
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 202 deletions.
8 changes: 8 additions & 0 deletions pkg/webhook/elasticquota/plugin_check_quota_meta_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ func (c *QuotaMetaChecker) GetQuotaTopologyInfo() *QuotaTopologySummary {
}
return quotaMetaCheck.QuotaTopo.getQuotaTopologyInfo()
}

func (c *QuotaMetaChecker) GetQuotaInfo(name string) *QuotaInfo {
if c.QuotaTopo == nil {
return nil
}

return c.QuotaTopo.getQuotaInfo(name)
}
4 changes: 4 additions & 0 deletions pkg/webhook/elasticquota/quota_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,7 @@ func (qt *quotaTopology) getQuotaTopologyInfo() *QuotaTopologySummary {
}
return result
}

func (qt *quotaTopology) getQuotaInfo(name string) *QuotaInfo {
return qt.quotaInfoMap[name]
}
36 changes: 9 additions & 27 deletions pkg/webhook/pod/mutating/multi_quota_tree_affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@ import (

admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
schedulingv1alpha1 "sigs.k8s.io/scheduler-plugins/pkg/apis/scheduling/v1alpha1"

"github.com/koordinator-sh/koordinator/apis/extension"
quotav1alpha1 "github.com/koordinator-sh/koordinator/apis/quota/v1alpha1"
"github.com/koordinator-sh/koordinator/pkg/features"
utilclient "github.com/koordinator-sh/koordinator/pkg/util/client"
utilfeature "github.com/koordinator-sh/koordinator/pkg/util/feature"
"github.com/koordinator-sh/koordinator/pkg/webhook/elasticquota"
)

func (h *PodMutatingHandler) addNodeAffinityForMultiQuotaTree(ctx context.Context, req admission.Request, pod *corev1.Pod) error {
Expand All @@ -46,38 +43,23 @@ func (h *PodMutatingHandler) addNodeAffinityForMultiQuotaTree(ctx context.Contex
return nil
}

plugin := elasticquota.NewPlugin(h.Decoder, h.Client)
quotaName := extension.GetQuotaName(pod)
quota := &schedulingv1alpha1.ElasticQuota{}
if quotaName == "" {
err := h.Client.Get(ctx, types.NamespacedName{Namespace: pod.Namespace, Name: pod.Namespace}, quota)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
} else {
quotaList := &schedulingv1alpha1.ElasticQuotaList{}
err := h.Client.List(ctx, quotaList, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", quotaName),
}, utilclient.DisableDeepCopy)
if err != nil {
return err
}
if len(quotaList.Items) == 0 {
return nil
}
quota = &quotaList.Items[0]
quotaName = pod.Namespace
}

treeID := extension.GetQuotaTreeID(quota)
if treeID == "" {
info := plugin.GetQuotaInfo(quotaName)
if info == nil {
return nil
}
if info.TreeID == "" {
return nil
}

profileList := &quotav1alpha1.ElasticQuotaProfileList{}
err := h.Client.List(ctx, profileList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{extension.LabelQuotaTreeID: treeID}),
LabelSelector: labels.SelectorFromSet(map[string]string{extension.LabelQuotaTreeID: info.TreeID}),
}, utilclient.DisableDeepCopy)
if err != nil {
return err
Expand Down
Loading

0 comments on commit cb3703b

Please sign in to comment.