Skip to content

Commit

Permalink
Merge pull request #2231 from Tharsanan1/adapter-to-agent
Browse files Browse the repository at this point in the history
Send GQL API
  • Loading branch information
CrowleyRajapakse authored Apr 2, 2024
2 parents 416fe0a + 932ea6f commit d5123de
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
7 changes: 7 additions & 0 deletions adapter/internal/controlplane/eventPublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ type API struct {
Vhost string `json:"vhost"`
SecurityScheme []string `json:"securityScheme"`
AuthHeader string `json:"authHeader"`
Operations []Operation `json:"operations"`
}

// Operation holds the path, verb, throttling and interceptor policy
type Operation struct {
Path string `json:"path"`
Verb string `json:"verb"`
}

// CORSPolicy hold cors configs
Expand Down
89 changes: 84 additions & 5 deletions adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (apiReconciler *APIReconciler) resolveAPIRefs(ctx context.Context, api dpv1
}

// Validate resource level extension refs resolved
extRefValErr := apiReconciler.validateHTTPRouteExtRefs(apiState)
extRefValErr := apiReconciler.validateRouteExtRefs(apiState)
if extRefValErr != nil {
return nil, extRefValErr
}
Expand All @@ -459,7 +459,7 @@ func (apiReconciler *APIReconciler) resolveAPIRefs(ctx context.Context, api dpv1
return &synchronizer.APIEvent{EventType: constants.Create, Events: []synchronizer.APIState{*apiState}, UpdatedEvents: []string{}}, nil
} else if cachedAPI, events, updated :=
apiReconciler.ods.UpdateAPIState(apiRef, apiState); updated {
if apiReconciler.apiPropagationEnabled && !apiState.APIDefinition.Spec.SystemAPI {
if apiReconciler.apiPropagationEnabled && !apiState.APIDefinition.Spec.SystemAPI {
apiHash := apiReconciler.getAPIHash(apiState)
if !hashFound || storedHash != apiHash {
apiReconciler.patchAPIHash(ctx, apiHash, apiState.APIDefinition.ObjectMeta.Name, apiState.APIDefinition.ObjectMeta.Namespace)
Expand Down Expand Up @@ -1449,7 +1449,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 @@ -2239,6 +2239,7 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,
corsPolicy := pickOneCorsForCP(&apiState)
vhost := getProdVhost(&apiState)
securityScheme, authHeader := prepareSecuritySchemeForCP(&apiState)
operations := prepareOperations(&apiState)
api := controlplane.API{
APIName: spec.APIName,
APIVersion: spec.APIVersion,
Expand All @@ -2259,6 +2260,7 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,
Vhost: vhost,
SecurityScheme: securityScheme,
AuthHeader: authHeader,
Operations: operations,
}
apiCPEvent.API = api
apiCPEvent.CRName = apiState.APIDefinition.ObjectMeta.Name
Expand All @@ -2267,7 +2269,7 @@ func (apiReconciler *APIReconciler) convertAPIStateToAPICp(ctx context.Context,

}

func (apiReconciler *APIReconciler) validateHTTPRouteExtRefs(apiState *synchronizer.APIState) error {
func (apiReconciler *APIReconciler) validateRouteExtRefs(apiState *synchronizer.APIState) error {
extRefs := []*gwapiv1b1.LocalObjectReference{}
if apiState.ProdHTTPRoute != nil {
for _, httpRoute := range apiState.ProdHTTPRoute.HTTPRoutePartitions {
Expand All @@ -2287,6 +2289,24 @@ func (apiReconciler *APIReconciler) validateHTTPRouteExtRefs(apiState *synchroni
}
}
}
if apiState.ProdGQLRoute != nil {
for _, gql := range apiState.ProdGQLRoute.GQLRoutePartitions {
for _, rule := range gql.Spec.Rules {
for _, filter := range rule.Filters {
extRefs = append(extRefs, filter.ExtensionRef)
}
}
}
}
if apiState.SandGQLRoute != nil {
for _, gql := range apiState.SandGQLRoute.GQLRoutePartitions {
for _, rule := range gql.Spec.Rules {
for _, filter := range rule.Filters {
extRefs = append(extRefs, filter.ExtensionRef)
}
}
}
}
for _, extRef := range extRefs {
if extRef != nil {
extKind := string(extRef.Kind)
Expand Down Expand Up @@ -2414,19 +2434,35 @@ func findProdSandEndpoints(apiState *synchronizer.APIState) (string, string, str
if apiState.ProdHTTPRoute != nil {
for _, backend := range apiState.ProdHTTPRoute.BackendMapping {
if len(backend.Backend.Spec.Services) > 0 {
sandEndpoint = fmt.Sprintf("%s:%d", backend.Backend.Spec.Services[0].Host, backend.Backend.Spec.Services[0].Port)
prodEndpoint = fmt.Sprintf("%s:%d", backend.Backend.Spec.Services[0].Host, backend.Backend.Spec.Services[0].Port)
endpointProtocol = string(backend.Backend.Spec.Protocol)
}
}
}
if apiState.SandHTTPRoute != nil {
for _, backend := range apiState.SandHTTPRoute.BackendMapping {
if len(backend.Backend.Spec.Services) > 0 {
sandEndpoint = fmt.Sprintf("%s:%d", backend.Backend.Spec.Services[0].Host, backend.Backend.Spec.Services[0].Port)
endpointProtocol = string(backend.Backend.Spec.Protocol)
}
}
}
if apiState.ProdGQLRoute != nil {
for _, backend := range apiState.ProdGQLRoute.BackendMapping {
if len(backend.Backend.Spec.Services) > 0 {
prodEndpoint = fmt.Sprintf("%s:%d", backend.Backend.Spec.Services[0].Host, backend.Backend.Spec.Services[0].Port)
endpointProtocol = string(backend.Backend.Spec.Protocol)
}
}
}
if apiState.SandGQLRoute != nil {
for _, backend := range apiState.SandGQLRoute.BackendMapping {
if len(backend.Backend.Spec.Services) > 0 {
sandEndpoint = fmt.Sprintf("%s:%d", backend.Backend.Spec.Services[0].Host, backend.Backend.Spec.Services[0].Port)
endpointProtocol = string(backend.Backend.Spec.Protocol)
}
}
}
return prodEndpoint, sandEndpoint, endpointProtocol
}

Expand Down Expand Up @@ -2470,6 +2506,13 @@ func getProdVhost(apiState *synchronizer.APIState) string {
}
}
}
if apiState.ProdGQLRoute != nil {
for _, gql := range apiState.ProdGQLRoute.GQLRoutePartitions {
if len(gql.Spec.Hostnames) > 0 {
return string(gql.Spec.Hostnames[0])
}
}
}
return "default.gw.wso2.com"
}

Expand Down Expand Up @@ -2522,3 +2565,39 @@ func prepareSecuritySchemeForCP(apiState *synchronizer.APIState) ([]string, stri
}
return []string{"oauth2", "oauth_basic_auth_api_key_mandatory"}, authHeader
}

func prepareOperations(apiState *synchronizer.APIState) []controlplane.Operation {
operations := []controlplane.Operation{}
if apiState.ProdHTTPRoute != nil && apiState.ProdHTTPRoute.HTTPRouteCombined != nil {
for _, rule := range apiState.ProdHTTPRoute.HTTPRouteCombined.Spec.Rules {
for _, match := range rule.Matches {
path := "/"
verb := "GET"
if match.Path != nil && match.Path.Value != nil {
path = *match.Path.Value
}
if match.Method != nil {
verb = string(*match.Method)
}
operations = append(operations, controlplane.Operation{Path: path, Verb: verb})
}
}
}
if apiState.ProdGQLRoute != nil && apiState.ProdGQLRoute.GQLRouteCombined != nil {
for _, rule := range apiState.ProdGQLRoute.GQLRouteCombined.Spec.Rules {
for _, match := range rule.Matches {
path := ""
verb := "QUERY"
if match.Path != nil {
path = *match.Path
}
if match.Type != nil {
verb = string(*match.Type)
}
operations = append(operations, controlplane.Operation{Path: path, Verb: verb})
}
}
}

return operations
}

0 comments on commit d5123de

Please sign in to comment.