Add port name and custom metric name to autoscaling utils

Signed-off-by: carlory <baofa.fan@daocloud.io>
This commit is contained in:
carlory 2025-10-10 10:55:37 +08:00
parent 8e3d76317e
commit 1dd82ce2a4
No known key found for this signature in database
GPG key ID: A045E1459D5F2869

View file

@ -60,6 +60,7 @@ const (
dynamicRequestSizeInMegabytes = 100
dynamicRequestSizeCustomMetric = 10
port = 80
portName = "http"
targetPort = 8080
sidecarTargetPort = 8081
timeoutRC = 120 * time.Second
@ -113,6 +114,7 @@ type ResourceConsumer struct {
dynamicClient dynamic.Interface
resourceClient dynamic.ResourceInterface
scaleClient scaleclient.ScalesGetter
customMetricName string
cpu chan int
mem chan int
customMetric chan int
@ -131,7 +133,7 @@ type ResourceConsumer struct {
// NewDynamicResourceConsumer is a wrapper to create a new dynamic ResourceConsumer
func NewDynamicResourceConsumer(ctx context.Context, name, nsName string, kind schema.GroupVersionKind, replicas, initCPUTotal, initMemoryTotal, initCustomMetric int, cpuLimit, memLimit int64, clientset clientset.Interface, scaleClient scaleclient.ScalesGetter, enableSidecar SidecarStatusType, sidecarType SidecarWorkloadType, podResources *v1.ResourceRequirements) *ResourceConsumer {
return newResourceConsumer(ctx, name, nsName, kind, replicas, initCPUTotal, initMemoryTotal, initCustomMetric, dynamicConsumptionTimeInSeconds,
return NewResourceConsumer(ctx, name, nsName, kind, replicas, customMetricName, initCPUTotal, initMemoryTotal, initCustomMetric, dynamicConsumptionTimeInSeconds,
dynamicRequestSizeInMillicores, dynamicRequestSizeInMegabytes, dynamicRequestSizeCustomMetric, cpuLimit, memLimit, clientset, scaleClient, nil, nil, enableSidecar, sidecarType, podResources)
}
@ -169,7 +171,7 @@ initMemoryTotal argument is in megabytes
memLimit argument is in megabytes, memLimit is a maximum amount of memory that can be consumed by a single pod
cpuLimit argument is in millicores, cpuLimit is a maximum amount of cpu that can be consumed by a single pod
*/
func newResourceConsumer(ctx context.Context, name, nsName string, kind schema.GroupVersionKind, replicas, initCPUTotal, initMemoryTotal, initCustomMetric, consumptionTimeInSeconds, requestSizeInMillicores,
func NewResourceConsumer(ctx context.Context, name, nsName string, kind schema.GroupVersionKind, replicas int, customMetricName string, initCPUTotal, initMemoryTotal, initCustomMetric, consumptionTimeInSeconds, requestSizeInMillicores,
requestSizeInMegabytes int, requestSizeCustomMetric int, cpuLimit, memLimit int64, clientset clientset.Interface, scaleClient scaleclient.ScalesGetter, podAnnotations, serviceAnnotations map[string]string, sidecarStatus SidecarStatusType, sidecarType SidecarWorkloadType, podResources *v1.ResourceRequirements) *ResourceConsumer {
if podAnnotations == nil {
podAnnotations = make(map[string]string)
@ -211,6 +213,7 @@ func newResourceConsumer(ctx context.Context, name, nsName string, kind schema.G
scaleClient: scaleClient,
resourceClient: resourceClient,
dynamicClient: dynamicClient,
customMetricName: customMetricName,
cpu: make(chan int),
mem: make(chan int),
customMetric: make(chan int),
@ -323,13 +326,13 @@ func (rc *ResourceConsumer) makeConsumeCustomMetric(ctx context.Context) {
select {
case delta = <-rc.customMetric:
if delta != 0 {
framework.Logf("RC %s: setting bump of metric %s to %d in total", rc.name, customMetricName, delta)
framework.Logf("RC %s: setting bump of metric %s to %d in total", rc.name, rc.customMetricName, delta)
} else {
framework.Logf("RC %s: disabling consumption of custom metric %s", rc.name, customMetricName)
framework.Logf("RC %s: disabling consumption of custom metric %s", rc.name, rc.customMetricName)
}
case <-tick:
if delta != 0 {
framework.Logf("RC %s: sending request to consume %d of custom metric %s", rc.name, delta, customMetricName)
framework.Logf("RC %s: sending request to consume %d of custom metric %s", rc.name, delta, rc.customMetricName)
rc.sendConsumeCustomMetric(ctx, delta)
}
tick = time.After(rc.sleepTime)
@ -350,7 +353,7 @@ func (rc *ResourceConsumer) sendConsumeCPURequest(ctx context.Context, millicore
return err
}
req := proxyRequest.Namespace(rc.nsName).
Name(rc.controllerName).
Name(fmt.Sprintf("%s:%s", rc.controllerName, portName)).
Suffix("ConsumeCPU").
Param("millicores", strconv.Itoa(millicores)).
Param("durationSec", strconv.Itoa(rc.consumptionTimeInSeconds)).
@ -381,7 +384,7 @@ func (rc *ResourceConsumer) sendConsumeMemRequest(ctx context.Context, megabytes
return err
}
req := proxyRequest.Namespace(rc.nsName).
Name(rc.controllerName).
Name(fmt.Sprintf("%s:%s", rc.controllerName, portName)).
Suffix("ConsumeMem").
Param("megabytes", strconv.Itoa(megabytes)).
Param("durationSec", strconv.Itoa(rc.consumptionTimeInSeconds)).
@ -412,9 +415,9 @@ func (rc *ResourceConsumer) sendConsumeCustomMetric(ctx context.Context, delta i
return err
}
req := proxyRequest.Namespace(rc.nsName).
Name(rc.controllerName).
Name(fmt.Sprintf("%s:%s", rc.controllerName, portName)).
Suffix("BumpMetric").
Param("metric", customMetricName).
Param("metric", rc.customMetricName).
Param("delta", strconv.Itoa(delta)).
Param("durationSec", strconv.Itoa(rc.consumptionTimeInSeconds)).
Param("requestSizeMetrics", strconv.Itoa(rc.requestSizeCustomMetric))
@ -571,9 +574,11 @@ func createService(ctx context.Context, c clientset.Interface, name, ns string,
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: annotations,
Labels: map[string]string{"name": name},
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{{
Name: portName,
Port: port,
TargetPort: intstr.FromInt32(int32(targetPort)),
}},