Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apis/config/v1alpha1/cluster_colocation_profile_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
)

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

// Probability indicates profile will make effect with a probability.
// +optional
Probability *intstr.IntOrString `json:"probability,omitempty"`

// QoSClass describes the type of Koordinator QoS that the Pod is running.
// The value will be injected into Pod as label koordinator.sh/qosClass.
// Options are LSE/LSR/LS/BE/SYSTEM.
Expand Down
6 changes: 6 additions & 0 deletions apis/config/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ spec:
- koord-batch
- koord-free
type: string
probability:
anyOf:
- type: integer
- type: string
description: Probability indicates profile will make effect with a
probability.
x-kubernetes-int-or-string: true
qosClass:
description: QoSClass describes the type of Koordinator QoS that the
Pod is running. The value will be injected into Pod as label koordinator.sh/qosClass.
Expand Down
28 changes: 27 additions & 1 deletion pkg/webhook/pod/mutating/cluster_colocation_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"

admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -28,6 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
Expand All @@ -39,6 +41,10 @@ import (
utilclient "github.com/koordinator-sh/koordinator/pkg/util/client"
)

var (
randIntnFn = rand.Intn
)

// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch
// +kubebuilder:rbac:groups=config.koordinator.sh,resources=clustercolocationprofiles,verbs=get;list;watch

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

for _, profile := range matchedProfiles {
err := h.doMutateByColocationProfile(ctx, pod, profile)
skip, err := shouldSkipProfile(profile)
if err != nil {
return err
}
if skip {
klog.V(4).Infof("skip mutate Pod %s/%s by clusterColocationProfile %s", pod.Namespace, pod.Name, profile.Name)
continue
}
err = h.doMutateByColocationProfile(ctx, pod, profile)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,6 +139,18 @@ func (h *PodMutatingHandler) matchObjectSelector(pod, oldPod *corev1.Pod, object
return matched, nil
}

func shouldSkipProfile(profile *configv1alpha1.ClusterColocationProfile) (bool, error) {
percent := 100
if profile.Spec.Probability != nil {
var err error
percent, err = intstr.GetScaledValueFromIntOrPercent(profile.Spec.Probability, 100, false)
if err != nil {
return false, err
}
}
return percent == 0 || (percent != 100 && randIntnFn(100) > percent), nil
}

func (h *PodMutatingHandler) doMutateByColocationProfile(ctx context.Context, pod *corev1.Pod, profile *configv1alpha1.ClusterColocationProfile) error {
if len(profile.Spec.Labels) > 0 {
if pod.Labels == nil {
Expand Down
Loading