Autoscaler Improvement (#64)
* update docs for network Signed-off-by: ChrisLiu <chrisliu1995@163.com>
This commit is contained in:
parent
b9af47ba84
commit
056cbe351c
|
|
@ -4,6 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
|
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/apimachinery/pkg/selection"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
@ -41,20 +44,10 @@ func (e *ExternalScaler) StreamIsActive(scaledObject *ScaledObjectRef, epsServer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ExternalScaler) GetMetricSpec(ctx context.Context, scaledObjectRef *ScaledObjectRef) (*GetMetricSpecResponse, error) {
|
func (e *ExternalScaler) GetMetricSpec(ctx context.Context, scaledObjectRef *ScaledObjectRef) (*GetMetricSpecResponse, error) {
|
||||||
name := scaledObjectRef.GetName()
|
|
||||||
ns := scaledObjectRef.GetNamespace()
|
|
||||||
gss := &gamekruiseiov1alpha1.GameServerSet{}
|
|
||||||
err := e.client.Get(ctx, types.NamespacedName{Namespace: ns, Name: name}, gss)
|
|
||||||
if err != nil {
|
|
||||||
klog.Error(err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
desireReplicas := gss.Spec.Replicas
|
|
||||||
klog.Infof("GameServerSet %s/%s TargetSize is %d", ns, name, *desireReplicas)
|
|
||||||
return &GetMetricSpecResponse{
|
return &GetMetricSpecResponse{
|
||||||
MetricSpecs: []*MetricSpec{{
|
MetricSpecs: []*MetricSpec{{
|
||||||
MetricName: "gssReplicas",
|
MetricName: "gssReplicas",
|
||||||
TargetSize: int64(*desireReplicas),
|
TargetSize: int64(1),
|
||||||
}},
|
}},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
@ -68,16 +61,33 @@ func (e *ExternalScaler) GetMetrics(ctx context.Context, metricRequest *GetMetri
|
||||||
klog.Error(err)
|
klog.Error(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
currentReplicas := gss.Status.CurrentReplicas
|
|
||||||
numWaitToBeDeleted := gss.Status.WaitToBeDeletedReplicas
|
isWaitToDelete, _ := labels.NewRequirement(gamekruiseiov1alpha1.GameServerOpsStateKey, selection.Equals, []string{string(gamekruiseiov1alpha1.WaitToDelete)})
|
||||||
if numWaitToBeDeleted == nil || currentReplicas == 0 {
|
notDeleting, _ := labels.NewRequirement(gamekruiseiov1alpha1.GameServerStateKey, selection.NotEquals, []string{string(gamekruiseiov1alpha1.Deleting)})
|
||||||
return nil, fmt.Errorf("GameServerSet %s/%s has not inited", ns, name)
|
isGssOwner, _ := labels.NewRequirement(gamekruiseiov1alpha1.GameServerOwnerGssKey, selection.Equals, []string{name})
|
||||||
|
|
||||||
|
podList := &corev1.PodList{}
|
||||||
|
err = e.client.List(ctx, podList, &client.ListOptions{
|
||||||
|
Namespace: ns,
|
||||||
|
LabelSelector: labels.NewSelector().Add(
|
||||||
|
*isWaitToDelete,
|
||||||
|
*notDeleting,
|
||||||
|
*isGssOwner,
|
||||||
|
),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
klog.Error(err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
klog.Infof("GameServerSet %s/%s desire replicas is %d", ns, name, currentReplicas-*numWaitToBeDeleted)
|
|
||||||
|
desireReplicas := int(*gss.Spec.Replicas)
|
||||||
|
numWaitToBeDeleted := len(podList.Items)
|
||||||
|
|
||||||
|
klog.Infof("GameServerSet %s/%s desire replicas is %d", ns, name, desireReplicas-numWaitToBeDeleted)
|
||||||
return &GetMetricsResponse{
|
return &GetMetricsResponse{
|
||||||
MetricValues: []*MetricValue{{
|
MetricValues: []*MetricValue{{
|
||||||
MetricName: "gssReplicas",
|
MetricName: "gssReplicas",
|
||||||
MetricValue: int64(currentReplicas - *numWaitToBeDeleted),
|
MetricValue: int64(desireReplicas - numWaitToBeDeleted),
|
||||||
}},
|
}},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue