|
@@ -163,7 +163,7 @@ func QueryPrometheus(
|
|
|
appLabel = ksmSvc.ObjectMeta.Labels["app.kubernetes.io/instance"]
|
|
appLabel = ksmSvc.ObjectMeta.Labels["app.kubernetes.io/instance"]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- query = createHPACurrentReplicasQuery(metricName, opts.Name, opts.Namespace, appLabel, hpaMetricName)
|
|
|
|
|
|
|
+ query = createHPACurrentReplicasQuery(metricName, opts.Name, opts.Namespace, "namespace", appLabel, hpaMetricName)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if opts.ShouldSum {
|
|
if opts.ShouldSum {
|
|
@@ -295,7 +295,7 @@ func getSelectionRegex(kind, name string) (string, error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func createHPAAbsoluteCPUThresholdQuery(cpuMetricName, metricName, podSelectionRegex, hpaName, namespace, appLabel, hpaMetricName string) string {
|
|
func createHPAAbsoluteCPUThresholdQuery(cpuMetricName, metricName, podSelectionRegex, hpaName, namespace, appLabel, hpaMetricName string) string {
|
|
|
- kubeMetricsPodSelector := getKubeMetricsPodSelector(podSelectionRegex, namespace)
|
|
|
|
|
|
|
+ kubeMetricsPodSelector := getKubeMetricsPodSelector(podSelectionRegex, namespace, "namespace")
|
|
|
|
|
|
|
|
kubeMetricsHPASelector := fmt.Sprintf(
|
|
kubeMetricsHPASelector := fmt.Sprintf(
|
|
|
`%s="%s",namespace="%s",metric_name="cpu",metric_target_type="utilization"`,
|
|
`%s="%s",namespace="%s",metric_name="cpu",metric_target_type="utilization"`,
|
|
@@ -334,57 +334,89 @@ func createHPAAbsoluteCPUThresholdQuery(cpuMetricName, metricName, podSelectionR
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func createHPAAbsoluteMemoryThresholdQuery(memMetricName, metricName, podSelectionRegex, hpaName, namespace, appLabel, hpaMetricName string) string {
|
|
func createHPAAbsoluteMemoryThresholdQuery(memMetricName, metricName, podSelectionRegex, hpaName, namespace, appLabel, hpaMetricName string) string {
|
|
|
- kubeMetricsPodSelector := getKubeMetricsPodSelector(podSelectionRegex, namespace)
|
|
|
|
|
|
|
+ kubeMetricsPodSelectorOne := getKubeMetricsPodSelector(podSelectionRegex, namespace, "namespace")
|
|
|
|
|
+ kubeMetricsPodSelectorTwo := getKubeMetricsPodSelector(podSelectionRegex, namespace, "exported_namespace")
|
|
|
|
|
|
|
|
- kubeMetricsHPASelector := fmt.Sprintf(
|
|
|
|
|
|
|
+ kubeMetricsHPASelectorOne := fmt.Sprintf(
|
|
|
`%s="%s",namespace="%s",metric_name="memory",metric_target_type="utilization"`,
|
|
`%s="%s",namespace="%s",metric_name="memory",metric_target_type="utilization"`,
|
|
|
hpaMetricName,
|
|
hpaMetricName,
|
|
|
hpaName,
|
|
hpaName,
|
|
|
namespace,
|
|
namespace,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ kubeMetricsHPASelectorTwo := fmt.Sprintf(
|
|
|
|
|
+ `%s="%s",exported_namespace="%s",metric_name="memory",metric_target_type="utilization"`,
|
|
|
|
|
+ hpaMetricName,
|
|
|
|
|
+ hpaName,
|
|
|
|
|
+ namespace,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
if memMetricName == "kube_pod_container_resource_requests" {
|
|
if memMetricName == "kube_pod_container_resource_requests" {
|
|
|
- kubeMetricsPodSelector += `,resource="memory",unit="byte"`
|
|
|
|
|
|
|
+ kubeMetricsPodSelectorOne += `,resource="memory",unit="byte"`
|
|
|
|
|
+ kubeMetricsPodSelectorTwo += `,resource="memory",unit="byte"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// the kube-state-metrics queries are less prone to error if the field app_kubernetes_io_instance is matched
|
|
// the kube-state-metrics queries are less prone to error if the field app_kubernetes_io_instance is matched
|
|
|
// as well
|
|
// as well
|
|
|
if appLabel != "" {
|
|
if appLabel != "" {
|
|
|
- kubeMetricsPodSelector += fmt.Sprintf(`,app_kubernetes_io_instance="%s"`, appLabel)
|
|
|
|
|
- kubeMetricsHPASelector += fmt.Sprintf(`,app_kubernetes_io_instance="%s"`, appLabel)
|
|
|
|
|
|
|
+ kubeMetricsPodSelectorOne += fmt.Sprintf(`,app_kubernetes_io_instance="%s"`, appLabel)
|
|
|
|
|
+ kubeMetricsPodSelectorTwo += fmt.Sprintf(`,app_kubernetes_io_instance="%s"`, appLabel)
|
|
|
|
|
+ kubeMetricsHPASelectorOne += fmt.Sprintf(`,app_kubernetes_io_instance="%s"`, appLabel)
|
|
|
|
|
+ kubeMetricsHPASelectorTwo += fmt.Sprintf(`,app_kubernetes_io_instance="%s"`, appLabel)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- requestMem := fmt.Sprintf(
|
|
|
|
|
|
|
+ requestMemOne := fmt.Sprintf(
|
|
|
`sum by (%s) (label_replace(%s{%s},"%s", "%s", "", ""))`,
|
|
`sum by (%s) (label_replace(%s{%s},"%s", "%s", "", ""))`,
|
|
|
hpaMetricName,
|
|
hpaMetricName,
|
|
|
memMetricName,
|
|
memMetricName,
|
|
|
- kubeMetricsPodSelector,
|
|
|
|
|
|
|
+ kubeMetricsPodSelectorOne,
|
|
|
hpaMetricName,
|
|
hpaMetricName,
|
|
|
hpaName,
|
|
hpaName,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- targetMemUtilThreshold := fmt.Sprintf(
|
|
|
|
|
|
|
+ targetMemUtilThresholdOne := fmt.Sprintf(
|
|
|
`%s{%s} / 100`,
|
|
`%s{%s} / 100`,
|
|
|
metricName,
|
|
metricName,
|
|
|
- kubeMetricsHPASelector,
|
|
|
|
|
|
|
+ kubeMetricsHPASelectorOne,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- return fmt.Sprintf(`%s * on(%s) %s`, requestMem, hpaMetricName, targetMemUtilThreshold)
|
|
|
|
|
|
|
+ requestMemTwo := fmt.Sprintf(
|
|
|
|
|
+ `sum by (%s) (label_replace(%s{%s},"%s", "%s", "", ""))`,
|
|
|
|
|
+ hpaMetricName,
|
|
|
|
|
+ memMetricName,
|
|
|
|
|
+ kubeMetricsPodSelectorTwo,
|
|
|
|
|
+ hpaMetricName,
|
|
|
|
|
+ hpaName,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ targetMemUtilThresholdTwo := fmt.Sprintf(
|
|
|
|
|
+ `%s{%s} / 100`,
|
|
|
|
|
+ metricName,
|
|
|
|
|
+ kubeMetricsHPASelectorTwo,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ return fmt.Sprintf(
|
|
|
|
|
+ `(%s * on(%s) %s) or (%s * on(%s) %s)`,
|
|
|
|
|
+ requestMemOne, hpaMetricName, targetMemUtilThresholdOne,
|
|
|
|
|
+ requestMemTwo, hpaMetricName, targetMemUtilThresholdTwo,
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func getKubeMetricsPodSelector(podSelectionRegex, namespace string) string {
|
|
|
|
|
|
|
+func getKubeMetricsPodSelector(podSelectionRegex, namespace, namespaceLabel string) string {
|
|
|
return fmt.Sprintf(
|
|
return fmt.Sprintf(
|
|
|
- `pod=~"%s",namespace="%s",container!="POD",container!=""`,
|
|
|
|
|
|
|
+ `pod=~"%s",%s="%s",container!="POD",container!=""`,
|
|
|
podSelectionRegex,
|
|
podSelectionRegex,
|
|
|
|
|
+ namespaceLabel,
|
|
|
namespace,
|
|
namespace,
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func createHPACurrentReplicasQuery(metricName, hpaName, namespace, appLabel, hpaMetricName string) string {
|
|
|
|
|
|
|
+func createHPACurrentReplicasQuery(metricName, hpaName, namespace, namespaceLabel, appLabel, hpaMetricName string) string {
|
|
|
kubeMetricsHPASelector := fmt.Sprintf(
|
|
kubeMetricsHPASelector := fmt.Sprintf(
|
|
|
- `%s="%s",namespace="%s"`,
|
|
|
|
|
|
|
+ `%s="%s",%s="%s"`,
|
|
|
hpaMetricName,
|
|
hpaMetricName,
|
|
|
hpaName,
|
|
hpaName,
|
|
|
|
|
+ namespaceLabel,
|
|
|
namespace,
|
|
namespace,
|
|
|
)
|
|
)
|
|
|
|
|
|