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

Allow ByteStash to work under subpath #21

Closed
Trungtin1011 opened this issue Nov 2, 2024 · 19 comments · Fixed by #23 or #30
Closed

Allow ByteStash to work under subpath #21

Trungtin1011 opened this issue Nov 2, 2024 · 19 comments · Fixed by #23 or #30
Assignees
Milestone

Comments

@Trungtin1011
Copy link
Contributor

Hi, I currently figured out that ByteStash cannot be hosted under subpath for Ingress deployment on Kubernetes.

Can you by somehow add the feature so that ByteStash can be hosted under subpath (/bytestash)?

@jordan-dalby jordan-dalby self-assigned this Nov 2, 2024
@jordan-dalby jordan-dalby added this to the v1.3.0 milestone Nov 2, 2024
@jordan-dalby jordan-dalby linked a pull request Nov 2, 2024 that will close this issue
@jordan-dalby
Copy link
Owner

jordan-dalby commented Nov 2, 2024

@Trungtin1011
I just released a dev version, can you test it?
ghcr.io/jordan-dalby/bytestash-dev:pr-23

Use BASE_PATH environment variable to set the desired path.
BASE_PATH=/bytestash

@Trungtin1011
Copy link
Contributor Author

Hi @jordan-dalby,

Thank you for your quick response. I've just tried with the new image and here are the results:

  1. API endpoint /api/snippets works fine with BASE_PATH environment variable setting
  2. I used Kubernetes Ingress with pathType = Prefix for Bytestash, but it seemed to be unable to load assets files for the UI such as assets/index-DhTxxoCL.js and assets/index-hGZFs1YF.css. The server cannot append the subpath to load those files (below image). I've also tried with Ingress re-write but not worked.
Screenshot 2024-11-03 at 10 25 21

I very appreciate that you are listening to our requests and hope this can be done in the next release.

@jordan-dalby
Copy link
Owner

@Trungtin1011
Can you send your Kubernetes Ingress file so I can take a look? It could be a configuration issue.

@Trungtin1011
Copy link
Contributor Author

@jordan-dalby,

Here is my Ingress manifest:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  labels:
    app: bytestash
  name: bytestash
spec:
  ingressClassName: nginx
  rules:
    - host: my-domain.com
      http:
        paths:
          - backend:
              service:
                name: bytestash
                port:
                  number: 5000
            path: /bytestash
            pathType: Prefix

@jordan-dalby
Copy link
Owner

jordan-dalby commented Nov 4, 2024

@Trungtin1011
Thanks, can you give this a try? All I did was add some annotations and change the path.
The issue is that ByteStash serves static files from /assets/file.js, not from /path/assets/file.js because the assets are built into the app and statically served to the client. So basically all I did was rewrite the target so that it points to /assets/file.js instead of /path/assets/file.js.

Let me know if it works!

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  labels:
    app: bytestash
  name: bytestash
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  ingressClassName: nginx
  rules:
    - host: my-domain.com
      http:
        paths:
          - backend:
              service:
                name: bytestash
                port:
                  number: 5000
            path: /bytestash(/|$)(.*)
            pathType: Prefix

@Trungtin1011
Copy link
Contributor Author

Trungtin1011 commented Nov 4, 2024

Hi @jordan-dalby,

The above example did not work, because using rewrite-target for nginx requires pathType = ImplementationSpecific (Warning: path /bytestash(/|$)(.*) cannot be used with pathType Prefix), so I tried with ImplementationSpecific also... The result is still the same.

I also tried with AWS LB Controller kind of Ingress but did not work.

@jordan-dalby
Copy link
Owner

@Trungtin1011
I haven't worked with Kubernetes Ingress before, are there any other ways you can think of to set the target to ignore the path?

I have one more idea on my end, but of course if we can fix this by modifying the ingress that would be best.

@Trungtin1011
Copy link
Contributor Author

@jordan-dalby,

In my opinion, if re-writing the target did not work, we have to try to update the application code.

I think we should proceed with your other idea

@jordan-dalby
Copy link
Owner

@Trungtin1011
I just pushed a new version, can you test it please?
ghcr.io/jordan-dalby/bytestash-dev:pr-23

Again, make sure that the base path is specified BASE_PATH=/bytestash

@jordan-dalby
Copy link
Owner

jordan-dalby commented Nov 4, 2024

@Trungtin1011
With the latest changes I was able to get bytestash working on my localhost with the /bytestash prefix using the same configuration file as you provided. Just make sure your deployment has the BASE_PATH environment variable set.

Please reopen if you have any issues.

@Trungtin1011
Copy link
Contributor Author

Hi @jordan-dalby,

Now I know why it did not work from my side.

I'd pulled your branch and run locally with docker-compose and it worked. The funny thing is - the redirection of the URL has been done from the server internally - while it should be done from the Kubernetes Ingress side.

If you have noticed, when you type for example http://localhost:5000, It immediately get redirected into http://localhost:5000/bytestash/ <- this is not really what everybody want.

So, I made some change to my ingress configuration like this (of course with BASE_PATH=/bytestash env):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bytestash
spec:
  ingressClassName: nginx
  rules:
  - host: my-domain.com
    http:
      paths:
      - backend:
          service:
            name: bytestash
            port:
              number: 5000
        path: /
        pathType: ImplementationSpecific

After I hit http://my-domain.com -> It works. The same when I hit http://my-domain.com/bytestash.

It failed when I change my Ingress path to /bytestash instead of just / - which is what I really want here.

@jordan-dalby
Copy link
Owner

@Trungtin1011
Ah okay I see. I didn't take this use case into account. Thanks.

@Trungtin1011
Copy link
Contributor Author

Trungtin1011 commented Nov 4, 2024

Thank you for understanding @jordan-dalby

My team is using you product for saving custom snippets and because we are currently hosting a lot of applications under 1 single Domain, adding support for subpath is really important and helpful.

If you have time, please correct the behavior of ByteStash with subpath for Kubernetes. The correct behavior should be:

  1. Set BASE_PATH environment variable (for example /bytestash)
  2. Ingress resource is configured with path: /bytestash and pathType: ImplementationSpecific

Thank you,

@jordan-dalby
Copy link
Owner

@Trungtin1011
Let me see what I can do. I'll send over builds to test as and when they are ready.

@jordan-dalby jordan-dalby reopened this Nov 4, 2024
@jordan-dalby jordan-dalby modified the milestones: v1.3.0, v1.4.0 Nov 4, 2024
@jordan-dalby jordan-dalby linked a pull request Nov 5, 2024 that will close this issue
@jordan-dalby
Copy link
Owner

@Trungtin1011
Please try this version, I removed all of the crap that was in there so hoping it should "just work". Forget the BASE_PATH environment variable.
ghcr.io/jordan-dalby/bytestash-dev:pr-30

@Trungtin1011
Copy link
Contributor Author

@jordan-dalby,

I don't understand why removing the BASE_PATH environment variable?

@jordan-dalby
Copy link
Owner

@Trungtin1011 ah ignore that. I figured out what the issue is. The client is built and served statically from /client/build, but when we use the ingress server these files are actually in /bytestash/client/build. This would be an easy fix if I could modify the client side code to use the /bytestash prefix but I can't do that without affecting everyone. The next best solution is to rewrite the requests when they come in to use the /bytestash prefix, but this is proving to be more tricky than anticipated!

I am working on it. I will ping again when I have a proper build.

@jordan-dalby
Copy link
Owner

jordan-dalby commented Nov 5, 2024

@Trungtin1011
I think I have it working now. Please re-test version
ghcr.io/jordan-dalby/bytestash-dev:pr-30

You do need to set BASE_PATH for the frontend to communicate with the API.

env:
- name: BASE_PATH
   value: /bytestash

Let me know if it's fixed!

@jordan-dalby jordan-dalby modified the milestones: v1.4.0, v1.3.0 Nov 5, 2024
@Trungtin1011
Copy link
Contributor Author

Trungtin1011 commented Nov 5, 2024

Thank you @jordan-dalby, it works now 💯

I very appreciate your work during the last few days. Can't wait to see the new 1.3.0 version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants