You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "Revolyssup (via GitHub)" <gi...@apache.org> on 2025/09/08 19:38:27 UTC

[PR] feat: add support for CORS httproutefilter [apisix-ingress-controller]

Revolyssup opened a new pull request, #2548:
URL: https://github.com/apache/apisix-ingress-controller/pull/2548

   <!-- Please answer these questions before submitting a pull request -->
   
   ### Type of change:
   
   <!-- Please delete options that are not relevant. -->
   
   <!-- Select all the options from below that matches the type your PR best -->
   
   - [ ] Bugfix
   - [ ] New feature provided
   - [ ] Improve performance
   - [ ] Backport patches
   - [ ] Documentation
   - [ ] Refactor
   - [ ] Chore
   - [ ] CI/CD or Tests
   
   ### What this PR does / why we need it:
   
   <!--- Why is this change required? What problem does it solve? -->
   <!--- If it fixes an open issue, please link to the issue here. -->
   
   ### Pre-submission checklist:
   
   <!--
   Please follow the requirements:
   1. Use Draft if the PR is not ready to be reviewed
   2. Test is required for the feat/fix PR, unless you have a good reason
   3. Doc is required for the feat PR
   4. Use a new commit to resolve review instead of `push -f`
   5. Use "request review" to notify the reviewer once you have resolved the review
   -->
   
   - [ ] Did you explain what problem does this PR solve? Or what new features have been added?
   - [ ] Have you added corresponding test cases?
   - [ ] Have you modified the corresponding document?
   - [ ] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix-ingress-controller#community) first**
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add support for CORS httproutefilter [apisix-ingress-controller]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #2548:
URL: https://github.com/apache/apisix-ingress-controller/pull/2548#discussion_r2332528357


##########
internal/adc/translator/httproute.go:
##########
@@ -129,6 +131,50 @@ func (t *Translator) fillPluginFromURLRewriteFilter(plugins adctypes.Plugins, ur
 	}
 }
 
+func (t *Translator) fillPluginFromHTTPCORSFilter(plugins adctypes.Plugins, cors *gatewayv1.HTTPCORSFilter) {
+	pluginName := adctypes.PluginCORS
+	obj := plugins[pluginName]
+	var plugin *adctypes.CorsConfig
+	if obj == nil {
+		plugin = &adctypes.CorsConfig{}
+		plugins[pluginName] = plugin
+	} else {
+		plugin = obj.(*adctypes.CorsConfig)
+	}
+
+	if len(cors.AllowOrigins) != 0 {
+		origins := make([]string, 0)

Review Comment:
   ```suggestion
   	if len(cors.AllowOrigins) > 0 {
   		origins := make([]string, len(cors.AllowOrigins))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add support for CORS httproutefilter [apisix-ingress-controller]

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on code in PR #2548:
URL: https://github.com/apache/apisix-ingress-controller/pull/2548#discussion_r2332687272


##########
internal/adc/translator/httproute.go:
##########
@@ -129,6 +131,50 @@ func (t *Translator) fillPluginFromURLRewriteFilter(plugins adctypes.Plugins, ur
 	}
 }
 
+func (t *Translator) fillPluginFromHTTPCORSFilter(plugins adctypes.Plugins, cors *gatewayv1.HTTPCORSFilter) {
+	pluginName := adctypes.PluginCORS
+	obj := plugins[pluginName]
+	var plugin *adctypes.CorsConfig
+	if obj == nil {
+		plugin = &adctypes.CorsConfig{}
+		plugins[pluginName] = plugin
+	} else {
+		plugin = obj.(*adctypes.CorsConfig)
+	}
+
+	if len(cors.AllowOrigins) != 0 {
+		origins := make([]string, 0)
+		for _, allowOrigin := range cors.AllowOrigins {
+			origins = append(origins, string(allowOrigin))
+		}
+		plugin.AllowOrigins = strings.Join(origins, ",")
+	}
+
+	if len(cors.AllowHeaders) != 0 {
+		headers := make([]string, 0)
+		for _, allowHeader := range cors.AllowHeaders {
+			headers = append(headers, string(allowHeader))
+		}
+		plugin.AllowHeaders = strings.Join(headers, ",")
+	}
+
+	if len(cors.AllowMethods) != 0 {
+		methods := make([]string, 0)
+		for _, allowMethod := range cors.AllowMethods {
+			methods = append(methods, string(allowMethod))
+		}
+		plugin.AllowHeaders = strings.Join(methods, ",")

Review Comment:
   Sorry this was a type. Headers and Methods were not being asserted in tests so it was missed. I have fixed it and added assertion for headers and methods.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add support for CORS httproutefilter [apisix-ingress-controller]

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup merged PR #2548:
URL: https://github.com/apache/apisix-ingress-controller/pull/2548


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add support for CORS httproutefilter [apisix-ingress-controller]

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on code in PR #2548:
URL: https://github.com/apache/apisix-ingress-controller/pull/2548#discussion_r2332187501


##########
test/e2e/gatewayapi/httproute.go:
##########
@@ -1702,6 +1702,73 @@ spec:
       port: 80
 `
 
+		var corsTestService = `
+apiVersion: v1
+kind: Service
+metadata:
+  name: cors-test-service
+spec:
+  selector:
+    app: cors-test
+  ports:
+  - port: 80
+    targetPort: 5678
+---
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: cors-test
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: cors-test
+  template:
+    metadata:
+      labels:
+        app: cors-test
+    spec:
+      containers:
+      - name: cors-test
+        image: hashicorp/http-echo
+        args: ["-text=hello", "-listen=:5678"]

Review Comment:
   Using this echo server for upstream because httpbin always adds the cors headers automatically.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add support for CORS httproutefilter [apisix-ingress-controller]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #2548:
URL: https://github.com/apache/apisix-ingress-controller/pull/2548#discussion_r2332525066


##########
internal/adc/translator/httproute.go:
##########
@@ -129,6 +131,50 @@ func (t *Translator) fillPluginFromURLRewriteFilter(plugins adctypes.Plugins, ur
 	}
 }
 
+func (t *Translator) fillPluginFromHTTPCORSFilter(plugins adctypes.Plugins, cors *gatewayv1.HTTPCORSFilter) {
+	pluginName := adctypes.PluginCORS
+	obj := plugins[pluginName]
+	var plugin *adctypes.CorsConfig
+	if obj == nil {
+		plugin = &adctypes.CorsConfig{}
+		plugins[pluginName] = plugin
+	} else {
+		plugin = obj.(*adctypes.CorsConfig)
+	}
+
+	if len(cors.AllowOrigins) != 0 {
+		origins := make([]string, 0)
+		for _, allowOrigin := range cors.AllowOrigins {
+			origins = append(origins, string(allowOrigin))
+		}
+		plugin.AllowOrigins = strings.Join(origins, ",")
+	}
+
+	if len(cors.AllowHeaders) != 0 {
+		headers := make([]string, 0)
+		for _, allowHeader := range cors.AllowHeaders {
+			headers = append(headers, string(allowHeader))
+		}
+		plugin.AllowHeaders = strings.Join(headers, ",")
+	}
+
+	if len(cors.AllowMethods) != 0 {
+		methods := make([]string, 0)
+		for _, allowMethod := range cors.AllowMethods {
+			methods = append(methods, string(allowMethod))
+		}
+		plugin.AllowHeaders = strings.Join(methods, ",")

Review Comment:
   headers?
   Is the test case coverage incomplete?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org