Skip to content

Commit

Permalink
Add propagation to APIM CP
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Jun 6, 2024
1 parent 2f908b5 commit cf4675c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
25 changes: 22 additions & 3 deletions adapter/internal/controlplane/eventPublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,30 @@ type API struct {
APIHash string `json:"-"`
}

// Headers contains the request and response header modifier information
type Headers struct {
RequestHeaders HeaderModifier `json:"requestHeaders"`
ResponseHeaders HeaderModifier `json:"responseHeaders"`
}

// HeaderModifier contains header modifier values
type HeaderModifier struct {
AddHeaders []Header `json:"addHeaders"`
RemoveHeaders []string `json:"removeHeaders"`
}

// Header contains the header information
type Header struct {
Name string `json:"headerName"`
Value string `json:"headerValue,omitempty"`
}

// Operation holds the path, verb, throttling and interceptor policy
type Operation struct {
Path string `json:"path"`
Verb string `json:"verb"`
Scopes []string `json:"scopes"`
Path string `json:"path"`
Verb string `json:"verb"`
Scopes []string `json:"scopes"`
Headers Headers `json:"headers"`
}

// CORSPolicy hold cors configs
Expand Down
47 changes: 46 additions & 1 deletion adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2673,14 +2673,45 @@ func prepareOperations(apiState *synchronizer.APIState) []controlplane.Operation
if apiState.ProdHTTPRoute != nil && apiState.ProdHTTPRoute.HTTPRouteCombined != nil {
for _, rule := range apiState.ProdHTTPRoute.HTTPRouteCombined.Spec.Rules {
scopes := []string{}
requestAddHeaders := []controlplane.Header{}
responseAddHeaders := []controlplane.Header{}
requestRemoveHeaders := []string{}
responseRemoveHeaders := []string{}
for _, filter := range rule.Filters {
if filter.ExtensionRef != nil && filter.ExtensionRef.Kind == "Scope" {
scope, found := apiState.ProdHTTPRoute.Scopes[types.NamespacedName{Namespace: apiState.APIDefinition.ObjectMeta.Namespace, Name: string(filter.ExtensionRef.Name)}.String()]
if found {
scopes = append(scopes, scope.Spec.Names...)
}
}

if filter.RequestHeaderModifier != nil {
requestHeaderModifier := filter.RequestHeaderModifier
for _, addHeader := range requestHeaderModifier.Add {
requestAddHeaders = append(requestAddHeaders, controlplane.Header{Name: string(addHeader.Name), Value: string(addHeader.Value)})
}
for _, setHeader := range requestHeaderModifier.Set {
requestAddHeaders = append(requestAddHeaders, controlplane.Header{Name: string(setHeader.Name), Value: string(setHeader.Value)})
}
for _, removeHeader := range requestHeaderModifier.Remove {
requestRemoveHeaders = append(requestRemoveHeaders, removeHeader)
}
}

if filter.ResponseHeaderModifier != nil {
responseHeaderModifier := filter.ResponseHeaderModifier
for _, addHeader := range responseHeaderModifier.Add {
responseAddHeaders = append(responseAddHeaders, controlplane.Header{Name: string(addHeader.Name), Value: string(addHeader.Value)})
}
for _, setHeader := range responseHeaderModifier.Set {
responseAddHeaders = append(responseAddHeaders, controlplane.Header{Name: string(setHeader.Name), Value: string(setHeader.Value)})
}
for _, removeHeader := range responseHeaderModifier.Remove {
responseRemoveHeaders = append(responseRemoveHeaders, removeHeader)
}
}
}

for _, match := range rule.Matches {
path := "/"
verb := "GET"
Expand All @@ -2694,7 +2725,21 @@ func prepareOperations(apiState *synchronizer.APIState) []controlplane.Operation
path = path + "*"
}
path = "^" + path + "$"
operations = append(operations, controlplane.Operation{Path: path, Verb: verb, Scopes: scopes})
operations = append(operations, controlplane.Operation{
Path: path,
Verb: verb,
Scopes: scopes,
Headers: controlplane.Headers{
RequestHeaders: controlplane.HeaderModifier{
AddHeaders: requestAddHeaders,
RemoveHeaders: requestRemoveHeaders,
},
ResponseHeaders: controlplane.HeaderModifier{
AddHeaders: responseAddHeaders,
RemoveHeaders: responseRemoveHeaders,
},
},
})
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions runtime/config-deployer-service/ballerina/APIClient.bal
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,11 @@ public class APIClient {
};
routeFilters.push(replacePathFilter);
APIOperationPolicies? operationPoliciesToUse = ();
if (apkConf.apiPolicies is APIOperationPolicies) {
operationPoliciesToUse = apkConf.apiPolicies;
APIOperationPolicies? operationPolicies = apkConf.apiPolicies;
if (operationPolicies is APIOperationPolicies && operationPolicies != {}) {
if operationPolicies.request is APKOperationPolicy[] || operationPolicies.response is APKOperationPolicy[] {
operationPoliciesToUse = apkConf.apiPolicies;
}
} else {
operationPoliciesToUse = operation.operationPolicies;
}
Expand Down

0 comments on commit cf4675c

Please sign in to comment.