Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

structural optimization #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion charts/hparecord/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ image:
httpPort: 8080
logLevel: 4
filterLabels: "app,sym-group,sym-zone"
metricsKinds: "qpm,dubboqpm,mq"
metricsKinds: "httpqpm,dubboqpm,mqlegacy"

rbac:
name: hparecord
Expand Down
24 changes: 10 additions & 14 deletions pkg/controller/controller_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

const (
appLabel = "app"

cpuName = "cpu"
)

var (
Expand All @@ -34,15 +32,14 @@ func filterExternalMetricsKind(MetricsType string) bool {
}

func (ctrl *Controller) handleMetrics(cluster string, hpa *v2beta2.HorizontalPodAutoscaler) error {
hpaName := hpa.GetName()
labels := hpa.GetLabels()

var app string
if len(labels) > 0 {
app = labels[appLabel]
}
if app == "" {
klog.Warningf("hpa: %s does not include label(app)", hpaName)
klog.Warningf("hpa: %s/%s does not include label(app)", hpa.Namespace, hpa.Name)
return nil
}

Expand All @@ -69,7 +66,7 @@ func (ctrl *Controller) handleMetrics(cluster string, hpa *v2beta2.HorizontalPod
case v2beta2.ExternalMetricSourceType:
metricsKind := convertMetricsKind(metric.External.Metric.Name)
if filterExternalMetricsKind(metricsKind) {
klog.Warningf("not supported metrics Kind: %s", metricsKind)
klog.Warningf("hpa: %s/%s not supported metrics kind: %s", hpa.Namespace, hpa.Name, metricsKind)
continue
}

Expand All @@ -80,22 +77,21 @@ func (ctrl *Controller) handleMetrics(cluster string, hpa *v2beta2.HorizontalPod
}
}
if !found {
klog.Warningf("hpa: %s has no supported metrics", hpaName)
klog.Warningf("hpa: %s/%s has no supported metrics", hpa.Namespace, hpa.Name)
return nil
}
return nil
}

func (ctrl *Controller) deleteMetrics(cluster string, hpa *v2beta2.HorizontalPodAutoscaler) error {
hpaName := hpa.GetName()
labels := hpa.GetLabels()

var app string
if len(labels) > 0 {
app = labels[appLabel]
}
if app == "" {
klog.Warningf("hpa: %s does not include label(app)", hpaName)
klog.Warningf("hpa: %s/%s does not include label(app)", hpa.Namespace, hpa.Name)
return nil
}

Expand All @@ -115,7 +111,7 @@ func (ctrl *Controller) deleteMetrics(cluster string, hpa *v2beta2.HorizontalPod
case v2beta2.ExternalMetricSourceType:
metricsKind := convertMetricsKind(metric.External.Metric.Name)
if filterExternalMetricsKind(metricsKind) {
klog.Warningf("not supported metrics Kind: %s", metricsKind)
klog.Warningf("hpa: %s/%s not supported metrics kind: %s", hpa.Namespace, hpa.Name, metricsKind)
continue
}

Expand All @@ -126,7 +122,7 @@ func (ctrl *Controller) deleteMetrics(cluster string, hpa *v2beta2.HorizontalPod
}
}
if !found {
klog.Warningf("hpa: %s has no supported metrics", hpaName)
klog.Warningf("hpa: %s/%s has no supported metrics", hpa.Namespace, hpa.Name)
return nil
}
return nil
Expand All @@ -137,7 +133,7 @@ func setResourceMetrics(resource corev1.ResourceName, metric v2beta2.MetricSpec,
targetCpuValue, currentCpuValue := calResourceMetricValue(resource, metric, status)
value := newMetricsValue(targetCpuValue, currentCpuValue)

client, err := newMetricsClient(string(resource))
client, err := newExternalMetricsClient(string(resource))
if err != nil {
return err
}
Expand All @@ -151,7 +147,7 @@ func setExternalMetrics(metricsKind string, metric v2beta2.MetricSpec, status v2
targetCpuValue, currentCpuValue := calExternalMetricValue(metricsKind, metric, status)
value := newMetricsValue(targetCpuValue, currentCpuValue)

client, err := newMetricsClient(metricsKind)
client, err := newExternalMetricsClient(metricsKind)
if err != nil {
return err
}
Expand All @@ -162,7 +158,7 @@ func setExternalMetrics(metricsKind string, metric v2beta2.MetricSpec, status v2

// deleteMetrics delete prometheus metrics
func deleteMetrics(metricsKind string, labels promLabels) error {
client, err := newMetricsClient(metricsKind)
client, err := newExternalMetricsClient(metricsKind)
if err != nil {
return err
}
Expand Down Expand Up @@ -205,7 +201,7 @@ func calExternalMetricValue(metricsKind string, metric v2beta2.MetricSpec, statu

// name: s0-QPS, keda generate it
func convertMetricsKind(name string) string {
reg, _ := regexp.Compile("^s\\d+-(.*)")
reg, _ := regexp.Compile(`^s\d+-(\w+)`)

subMatch := reg.FindStringSubmatch(name)
if len(subMatch) > 1 {
Expand Down
13 changes: 9 additions & 4 deletions pkg/controller/controller_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ func Test_convertMetricsKind(t *testing.T) {
},
{
name: "test-2",
args: args{name: "s1-DUBBO-QPM"},
want: "dubbo-qpm",
args: args{name: "s1-DUBBOQPM"},
want: "dubboqpm",
},
{
name: "test-2",
args: args{name: "s11111-DUBBO-QPM"},
want: "dubbo-qpm",
args: args{name: "s11111-DUBBO_QPM"},
want: "dubbo_qpm",
},
{
name: "test-2",
args: args{name: "s11111-cron-asia-shanghai-30xxxx-45xxxx"},
want: "cron",
},
}
for _, tt := range tests {
Expand Down
28 changes: 0 additions & 28 deletions pkg/controller/cpu_metrics.go

This file was deleted.

14 changes: 13 additions & 1 deletion pkg/controller/external_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type externalMetricsClient struct {
api.Metrics
}

func newMetricsClient(kind string) (*externalMetricsClient, error) {
func newExternalMetricsClient(kind string) (*externalMetricsClient, error) {
m, err := metrics.NewMetrics(kind, nil)
if err != nil {
return nil, err
Expand All @@ -27,3 +27,15 @@ func (c *externalMetricsClient) deletePromMetrics(label promLabels) {
c.DeleteWithLabels("target_value", label)
c.DeleteWithLabels("current_value", label)
}

type metricsValue struct {
TargetValue float64
CurrentValue float64
}

func newMetricsValue(targetValue, currentValue int64) metricsValue {
return metricsValue{
TargetValue: float64(targetValue),
CurrentValue: float64(currentValue),
}
}
14 changes: 14 additions & 0 deletions pkg/controller/hpa_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ func (c *hpaMetricsClient) deletePromMetrics(label promLabels) {
c.DeleteWithLabels("max_replicas", label)
c.DeleteWithLabels("min_replicas", label)
}

type hpaValue struct {
CurrentReplicas float64
MaxReplicas float64
MinReplicas float64
}

func newHpaValue(currentReplicas, maxReplicas, minReplicas int32) hpaValue {
return hpaValue{
CurrentReplicas: float64(currentReplicas),
MaxReplicas: float64(maxReplicas),
MinReplicas: float64(minReplicas),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,6 @@ func newPromLabels(cluster string, labels map[string]string) promLabels {
return result
}

type hpaValue struct {
CurrentReplicas float64
MaxReplicas float64
MinReplicas float64
}

func newHpaValue(currentReplicas, maxReplicas, minReplicas int32) hpaValue {
return hpaValue{
CurrentReplicas: float64(currentReplicas),
MaxReplicas: float64(maxReplicas),
MinReplicas: float64(minReplicas),
}
}

type metricsValue struct {
TargetValue float64
CurrentValue float64
}

func newMetricsValue(targetValue, currentValue int64) metricsValue {
return metricsValue{
TargetValue: float64(targetValue),
CurrentValue: float64(currentValue),
}
}

func startHTTPPrometheus(ctx context.Context) {
server := &http.Server{
Addr: ":8080",
Expand Down