Skip to content

Commit

Permalink
fix scope index gql
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Apr 8, 2024
1 parent 48f9e5b commit 1262ada
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
apiAPIPolicyResourceIndex = "apiAPIPolicyResourceIndex"
serviceHTTPRouteIndex = "serviceHTTPRouteIndex"
httprouteScopeIndex = "httprouteScopeIndex"
gqlRouteScopeIndex = "gqlRouteScopeIndex"
configMapBackend = "configMapBackend"
configMapAPIDefinition = "configMapAPIDefinition"
secretBackend = "secretBackend"
Expand Down Expand Up @@ -499,7 +500,7 @@ func isAPIPropagatable(apiState *synchronizer.APIState) bool {
return false
}
// Only valid organization's APIs can be propagated to CP
return utils.ContainsString(validOrgs, apiState.APIDefinition.Spec.Organization)
return utils.ContainsString(validOrgs, apiState.APIDefinition.Spec.Organization)
}

func (apiReconciler *APIReconciler) resolveGQLRouteRefs(ctx context.Context, gqlRouteRefs []string,
Expand Down Expand Up @@ -1461,6 +1462,23 @@ func (apiReconciler *APIReconciler) getAPIsForScope(ctx context.Context, obj k8c
httpRoute := httpRouteList.Items[item]
requests = append(requests, apiReconciler.getAPIForHTTPRoute(ctx, &httpRoute)...)
}

gqlRouteList := &dpv1alpha2.GQLRouteList{}
if err := apiReconciler.client.List(ctx, gqlRouteList, &k8client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(gqlRouteScopeIndex, utils.NamespacedName(scope).String()),
}); err != nil {
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2625, logging.CRITICAL, "Unable to find associated GQLRoute: %s", utils.NamespacedName(scope).String()))
return []reconcile.Request{}
}

if len(gqlRouteList.Items) == 0 {
loggers.LoggerAPKOperator.Debugf("GQLRoutes for scope not found: %s", utils.NamespacedName(scope).String())
}
for item := range gqlRouteList.Items {
httpRoute := gqlRouteList.Items[item]
requests = append(requests, apiReconciler.getAPIForGQLRoute(ctx, &httpRoute)...)
}

return requests
}

Expand All @@ -1472,7 +1490,7 @@ func (apiReconciler *APIReconciler) getAPIsForBackend(ctx context.Context, obj k
loggers.LoggerAPKOperator.ErrorC(logging.PrintError(logging.Error2622, logging.TRIVIAL, "Unexpected object type, bypassing reconciliation: %v", backend))
return []reconcile.Request{}
}

httpRouteList := &gwapiv1b1.HTTPRouteList{}
if err := apiReconciler.client.List(ctx, httpRouteList, &k8client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(backendHTTPRouteIndex, utils.NamespacedName(backend).String()),
Expand Down Expand Up @@ -1668,6 +1686,25 @@ func addIndexes(ctx context.Context, mgr manager.Manager) error {
return err
}

if err := mgr.GetFieldIndexer().IndexField(ctx, &dpv1alpha2.GQLRoute{}, gqlRouteAPIIndex,
func(rawObj k8client.Object) []string {
gqlRoute := rawObj.(*dpv1alpha2.GQLRoute)
var scopes []string
for _, rule := range gqlRoute.Spec.Rules {
for _, filter := range rule.Filters {
if filter.ExtensionRef != nil && filter.ExtensionRef.Kind == constants.KindScope {
scopes = append(scopes, types.NamespacedName{
Namespace: gqlRoute.Namespace,
Name: string(filter.ExtensionRef.Name),
}.String())
}
}
}
return scopes
}); err != nil {
return err
}

// Backend to HTTPRoute indexer
if err := mgr.GetFieldIndexer().IndexField(ctx, &gwapiv1b1.HTTPRoute{}, backendHTTPRouteIndex,
func(rawObj k8client.Object) []string {
Expand Down

0 comments on commit 1262ada

Please sign in to comment.