Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unitary FirewallRule reconciliation #165

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 54 additions & 40 deletions internal/controller/firewallrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -107,7 +106,7 @@ func (r *FirewallRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request
// reconcile FirewallRules for the current node and update the
// FirewallRule with the node annotation
if previousNodeName == "" && currentNodeName != "" {
if err := r.reconcileNodeFirewallRules(ctx, log, currentNodeName); err != nil {
if err := r.reconcileNodeFirewallRules(ctx, log, currentNodeName, firewallRule); err != nil {
log.Error(err, "Failed to reconcile node FirewallRules")
return ctrl.Result{}, err
}
Expand All @@ -129,7 +128,7 @@ func (r *FirewallRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request
// Node name has changed, reconcile FirewallRules for the previous node,
// then update the FirewallRule to remove the node annotation
if previousNodeName != "" && currentNodeName != previousNodeName {
if err := r.reconcileNodeFirewallRules(ctx, log, previousNodeName); err != nil {
if err := r.reconcileNodeFirewallRules(ctx, log, previousNodeName, firewallRule); err != nil {
log.Error(err, "Failed to reconcile node FirewallRules")
return ctrl.Result{}, err
}
Expand All @@ -146,7 +145,7 @@ func (r *FirewallRuleReconciler) Reconcile(ctx context.Context, req ctrl.Request

// Node name has not changed, reconcile FirewallRules for the current node
if previousNodeName != "" && currentNodeName == previousNodeName {
if err := r.reconcileNodeFirewallRules(ctx, log, currentNodeName); err != nil {
if err := r.reconcileNodeFirewallRules(ctx, log, currentNodeName, firewallRule); err != nil {
log.Error(err, "Failed to reconcile node FirewallRules")
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -183,6 +182,7 @@ func (r *FirewallRuleReconciler) reconcileNodeFirewallRules(
ctx context.Context,
log logr.Logger,
nodeName string,
firewallrule *v1alpha1.FirewallRule,
) error {
var firewallRuleList v1alpha1.FirewallRuleList
if err := r.List(ctx, &firewallRuleList, client.MatchingFields{firewallRuleNodeNameKey: nodeName}); err != nil {
Expand All @@ -200,7 +200,7 @@ func (r *FirewallRuleReconciler) reconcileNodeFirewallRules(
return err
}
} else {
if err := r.reconcileFirewallRules(ctx, log, nodeName, firewallRuleList.Items); err != nil {
if err := r.reconcileFirewallRule(ctx, log, nodeName, firewallrule); err != nil {
log.Error(err, "Failed to reconcile FirewallRules")
return err
}
Expand All @@ -209,6 +209,38 @@ func (r *FirewallRuleReconciler) reconcileNodeFirewallRules(
return nil
}

// patchFirewallRuleStatus updates the status of a FirewallRule resource if there are any changes.
// It patches the status with the new status provided and updates the LastTransitionTime if there are differences.
//
// Parameters:
//
// ctx - The context for the request.
// r - The FirewallRuleReconciler responsible for reconciling the FirewallRule resource.
// firewallRule - The FirewallRule resource to be updated.
// newStatus - The new status to be applied to the FirewallRule resource.
//
// Returns:
//
// error - An error if the patch operation fails, otherwise nil.
func patchFirewallRuleStatus(
ctx context.Context,
r *FirewallRuleReconciler,
firewallRule *v1alpha1.FirewallRule,
newStatus v1alpha1.FirewallRuleStatus,
) error {
existingFR := firewallRule.DeepCopy()
firewallRule.Status = newStatus
firewallRule.Status.LastTransitionTime = existingFR.Status.LastTransitionTime

if !reflect.DeepEqual(firewallRule.Status, existingFR.Status) {
firewallRule.Status.LastTransitionTime = metav1.Now()
if err := r.Status().Patch(ctx, firewallRule, client.MergeFrom(existingFR)); err != nil {
return err
}
}
return nil
}

// reconcileFirewallRules reconciles the firewall rules for a specific node.
// It retrieves the node information and updates the firewall rules accordingly.
// If the node is not found, it triggers the deletion of firewall rules on the provider side.
Expand All @@ -222,13 +254,15 @@ func (r *FirewallRuleReconciler) reconcileNodeFirewallRules(
//
// Returns:
// - error: An error if the reconciliation process fails, otherwise nil.
func (r *FirewallRuleReconciler) reconcileFirewallRules(
func (r *FirewallRuleReconciler) reconcileFirewallRule(
ctx context.Context,
log logr.Logger,
nodeName string,
firewallRules []v1alpha1.FirewallRule,
firewallRule *v1alpha1.FirewallRule,
) error {
var node corev1.Node
var firewallRules v1alpha1.FirewallRuleList

if err := r.Get(ctx, types.NamespacedName{Name: nodeName}, &node); err != nil {
if apierrors.IsNotFound(err) {
if err := r.Provider.ReconcileFirewallRulesDeletion(ctx, log, nodeName); err != nil {
Expand All @@ -242,45 +276,25 @@ func (r *FirewallRuleReconciler) reconcileFirewallRules(
return err
}

status, conditions, err := r.Provider.ReconcileFirewallRules(ctx, log, nodeName, r.Provider.GetInstanceID(node), firewallRules)
if err := r.List(ctx, &firewallRules, client.MatchingFields{firewallRuleNodeNameKey: nodeName}); err != nil {
log.Error(err, "Unable to list FirewallRules")
return err
}

status, err := r.Provider.ReconcileFirewallRule(ctx, log, nodeName, r.Provider.GetInstanceID(node), firewallRule, firewallRules.Items)
if err != nil {
for _, fr := range firewallRules {
for _, condition := range conditions {
condition.ObservedGeneration = fr.Generation
meta.SetStatusCondition(&status.Conditions, condition)
}
existingFR := fr.DeepCopy()
fr.Status = status
fr.Status.LastTransitionTime = existingFR.Status.LastTransitionTime
if !reflect.DeepEqual(fr.Status, existingFR.Status) {
fr.Status.LastTransitionTime = metav1.Now()
patchErr := r.Status().Patch(ctx, &fr, client.MergeFrom(existingFR))
if patchErr != nil {
log.Error(errors.Join(patchErr, err), "Failed to patch FirewallRule status during error handling")
return fmt.Errorf("failed to patch FirewallRule status during error handling: %w", errors.Join(patchErr, err))
}
}
if patchErr := patchFirewallRuleStatus(ctx, r, firewallRule, status); patchErr != nil {
log.Error(errors.Join(patchErr, err), "Failed to patch FirewallRule status during error handling")
return fmt.Errorf("failed to patch FirewallRule status during error handling: %w", errors.Join(patchErr, err))
}

log.Error(err, "Failed to reconcile FirewallRule")
return err
}

for _, fr := range firewallRules {
for _, condition := range conditions {
meta.SetStatusCondition(&status.Conditions, condition)
condition.ObservedGeneration = fr.GetGeneration()
}
existingFR := fr.DeepCopy()
fr.Status = status
fr.Status.LastTransitionTime = existingFR.Status.LastTransitionTime
if !reflect.DeepEqual(fr.Status, existingFR.Status) {
fr.Status.LastTransitionTime = metav1.Now()
err := r.Status().Patch(ctx, &fr, client.MergeFrom(existingFR))
if err != nil {
log.Error(err, "Failed to patch FirewallRule status")
return fmt.Errorf("failed to patch FirewallRule status: %w", err)
}
}
if err := patchFirewallRuleStatus(ctx, r, firewallRule, status); err != nil {
log.Error(err, "Failed to patch FirewallRule status")
return fmt.Errorf("failed to patch FirewallRule status: %w", err)
}

return nil
Expand Down
Loading
Loading