Skip to content

Commit

Permalink
ipam: fix ha
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli authored and adamjensenbot committed Dec 3, 2024
1 parent f5c494f commit 1854a5e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/ipam/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -51,6 +52,7 @@ var (

func init() {
utilruntime.Must(corev1.AddToScheme(scheme))
utilruntime.Must(appsv1.AddToScheme(scheme))
utilruntime.Must(ipamv1alpha1.AddToScheme(scheme))
}

Expand Down Expand Up @@ -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"))

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions deployments/liqo/templates/liqo-ipam-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions pkg/ipam/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Options struct {
RenewDeadline time.Duration
RetryPeriod time.Duration
PodName string
DeploymentName string

ServerOpts ServerOptions
}
6 changes: 6 additions & 0 deletions pkg/leaderelection/blockingelection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions pkg/leaderelection/leaderelection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/e2e/pipeline/installer/liqoctl/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1854a5e

Please sign in to comment.