From 1854a5ea960856eb61f69975b49d856a79961e54 Mon Sep 17 00:00:00 2001 From: Alessandro Olivero Date: Mon, 2 Dec 2024 10:39:47 +0100 Subject: [PATCH] ipam: fix ha --- cmd/ipam/main.go | 10 ++++++++-- deployments/liqo/templates/liqo-ipam-deployment.yaml | 1 + pkg/ipam/options.go | 1 + pkg/leaderelection/blockingelection.go | 6 ++++++ pkg/leaderelection/leaderelection.go | 1 + test/e2e/pipeline/installer/liqoctl/setup.sh | 1 + 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/ipam/main.go b/cmd/ipam/main.go index b879a46198..29a75454fd 100644 --- a/cmd/ipam/main.go +++ b/cmd/ipam/main.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/cobra" "google.golang.org/grpc" "google.golang.org/grpc/health/grpc_health_v1" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -51,6 +52,7 @@ var ( func init() { utilruntime.Must(corev1.AddToScheme(scheme)) + utilruntime.Must(appsv1.AddToScheme(scheme)) utilruntime.Must(ipamv1alpha1.AddToScheme(scheme)) } @@ -93,6 +95,7 @@ func main() { "The duration the LeaderElector clients should wait between tries of actions.") cmd.Flags().StringVar(&options.PodName, "pod-name", "", "The name of the pod running the IPAM service.") + cmd.Flags().StringVar(&options.DeploymentName, "deployment-name", "", "The name of the deployment running the IPAM service.") utilruntime.Must(cmd.MarkFlagRequired("pod-name")) @@ -122,13 +125,16 @@ func run(cmd *cobra.Command, _ []string) error { if options.EnableLeaderElection { if leader, err := leaderelection.Blocking(ctx, cfg, record.NewBroadcaster(), &leaderelection.Opts{ PodInfo: leaderelection.PodInfo{ - PodName: options.PodName, - Namespace: options.LeaderElectionNamespace, + PodName: options.PodName, + Namespace: options.LeaderElectionNamespace, + DeploymentName: &options.DeploymentName, }, + Client: cl, LeaderElectorName: options.LeaderElectionName, LeaseDuration: options.LeaseDuration, RenewDeadline: options.RenewDeadline, RetryPeriod: options.RetryPeriod, + LabelLeader: true, }); err != nil { return err } else if !leader { diff --git a/deployments/liqo/templates/liqo-ipam-deployment.yaml b/deployments/liqo/templates/liqo-ipam-deployment.yaml index 0f998ee1a2..39d23e5825 100644 --- a/deployments/liqo/templates/liqo-ipam-deployment.yaml +++ b/deployments/liqo/templates/liqo-ipam-deployment.yaml @@ -55,6 +55,7 @@ spec: {{- if $ha }} - --leader-election - --leader-election-namespace=$(POD_NAMESPACE) + - --deployment-name={{ include "liqo.prefixedName" $ipamConfig }} {{- end }} {{- if .Values.ipam.pools }} {{- $d := dict "commandName" "--pools" "list" .Values.ipam.pools }} diff --git a/pkg/ipam/options.go b/pkg/ipam/options.go index 828b7a54fd..05becefc04 100644 --- a/pkg/ipam/options.go +++ b/pkg/ipam/options.go @@ -27,6 +27,7 @@ type Options struct { RenewDeadline time.Duration RetryPeriod time.Duration PodName string + DeploymentName string ServerOpts ServerOptions } diff --git a/pkg/leaderelection/blockingelection.go b/pkg/leaderelection/blockingelection.go index 562a2931a5..9b36eabb60 100644 --- a/pkg/leaderelection/blockingelection.go +++ b/pkg/leaderelection/blockingelection.go @@ -60,6 +60,12 @@ func Blocking(ctx context.Context, rc *rest.Config, eb record.EventBroadcaster, lock.Lock() defer lock.Unlock() klog.Infof("Leader election: this pod is the leader") + if opts.LabelLeader && opts.Client != nil { + if err := handleLeaderLabelWithClient(ctx, opts.Client, &opts.PodInfo); err != nil { + klog.Errorf("Failed to label leader pod: %v", err) + os.Exit(1) + } + } close(elected) }, OnStoppedLeading: func() { diff --git a/pkg/leaderelection/leaderelection.go b/pkg/leaderelection/leaderelection.go index e6f0c0300a..16bd1f6595 100644 --- a/pkg/leaderelection/leaderelection.go +++ b/pkg/leaderelection/leaderelection.go @@ -60,6 +60,7 @@ type PodInfo struct { // Opts contains the options to configure the leader election mechanism. type Opts struct { PodInfo + Client client.Client LeaderElectorName string LeaseDuration time.Duration RenewDeadline time.Duration diff --git a/test/e2e/pipeline/installer/liqoctl/setup.sh b/test/e2e/pipeline/installer/liqoctl/setup.sh index e8fcb368b8..1e45ad573a 100755 --- a/test/e2e/pipeline/installer/liqoctl/setup.sh +++ b/test/e2e/pipeline/installer/liqoctl/setup.sh @@ -111,6 +111,7 @@ do if [[ "${INFRA}" == "cluster-api" ]]; then LIQO_PROVIDER="kubeadm" COMMON_ARGS=("${COMMON_ARGS[@]}" --set "networking.gatewayTemplates.replicas=$HA_REPLICAS" ) + COMMON_ARGS=("${COMMON_ARGS[@]}" --set "ipam.internal.replicas=$HA_REPLICAS" ) else LIQO_PROVIDER="${INFRA}" fi