Skip to content

Commit

Permalink
Use ClusterAutoscalerStatus as internal model
Browse files Browse the repository at this point in the history
Since the yaml version of the configmap is the future proof, and directly imported from cluster-autoscaler sources, this change makes it first class citizen.
  • Loading branch information
antonincms committed Aug 2, 2024
1 parent 6ec70b9 commit 8165125
Show file tree
Hide file tree
Showing 7 changed files with 375 additions and 282 deletions.
18 changes: 9 additions & 9 deletions controllers/priorityexpander_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (r *PriorityExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

// ... and parse it.
var status *clusterautoscaler.Status
var status *clusterautoscaler.ClusterAutoscalerStatus
if !r.Configuration.ClusterAutoscalerStatusLegacyFormat {
s, err := clusterautoscaler.ParseYamlStatus(readableStatus)
if err != nil {
Expand All @@ -141,14 +141,14 @@ func (r *PriorityExpanderReconciler) Reconcile(ctx context.Context, req ctrl.Req
oroot := map[string]map[string]int32{}
for _, node := range status.NodeGroups {
oroot[node.Name] = make(map[string]int32)
oroot[node.Name]["CloudProviderTarget"] = node.Health.CloudProviderTarget
oroot[node.Name]["Ready"] = node.Health.Ready
oroot[node.Name]["Unready"] = node.Health.Unready
oroot[node.Name]["NotStarted"] = node.Health.NotStarted
oroot[node.Name]["Registered"] = node.Health.Registered
oroot[node.Name]["LongUnregistered"] = node.Health.LongUnregistered
oroot[node.Name]["MinSize"] = node.Health.MinSize
oroot[node.Name]["MaxSize"] = node.Health.MaxSize
oroot[node.Name]["CloudProviderTarget"] = int32(node.Health.CloudProviderTarget)
oroot[node.Name]["Ready"] = int32(node.Health.NodeCounts.Registered.Ready)
oroot[node.Name]["Unready"] = int32(node.Health.NodeCounts.Registered.Unready.Total)
oroot[node.Name]["NotStarted"] = int32(node.Health.NodeCounts.Registered.NotStarted)
oroot[node.Name]["Registered"] = int32(node.Health.NodeCounts.Registered.Total)
oroot[node.Name]["LongUnregistered"] = int32(node.Health.NodeCounts.LongUnregistered)
oroot[node.Name]["MinSize"] = int32(node.Health.MinSize)
oroot[node.Name]["MaxSize"] = int32(node.Health.MaxSize)
}

// Create new PriorityExpander template and parse it
Expand Down
36 changes: 18 additions & 18 deletions controllers/scheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (r *SchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

// Parse it and retrieve NodeGroups from targets and fallbacks
var status *clusterautoscaler.Status
var status *clusterautoscaler.ClusterAutoscalerStatus
if !r.Configuration.ClusterAutoscalerStatusLegacyFormat {
s, err := clusterautoscaler.ParseYamlStatus(readableStatus)
if err != nil {
Expand All @@ -155,7 +155,7 @@ func (r *SchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if len(asgTargets) == 0 {
asgTargets = []string{scheduler.Spec.ASGTarget}
}
targetNodeGroups := make([]clusterautoscaler.NodeGroup, 0, len(asgTargets))
targetNodeGroups := make([]clusterautoscaler.NodeGroupStatus, 0, len(asgTargets))
for _, target := range asgTargets {
targetNodeGroup := clusterautoscaler.GetNodeGroupWithName(status.NodeGroups, target)
if targetNodeGroup == nil {
Expand All @@ -173,12 +173,12 @@ func (r *SchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (

// Update target statuses
for i := range targetNodeGroups {
for _, s := range []clusterautoscaler.ScaleUpStatus{
clusterautoscaler.ScaleUpNeeded,
clusterautoscaler.ScaleUpNotNeeded,
clusterautoscaler.ScaleUpInProgress,
clusterautoscaler.ScaleUpNoActivity,
clusterautoscaler.ScaleUpBackoff,
for _, s := range []clusterautoscaler.ClusterAutoscalerConditionStatus{
clusterautoscaler.ClusterAutoscalerNeeded,
clusterautoscaler.ClusterAutoscalerNotNeeded,
clusterautoscaler.ClusterAutoscalerInProgress,
clusterautoscaler.ClusterAutoscalerNoActivity,
clusterautoscaler.ClusterAutoscalerBackoff,
} {
targetNodeGroupStatus := metrics.SchedulerTargetNodeGroupStatus.With(prometheus.Labels{
"node_group_name": targetNodeGroups[i].Name,
Expand Down Expand Up @@ -288,7 +288,7 @@ func (r *SchedulerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if down > 0 {
scaleDownAllowed := false
for i := range targetNodeGroups {
if targetNodeGroups[i].ScaleUp.Status != clusterautoscaler.ScaleUpBackoff {
if targetNodeGroups[i].ScaleUp.Status != clusterautoscaler.ClusterAutoscalerBackoff {
scaleDownAllowed = true
break
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func getMatchedPolicy(m []matchedPolicy, p corev1alpha1.SchedulerPolicy) *matche
// nodeGroupIntOrFieldValue returns the desired value matching IntOrField.
// Field returns the NodeGroup Field value ans has priority over Int if a valid
// Field is given.
func nodeGroupIntOrFieldValue(ngs []clusterautoscaler.NodeGroup, iof corev1alpha1.IntOrField) int32 {
func nodeGroupIntOrFieldValue(ngs []clusterautoscaler.NodeGroupStatus, iof corev1alpha1.IntOrField) int32 {
if iof.FieldVal == nil {
return iof.IntVal
}
Expand All @@ -531,35 +531,35 @@ func nodeGroupIntOrFieldValue(ngs []clusterautoscaler.NodeGroup, iof corev1alpha
switch *iof.FieldVal {
case corev1alpha1.FieldReady:
for i := range ngs {
val += ngs[i].Health.Ready
val += int32(ngs[i].Health.NodeCounts.Registered.Ready)
}
case corev1alpha1.FieldUnready:
for i := range ngs {
val += ngs[i].Health.Unready
val += int32(ngs[i].Health.NodeCounts.Registered.Unready.Total)
}
case corev1alpha1.FieldNotStarted:
for i := range ngs {
val += ngs[i].Health.NotStarted
val += int32(ngs[i].Health.NodeCounts.Registered.NotStarted)
}
case corev1alpha1.FieldRegistered:
for i := range ngs {
val += ngs[i].Health.Registered
val += int32(ngs[i].Health.NodeCounts.Registered.Total)
}
case corev1alpha1.FieldLongUnregistered:
for i := range ngs {
val += ngs[i].Health.LongUnregistered
val += int32(ngs[i].Health.NodeCounts.LongUnregistered)
}
case corev1alpha1.FieldCloudProviderTarget:
for i := range ngs {
val += ngs[i].Health.CloudProviderTarget
val += int32(ngs[i].Health.CloudProviderTarget)
}
}

return val
}

// matchPolicy returns if given NodeGroup match desired Scheduler policy.
func matchPolicy(ngs []clusterautoscaler.NodeGroup, policy corev1alpha1.SchedulerPolicy) bool {
func matchPolicy(ngs []clusterautoscaler.NodeGroupStatus, policy corev1alpha1.SchedulerPolicy) bool {
left := nodeGroupIntOrFieldValue(ngs, policy.LeftOperand)
right := nodeGroupIntOrFieldValue(ngs, policy.RightOperand)

Expand All @@ -583,7 +583,7 @@ func matchPolicy(ngs []clusterautoscaler.NodeGroup, policy corev1alpha1.Schedule
}

// replicas returns the number of required replicas.
func nodeGroupReplicas(ngs []clusterautoscaler.NodeGroup, operation corev1alpha1.IntOrArithmeticOperation) int32 {
func nodeGroupReplicas(ngs []clusterautoscaler.NodeGroupStatus, operation corev1alpha1.IntOrArithmeticOperation) int32 {
if operation.OperationVal == nil {
return operation.IntVal
}
Expand Down
Loading

0 comments on commit 8165125

Please sign in to comment.