|
| 1 | +/* |
| 2 | +Copyright 2018 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 status |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + "k8s.io/autoscaler/cluster-autoscaler/clusterstate" |
| 23 | + "k8s.io/autoscaler/cluster-autoscaler/context" |
| 24 | + "k8s.io/autoscaler/cluster-autoscaler/metrics" |
| 25 | + "k8s.io/autoscaler/cluster-autoscaler/utils/backoff" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + // unknownErrorCode means that the cloud provider has not provided an error code. |
| 30 | + unknownErrorCode = "unknown" |
| 31 | +) |
| 32 | + |
| 33 | +// BackoffReasonStatus contains information about backoff status and reason |
| 34 | +type BackoffReasonStatus map[string]bool |
| 35 | + |
| 36 | +// MetricsAutoscalingStatusProcessor is used to update metrics after each autoscaling iteration. |
| 37 | +type MetricsAutoscalingStatusProcessor struct { |
| 38 | + backoffReasonStatus map[string]BackoffReasonStatus |
| 39 | +} |
| 40 | + |
| 41 | +// Process queries the health status and backoff situation of all node groups and updates metrics after each autoscaling iteration. |
| 42 | +func (p *MetricsAutoscalingStatusProcessor) Process(context *context.AutoscalingContext, csr *clusterstate.ClusterStateRegistry, now time.Time) error { |
| 43 | + for _, nodeGroup := range context.CloudProvider.NodeGroups() { |
| 44 | + if !nodeGroup.Exist() { |
| 45 | + continue |
| 46 | + } |
| 47 | + metrics.UpdateNodeGroupHealthStatus(nodeGroup.Id(), csr.IsNodeGroupHealthy(nodeGroup.Id())) |
| 48 | + backoffStatus := csr.BackoffStatusForNodeGroup(nodeGroup, now) |
| 49 | + p.updateNodeGroupBackoffStatusMetrics(nodeGroup.Id(), backoffStatus) |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// CleanUp cleans up the processor's internal structures. |
| 55 | +func (p *MetricsAutoscalingStatusProcessor) CleanUp() { |
| 56 | +} |
| 57 | + |
| 58 | +// updateNodeGroupBackoffStatusMetrics updates metrics about backoff situation and reason of the node group |
| 59 | +func (p *MetricsAutoscalingStatusProcessor) updateNodeGroupBackoffStatusMetrics(nodeGroup string, backoffStatus backoff.Status) { |
| 60 | + if _, ok := p.backoffReasonStatus[nodeGroup]; ok { |
| 61 | + for reason := range p.backoffReasonStatus[nodeGroup] { |
| 62 | + p.backoffReasonStatus[nodeGroup][reason] = false |
| 63 | + } |
| 64 | + } else { |
| 65 | + p.backoffReasonStatus[nodeGroup] = make(BackoffReasonStatus) |
| 66 | + } |
| 67 | + |
| 68 | + if backoffStatus.IsBackedOff { |
| 69 | + errorCode := backoffStatus.ErrorInfo.ErrorCode |
| 70 | + if errorCode == "" { |
| 71 | + // prevent error code from being empty. |
| 72 | + errorCode = unknownErrorCode |
| 73 | + } |
| 74 | + p.backoffReasonStatus[nodeGroup][errorCode] = true |
| 75 | + } |
| 76 | + metrics.UpdateNodeGroupBackOffStatus(nodeGroup, p.backoffReasonStatus[nodeGroup]) |
| 77 | +} |
0 commit comments