Skip to content

Commit

Permalink
Add auto scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
richardchen331 committed Feb 11, 2023
1 parent 971acf8 commit 399fea3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 12 deletions.
9 changes: 8 additions & 1 deletion cloud/scope/managedmachinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,20 @@ func ConvertToSdkNodePool(nodePool infrav1exp.GCPManagedMachinePool, machinePool
}
sdkNodePool := containerpb.NodePool{
Name: nodePoolName,
InitialNodeCount: nodePool.Spec.NodeCount,
InitialNodeCount: nodePool.Spec.InitialNodeCount,
Config: &containerpb.NodeConfig{
Labels: nodePool.Spec.KubernetesLabels,
Taints: infrav1exp.ConvertToSdkTaint(nodePool.Spec.KubernetesTaints),
Metadata: nodePool.Spec.AdditionalLabels,
},
}
if nodePool.Spec.Scaling != nil {
sdkNodePool.Autoscaling = &containerpb.NodePoolAutoscaling{
Enabled: true,
MinNodeCount: *nodePool.Spec.Scaling.MinCount,
MaxNodeCount: *nodePool.Spec.Scaling.MaxCount,
}
}
if machinePool.Spec.Template.Spec.Version != nil {
sdkNodePool.Version = *machinePool.Spec.Template.Spec.Version
}
Expand Down
39 changes: 37 additions & 2 deletions cloud/services/container/nodepools/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
return ctrl.Result{RequeueAfter: reconciler.DefaultRetryTime}, nil
}

needUpdateAutoscaling, setNodePoolAutoscalingRequest := s.checkDiffAndPrepareUpdateAutoscaling(nodePool)
if needUpdateAutoscaling {
log.Info("Auto scaling update required")
err = s.updateNodePoolAutoscaling(ctx, setNodePoolAutoscalingRequest)
if err != nil {
return ctrl.Result{}, err
}
log.Info("Node pool auto scaling updating in progress")
s.scope.GCPManagedMachinePool.Status.Ready = true
conditions.MarkTrue(s.scope.ConditionSetter(), infrav1exp.GKEMachinePoolUpdatingCondition)
return ctrl.Result{RequeueAfter: reconciler.DefaultRetryTime}, nil
}

needUpdateSize, setNodePoolSizeRequest := s.checkDiffAndPrepareUpdateSize(nodePool)
if needUpdateSize {
log.Info("Size update required")
Expand Down Expand Up @@ -268,6 +281,15 @@ func (s *Service) updateNodePoolVersionOrImage(ctx context.Context, updateNodePo
return nil
}

func (s *Service) updateNodePoolAutoscaling(ctx context.Context, setNodePoolAutoscalingRequest *containerpb.SetNodePoolAutoscalingRequest) error {
_, err := s.scope.ManagedMachinePoolClient().SetNodePoolAutoscaling(ctx, setNodePoolAutoscalingRequest)
if err != nil {
return err
}

return nil
}

func (s *Service) updateNodePoolSize(ctx context.Context, setNodePoolSizeRequest *containerpb.SetNodePoolSizeRequest) error {
_, err := s.scope.ManagedMachinePoolClient().SetNodePoolSize(ctx, setNodePoolSizeRequest)
if err != nil {
Expand Down Expand Up @@ -317,14 +339,27 @@ func (s *Service) checkDiffAndPrepareUpdateVersionOrImage(existingNodePool *cont
return needUpdate, &updateNodePoolRequest
}

func (s *Service) checkDiffAndPrepareUpdateAutoscaling(existingNodePool *containerpb.NodePool) (bool, *containerpb.SetNodePoolAutoscalingRequest) {
needUpdate := false
desiredNodePool := scope.ConvertToSdkNodePool(*s.scope.GCPManagedMachinePool, *s.scope.MachinePool)
setNodePoolAutoscalingRequest := containerpb.SetNodePoolAutoscalingRequest{
Name: s.scope.NodePoolFullName(),
}
if !reflect.DeepEqual(desiredNodePool.Autoscaling, existingNodePool.Autoscaling) {
needUpdate = true
setNodePoolAutoscalingRequest.Autoscaling = desiredNodePool.Autoscaling
}
return needUpdate, &setNodePoolAutoscalingRequest
}

func (s *Service) checkDiffAndPrepareUpdateSize(existingNodePool *containerpb.NodePool) (bool, *containerpb.SetNodePoolSizeRequest) {
needUpdate := false
setNodePoolSizeRequest := containerpb.SetNodePoolSizeRequest{
Name: s.scope.NodePoolFullName(),
}
if s.scope.GCPManagedMachinePool.Spec.NodeCount != existingNodePool.InitialNodeCount {
if s.scope.GCPManagedMachinePool.Spec.InitialNodeCount != existingNodePool.InitialNodeCount {
needUpdate = true
setNodePoolSizeRequest.NodeCount = s.scope.GCPManagedMachinePool.Spec.NodeCount
setNodePoolSizeRequest.NodeCount = s.scope.GCPManagedMachinePool.Spec.InitialNodeCount
}
return needUpdate, &setNodePoolSizeRequest
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ spec:
GCP resources managed by the GCP provider, in addition to the ones
added by default.
type: object
initialNodeCount:
description: InitialNodeCount represents the initial number of nodes
for the pool. In regional or multi-zonal clusters, this is the number
of nodes per zone.
format: int32
type: integer
kubernetesLabels:
additionalProperties:
type: string
Expand Down Expand Up @@ -82,12 +88,6 @@ spec:
- value
type: object
type: array
nodeCount:
description: NodeCount represents the initial number of nodes for
the pool. In regional or multi-zonal clusters, this is the number
of nodes per zone.
format: int32
type: integer
nodePoolName:
description: NodePoolName specifies the name of the GKE node pool
corresponding to this MachinePool. If you don't specify a name then
Expand All @@ -101,8 +101,18 @@ spec:
items:
type: string
type: array
scaling:
description: Scaling specifies scaling for the node pool
properties:
maxCount:
format: int32
type: integer
minCount:
format: int32
type: integer
type: object
required:
- nodeCount
- initialNodeCount
type: object
status:
description: GCPManagedMachinePoolStatus defines the observed state of
Expand Down
13 changes: 11 additions & 2 deletions exp/api/v1beta1/gcpmanagedmachinepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ type GCPManagedMachinePoolSpec struct {
// then a default name will be created based on the namespace and name of the managed machine pool.
// +optional
NodePoolName string `json:"nodePoolName,omitempty"`
// NodeCount represents the initial number of nodes for the pool.
// InitialNodeCount represents the initial number of nodes for the pool.
// In regional or multi-zonal clusters, this is the number of nodes per zone.
NodeCount int32 `json:"nodeCount"`
InitialNodeCount int32 `json:"initialNodeCount"`
// Scaling specifies scaling for the node pool
// +optional
Scaling *NodePoolAutoScaling `json:"scaling,omitempty"`
// KubernetesLabels specifies the labels to apply to the nodes of the node pool.
// +optional
KubernetesLabels infrav1.Labels `json:"kubernetesLabels,omitempty"`
Expand Down Expand Up @@ -88,6 +91,12 @@ type GCPManagedMachinePoolList struct {
Items []GCPManagedMachinePool `json:"items"`
}

// NodePoolAutoScaling specifies scaling options.
type NodePoolAutoScaling struct {
MinCount *int32 `json:"minCount,omitempty"`
MaxCount *int32 `json:"maxCount,omitempty"`
}

// GetConditions returns the machine pool conditions.
func (r *GCPManagedMachinePool) GetConditions() clusterv1.Conditions {
return r.Status.Conditions
Expand Down
30 changes: 30 additions & 0 deletions exp/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 399fea3

Please sign in to comment.