fix: fix HorizontalPodAutoscaler update at every reconcile
This commit is contained in:
@@ -18,8 +18,8 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
)
|
||||
|
||||
// createDeployment creates Kubernetes deployment
|
||||
func createDeployment(r GatewayReconciler, ctx context.Context, req ctrl.Request, gateway gomaprojv1beta1.Gateway, imageName string) error {
|
||||
// createUpdateDeployment creates Kubernetes deployment
|
||||
func createUpdateDeployment(r GatewayReconciler, ctx context.Context, req ctrl.Request, gateway gomaprojv1beta1.Gateway, imageName string) error {
|
||||
logger := log.FromContext(ctx)
|
||||
// Define the desired Deployment
|
||||
deployment := &v1.Deployment{
|
||||
|
||||
@@ -19,21 +19,19 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||
"strings"
|
||||
|
||||
gomaprojv1beta1 "github.com/jkaninda/goma-operator/api/v1beta1"
|
||||
"gopkg.in/yaml.v3"
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GatewayReconciler reconciles a Gateway object
|
||||
@@ -157,7 +155,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
|
||||
}
|
||||
|
||||
}
|
||||
err = createDeployment(*r, ctx, req, *gateway, imageName)
|
||||
err = createUpdateDeployment(*r, ctx, req, *gateway, imageName)
|
||||
if err != nil {
|
||||
addCondition(&gateway.Status, "DeploymentNotReady", metav1.ConditionFalse, "DeploymentNotReady", "Failed to created deployment for Gateway")
|
||||
logger.Error(err, "Failed to create Deployment")
|
||||
@@ -185,11 +183,10 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
|
||||
|
||||
}
|
||||
gateway.Status.Routes = int32(len(gomaConfig.Gateway.Routes))
|
||||
if err := r.updateStatus(ctx, gateway); err != nil {
|
||||
if err = r.updateStatus(ctx, gateway); err != nil {
|
||||
logger.Error(err, "Failed to update resource status")
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
logger.Info("Successfully updated resource status")
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
@@ -35,8 +34,6 @@ func gatewayConfig(r GatewayReconciler, ctx context.Context, req ctrl.Request, g
|
||||
logger.Error(err, "Failed to list Middlewares")
|
||||
return *gomaConfig
|
||||
}
|
||||
logger.Info(fmt.Sprintf("Listing Routes: size: %d", len(routes.Items)))
|
||||
|
||||
for _, route := range routes.Items {
|
||||
logger.Info("Found Route", "Name", route.Name)
|
||||
if route.Spec.Gateway == gateway.Name {
|
||||
@@ -76,8 +73,6 @@ func updateGatewayConfig(r RouteReconciler, ctx context.Context, req ctrl.Reques
|
||||
logger.Error(err, "Failed to list Middlewares")
|
||||
return err
|
||||
}
|
||||
logger.Info(fmt.Sprintf("Listing Routes: size: %d", len(routes.Items)))
|
||||
|
||||
for _, route := range routes.Items {
|
||||
logger.Info("Found Route", "Name", route.Name)
|
||||
if route.Spec.Gateway == gateway.Name {
|
||||
|
||||
@@ -83,6 +83,7 @@ func createHpa(r GatewayReconciler, ctx context.Context, req ctrl.Request, gatew
|
||||
}
|
||||
logger.Info("Created HorizontalPodAutoscaler", "HorizontalPodAutoscaler.Name", hpa.Name)
|
||||
} else {
|
||||
logger.Info("HorizontalPodAutoscaler already exists", "HorizontalPodAutoscaler.Name", hpa.Name)
|
||||
// Update the Deployment if the spec has changed
|
||||
if !equalHpaSpec(existHpa, *hpa) {
|
||||
existHpa.Spec = hpa.Spec
|
||||
@@ -98,17 +99,16 @@ func createHpa(r GatewayReconciler, ctx context.Context, req ctrl.Request, gatew
|
||||
|
||||
// Helper function to compare Deployment specs
|
||||
func equalHpaSpec(existing, desired autoscalingv2.HorizontalPodAutoscaler) bool {
|
||||
// A deep equality check or field-by-field comparison would be more accurate
|
||||
if existing.Spec.MinReplicas != desired.Spec.MinReplicas {
|
||||
if *existing.Spec.MinReplicas != *desired.Spec.MinReplicas {
|
||||
return false
|
||||
}
|
||||
if existing.Spec.MaxReplicas != desired.Spec.MaxReplicas {
|
||||
return false
|
||||
}
|
||||
if existing.Spec.Metrics[0].Resource.Target.AverageUtilization != desired.Spec.Metrics[0].Resource.Target.AverageUtilization {
|
||||
if *existing.Spec.Metrics[0].Resource.Target.AverageUtilization != *desired.Spec.Metrics[0].Resource.Target.AverageUtilization {
|
||||
return false
|
||||
}
|
||||
if existing.Spec.Metrics[1].Resource.Target.AverageUtilization != desired.Spec.Metrics[1].Resource.Target.AverageUtilization {
|
||||
if *existing.Spec.Metrics[1].Resource.Target.AverageUtilization != *desired.Spec.Metrics[1].Resource.Target.AverageUtilization {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user