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

fix: Delete unused status keys from watchable #2782

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
178 changes: 173 additions & 5 deletions internal/gatewayapi/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"context"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
v1 "sigs.k8s.io/gateway-api/apis/v1"

"github.com/envoyproxy/gateway/api/v1alpha1"
Expand Down Expand Up @@ -56,15 +57,21 @@
// so when a delete is triggered, delete all IR keys
if update.Delete || val == nil {
r.deleteAllIRKeys()
r.deleteAllStatusKeys()

Check warning on line 60 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L60

Added line #L60 was not covered by tests
return
}

var curKeys, newKeys []string
// IR keys for watchable
var curIRKeys, newIRKeys []string

Check warning on line 66 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L65-L66

Added lines #L65 - L66 were not covered by tests
// Get current IR keys
for key := range r.InfraIR.LoadAll() {
curKeys = append(curKeys, key)
curIRKeys = append(curIRKeys, key)

Check warning on line 69 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L69

Added line #L69 was not covered by tests
}

// Get the current DeletableStatus which manages status keys to be deleted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add a comment here explaining the logic

  1. Lets get all status keys from watchable and save it in this structure
  2. Lets delete any current keys that are valid from this structure
  3. The remaining keys, are the keys that are no longer valid, and will be deleted before we exit this function

deletableStatus := r.getDeletableStatus()

Check warning on line 74 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L73-L74

Added lines #L73 - L74 were not covered by tests
for _, resources := range *val {
// Translate and publish IRs.
t := &gatewayapi.Translator{
Expand Down Expand Up @@ -95,7 +102,7 @@
errChan <- err
} else {
r.InfraIR.Store(key, val)
newKeys = append(newKeys, key)
newIRKeys = append(newIRKeys, key)

Check warning on line 105 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L105

Added line #L105 was not covered by tests
}
}

Expand All @@ -114,61 +121,75 @@
gateway := gateway
key := utils.NamespacedName(gateway)
r.ProviderResources.GatewayStatuses.Store(key, &gateway.Status)
delete(deletableStatus.GatewayStatusKeys, key)

Check warning on line 124 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L124

Added line #L124 was not covered by tests
}
for _, httpRoute := range result.HTTPRoutes {
httpRoute := httpRoute
key := utils.NamespacedName(httpRoute)
r.ProviderResources.HTTPRouteStatuses.Store(key, &httpRoute.Status)
delete(deletableStatus.HTTPRouteStatusKeys, key)

Check warning on line 130 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L130

Added line #L130 was not covered by tests
}
for _, grpcRoute := range result.GRPCRoutes {
grpcRoute := grpcRoute
key := utils.NamespacedName(grpcRoute)
r.ProviderResources.GRPCRouteStatuses.Store(key, &grpcRoute.Status)
delete(deletableStatus.GRPCRouteStatusKeys, key)

Check warning on line 136 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L136

Added line #L136 was not covered by tests
}

for _, tlsRoute := range result.TLSRoutes {
tlsRoute := tlsRoute
key := utils.NamespacedName(tlsRoute)
r.ProviderResources.TLSRouteStatuses.Store(key, &tlsRoute.Status)
delete(deletableStatus.TLSRouteStatusKeys, key)

Check warning on line 143 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L143

Added line #L143 was not covered by tests
}
for _, tcpRoute := range result.TCPRoutes {
tcpRoute := tcpRoute
key := utils.NamespacedName(tcpRoute)
r.ProviderResources.TCPRouteStatuses.Store(key, &tcpRoute.Status)
delete(deletableStatus.TCPRouteStatusKeys, key)

Check warning on line 149 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L149

Added line #L149 was not covered by tests
}
for _, udpRoute := range result.UDPRoutes {
udpRoute := udpRoute
key := utils.NamespacedName(udpRoute)
r.ProviderResources.UDPRouteStatuses.Store(key, &udpRoute.Status)
delete(deletableStatus.UDPRouteStatusKeys, key)

Check warning on line 155 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L155

Added line #L155 was not covered by tests
}
for _, clientTrafficPolicy := range result.ClientTrafficPolicies {
clientTrafficPolicy := clientTrafficPolicy
key := utils.NamespacedName(clientTrafficPolicy)
r.ProviderResources.ClientTrafficPolicyStatuses.Store(key, &clientTrafficPolicy.Status)
delete(deletableStatus.ClientTrafficPolicyStatusKeys, key)

Check warning on line 161 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L161

Added line #L161 was not covered by tests
}
for _, backendTrafficPolicy := range result.BackendTrafficPolicies {
backendTrafficPolicy := backendTrafficPolicy
key := utils.NamespacedName(backendTrafficPolicy)
r.ProviderResources.BackendTrafficPolicyStatuses.Store(key, &backendTrafficPolicy.Status)
delete(deletableStatus.BackendTrafficPolicyStatusKeys, key)

Check warning on line 167 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L167

Added line #L167 was not covered by tests
}
for _, securityPolicy := range result.SecurityPolicies {
securityPolicy := securityPolicy
key := utils.NamespacedName(securityPolicy)
r.ProviderResources.SecurityPolicyStatuses.Store(key, &securityPolicy.Status)
delete(deletableStatus.SecurityPolicyStatusKeys, key)

Check warning on line 173 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L173

Added line #L173 was not covered by tests
}
for _, backendTLSPolicy := range result.BackendTLSPolicies {
backendTLSPolicy := backendTLSPolicy
key := utils.NamespacedName(backendTLSPolicy)
r.ProviderResources.BackendTLSPolicyStatuses.Store(key, &backendTLSPolicy.Status)
delete(deletableStatus.BackendTLSPolicyStatusKeys, key)

Check warning on line 179 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L179

Added line #L179 was not covered by tests
}
}
// Delete keys

// Delete IR keys
// There is a 1:1 mapping between infra and xds IR keys
delKeys := getIRKeysToDelete(curKeys, newKeys)
delKeys := getIRKeysToDelete(curIRKeys, newIRKeys)

Check warning on line 185 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L185

Added line #L185 was not covered by tests
for _, key := range delKeys {
r.InfraIR.Delete(key)
r.XdsIR.Delete(key)
}

// Delete status keys
r.deleteStatusKeys(deletableStatus)

Check warning on line 192 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L192

Added line #L192 was not covered by tests
},
)
r.Logger.Info("shutting down")
Expand All @@ -182,6 +203,153 @@
}
}

type DeletableStatus struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer StatusesToDelete

GatewayStatusKeys map[types.NamespacedName]bool
HTTPRouteStatusKeys map[types.NamespacedName]bool
GRPCRouteStatusKeys map[types.NamespacedName]bool
TLSRouteStatusKeys map[types.NamespacedName]bool
TCPRouteStatusKeys map[types.NamespacedName]bool
UDPRouteStatusKeys map[types.NamespacedName]bool

ClientTrafficPolicyStatusKeys map[types.NamespacedName]bool
BackendTrafficPolicyStatusKeys map[types.NamespacedName]bool
SecurityPolicyStatusKeys map[types.NamespacedName]bool
BackendTLSPolicyStatusKeys map[types.NamespacedName]bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we move BackendTLSPolicyStatusKeys near L212, since its an upstream resource like UDPRouteStatus

}

func (r *Runner) getDeletableStatus() *DeletableStatus {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer if this API was called getAllStatuses

it took me 2 readings to figure out your approach of collecting all statuses and then deleting the current ones from this truck and then deleting the remaining ones from watchable :)

// Maps storing status keys to be deleted
ds := &DeletableStatus{
GatewayStatusKeys: make(map[types.NamespacedName]bool),
HTTPRouteStatusKeys: make(map[types.NamespacedName]bool),
GRPCRouteStatusKeys: make(map[types.NamespacedName]bool),
TLSRouteStatusKeys: make(map[types.NamespacedName]bool),
TCPRouteStatusKeys: make(map[types.NamespacedName]bool),
UDPRouteStatusKeys: make(map[types.NamespacedName]bool),

ClientTrafficPolicyStatusKeys: make(map[types.NamespacedName]bool),
BackendTrafficPolicyStatusKeys: make(map[types.NamespacedName]bool),
SecurityPolicyStatusKeys: make(map[types.NamespacedName]bool),
BackendTLSPolicyStatusKeys: make(map[types.NamespacedName]bool),
}

// Get current status keys
for key := range r.ProviderResources.GatewayStatuses.LoadAll() {
ds.GatewayStatusKeys[key] = true
}
for key := range r.ProviderResources.HTTPRouteStatuses.LoadAll() {
ds.HTTPRouteStatusKeys[key] = true
}
for key := range r.ProviderResources.GRPCRouteStatuses.LoadAll() {
ds.GRPCRouteStatusKeys[key] = true
}
for key := range r.ProviderResources.TLSRouteStatuses.LoadAll() {
ds.TLSRouteStatusKeys[key] = true
}
for key := range r.ProviderResources.TCPRouteStatuses.LoadAll() {
ds.TCPRouteStatusKeys[key] = true
}
for key := range r.ProviderResources.UDPRouteStatuses.LoadAll() {
ds.UDPRouteStatusKeys[key] = true
}

for key := range r.ProviderResources.ClientTrafficPolicyStatuses.LoadAll() {
ds.ClientTrafficPolicyStatusKeys[key] = true
}

Check warning on line 258 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L257-L258

Added lines #L257 - L258 were not covered by tests
for key := range r.ProviderResources.BackendTrafficPolicyStatuses.LoadAll() {
ds.BackendTrafficPolicyStatusKeys[key] = true
}

Check warning on line 261 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L260-L261

Added lines #L260 - L261 were not covered by tests
for key := range r.ProviderResources.SecurityPolicyStatuses.LoadAll() {
ds.SecurityPolicyStatusKeys[key] = true
}

Check warning on line 264 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L263-L264

Added lines #L263 - L264 were not covered by tests
for key := range r.ProviderResources.BackendTLSPolicyStatuses.LoadAll() {
ds.BackendTLSPolicyStatusKeys[key] = true
}

Check warning on line 267 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L266-L267

Added lines #L266 - L267 were not covered by tests

return ds
}

func (r *Runner) deleteStatusKeys(ds *DeletableStatus) {
for key := range ds.GatewayStatusKeys {
r.ProviderResources.GatewayStatuses.Delete(key)
delete(ds.GatewayStatusKeys, key)
}
for key := range ds.HTTPRouteStatusKeys {
r.ProviderResources.HTTPRouteStatuses.Delete(key)
delete(ds.HTTPRouteStatusKeys, key)
}
for key := range ds.GRPCRouteStatusKeys {
r.ProviderResources.GRPCRouteStatuses.Delete(key)
delete(ds.GRPCRouteStatusKeys, key)
}
for key := range ds.TLSRouteStatusKeys {
r.ProviderResources.TLSRouteStatuses.Delete(key)
delete(ds.TLSRouteStatusKeys, key)
}
for key := range ds.TCPRouteStatusKeys {
r.ProviderResources.TCPRouteStatuses.Delete(key)
delete(ds.TCPRouteStatusKeys, key)
}
for key := range ds.UDPRouteStatusKeys {
r.ProviderResources.UDPRouteStatuses.Delete(key)
delete(ds.UDPRouteStatusKeys, key)
}

for key := range ds.ClientTrafficPolicyStatusKeys {
r.ProviderResources.ClientTrafficPolicyStatuses.Delete(key)
delete(ds.ClientTrafficPolicyStatusKeys, key)
}

Check warning on line 301 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L299-L301

Added lines #L299 - L301 were not covered by tests
for key := range ds.BackendTrafficPolicyStatusKeys {
r.ProviderResources.BackendTrafficPolicyStatuses.Delete(key)
delete(ds.BackendTrafficPolicyStatusKeys, key)
}

Check warning on line 305 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L303-L305

Added lines #L303 - L305 were not covered by tests
for key := range ds.SecurityPolicyStatusKeys {
r.ProviderResources.SecurityPolicyStatuses.Delete(key)
delete(ds.SecurityPolicyStatusKeys, key)
}

Check warning on line 309 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L307-L309

Added lines #L307 - L309 were not covered by tests
for key := range ds.BackendTLSPolicyStatusKeys {
r.ProviderResources.BackendTLSPolicyStatuses.Delete(key)
delete(ds.BackendTLSPolicyStatusKeys, key)
}

Check warning on line 313 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L311-L313

Added lines #L311 - L313 were not covered by tests
}

// deleteAllStatusKeys deletes all status keys stored by the subscriber.
func (r *Runner) deleteAllStatusKeys() {
// Fields of GatewayAPIStatuses
for key := range r.ProviderResources.GatewayStatuses.LoadAll() {
r.ProviderResources.GatewayStatuses.Delete(key)
}
for key := range r.ProviderResources.HTTPRouteStatuses.LoadAll() {
r.ProviderResources.HTTPRouteStatuses.Delete(key)
}
for key := range r.ProviderResources.GRPCRouteStatuses.LoadAll() {
r.ProviderResources.GRPCRouteStatuses.Delete(key)
}
for key := range r.ProviderResources.TLSRouteStatuses.LoadAll() {
r.ProviderResources.TLSRouteStatuses.Delete(key)
}
for key := range r.ProviderResources.TCPRouteStatuses.LoadAll() {
r.ProviderResources.TCPRouteStatuses.Delete(key)
}
for key := range r.ProviderResources.UDPRouteStatuses.LoadAll() {
r.ProviderResources.UDPRouteStatuses.Delete(key)
}

// Fields of PolicyStatuses
for key := range r.ProviderResources.ClientTrafficPolicyStatuses.LoadAll() {
r.ProviderResources.ClientTrafficPolicyStatuses.Delete(key)
}

Check warning on line 341 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L340-L341

Added lines #L340 - L341 were not covered by tests
for key := range r.ProviderResources.BackendTrafficPolicyStatuses.LoadAll() {
r.ProviderResources.BackendTrafficPolicyStatuses.Delete(key)
}

Check warning on line 344 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L343-L344

Added lines #L343 - L344 were not covered by tests
for key := range r.ProviderResources.SecurityPolicyStatuses.LoadAll() {
r.ProviderResources.SecurityPolicyStatuses.Delete(key)
}

Check warning on line 347 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L346-L347

Added lines #L346 - L347 were not covered by tests
for key := range r.ProviderResources.BackendTLSPolicyStatuses.LoadAll() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets move this to L334

r.ProviderResources.BackendTLSPolicyStatuses.Delete(key)
}

Check warning on line 350 in internal/gatewayapi/runner/runner.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/runner/runner.go#L349-L350

Added lines #L349 - L350 were not covered by tests
}

// getIRKeysToDelete returns the list of IR keys to delete
// based on the difference between the current keys and the
// new keys parameters passed to the function.
Expand Down
Loading