Skip to content

Commit ab19627

Browse files
ZiMengShengwangjianyu.wjy
andauthored
koord-manager: ClusterColocationProfile support grayscale (#1322)
Signed-off-by: wangjianyu.wjy <wangjianyu.wjy@alibaba-inc.com> Co-authored-by: wangjianyu.wjy <wangjianyu.wjy@alibaba-inc.com>
1 parent 07edd75 commit ab19627

File tree

5 files changed

+551
-70
lines changed

5 files changed

+551
-70
lines changed

apis/config/v1alpha1/cluster_colocation_profile_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1alpha1
1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/apimachinery/pkg/util/intstr"
2223
)
2324

2425
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
@@ -38,6 +39,10 @@ type ClusterColocationProfileSpec struct {
3839
// +optional
3940
Selector *metav1.LabelSelector `json:"selector,omitempty"`
4041

42+
// Probability indicates profile will make effect with a probability.
43+
// +optional
44+
Probability *intstr.IntOrString `json:"probability,omitempty"`
45+
4146
// QoSClass describes the type of Koordinator QoS that the Pod is running.
4247
// The value will be injected into Pod as label koordinator.sh/qosClass.
4348
// Options are LSE/LSR/LS/BE/SYSTEM.

apis/config/v1alpha1/zz_generated.deepcopy.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/config.koordinator.sh_clustercolocationprofiles.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ spec:
119119
- koord-batch
120120
- koord-free
121121
type: string
122+
probability:
123+
anyOf:
124+
- type: integer
125+
- type: string
126+
description: Probability indicates profile will make effect with a
127+
probability.
128+
x-kubernetes-int-or-string: true
122129
qosClass:
123130
description: QoSClass describes the type of Koordinator QoS that the
124131
Pod is running. The value will be injected into Pod as label koordinator.sh/qosClass.

pkg/webhook/pod/mutating/cluster_colocation_profile.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"math/rand"
2324

2425
admissionv1 "k8s.io/api/admission/v1"
2526
corev1 "k8s.io/api/core/v1"
@@ -28,6 +29,7 @@ import (
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/labels"
3031
"k8s.io/apimachinery/pkg/types"
32+
"k8s.io/apimachinery/pkg/util/intstr"
3133
"k8s.io/apimachinery/pkg/util/strategicpatch"
3234
"k8s.io/klog/v2"
3335
"k8s.io/utils/pointer"
@@ -39,6 +41,10 @@ import (
3941
utilclient "github.com/koordinator-sh/koordinator/pkg/util/client"
4042
)
4143

44+
var (
45+
randIntnFn = rand.Intn
46+
)
47+
4248
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch
4349
// +kubebuilder:rbac:groups=config.koordinator.sh,resources=clustercolocationprofiles,verbs=get;list;watch
4450

@@ -79,7 +85,15 @@ func (h *PodMutatingHandler) clusterColocationProfileMutatingPod(ctx context.Con
7985
}
8086

8187
for _, profile := range matchedProfiles {
82-
err := h.doMutateByColocationProfile(ctx, pod, profile)
88+
skip, err := shouldSkipProfile(profile)
89+
if err != nil {
90+
return err
91+
}
92+
if skip {
93+
klog.V(4).Infof("skip mutate Pod %s/%s by clusterColocationProfile %s", pod.Namespace, pod.Name, profile.Name)
94+
continue
95+
}
96+
err = h.doMutateByColocationProfile(ctx, pod, profile)
8397
if err != nil {
8498
return err
8599
}
@@ -125,6 +139,18 @@ func (h *PodMutatingHandler) matchObjectSelector(pod, oldPod *corev1.Pod, object
125139
return matched, nil
126140
}
127141

142+
func shouldSkipProfile(profile *configv1alpha1.ClusterColocationProfile) (bool, error) {
143+
percent := 100
144+
if profile.Spec.Probability != nil {
145+
var err error
146+
percent, err = intstr.GetScaledValueFromIntOrPercent(profile.Spec.Probability, 100, false)
147+
if err != nil {
148+
return false, err
149+
}
150+
}
151+
return percent == 0 || (percent != 100 && randIntnFn(100) > percent), nil
152+
}
153+
128154
func (h *PodMutatingHandler) doMutateByColocationProfile(ctx context.Context, pod *corev1.Pod, profile *configv1alpha1.ClusterColocationProfile) error {
129155
if len(profile.Spec.Labels) > 0 {
130156
if pod.Labels == nil {

0 commit comments

Comments
 (0)