2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-27 04:59:51 -04:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-15 10:48:32 -04:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2022-06-27 04:59:51 -04:00
|
|
|
|
2022-06-27 06:35:02 -04:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
2022-06-27 04:59:51 -04:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2022-06-27 06:35:02 -04:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/client-go/util/retry"
|
2022-06-27 04:59:51 -04:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2022-09-15 10:48:32 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/builder"
|
2022-06-27 04:59:51 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2022-09-15 10:48:32 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/handler"
|
2022-06-27 04:59:51 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2022-09-15 10:48:32 -04:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/source"
|
2022-06-27 04:59:51 -04:00
|
|
|
|
2023-01-04 13:04:28 -05:00
|
|
|
updatev1alpha1 "github.com/edgelesssys/constellation/v2/operators/constellation-node-operator/v2/api/v1alpha1"
|
2022-06-27 04:59:51 -04:00
|
|
|
)
|
|
|
|
|
2022-09-12 07:45:31 -04:00
|
|
|
// AutoscalingStrategyReconciler reconciles a AutoscalingStrategy object.
|
2022-06-27 04:59:51 -04:00
|
|
|
type AutoscalingStrategyReconciler struct {
|
|
|
|
client.Client
|
|
|
|
Scheme *runtime.Scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=autoscalingstrategies,verbs=get;list;watch;create;update;patch;delete
|
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=autoscalingstrategies/status,verbs=get;update;patch
|
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=autoscalingstrategies/finalizers,verbs=update
|
2022-09-15 10:48:32 -04:00
|
|
|
//+kubebuilder:rbac:groups=update.edgeless.systems,resources=scalinggroups,verbs=get;list;watch
|
2022-06-27 06:35:02 -04:00
|
|
|
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;delete
|
2022-06-27 04:59:51 -04:00
|
|
|
|
2022-06-27 06:35:02 -04:00
|
|
|
// Reconcile enabled or disables the cluster-autoscaler based on the AutoscalingStrategy spec
|
|
|
|
// by modifying the replicas of the Deployment.
|
2022-06-27 04:59:51 -04:00
|
|
|
func (r *AutoscalingStrategyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
2022-06-27 06:35:02 -04:00
|
|
|
logr := log.FromContext(ctx)
|
2022-06-27 04:59:51 -04:00
|
|
|
|
2022-06-27 06:35:02 -04:00
|
|
|
var desiredAutoscalingStrategy updatev1alpha1.AutoscalingStrategy
|
|
|
|
if err := r.Get(ctx, req.NamespacedName, &desiredAutoscalingStrategy); err != nil {
|
|
|
|
logr.Error(err, "Unable to fetch AutoscalingStrategy")
|
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
}
|
|
|
|
var expectedReplicas int32
|
|
|
|
if desiredAutoscalingStrategy.Spec.Enabled {
|
|
|
|
expectedReplicas = 1
|
|
|
|
}
|
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
var scalingGroups updatev1alpha1.ScalingGroupList
|
|
|
|
if err := r.List(ctx, &scalingGroups); err != nil {
|
|
|
|
logr.Error(err, "Unable to fetch ScalingGroups")
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
autoscalerArgs := []string{"./cluster-autoscaler"}
|
|
|
|
for key, val := range desiredAutoscalingStrategy.Spec.AutoscalerExtraArgs {
|
|
|
|
autoscalerArgs = append(autoscalerArgs, "--"+key+"="+val)
|
|
|
|
}
|
|
|
|
const nodeGroupFmt = "--nodes=%d:%d:%s"
|
|
|
|
for _, group := range scalingGroups.Items {
|
|
|
|
// Don't autoscale control plane nodes for safety reasons.
|
|
|
|
if group.Spec.Autoscaling && group.Spec.Role != updatev1alpha1.ControlPlaneRole {
|
|
|
|
groupArg := fmt.Sprintf(nodeGroupFmt, group.Spec.Min, group.Spec.Max, group.Spec.AutoscalerGroupName)
|
|
|
|
autoscalerArgs = append(autoscalerArgs, groupArg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(autoscalerArgs[1:])
|
|
|
|
|
2022-06-27 06:35:02 -04:00
|
|
|
var autoscalerDeployment appsv1.Deployment
|
2022-09-15 10:48:32 -04:00
|
|
|
deploymentKey := client.ObjectKey{
|
|
|
|
Namespace: desiredAutoscalingStrategy.Spec.DeploymentNamespace,
|
|
|
|
Name: desiredAutoscalingStrategy.Spec.DeploymentName,
|
|
|
|
}
|
|
|
|
if err := r.Get(ctx, deploymentKey, &autoscalerDeployment); err != nil {
|
2022-06-27 06:35:02 -04:00
|
|
|
logr.Error(err, "Unable to fetch autoscaler Deployment")
|
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if autoscalerDeployment.Spec.Replicas == nil || *autoscalerDeployment.Spec.Replicas == 0 {
|
|
|
|
desiredAutoscalingStrategy.Status.Enabled = false
|
|
|
|
desiredAutoscalingStrategy.Status.Replicas = 0
|
|
|
|
} else {
|
|
|
|
desiredAutoscalingStrategy.Status.Enabled = true
|
|
|
|
desiredAutoscalingStrategy.Status.Replicas = *autoscalerDeployment.Spec.Replicas
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.tryUpdateStatus(ctx, req.NamespacedName, desiredAutoscalingStrategy.Status); err != nil {
|
|
|
|
logr.Error(err, "Unable to update AutoscalingStrategy status")
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
}
|
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
var needUpdate bool
|
2022-06-27 06:35:02 -04:00
|
|
|
if autoscalerDeployment.Spec.Replicas == nil || *autoscalerDeployment.Spec.Replicas != expectedReplicas {
|
2022-09-15 10:48:32 -04:00
|
|
|
needUpdate = needUpdate || true
|
|
|
|
}
|
|
|
|
containers := autoscalerDeployment.Spec.Template.Spec.Containers
|
|
|
|
if len(containers) != 0 && containers[0].Command == nil { // uninitialized
|
|
|
|
needUpdate = needUpdate || true
|
|
|
|
}
|
|
|
|
if len(containers) != 0 && containers[0].Command != nil { // args updated
|
|
|
|
if len(containers[0].Command) != len(autoscalerArgs) {
|
|
|
|
needUpdate = needUpdate || true
|
|
|
|
} else {
|
|
|
|
for i, arg := range containers[0].Command {
|
|
|
|
if arg != autoscalerArgs[i] {
|
|
|
|
needUpdate = needUpdate || true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !needUpdate {
|
|
|
|
return ctrl.Result{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logr.Info("Updating autoscaling replicas and command", "expectedReplicas", expectedReplicas, "autoscalerArgs", autoscalerArgs)
|
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
if err := r.Get(ctx, deploymentKey, &autoscalerDeployment); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-27 06:35:02 -04:00
|
|
|
autoscalerDeployment.Spec.Replicas = &expectedReplicas
|
2022-09-15 10:48:32 -04:00
|
|
|
if len(containers) != 0 {
|
|
|
|
logr.Info("Updating autoscaler command", "old", containers[0].Command, "new", autoscalerArgs)
|
|
|
|
autoscalerDeployment.Spec.Template.Spec.Containers[0].Command = autoscalerArgs
|
2022-06-27 06:35:02 -04:00
|
|
|
}
|
2022-09-15 10:48:32 -04:00
|
|
|
return r.Update(ctx, &autoscalerDeployment)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logr.Error(err, "Unable to update autoscaler Deployment")
|
|
|
|
return ctrl.Result{}, err
|
2022-06-27 06:35:02 -04:00
|
|
|
}
|
2022-06-27 04:59:51 -04:00
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
return ctrl.Result{Requeue: true}, nil
|
2022-06-27 04:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
|
|
func (r *AutoscalingStrategyReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
|
|
For(&updatev1alpha1.AutoscalingStrategy{}).
|
2022-09-15 10:48:32 -04:00
|
|
|
Watches(
|
|
|
|
&source.Kind{Type: &updatev1alpha1.ScalingGroup{}},
|
|
|
|
handler.EnqueueRequestsFromMapFunc(r.findObjectsForDeployment),
|
|
|
|
builder.WithPredicates(scalingGroupChangePredicate()),
|
|
|
|
).
|
2022-06-27 04:59:51 -04:00
|
|
|
Complete(r)
|
|
|
|
}
|
2022-06-27 06:35:02 -04:00
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
func (r *AutoscalingStrategyReconciler) findObjectsForDeployment(_ client.Object) []reconcile.Request {
|
|
|
|
var autoscalingStrats updatev1alpha1.AutoscalingStrategyList
|
|
|
|
err := r.List(context.Background(), &autoscalingStrats)
|
|
|
|
if err != nil {
|
|
|
|
return []reconcile.Request{}
|
|
|
|
}
|
|
|
|
|
|
|
|
requests := make([]reconcile.Request, len(autoscalingStrats.Items))
|
|
|
|
for i, item := range autoscalingStrats.Items {
|
|
|
|
requests[i] = reconcile.Request{
|
|
|
|
NamespacedName: types.NamespacedName{
|
|
|
|
Name: item.GetName(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return requests
|
|
|
|
}
|
|
|
|
|
|
|
|
// scalingGroupChangePredicate filters events on scaling group resources.
|
|
|
|
func scalingGroupChangePredicate() predicate.Predicate {
|
|
|
|
return predicate.Funcs{
|
|
|
|
CreateFunc: func(e event.CreateEvent) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
UpdateFunc: func(e event.UpdateEvent) bool {
|
|
|
|
oldGroup, ok := e.ObjectOld.(*updatev1alpha1.ScalingGroup)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
newGroup, ok := e.ObjectNew.(*updatev1alpha1.ScalingGroup)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case oldGroup.Spec.Min != newGroup.Spec.Min:
|
|
|
|
return true
|
|
|
|
case oldGroup.Spec.Max != newGroup.Spec.Max:
|
|
|
|
return true
|
|
|
|
case oldGroup.Spec.Autoscaling != newGroup.Spec.Autoscaling:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DeleteFunc: func(e event.DeleteEvent) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
GenericFunc: func(e event.GenericEvent) bool {
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 06:35:02 -04:00
|
|
|
// tryUpdateStatus attempts to update the AutoscalingStrategy status field in a retry loop.
|
|
|
|
func (r *AutoscalingStrategyReconciler) tryUpdateStatus(ctx context.Context, name types.NamespacedName, status updatev1alpha1.AutoscalingStrategyStatus) error {
|
|
|
|
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
var autoscalingStrategy updatev1alpha1.AutoscalingStrategy
|
|
|
|
if err := r.Get(ctx, name, &autoscalingStrategy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
autoscalingStrategy.Status = *status.DeepCopy()
|
2023-01-13 10:49:41 -05:00
|
|
|
return r.Status().Update(ctx, &autoscalingStrategy)
|
2022-06-27 06:35:02 -04:00
|
|
|
})
|
|
|
|
}
|