...

Source file src/k8s.io/kubernetes/pkg/controller/podautoscaler/metrics/utilization.go

Documentation: k8s.io/kubernetes/pkg/controller/podautoscaler/metrics

     1  /*
     2  Copyright 2015 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package metrics
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  // GetResourceUtilizationRatio takes in a set of metrics, a set of matching requests,
    24  // and a target utilization percentage, and calculates the ratio of
    25  // desired to actual utilization (returning that, the actual utilization, and the raw average value)
    26  func GetResourceUtilizationRatio(metrics PodMetricsInfo, requests map[string]int64, targetUtilization int32) (utilizationRatio float64, currentUtilization int32, rawAverageValue int64, err error) {
    27  	metricsTotal := int64(0)
    28  	requestsTotal := int64(0)
    29  	numEntries := 0
    30  
    31  	for podName, metric := range metrics {
    32  		request, hasRequest := requests[podName]
    33  		if !hasRequest {
    34  			// we check for missing requests elsewhere, so assuming missing requests == extraneous metrics
    35  			continue
    36  		}
    37  
    38  		metricsTotal += metric.Value
    39  		requestsTotal += request
    40  		numEntries++
    41  	}
    42  
    43  	// if the set of requests is completely disjoint from the set of metrics,
    44  	// then we could have an issue where the requests total is zero
    45  	if requestsTotal == 0 {
    46  		return 0, 0, 0, fmt.Errorf("no metrics returned matched known pods")
    47  	}
    48  
    49  	currentUtilization = int32((metricsTotal * 100) / requestsTotal)
    50  
    51  	return float64(currentUtilization) / float64(targetUtilization), currentUtilization, metricsTotal / int64(numEntries), nil
    52  }
    53  
    54  // GetMetricUsageRatio takes in a set of metrics and a target usage value,
    55  // and calculates the ratio of desired to actual usage
    56  // (returning that and the actual usage)
    57  func GetMetricUsageRatio(metrics PodMetricsInfo, targetUsage int64) (usageRatio float64, currentUsage int64) {
    58  	metricsTotal := int64(0)
    59  	for _, metric := range metrics {
    60  		metricsTotal += metric.Value
    61  	}
    62  
    63  	currentUsage = metricsTotal / int64(len(metrics))
    64  
    65  	return float64(currentUsage) / float64(targetUsage), currentUsage
    66  }
    67  

View as plain text