Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help request: How To Set Custom Headers with k8s Ingress .yaml (syntax) #1860

Closed
illflavored opened this issue Jun 12, 2023 · 4 comments
Closed
Labels
question Further information is requested stale

Comments

@illflavored
Copy link

illflavored commented Jun 12, 2023

Description

I'm trying to use the response-rewrite headers.set functionality found here:
https://apisix.apache.org/zh/docs/apisix/plugins/response-rewrite/

Rewrite the response header. The format is {"name": "value", ...}. This value can contain NGINX variables $varin the format, eg $remote_addr $balancer_ip.

I'm on kubernetes, so I need to be able to utilize this in .yaml form, but the documentation doesn't give an example. I've tried a few variations as a best guess. My ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: my-namespace
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-apisix"
    k8s.apisix.apache.org/rewrite-target-regex: "/my-path/(.*)"
    k8s.apisix.apache.org/rewrite-target-regex-template: "/$1"
    k8s.apisix.apache.org/http-to-https: "true"
    kubernetes.io/tls-acme: "true"
    k8s.apisix.apache.org/enable-response-rewrite: "true"
    k8s.apisix.apache.org/response-rewrite:
      headers:
        set:
          name: "Location",
          exists-action: "override"
          value: '@("https://" + context.Request.OriginalUrl.Host + context.Request.OriginalUrl.Path)'
spec:
  ingressClassName: apisix
  tls:
    - hosts:
        - mydomain.com
      secretName: my-secret
  rules:
  - host: mydomain.com
    http:
      paths:
      - backend:
          service:
            name: my-service
            port:
              number: 80
        path: /my-path
        pathType: Prefix

My kubectl apply is consistently giving me output message as such:

kubectl apply -f apisix.yaml
error: error validating "apisix.yaml": error validating data: ValidationError(Ingress.metadata.annotations.k8s.apisix.apache.org/response-rewrite): invalid type for io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.annotations: got "map", expected "string"; if you choose to ignore these errors, turn validation off with --validate=false

Specifically what I'm trying to do is port over these header rules from another API manager we're using on the inbound requests. Are there working examples of these annotations and their syntax in .yaml form?

<set-header name="Location" exists-action="override">
    <value>@("https://" + context.Request.OriginalUrl.Host + context.Request.OriginalUrl.Path)</value>
</set-header> 

Environment

  • APISIX helm chart version: apisix-1.4.0
  • APP VERSION 3.3.0
  • Operating system k8s
@tao12345666333 tao12345666333 transferred this issue from apache/apisix Jun 13, 2023
@tao12345666333 tao12345666333 added the question Further information is requested label Jun 13, 2023
@tao12345666333
Copy link
Member

If you are using APISIX Ingress controller, please refer to:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: apisix
k8s.apisix.apache.org/enable-response-rewrite: "true"
k8s.apisix.apache.org/response-rewrite-status-code: "400"
k8s.apisix.apache.org/response-rewrite-body: "bar-body"
k8s.apisix.apache.org/response-rewrite-body-base64: "false"
name: ingress-networking-v1

@Revolyssup
Copy link
Contributor

Revolyssup commented Jun 13, 2023

@illflavored Currently there is no annotation support on Ingress resource for overriding headers. Though you can use ApisixPluginConfig

We can also implement this in Ingress resource. I will create a PR for this.

@github-actions
Copy link

This issue has been marked as stale due to 90 days of inactivity. It will be closed in 30 days if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the dev@apisix.apache.org list. Thank you for your contributions.

@github-actions github-actions bot added the stale label Sep 12, 2023
@Revolyssup
Copy link
Contributor

Fixed by #1861

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale
Projects
None yet
Development

No branches or pull requests

3 participants