Skip to content

Commit

Permalink
refactor: handle kube-vip better
Browse files Browse the repository at this point in the history
* add constants for "KUBE_VIP" and "CPEM" VIPManager IDs
* Get the Elastic IP reservation when using Kube-VIP (used to do this previously but it got accidentally removed)

Signed-off-by: Chris Privitere <23177737+cprivitere@users.noreply.github.com>
  • Loading branch information
cprivitere committed Jun 21, 2024
1 parent a8247e9 commit 6accec5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
6 changes: 6 additions & 0 deletions api/v1beta1/packetcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const (
ClusterFinalizer = "packetcluster.infrastructure.cluster.x-k8s.io"
// NetworkInfrastructureReadyCondition reports of current status of cluster infrastructure.
NetworkInfrastructureReadyCondition clusterv1.ConditionType = "NetworkInfrastructureReady"
// EMLBVIPID is the string used to refer to the EMLB load balancer and VIP Manager type.
EMLBVIPID = "EMLB"
// CPEMID is the string used to refer to the CPEM load balancer and VIP Manager type.
CPEMID = "CPEM"
// KUBEVIPID is the string used to refer to the Kube VIP load balancer and VIP Manager type.
KUBEVIPID = "KUBE_VIP"
)

// VIPManagerType describes if the VIP will be managed by CPEM or kube-vip or Equinix Metal Load Balancer.
Expand Down
8 changes: 4 additions & 4 deletions controllers/packetcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (r *PacketClusterReconciler) reconcileNormal(ctx context.Context, clusterSc
packetCluster := clusterScope.PacketCluster

switch {
case packetCluster.Spec.VIPManager == emlb.EMLBVIPID:
case packetCluster.Spec.VIPManager == infrav1.EMLBVIPID:
if !packetCluster.Spec.ControlPlaneEndpoint.IsValid() {
// Create new EMLB object
lb := emlb.NewEMLB(r.PacketClient.GetConfig().DefaultHeader["X-Auth-Token"], packetCluster.Spec.ProjectID, packetCluster.Spec.Metro)
Expand All @@ -135,15 +135,15 @@ func (r *PacketClusterReconciler) reconcileNormal(ctx context.Context, clusterSc
return err
}
}
case packetCluster.Spec.VIPManager == "KUBE_VIP":
case packetCluster.Spec.VIPManager == infrav1.KUBEVIPID:
log.Info("KUBE_VIP VIPManager Detected")
if err := r.PacketClient.EnableProjectBGP(ctx, packetCluster.Spec.ProjectID); err != nil {
log.Error(err, "error enabling bgp for project")
return err
}
}

if packetCluster.Spec.VIPManager != emlb.EMLBVIPID {
if packetCluster.Spec.VIPManager != infrav1.EMLBVIPID {
ipReserv, err := r.PacketClient.GetIPByClusterIdentifier(ctx, clusterScope.Namespace(), clusterScope.Name(), packetCluster.Spec.ProjectID)
switch {
case errors.Is(err, packet.ErrControlPlanEndpointNotFound):
Expand Down Expand Up @@ -192,7 +192,7 @@ func (r *PacketClusterReconciler) reconcileDelete(ctx context.Context, clusterSc

packetCluster := clusterScope.PacketCluster

if packetCluster.Spec.VIPManager == emlb.EMLBVIPID {
if packetCluster.Spec.VIPManager == infrav1.EMLBVIPID {
// Create new EMLB object
lb := emlb.NewEMLB(r.PacketClient.GetConfig().DefaultHeader["X-Auth-Token"], packetCluster.Spec.ProjectID, packetCluster.Spec.Metro)

Expand Down
16 changes: 7 additions & 9 deletions controllers/packetmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
var controlPlaneEndpointAddress string
var cpemLBConfig string
var emlbID string
switch {
case machineScope.PacketCluster.Spec.VIPManager == "CPEM":
switch machineScope.PacketCluster.Spec.VIPManager {
case infrav1.CPEMID, infrav1.KUBEVIPID:
controlPlaneEndpoint, _ = r.PacketClient.GetIPByClusterIdentifier(
ctx,
machineScope.Cluster.Namespace,
Expand All @@ -368,9 +368,7 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
addrs = append(addrs, a)
}
controlPlaneEndpointAddress = controlPlaneEndpoint.GetAddress()
case machineScope.PacketCluster.Spec.VIPManager == "KUBE_VIP":
controlPlaneEndpointAddress = controlPlaneEndpoint.GetAddress()
case machineScope.PacketCluster.Spec.VIPManager == emlb.EMLBVIPID:
case infrav1.EMLBVIPID:
controlPlaneEndpointAddress = machineScope.Cluster.Spec.ControlPlaneEndpoint.Host
cpemLBConfig = "emlb:///" + machineScope.PacketCluster.Spec.Metro
emlbID = machineScope.PacketCluster.Annotations["equinix.com/loadbalancerID"]
Expand Down Expand Up @@ -406,7 +404,7 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
machineScope.SetProviderID(dev.GetId())
machineScope.SetInstanceStatus(infrav1.PacketResourceStatus(dev.GetState()))

if machineScope.PacketCluster.Spec.VIPManager == "KUBE_VIP" {
if machineScope.PacketCluster.Spec.VIPManager == infrav1.KUBEVIPID {
if err := r.PacketClient.EnsureNodeBGPEnabled(ctx, dev.GetId()); err != nil {
// Do not treat an error enabling bgp on machine as fatal
return ctrl.Result{RequeueAfter: time.Second * 20}, fmt.Errorf("failed to enable bgp on machine %s: %w", machineScope.Name(), err)
Expand All @@ -428,7 +426,7 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
log.Info("Machine instance is active", "instance-id", machineScope.ProviderID())

switch {
case machineScope.PacketCluster.Spec.VIPManager == "CPEM":
case machineScope.PacketCluster.Spec.VIPManager == infrav1.CPEMID:
controlPlaneEndpoint, _ = r.PacketClient.GetIPByClusterIdentifier(
ctx,
machineScope.Cluster.Namespace,
Expand All @@ -443,7 +441,7 @@ func (r *PacketMachineReconciler) reconcile(ctx context.Context, machineScope *s
return ctrl.Result{RequeueAfter: time.Second * 20}, nil
}
}
case machineScope.PacketCluster.Spec.VIPManager == emlb.EMLBVIPID:
case machineScope.PacketCluster.Spec.VIPManager == infrav1.EMLBVIPID:
if machineScope.IsControlPlane() {
// Create new EMLB object
lb := emlb.NewEMLB(r.PacketClient.GetConfig().DefaultHeader["X-Auth-Token"], machineScope.PacketCluster.Spec.ProjectID, machineScope.PacketCluster.Spec.Metro)
Expand Down Expand Up @@ -545,7 +543,7 @@ func (r *PacketMachineReconciler) reconcileDelete(ctx context.Context, machineSc
return fmt.Errorf("%w: %s", errMissingDevice, packetmachine.Name)
}

if machineScope.PacketCluster.Spec.VIPManager == emlb.EMLBVIPID {
if machineScope.PacketCluster.Spec.VIPManager == infrav1.EMLBVIPID {
// Create new EMLB object
lb := emlb.NewEMLB(r.PacketClient.GetConfig().DefaultHeader["X-Auth-Token"], machineScope.PacketCluster.Spec.ProjectID, packetmachine.Spec.Metro)

Expand Down
2 changes: 0 additions & 2 deletions internal/emlb/emlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ const (
loadBalancerPoolIDAnnotation = "equinix.com/loadbalancerpoolID"
// loadBalancerPoolOriginIDAnnotation is the anotation key representing the origin ID of a PacketMachine.
loadBalancerOriginIDAnnotation = "equinix.com/loadbalanceroriginID"
// EMLBVIPID is the stringused to refer to the EMLB load balancer and VIP Manager type.
EMLBVIPID = "EMLB"
// loadbalancerTokenExchangeURL is the default URL to use for Token Exchange to talk to the Equinix Metal Load Balancer API.
loadbalancerTokenExchnageURL = "https://iam.metalctrl.io/api-keys/exchange" //nolint:gosec
)
Expand Down

0 comments on commit 6accec5

Please sign in to comment.