Skip to content

Commit

Permalink
chore: optimized code (#4514)
Browse files Browse the repository at this point in the history
* chore: optimized code

Signed-off-by: zirain <[email protected]>

* revert

Signed-off-by: zirain <[email protected]>

---------

Signed-off-by: zirain <[email protected]>
  • Loading branch information
zirain authored Oct 28, 2024
1 parent 6ccbbac commit 7ad18fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
41 changes: 24 additions & 17 deletions internal/provider/kubernetes/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (r *gatewayAPIReconciler) managedGatewayClasses(ctx context.Context) ([]*gw
// so clean-up dependents.
if !gwClass.DeletionTimestamp.IsZero() &&
!slice.ContainsString(gwClass.Finalizers, gatewayClassFinalizer) {
r.log.Info("gatewayclass marked for deletion")
r.log.Info("gatewayclass marked for deletion", "name", gwClass.Name)
cc.removeMatch(&gwClass)
continue
}
Expand Down Expand Up @@ -383,8 +383,9 @@ func (r *gatewayAPIReconciler) processBackendRefs(ctx context.Context, gwcResour
"name", string(backendRef.Name))
} else {
resourceMappings.allAssociatedNamespaces.Insert(serviceImport.Namespace)
if !resourceMappings.allAssociatedServiceImports.Has(utils.NamespacedName(serviceImport).String()) {
resourceMappings.allAssociatedServiceImports.Insert(utils.NamespacedName(serviceImport).String())
key := utils.NamespacedName(serviceImport).String()
if !resourceMappings.allAssociatedServiceImports.Has(key) {
resourceMappings.allAssociatedServiceImports.Insert(key)
gwcResource.ServiceImports = append(gwcResource.ServiceImports, serviceImport)
r.log.Info("added ServiceImport to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
Expand All @@ -399,11 +400,14 @@ func (r *gatewayAPIReconciler) processBackendRefs(ctx context.Context, gwcResour
r.log.Error(err, "failed to get Backend", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
} else {
resourceMappings.allAssociatedNamespaces[backend.Namespace] = struct{}{}
backend.Status = egv1a1.BackendStatus{}
gwcResource.Backends = append(gwcResource.Backends, backend)
r.log.Info("added Backend to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
resourceMappings.allAssociatedNamespaces.Insert(backend.Namespace)
key := utils.NamespacedName(backend).String()
if !resourceMappings.allAssociatedBackends.Has(key) {
resourceMappings.allAssociatedBackends.Insert(key)
gwcResource.Backends = append(gwcResource.Backends, backend)
r.log.Info("added Backend to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
}
}
}

Expand All @@ -414,17 +418,18 @@ func (r *gatewayAPIReconciler) processBackendRefs(ctx context.Context, gwcResour
client.MatchingLabels(map[string]string{
endpointSliceLabelKey: string(backendRef.Name),
}),
client.InNamespace(string(*backendRef.Namespace)),
client.InNamespace(*backendRef.Namespace),
}
if err := r.client.List(ctx, endpointSliceList, opts...); err != nil {
r.log.Error(err, "failed to get EndpointSlices", "namespace", string(*backendRef.Namespace),
backendRefKind, string(backendRef.Name))
} else {
for _, endpointSlice := range endpointSliceList.Items {
endpointSlice := endpointSlice //nolint:copyloopvar
if !resourceMappings.allAssociatedEndpointSlices.Has(utils.NamespacedName(&endpointSlice).String()) {
resourceMappings.allAssociatedEndpointSlices.Insert(utils.NamespacedName(&endpointSlice).String())
r.log.Info("added EndpointSlice to resource tree", "namespace", endpointSlice.Namespace,
key := utils.NamespacedName(&endpointSlice).String()
if !resourceMappings.allAssociatedEndpointSlices.Has(key) {
resourceMappings.allAssociatedEndpointSlices.Insert(key)
r.log.Info("added EndpointSlice to resource tree",
"namespace", endpointSlice.Namespace,
"name", endpointSlice.Name)
gwcResource.EndpointSlices = append(gwcResource.EndpointSlices, &endpointSlice)
}
Expand Down Expand Up @@ -567,8 +572,9 @@ func (r *gatewayAPIReconciler) processOIDCHMACSecret(ctx context.Context, resour
return
}

if !resourceMap.allAssociatedSecrets.Has(utils.NamespacedName(&secret).String()) {
resourceMap.allAssociatedSecrets.Insert(utils.NamespacedName(&secret).String())
key := utils.NamespacedName(&secret).String()
if !resourceMap.allAssociatedSecrets.Has(key) {
resourceMap.allAssociatedSecrets.Insert(key)
resourceTree.Secrets = append(resourceTree.Secrets, &secret)
r.log.Info("processing OIDC HMAC Secret", "namespace", r.namespace, "name", oidcHMACSecretName)
}
Expand Down Expand Up @@ -626,8 +632,9 @@ func (r *gatewayAPIReconciler) processSecretRef(
}
}
resourceMap.allAssociatedNamespaces.Insert(secretNS) // TODO Zhaohuabing do we need this line?
if !resourceMap.allAssociatedSecrets.Has(utils.NamespacedName(secret).String()) {
resourceMap.allAssociatedSecrets.Insert(utils.NamespacedName(secret).String())
key := utils.NamespacedName(secret).String()
if !resourceMap.allAssociatedSecrets.Has(key) {
resourceMap.allAssociatedSecrets.Insert(key)
resourceTree.Secrets = append(resourceTree.Secrets, secret)
r.log.Info("processing Secret", "namespace", secretNS, "name", string(secretRef.Name))
}
Expand Down
43 changes: 23 additions & 20 deletions internal/provider/kubernetes/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,47 @@ import (
)

type resourceMappings struct {
// Map for storing Gateways' NamespacedNames.
// Set for storing Gateways' NamespacedNames.
allAssociatedGateways sets.Set[string]
// Map for storing ReferenceGrants' NamespacedNames.
// Set for storing ReferenceGrants' NamespacedNames.
allAssociatedReferenceGrants sets.Set[string]
// Map for storing ServiceImports' NamespacedNames.
// Set for storing ServiceImports' NamespacedNames.
allAssociatedServiceImports sets.Set[string]
// Map for storing EndpointSlices' NamespacedNames.
// Set for storing EndpointSlices' NamespacedNames.
allAssociatedEndpointSlices sets.Set[string]
// Map for storing Secrets' NamespacedNames.
// Set for storing Backends' NamespacedNames.
allAssociatedBackends sets.Set[string]
// Set for storing Secrets' NamespacedNames.
allAssociatedSecrets sets.Set[string]
// Map for storing ConfigMaps' NamespacedNames.
// Set for storing ConfigMaps' NamespacedNames.
allAssociatedConfigMaps sets.Set[string]
// Map for storing namespaces for Route, Service and Gateway objects.
// Set for storing namespaces for Route, Service and Gateway objects.
allAssociatedNamespaces sets.Set[string]
// Map for storing EnvoyProxies' NamespacedNames attaching to Gateway or GatewayClass.
// Set for storing EnvoyProxies' NamespacedNames attaching to Gateway or GatewayClass.
allAssociatedEnvoyProxies sets.Set[string]
// Map for storing EnvoyPatchPolicies' NamespacedNames attaching to Gateway.
// Set for storing EnvoyPatchPolicies' NamespacedNames attaching to Gateway.
allAssociatedEnvoyPatchPolicies sets.Set[string]
// Map for storing TLSRoutes' NamespacedNames attaching to various Gateway objects.
// Set for storing TLSRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedTLSRoutes sets.Set[string]
// Map for storing HTTPRoutes' NamespacedNames attaching to various Gateway objects.
// Set for storing HTTPRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedHTTPRoutes sets.Set[string]
// Map for storing GRPCRoutes' NamespacedNames attaching to various Gateway objects.
// Set for storing GRPCRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedGRPCRoutes sets.Set[string]
// Map for storing TCPRoutes' NamespacedNames attaching to various Gateway objects.
// Set for storing TCPRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedTCPRoutes sets.Set[string]
// Map for storing UDPRoutes' NamespacedNames attaching to various Gateway objects.
// Set for storing UDPRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedUDPRoutes sets.Set[string]
// Map for storing backendRefs' BackendObjectReference referred by various Route objects.
// Set for storing backendRefs' BackendObjectReference referred by various Route objects.
allAssociatedBackendRefs sets.Set[gwapiv1.BackendObjectReference]
// Map for storing ClientTrafficPolicies' NamespacedNames referred by various Route objects.
// Set for storing ClientTrafficPolicies' NamespacedNames referred by various Route objects.
allAssociatedClientTrafficPolicies sets.Set[string]
// Map for storing BackendTrafficPolicies' NamespacedNames referred by various Route objects.
// Set for storing BackendTrafficPolicies' NamespacedNames referred by various Route objects.
allAssociatedBackendTrafficPolicies sets.Set[string]
// Map for storing SecurityPolicies' NamespacedNames referred by various Route objects.
// Set for storing SecurityPolicies' NamespacedNames referred by various Route objects.
allAssociatedSecurityPolicies sets.Set[string]
// Map for storing BackendTLSPolicies' NamespacedNames referred by various Backend objects.
// Set for storing BackendTLSPolicies' NamespacedNames referred by various Backend objects.
allAssociatedBackendTLSPolicies sets.Set[string]
// Map for storing EnvoyExtensionPolicies' NamespacedNames attaching to various Gateway objects.
// Set for storing EnvoyExtensionPolicies' NamespacedNames attaching to various Gateway objects.
allAssociatedEnvoyExtensionPolicies sets.Set[string]
// extensionRefFilters is a map of filters managed by an extension.
// The key is the namespaced name, group and kind of the filter and the value is the
Expand All @@ -70,6 +72,7 @@ func newResourceMapping() *resourceMappings {
allAssociatedReferenceGrants: sets.New[string](),
allAssociatedServiceImports: sets.New[string](),
allAssociatedEndpointSlices: sets.New[string](),
allAssociatedBackends: sets.New[string](),
allAssociatedSecrets: sets.New[string](),
allAssociatedConfigMaps: sets.New[string](),
allAssociatedNamespaces: sets.New[string](),
Expand Down

0 comments on commit 7ad18fa

Please sign in to comment.