Skip to content

feat: add Gateway discovery to detect unused Gateway API resources#505

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-504
Draft

feat: add Gateway discovery to detect unused Gateway API resources#505
Copilot wants to merge 5 commits intomainfrom
copilot/fix-504

Conversation

Copy link
Contributor

Copilot AI commented Sep 17, 2025

This PR implements Gateway discovery functionality to identify unused Gateway API resources in Kubernetes clusters. The feature addresses the common issue where Gateways are created but become orphaned due to missing GatewayClasses or detached Routes.

What this PR does / why we need it?

Gateway Detection Logic:

  • Detects Gateways referencing non-existing GatewayClasses
  • Identifies Gateways with no attached Routes (HTTPRoute, TCPRoute, UDPRoute, etc.)
  • Follows the rule: "At least one valid route attachment makes the Gateway considered USED"
  • Supports all standard kor features (labels, owner references, filtering, deletion)
  • Fully integrated with kor all and multi-resource commands

Command Interface:

kor gateway --show-reason        # Standalone gateway discovery
kor gw -n production              # Using alias
kor gateways --output json        # Multiple aliases supported
kor all                           # Includes gateways in all resources scan
kor configmap,gateway,secret      # Multi-resource query support

Example Output:

Unused Gateways:
+---+------------+-----------------+--------------------------------------------+
| # | NAMESPACE  | RESOURCE NAME   | REASON                                     |
+---+------------+-----------------+--------------------------------------------+
| 1 | default    | web-gateway     | Gateway references a non-existing class   |
| 2 | prod       | api-gateway     | Gateway has no attached routes             |
+---+------------+-----------------+--------------------------------------------+

PR Checklist

  • This PR adds K8s exceptions (false positives)
  • This PR adds new code
  • This PR includes tests for new/existing code
  • This PR adds docs

Technical Implementation

Following the CONTRIBUTING.md guide, this PR includes all required files:

  • Gateway API Integration: Added sigs.k8s.io/gateway-api v1.0.0 dependency
  • Core Implementation: pkg/kor/gateways.go with Gateway discovery logic
  • Comprehensive Testing: 6 test cases in pkg/kor/gateways_test.go covering all scenarios including TCP/UDP routes
  • Test Resources: Added CreateTestGateway, CreateTestGatewayClass, and CreateTestHTTPRoute helpers in pkg/kor/create_test_resources.go
  • All Command Integration: Gateway support in pkg/kor/all.go for kor all command
  • Multi-Resource Support: Gateway support in pkg/kor/multi.go for comma-separated queries
  • Deletion Support: Gateway deletion handled via processNamespaceGateways() with delete flag
  • Exporter Integration: Gateway support in pkg/kor/exporter.go for Prometheus metrics
  • CLI Commands: cmd/kor/gateways.go for standalone gateway discovery
  • Client Management: Added GetGatewayClient() utility in pkg/kor/kor.go
  • RBAC Permissions: Updated charts/kor/templates/role.yaml with Gateway API permissions
  • Exception Configuration: Created pkg/kor/exceptions/gateways/gateways.json
  • Documentation: Complete README updates with examples and usage

Gateway API Client Handling:

Since Gateway API requires a separate client (sigs.k8s.io/gateway-api/pkg/client/clientset/versioned), this PR follows the established pattern of passing multiple specialized clients (similar to apiExtClient and dynamicClient) throughout the codebase to maintain consistency and backward compatibility.

Route Type Support

The implementation checks for all major Gateway API route types:

  • HTTPRoute (v1)
  • TCPRoute (v1alpha2)
  • UDPRoute (v1alpha2)
  • GRPCRoute and TLSRoute support ready for future versions

Backwards Compatibility

All existing functionality remains unchanged. The feature follows established kor patterns and doesn't modify any existing resource detection logic. All existing tests continue to pass.

GitHub Issue

Closes #504

Notes for your reviewers

This implementation fully adheres to the repository's contribution guidelines. All required files per CONTRIBUTING.md have been created/updated. The Gateway client is consistently passed through all affected functions to maintain the existing architectural patterns.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits September 17, 2025 20:01
Co-authored-by: yonahd <47282577+yonahd@users.noreply.github.com>
Co-authored-by: yonahd <47282577+yonahd@users.noreply.github.com>
Co-authored-by: yonahd <47282577+yonahd@users.noreply.github.com>
Copilot AI changed the title [WIP] Feature: Discover unused gateways feat: add Gateway discovery to detect unused Gateway API resources Sep 17, 2025
Copilot AI requested a review from yonahd September 17, 2025 20:14
@yonahd
Copy link
Owner

yonahd commented Nov 12, 2025

@copilot please follow the contributing docs https://github.com/yonahd/kor/blob/main/CONTRIBUTING.md

…te.go and create_test_resources.go

Co-authored-by: yonahd <47282577+yonahd@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 12, 2025

@copilot please follow the contributing docs https://github.com/yonahd/kor/blob/main/CONTRIBUTING.md

I've now completed all requirements from the CONTRIBUTING.md guide (commit 7e0262a):

Added/Updated Files:

  • ✅ pkg/kor/create_test_resources.go - Gateway test resource helpers
  • ✅ pkg/kor/all.go - Gateway in kor all command
  • ✅ pkg/kor/multi.go - Gateway in multi-resource queries
  • ✅ pkg/kor/exporter.go - Gateway in Prometheus exporter
  • ✅ Updated all cmd files to pass Gateway client

Technical Note:
Gateway API requires a separate client from the standard k8s client. I've added GetGatewayClient() and updated function signatures in all.go, multi.go, and exporter.go to maintain consistency with the existing pattern for handling multiple clients (like apiExtClient and dynamicClient).

All tests passing ✓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Discover unused gateways

2 participants