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

Be able to set route id in Kubernetes resource #1881

Open
frankkoornstra opened this issue Jun 28, 2023 · 9 comments
Open

Be able to set route id in Kubernetes resource #1881

frankkoornstra opened this issue Jun 28, 2023 · 9 comments
Labels
discuss enhancement New feature or request

Comments

@frankkoornstra
Copy link

Description

When a route gets created from the Kubernetes resource there's no way to create the id deterministically (by providing a route_id for example) or to even get the id from the status of the resource; only when I log in to the dashboard, I can get the route id.

I need the route id to be able to programmatically create consumers that I want to restrict to a route with consumer-restrict through the Admin API . Also I need it to analyze my reported traffic in a completely different service.

I would be very happy if we could determine the id beforehand with an optional route_id in the spec. If that's not possible, could the id be returned in the status of the k8s resource?

@tao12345666333 tao12345666333 added enhancement New feature or request discuss labels Jun 28, 2023
@tao12345666333
Copy link
Member

Here are a few clarifications:

  1. Do not mix declarative configuration and admin API

  2. It is not a good idea to rely on ids that have no clear meaning

But if you want to get the route id, it's a good idea to go back to status.

You can also check out the current build logic, it's fixed

@frankkoornstra
Copy link
Author

frankkoornstra commented Jun 28, 2023

Being able to determine the ID beforehand - let's say user-management - would save us a lot of trouble because we can rely on a naming convention and we'd now we'd have the right route. We'd also not have to communicate a random id to all the services that use it.

Re 1: I don't think I understand what you mean. The Admin API already exposes the possibility of determining the ID beforehand as far as I can see, it's just that the Ingress Controller does not make use of it.
Re 2: I agree so that's why I want to make the ID mean something, say user-management, something I'd like to control from the k8s resource 😃

@shreemaan-abhishek
Copy link
Contributor

@frankkoornstra are you planning to raise a PR?

@tao12345666333
Copy link
Member

Because in a Kubernetes environment (through declarative configuration), its name is obviously more intuitive. However, we also need to consider isolation based on namespaces. Users may deploy the same content in multiple namespaces, so we cannot directly use its name as an ID. Therefore, we regenerate its ID.

@frankkoornstra
Copy link
Author

frankkoornstra commented Jul 5, 2023

For clarity: I'm not suggesting using the actual metadata.name of the k8s resource for its id, but instead introduce a (nullable) field in the spec, maybe spec.route_id that could be set to an arbitrary (but predetermined) value and would be used to make the id for the route. The maker of the ApisixRoute k8s resource would be responsible for making the id unique. If the spec field would be empty, we could fall back to the current id creation mechanism.

So you'd have something like this:

apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
  name: echo
spec:
  route_id: echo-route    <-- This would become the id of the route
  http:
    - name: echo-free
      match:
...

@shreemaan-abhishek I don't have the faintest idea how to write Go so sorry to say that I can't raise a PR.

@Gallardot
Copy link
Member

After the APISIX Route is submitted as a k8s resource, the name of the generated apisix route is named using the following rule. How about using the same rule for the ID?

route := apisixv1.NewDefaultRoute()
route.Name = apisixv1.ComposeRouteName(ar.Namespace, ar.Name, part.Name)
route.ID = id.GenID(route.Name)

https://github.com/apache/apisix-ingress-controller/blob/master/pkg/types/apisix/v1/types.go#L619
func ComposeRouteName(namespace, name string, rule string) string {
// FIXME Use sync.Pool to reuse this buffer if the upstream
// name composing code path is hot.
p := make([]byte, 0, len(namespace)+len(name)+len(rule)+2)
buf := bytes.NewBuffer(p)
buf.WriteString(namespace)
buf.WriteByte('_')
buf.WriteString(name)
buf.WriteByte('_')
buf.WriteString(rule)
return buf.String()
}

@frankkoornstra
Copy link
Author

That would definitely work 👍

@tao12345666333
Copy link
Member

This will be a break change. Is anyone willing to submit a PR to implement it?

@frankkoornstra
Copy link
Author

I wish I could but am not capable in Go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants