0

Basically a follow-up question from the one I've asked here: Kubernetes AKS ingress & MVC routing

I have a AKS environment including an ingress controller, an Angular SPA and an .NET Core Web API. Ingress routing with a subroute, either routing to the SPA or to the Web API.

  rules:
    - host: 09ab799fd5674c4594a5.centralus.aksapp.io
      http:
        paths:
          - path: /ea/api
            pathType: Prefix
            backend:
              service:
                name: releaseeab-eab-chart
                port:
                  number: {{ .Values.externalService.port }}
          - path: /ea
            pathType: Prefix
            backend:
              service:
                name: releaseeaf-eaf-chart
                port:
                  number: {{ .Values.externalService.port }}

In the MVC solution, I set the BasePath via ENV Variable to the appropriate path, which works very well, as all links etc. are also relative to this path, working perfectly fine with the routing.

The problem is that I can't do the same with Angular; I'm using a nginx rewrite to cut the path

        location / {
            rewrite ^/ea(/|$)(.*)$ /$2 last;
            try_files $uri $uri/ =404;
        }

This leads to the correct index.html, but all the script paths are still relative: enter image description here

As Angular during the buildtime doesn't know anything about subpaths, it doesn't use the correct URL, not hitting the ingress. I know Angular can set the basepath, but I see a bit of a conceptual problem here: in the build, Angular should not be aware at all of anything regarding the environments it is hosted. I also really don't want to build different artifacts for different environments , but use the same Artifacts for all of them.

From what I see, one possibility would be to hack something during the deployment like here described https://nkpremices.com/dynamically-set-angular-env-variables-in-docker/, overruling the default Angular index.html, but this gets really messy.

I'm pretty sure I'm not the first person hosting a PWA behind an Ingress subpath. Is there a proper way to make this work without artifacts per environment?

Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • Is the /ea/ subpath reflecting your stage? Something like 'early access', or why does it change? – Thomas Jul 14 '22 at 11:11
  • What about adjusting your `base href` in index.html? https://angular.io/guide/deployment#the-base-tag – Octavian Mărculescu Jul 14 '22 at 11:44
  • I just realized you are looking for a more configurable alternative. You can look into the [`APP_BASE_HREF`](https://angular.io/api/common/APP_BASE_HREF) injection token then. Maybe this can help you. Maybe read the base href from an environment variable? – Octavian Mărculescu Jul 14 '22 at 11:59
  • @Thomas /ea/ is just an example (shortcut for ExampleApp), but yes, it could be staging etc., therefore something dynamic and not hardcoded. – Matthias Müller Jul 14 '22 at 13:03
  • @OctavianMărculescu Thank you, yes that would be the solution to go, I could feed ENV variables via Kubernetes means, but as far as I'm aware, JavaScript can't read ENV variables. I'm also too late, as the initial javascripts in the Index.html are the problem, therefore the Angular App itself isn't loaded. – Matthias Müller Jul 14 '22 at 13:04
  • Oh, I see. Maybe there's still a way to do it using configMaps? https://medium.com/bb-tutorials-and-thoughts/angular-how-to-read-environment-info-at-runtime-for-ci-cd-9a788478aa9b – Octavian Mărculescu Jul 14 '22 at 13:13
  • I still can't wrap my head around your requirement. We use subdomains for testing stages and separating apps. Works great and no need to fiddle with the path. (When changing the path you would obviously need to cover that case in your e2e tests anyway - I see no benefit in that effort) – Thomas Jul 14 '22 at 13:51
  • @Thomas Thank you for the feedback, as this is a PoC of mine, I might do something conceptually wrong. But let's say I would like to have on-demand environments to test stuff. I would need to set up the DNS zones etc. each time in Azure. Wouldn't using Kubernetes Namespaces and subroutes be way easier, as it is handled all Kubernetes-internally and basically only in one place aka the Ingress? – Matthias Müller Jul 15 '22 at 06:41
  • I think you let infrastructure constraints guide you towards a bad design. Just use a wildcard subdomain. No need to have it all implemented using azure features. just point the dns to a kubernetes loadbalancer. feel free to reach out if you would like to discuss in more depth, see profile for contact details. – Thomas Jul 15 '22 at 12:13

0 Answers0