Skip to content

Commit

Permalink
Handle external network policy creation err for already exists scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
evyatarmeged committed Dec 15, 2024
1 parent d3f0997 commit 3cc21f5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/operator/controllers/external_traffic/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ func (r *NetworkPolicyHandler) createOrUpdateNetworkPolicy(
logrus.Debugf("Creating network policy to allow external traffic to %s (ns %s)", endpoints.GetName(), endpoints.GetNamespace())
err := r.client.Create(ctx, newPolicy)
if err != nil {
r.RecordWarningEventf(owner, ReasonCreatingExternalTrafficPolicyFailed, "failed to create external traffic network policy: %s", err.Error())
return errors.Wrap(err)
return r.handleCreationErrors(ctx, err, newPolicy, owner)
}
r.RecordNormalEvent(owner, ReasonCreatedExternalTrafficPolicy, successMsg)
return nil
Expand All @@ -99,7 +98,19 @@ func (r *NetworkPolicyHandler) createOrUpdateNetworkPolicy(
}

return r.updatePolicy(ctx, existingPolicy, newPolicy, owner)
}

func (r *NetworkPolicyHandler) handleCreationErrors(ctx context.Context, err error, policy *v1.NetworkPolicy, owner *corev1.Service) error {
if !k8serrors.IsAlreadyExists(err) {
r.RecordWarningEventf(owner, ReasonCreatingExternalTrafficPolicyFailed, "failed to create external traffic network policy: %s", err.Error())
return errors.Wrap(err)
}
existingPolicy := &v1.NetworkPolicy{}
err = r.client.Get(ctx, types.NamespacedName{Name: policy.Name, Namespace: policy.Namespace}, existingPolicy)
if err != nil {
return errors.Wrap(err) // Don't retry anymore
}
return r.updatePolicy(ctx, existingPolicy, policy, owner)
}

func (r *NetworkPolicyHandler) updatePolicy(ctx context.Context, existingPolicy *v1.NetworkPolicy, newPolicy *v1.NetworkPolicy, owner *corev1.Service) error {
Expand Down

0 comments on commit 3cc21f5

Please sign in to comment.