Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

Spring MVC redirect location with additional K8S Ingress path

I'm developing an simple Spring Boot application and trying to deploy it in cluster.

Controller looks like this:

@Controller
@RequestMapping("/")
@RequiredArgsConstructor
public class UiController {
    private final ScenarioRepository scenarioRepository;
    
    @GetMapping
    public String index() {
        return "redirect:/scenarios";
    }

    @GetMapping("/debug")
    public String debug(HttpServletRequest request) {
        log.info("RequestURI: {}", request.getRequestURI());
        log.info("Context path: {}", request.getContextPath());
        log.info("Servlet path: {}", request.getSession());
        log.info("Path translated: {}", request.getPathTranslated());
        log.info("Path info: {}", request.getPathInfo());
        return "redirect:/scenarios";
    }

    @GetMapping("/scenarios")
    public String scenarios() {
        List<Scenario> scenarios = scenarioRepository.findAll();
        model.addAttribute("scenarios", scenarios);
        return "scenarios";
    }
}

Ingress configuration:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myservice
  namespace: dev
  labels:
    app.kubernetes.io/name: myservice
    app.kubernetes.io/instance: myservice
    app.kubernetes.io/version: "1.0"
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: dev.cluster
      http:
        paths:
          - path: /apps/myservice/(.*)
            backend:
              serviceName: myservice
              servicePort: http

No extra web/spring configuration.

With this configuration I expect following behaviour:

  • Locally: request to http://localhost/ redirects to http://localhost/scenarios
  • Cluster: request to http://dev.cluster/apps/myservice/ redirects to http://dev.cluster/apps/myservice/scenarios

Locally in my IDE it is work correctly. But when I deploy it into K8S index request redirects me to http://dev.cluster/scenarios with missing ingress path /apps/myservice/.

Access /debug in cluster produces following output:

12:43:21.338 Servlet path: org.apache.catalina.session.StandardSessionFacade@12da0eae
12:43:21.338 Path translated: null
12:43:21.338 Path info: null
12:43:21.236 RequestURI: /debug
12:43:21.236 Context path: 

And location header in response: http://dev.cluster/scenarios. Obviously, spring does not know anything about ingresses rewrite and generates illegal redirect URL.

So, question is: how do I fix this configuration issue?

question from:https://stackoverflow.com/questions/65870001/spring-mvc-redirect-location-with-additional-k8s-ingress-path

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...