Skip to content

Commit

Permalink
Handle cluster status reconciliation with autoscaling configured
Browse files Browse the repository at this point in the history
  • Loading branch information
sp1999 committed Jun 4, 2024
1 parent 8e85e1e commit cd5f1a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/api/v1alpha1/condition_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const (

// ExternalEtcdNotAvailable reports the Cluster status is waiting for Etcd to be available.
ExternalEtcdNotAvailable = "ExternalEtcdNotAvailable"

// AutoscalerConstraintNotMetReason reports the Cluster status is waiting for autoscaler constraint to be met.
AutoscalerConstraintNotMetReason = "AutoscalerConstraintNotMet"
)

const (
Expand Down
9 changes: 5 additions & 4 deletions pkg/clusterapi/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)

// Autoscaler annotation constants.
const (
nodeGroupMinSizeAnnotation = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size"
nodeGroupMaxSizeAnnotation = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size"
NodeGroupMinSizeAnnotation = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size"
NodeGroupMaxSizeAnnotation = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size"
)

func ConfigureAutoscalingInMachineDeployment(md *clusterv1.MachineDeployment, autoscalingConfig *anywherev1.AutoScalingConfiguration) {
Expand All @@ -22,6 +23,6 @@ func ConfigureAutoscalingInMachineDeployment(md *clusterv1.MachineDeployment, au
md.ObjectMeta.Annotations = map[string]string{}
}

md.ObjectMeta.Annotations[nodeGroupMinSizeAnnotation] = strconv.Itoa(autoscalingConfig.MinCount)
md.ObjectMeta.Annotations[nodeGroupMaxSizeAnnotation] = strconv.Itoa(autoscalingConfig.MaxCount)
md.ObjectMeta.Annotations[NodeGroupMinSizeAnnotation] = strconv.Itoa(autoscalingConfig.MinCount)
md.ObjectMeta.Annotations[NodeGroupMaxSizeAnnotation] = strconv.Itoa(autoscalingConfig.MaxCount)
}
30 changes: 28 additions & 2 deletions pkg/controller/clusters/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/clusterapi"
"github.com/aws/eks-anywhere/pkg/controller"
)

Expand Down Expand Up @@ -198,11 +199,17 @@ func updateWorkersReadyCondition(cluster *anywherev1.Cluster, machineDeployments
}

totalExpected := 0
wngWithAutoScalingConfigurationMap := make(map[string]anywherev1.AutoScalingConfiguration)
for _, wng := range cluster.Spec.WorkerNodeGroupConfigurations {
totalExpected += *wng.Count
// We want to consider only the worker node groups which don't have autoscaling configuration for expected worker nodes count.
if wng.AutoScalingConfiguration == nil {
totalExpected += *wng.Count
} else {
wngWithAutoScalingConfigurationMap[wng.Name] = *wng.AutoScalingConfiguration
}
}

// First, we need aggregate the number of nodes across worker node groups to be able to assess the condition of the workers
// First, we need to aggregate the number of nodes across worker node groups to be able to assess the condition of the workers
// as a whole.
totalReadyReplicas := 0
totalUpdatedReplicas := 0
Expand All @@ -215,6 +222,13 @@ func updateWorkersReadyCondition(cluster *anywherev1.Cluster, machineDeployments
return
}

// Skip updating the replicas for the machine deployments which have autoscaling configuration annotation
if md.ObjectMeta.Annotations != nil {
if _, ok := md.ObjectMeta.Annotations[clusterapi.NodeGroupMinSizeAnnotation]; ok {
continue
}
}

totalReadyReplicas += int(md.Status.ReadyReplicas)
totalUpdatedReplicas += int(md.Status.UpdatedReplicas)
totalReplicas += int(md.Status.Replicas)
Expand Down Expand Up @@ -253,6 +267,18 @@ func updateWorkersReadyCondition(cluster *anywherev1.Cluster, machineDeployments
return
}

for _, md := range machineDeployments {
if wng, exists := wngWithAutoScalingConfigurationMap[md.ObjectMeta.Name]; exists {
minCount := wng.MinCount
maxCount := wng.MaxCount
replicas := int(md.Status.Replicas)
if replicas < minCount || replicas > maxCount {
conditions.MarkFalse(cluster, anywherev1.WorkersReadyCondition, anywherev1.AutoscalerConstraintNotMetReason, clusterv1.ConditionSeverityInfo, "Worker nodes count for %s not between %d and %d yet (%d actual)", md.ObjectMeta.Name, minCount, maxCount, replicas)
return
}
}
}

conditions.MarkTrue(cluster, anywherev1.WorkersReadyCondition)
}

Expand Down

0 comments on commit cd5f1a2

Please sign in to comment.