Skip to content

Commit

Permalink
Harmonize imports, func layout and log usage in apinetlet controlle…
Browse files Browse the repository at this point in the history
…rs (#315)
  • Loading branch information
balpert89 authored Aug 9, 2024
1 parent 65060ff commit a644e0e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 169 deletions.
78 changes: 35 additions & 43 deletions apinetlet/controllers/loadbalancer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"strings"

"github.com/go-logr/logr"
"github.com/ironcore-dev/controller-utils/clientutils"
"golang.org/x/exp/slices"

apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
"github.com/ironcore-dev/ironcore-net/apimachinery/api/net"
apinetletclient "github.com/ironcore-dev/ironcore-net/apinetlet/client"
Expand All @@ -18,11 +19,12 @@ import (
apinetv1alpha1ac "github.com/ironcore-dev/ironcore-net/client-go/applyconfigurations/core/v1alpha1"
metav1ac "github.com/ironcore-dev/ironcore-net/client-go/applyconfigurations/meta/v1"
"github.com/ironcore-dev/ironcore-net/client-go/ironcorenet"

"github.com/ironcore-dev/controller-utils/clientutils"
commonv1alpha1 "github.com/ironcore-dev/ironcore/api/common/v1alpha1"
ipamv1alpha1 "github.com/ironcore-dev/ironcore/api/ipam/v1alpha1"
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
"github.com/ironcore-dev/ironcore/utils/predicates"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -76,15 +78,15 @@ func (r *LoadBalancerReconciler) Reconcile(ctx context.Context, req ctrl.Request
func (r *LoadBalancerReconciler) deleteGone(ctx context.Context, log logr.Logger, loadBalancerKey client.ObjectKey) (ctrl.Result, error) {
log.V(1).Info("Delete gone")

log.V(1).Info("Deleting any matching apinet public ips")
log.V(1).Info("Deleting any matching APINet load balancers")
if err := r.APINetClient.DeleteAllOf(ctx, &apinetv1alpha1.LoadBalancer{},
client.InNamespace(r.APINetNamespace),
apinetletclient.MatchingSourceKeyLabels(r.Scheme(), r.RESTMapper(), loadBalancerKey, &networkingv1alpha1.LoadBalancer{}),
); err != nil {
return ctrl.Result{}, fmt.Errorf("error deleting apinet public ips: %w", err)
return ctrl.Result{}, fmt.Errorf("error deleting APINet load balancers: %w", err)
}

log.V(1).Info("Issued delete for any leftover APINet public ip")
log.V(1).Info("Issued delete for any leftover APINet load balancer")
return ctrl.Result{}, nil
}

Expand All @@ -111,7 +113,7 @@ func (r *LoadBalancerReconciler) delete(ctx context.Context, log logr.Logger, lo
}
if err := r.APINetClient.Delete(ctx, apiNetLoadBalancer); err != nil {
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("error deleting apinet load balancer: %w", err)
return ctrl.Result{}, fmt.Errorf("error deleting APINet load balancer: %w", err)
}

log.V(1).Info("APINet load balancer is gone, removing finalizer")
Expand Down Expand Up @@ -152,7 +154,7 @@ func (r *LoadBalancerReconciler) reconcile(ctx context.Context, log logr.Logger,
log.V(1).Info("Applying APINet load balancer")
apiNetLoadBalancer, err := r.applyAPINetLoadBalancer(ctx, loadBalancer, apiNetNetworkName)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error applying apinet load balancer: %w", err)
return ctrl.Result{}, fmt.Errorf("error applying APINet load balancer: %w", err)
}

log.V(1).Info("Manage APINet load balancer routing")
Expand All @@ -172,11 +174,7 @@ func (r *LoadBalancerReconciler) reconcile(ctx context.Context, log logr.Logger,
return ctrl.Result{}, nil
}

func (r *LoadBalancerReconciler) manageAPINetLoadBalancerRouting(
ctx context.Context,
loadBalancer *networkingv1alpha1.LoadBalancer,
apiNetLoadBalancer *apinetv1alpha1.LoadBalancer,
) error {
func (r *LoadBalancerReconciler) manageAPINetLoadBalancerRouting(ctx context.Context, loadBalancer *networkingv1alpha1.LoadBalancer, apiNetLoadBalancer *apinetv1alpha1.LoadBalancer) error {
loadBalancerRouting := &networkingv1alpha1.LoadBalancerRouting{}
if err := r.Get(ctx, client.ObjectKeyFromObject(loadBalancer), loadBalancerRouting); client.IgnoreNotFound(err) != nil {
return fmt.Errorf("error getting load balancer routing: %w", err)
Expand Down Expand Up @@ -220,14 +218,12 @@ func (r *LoadBalancerReconciler) manageAPINetLoadBalancerRouting(
Destinations: apiNetDsts,
}
if err := r.APINetClient.Patch(ctx, apiNetLoadBalancerRouting, client.Apply, fieldOwner, client.ForceOwnership); err != nil {
return fmt.Errorf("error applying apinet load balancer routing: %w", err)
return fmt.Errorf("error applying APINet load balancer routing: %w", err)
}
return nil
}

func (r *LoadBalancerReconciler) getPublicLoadBalancerAPINetIPs(
loadBalancer *networkingv1alpha1.LoadBalancer,
) []*apinetv1alpha1ac.LoadBalancerIPApplyConfiguration {
func (r *LoadBalancerReconciler) getPublicLoadBalancerAPINetIPs(loadBalancer *networkingv1alpha1.LoadBalancer) []*apinetv1alpha1ac.LoadBalancerIPApplyConfiguration {
res := make([]*apinetv1alpha1ac.LoadBalancerIPApplyConfiguration, len(loadBalancer.Spec.IPFamilies))
for i, ipFamily := range loadBalancer.Spec.IPFamilies {
res[i] = apinetv1alpha1ac.LoadBalancerIP().
Expand All @@ -237,10 +233,7 @@ func (r *LoadBalancerReconciler) getPublicLoadBalancerAPINetIPs(
return res
}

func (r *LoadBalancerReconciler) getInternalLoadBalancerAPINetIPs(
ctx context.Context,
loadBalancer *networkingv1alpha1.LoadBalancer,
) ([]*apinetv1alpha1ac.LoadBalancerIPApplyConfiguration, error) {
func (r *LoadBalancerReconciler) getInternalLoadBalancerAPINetIPs(ctx context.Context, loadBalancer *networkingv1alpha1.LoadBalancer) ([]*apinetv1alpha1ac.LoadBalancerIPApplyConfiguration, error) {
var ips []*apinetv1alpha1ac.LoadBalancerIPApplyConfiguration
for i, ip := range loadBalancer.Spec.IPs {
switch {
Expand Down Expand Up @@ -300,34 +293,33 @@ func (r *LoadBalancerReconciler) applyAPINetLoadBalancer(ctx context.Context, lo
ips = r.getPublicLoadBalancerAPINetIPs(loadBalancer)
}

apiNetLoadBalancerApplyCfg :=
apinetv1alpha1ac.LoadBalancer(string(loadBalancer.UID), r.APINetNamespace).
WithLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer)).
WithSpec(apinetv1alpha1ac.LoadBalancerSpec().
WithType(apiNetLoadBalancerType).
WithNetworkRef(corev1.LocalObjectReference{Name: apiNetNetworkName}).
WithIPs(ips...).
WithPorts(loadBalancerPortsToAPINetLoadBalancerPortConfigs(loadBalancer.Spec.Ports)...).
WithSelector(metav1ac.LabelSelector().WithMatchLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer))).
WithTemplate(
apinetv1alpha1ac.InstanceTemplate().
WithLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer)).
WithSpec(apinetv1alpha1ac.InstanceSpec().
WithAffinity(apinetv1alpha1ac.Affinity().
WithInstanceAntiAffinity(apinetv1alpha1ac.InstanceAntiAffinity().WithRequiredDuringSchedulingIgnoredDuringExecution(
apinetv1alpha1ac.InstanceAffinityTerm().
WithTopologyKey(apinetv1alpha1.TopologyZoneLabel).
WithLabelSelector(metav1ac.LabelSelector().WithMatchLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer))),
)),
),
apiNetLoadBalancerApplyCfg := apinetv1alpha1ac.LoadBalancer(string(loadBalancer.UID), r.APINetNamespace).
WithLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer)).
WithSpec(apinetv1alpha1ac.LoadBalancerSpec().
WithType(apiNetLoadBalancerType).
WithNetworkRef(corev1.LocalObjectReference{Name: apiNetNetworkName}).
WithIPs(ips...).
WithPorts(loadBalancerPortsToAPINetLoadBalancerPortConfigs(loadBalancer.Spec.Ports)...).
WithSelector(metav1ac.LabelSelector().WithMatchLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer))).
WithTemplate(
apinetv1alpha1ac.InstanceTemplate().
WithLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer)).
WithSpec(apinetv1alpha1ac.InstanceSpec().
WithAffinity(apinetv1alpha1ac.Affinity().
WithInstanceAntiAffinity(apinetv1alpha1ac.InstanceAntiAffinity().WithRequiredDuringSchedulingIgnoredDuringExecution(
apinetv1alpha1ac.InstanceAffinityTerm().
WithTopologyKey(apinetv1alpha1.TopologyZoneLabel).
WithLabelSelector(metav1ac.LabelSelector().WithMatchLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), loadBalancer))),
)),
),
),
)
),
),
)
apiNetLoadBalancer, err := r.APINetInterface.CoreV1alpha1().
LoadBalancers(r.APINetNamespace).
Apply(ctx, apiNetLoadBalancerApplyCfg, metav1.ApplyOptions{FieldManager: string(fieldOwner), Force: true})
if err != nil {
return nil, fmt.Errorf("error applying apinet load balancer: %w", err)
return nil, fmt.Errorf("error applying APINet load balancer: %w", err)
}
return apiNetLoadBalancer, nil
}
Expand Down
29 changes: 15 additions & 14 deletions apinetlet/controllers/natgateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"slices"

"github.com/go-logr/logr"
"github.com/ironcore-dev/controller-utils/clientutils"

apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
apinetletclient "github.com/ironcore-dev/ironcore-net/apinetlet/client"
"github.com/ironcore-dev/ironcore-net/apinetlet/handler"
apinetv1alpha1ac "github.com/ironcore-dev/ironcore-net/client-go/applyconfigurations/core/v1alpha1"
"github.com/ironcore-dev/ironcore-net/client-go/ironcorenet"

"github.com/ironcore-dev/controller-utils/clientutils"
commonv1alpha1 "github.com/ironcore-dev/ironcore/api/common/v1alpha1"
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
"github.com/ironcore-dev/ironcore/utils/generic"
Expand Down Expand Up @@ -102,7 +104,7 @@ func (r *NATGatewayReconciler) delete(ctx context.Context, log logr.Logger, natG
}
if err := r.APINetClient.Delete(ctx, apiNetNATGateway); err != nil {
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("error deleting apinet NAT Gateway: %w", err)
return ctrl.Result{}, fmt.Errorf("error deleting APINet NAT Gateway: %w", err)
}

log.V(1).Info("APINet NAT gateway is gone, removing finalizer")
Expand Down Expand Up @@ -141,17 +143,16 @@ func (r *NATGatewayReconciler) reconcile(ctx context.Context, log logr.Logger, n
return ctrl.Result{}, nil
}

apiNetNATGatewayCfg :=
apinetv1alpha1ac.NATGateway(string(natGateway.UID), r.APINetNamespace).
WithLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), natGateway)).
WithSpec(apinetv1alpha1ac.NATGatewaySpec().
WithIPFamily(natGateway.Spec.IPFamily).
WithNetworkRef(corev1.LocalObjectReference{Name: networkName}).
WithPortsPerNetworkInterface(generic.Deref(
natGateway.Spec.PortsPerNetworkInterface,
networkingv1alpha1.DefaultPortsPerNetworkInterface,
)),
)
apiNetNATGatewayCfg := apinetv1alpha1ac.NATGateway(string(natGateway.UID), r.APINetNamespace).
WithLabels(apinetletclient.SourceLabels(r.Scheme(), r.RESTMapper(), natGateway)).
WithSpec(apinetv1alpha1ac.NATGatewaySpec().
WithIPFamily(natGateway.Spec.IPFamily).
WithNetworkRef(corev1.LocalObjectReference{Name: networkName}).
WithPortsPerNetworkInterface(generic.Deref(
natGateway.Spec.PortsPerNetworkInterface,
networkingv1alpha1.DefaultPortsPerNetworkInterface,
)),
)
apiNetNATGateway, err := r.APINetInterface.CoreV1alpha1().
NATGateways(r.APINetNamespace).
Apply(ctx, apiNetNATGatewayCfg, metav1.ApplyOptions{FieldManager: string(fieldOwner), Force: true})
Expand All @@ -177,7 +178,7 @@ func (r *NATGatewayReconciler) reconcile(ctx context.Context, log logr.Logger, n
}
_ = ctrl.SetControllerReference(apiNetNATGateway, apiNetNATGatewayAutoscaler, r.Scheme())
if err := r.APINetClient.Patch(ctx, apiNetNATGatewayAutoscaler, client.Apply, client.ForceOwnership, fieldOwner); err != nil {
return ctrl.Result{}, fmt.Errorf("error applying apinet NAT gateway autoscaler: %w", err)
return ctrl.Result{}, fmt.Errorf("error applying APINet NAT gateway autoscaler: %w", err)
}

natGatewayIPs := apiNetIPsToIPs(apinetv1alpha1.GetNATGatewayIPs(apiNetNATGateway))
Expand Down
31 changes: 16 additions & 15 deletions apinetlet/controllers/network_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"slices"

"github.com/go-logr/logr"
"github.com/ironcore-dev/controller-utils/clientutils"

apinetv1alpha1 "github.com/ironcore-dev/ironcore-net/api/core/v1alpha1"
apinetletclient "github.com/ironcore-dev/ironcore-net/apinetlet/client"
"github.com/ironcore-dev/ironcore-net/apinetlet/handler"
"github.com/ironcore-dev/ironcore-net/apinetlet/provider"

"github.com/ironcore-dev/controller-utils/clientutils"
ipamv1alpha1 "github.com/ironcore-dev/ironcore/api/ipam/v1alpha1"
networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
"github.com/ironcore-dev/ironcore/utils/predicates"
Expand Down Expand Up @@ -64,15 +66,15 @@ func (r *NetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
func (r *NetworkReconciler) deleteGone(ctx context.Context, log logr.Logger, networkKey client.ObjectKey) (ctrl.Result, error) {
log.V(1).Info("Delete gone")

log.V(1).Info("Deleting any matching apinet networks")
log.V(1).Info("Deleting any matching APINet networks")
if err := r.APINetClient.DeleteAllOf(ctx, &apinetv1alpha1.Network{},
client.InNamespace(r.APINetNamespace),
apinetletclient.MatchingSourceKeyLabels(r.Scheme(), r.RESTMapper(), networkKey, &networkingv1alpha1.Network{}),
); err != nil {
return ctrl.Result{}, fmt.Errorf("error deleting apinet networks: %w", err)
return ctrl.Result{}, fmt.Errorf("error deleting APINet networks: %w", err)
}

log.V(1).Info("Issued delete for any leftover apinet network")
log.V(1).Info("Issued delete for any leftover APINet network")
return ctrl.Result{}, nil
}

Expand All @@ -92,7 +94,7 @@ func (r *NetworkReconciler) delete(ctx context.Context, log logr.Logger, network
return ctrl.Result{}, nil
}

log.V(1).Info("Deleting target apinet network if any")
log.V(1).Info("Deleting target APINet network if any")
apiNetNetwork := &apinetv1alpha1.Network{
ObjectMeta: metav1.ObjectMeta{
Namespace: r.APINetNamespace,
Expand All @@ -101,18 +103,18 @@ func (r *NetworkReconciler) delete(ctx context.Context, log logr.Logger, network
}
if err := r.APINetClient.Delete(ctx, apiNetNetwork); err != nil {
if !apierrors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("error deleting target apinet network: %w", err)
return ctrl.Result{}, fmt.Errorf("error deleting target APINet network: %w", err)
}

log.V(1).Info("Target apinet network is gone, removing finalizer")
log.V(1).Info("Target APINet network is gone, removing finalizer")
if err := clientutils.PatchRemoveFinalizer(ctx, r.Client, network, networkFinalizer); err != nil {
return ctrl.Result{}, fmt.Errorf("error removing finalizer: %w", err)
}
log.V(1).Info("Removed finalizer")
return ctrl.Result{}, nil
}

log.V(1).Info("Target apinet network is not yet gone, requeueing")
log.V(1).Info("Target APINet network is not yet gone, requeueing")
return ctrl.Result{Requeue: true}, nil
}

Expand Down Expand Up @@ -146,7 +148,7 @@ func (r *NetworkReconciler) reconcile(ctx context.Context, log logr.Logger, netw
log.Error(err, "Error updating network state")
}
}
return ctrl.Result{}, fmt.Errorf("error applying apinet network: %w", err)
return ctrl.Result{}, fmt.Errorf("error applying APINet network: %w", err)
}
log = log.WithValues("ID", apiNetNetwork.Spec.ID)
log.V(1).Info("Applied APINet network")
Expand Down Expand Up @@ -199,10 +201,10 @@ func (r *NetworkReconciler) applyAPINetNetwork(ctx context.Context, log logr.Log

var peerings []apinetv1alpha1.NetworkPeering
for _, peeringClaimRef := range network.Spec.PeeringClaimRefs {
log.V(1).Info("Get apinet network for target network")
log.V(1).Info("Get APINet network for target network")
targetApinetNetwork := &apinetv1alpha1.Network{}
if err := r.APINetClient.Get(ctx, client.ObjectKey{Namespace: r.APINetNamespace, Name: string(peeringClaimRef.UID)}, targetApinetNetwork); err != nil {
log.V(1).Info("target apinet network is not created yet")
log.V(1).Info("target APINet network is not created yet")
break
}

Expand All @@ -216,7 +218,7 @@ func (r *NetworkReconciler) applyAPINetNetwork(ctx context.Context, log logr.Log
if idx != -1 {
peeringPrefixes, err := r.getAPINetNetworkPeeringPrefixes(ctx, network.Spec.Peerings[idx].Prefixes, network.Namespace)
if err != nil {
return nil, fmt.Errorf("error getting apinet network peering prefixes: %w", err)
return nil, fmt.Errorf("error getting APINet network peering prefixes: %w", err)
}

peerings = append(peerings, apinetv1alpha1.NetworkPeering{
Expand All @@ -230,13 +232,12 @@ func (r *NetworkReconciler) applyAPINetNetwork(ctx context.Context, log logr.Log

log.V(1).Info("Applying APINet network")
if err := r.APINetClient.Patch(ctx, apiNetNetwork, client.Apply, fieldOwner, client.ForceOwnership); err != nil {
return nil, fmt.Errorf("error applying apinet network: %w", err)
return nil, fmt.Errorf("error applying APINet network: %w", err)
}
return apiNetNetwork, nil
}

func (r *NetworkReconciler) getAPINetNetworkPeeringPrefixes(ctx context.Context, peeringPrefixes []networkingv1alpha1.PeeringPrefix,
networkNamespace string) ([]apinetv1alpha1.PeeringPrefix, error) {
func (r *NetworkReconciler) getAPINetNetworkPeeringPrefixes(ctx context.Context, peeringPrefixes []networkingv1alpha1.PeeringPrefix, networkNamespace string) ([]apinetv1alpha1.PeeringPrefix, error) {
apinetPeeringPrefixes := []apinetv1alpha1.PeeringPrefix{}
for _, prefix := range peeringPrefixes {
if prefix.Prefix != nil {
Expand Down
Loading

0 comments on commit a644e0e

Please sign in to comment.