Skip to content

Commit b07db5e

Browse files
committed
fix review comments.
1 parent db45f23 commit b07db5e

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

gateway/gateway-controller/pkg/api/handlers/handlers.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,15 @@ func (s *APIServer) CreateAPI(c *gin.Context) {
279279
// API was updated and no longer has policies, remove the existing policy configuration
280280
policyID := result.StoredConfig.ID + "-policies"
281281
if err := s.policyManager.RemovePolicy(policyID); err != nil {
282-
// Log at debug level since policy may not exist if API never had policies
283-
log.Debug("No policy configuration to remove", slog.String("policy_id", policyID))
282+
// Only treat "policy not found" as non-error (API may never have had policies)
283+
// Other errors (storage failures, snapshot update failures) should be logged as errors
284+
if errors.Is(err, storage.ErrPolicyNotFound) {
285+
log.Debug("No policy configuration to remove", slog.String("policy_id", policyID))
286+
} else {
287+
log.Error("Failed to remove policy configuration",
288+
slog.Any("error", err),
289+
slog.String("policy_id", policyID))
290+
}
284291
} else {
285292
log.Info("Derived policy configuration removed (API no longer has policies)",
286293
slog.String("policy_id", policyID))

gateway/gateway-controller/pkg/controlplane/client.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323
"crypto/tls"
2424
"encoding/json"
25+
"errors"
2526
"fmt"
2627
"log/slog"
2728
"net/http"
@@ -657,21 +658,28 @@ func (c *Client) handleAPIDeployedEvent(event map[string]interface{}) {
657658
slog.String("correlation_id", deployedEvent.CorrelationID))
658659
}
659660
} else if result.IsUpdate {
660-
// No policies but this is an update, so remove any existing policies
661+
// API was updated and no longer has policies, remove the existing policy configuration
661662
policyID := result.StoredConfig.ID + "-policies"
662-
if _, err := c.policyManager.GetPolicy(policyID); err == nil {
663-
if err := c.policyManager.RemovePolicy(policyID); err != nil {
664-
c.logger.Error("Failed to remove policy from policy engine",
665-
slog.Any("error", err),
663+
if err := c.policyManager.RemovePolicy(policyID); err != nil {
664+
// Only treat "not found" as non-error (API may never have had policies)
665+
// Other errors (storage failures, snapshot update failures) should be logged as errors
666+
if errors.Is(err, storage.ErrPolicyNotFound) {
667+
c.logger.Debug("No policy configuration to remove",
666668
slog.String("api_id", apiID),
667669
slog.String("policy_id", policyID),
668670
slog.String("correlation_id", deployedEvent.CorrelationID))
669671
} else {
670-
c.logger.Info("Successfully removed policy from policy engine",
672+
c.logger.Error("Failed to remove policy configuration",
673+
slog.Any("error", err),
671674
slog.String("api_id", apiID),
672675
slog.String("policy_id", policyID),
673676
slog.String("correlation_id", deployedEvent.CorrelationID))
674677
}
678+
} else {
679+
c.logger.Info("Derived policy configuration removed",
680+
slog.String("api_id", apiID),
681+
slog.String("policy_id", policyID),
682+
slog.String("correlation_id", deployedEvent.CorrelationID))
675683
}
676684
}
677685
} else if c.policyManager == nil {

gateway/gateway-controller/pkg/storage/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var (
2525
// ErrNotFound is returned when a configuration is not found
2626
ErrNotFound = errors.New("configuration not found")
2727

28+
// ErrPolicyNotFound is returned when a policy configuration is not found
29+
ErrPolicyNotFound = errors.New("policy configuration not found")
30+
2831
// ErrConflict is returned when a configuration with the same name/version already exists
2932
ErrConflict = errors.New("configuration already exists")
3033

gateway/gateway-controller/pkg/storage/policy_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *PolicyStore) Delete(id string) error {
108108

109109
policy, exists := s.policies[id]
110110
if !exists {
111-
return fmt.Errorf("policy configuration with ID %s not found", id)
111+
return fmt.Errorf("policy configuration with ID %s: %w", id, ErrPolicyNotFound)
112112
}
113113

114114
// Remove from both maps

0 commit comments

Comments
 (0)