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

[NET-1484] Support for namespaces, partitions in consul endpoints #1842

Merged
merged 7 commits into from
Nov 30, 2023

Conversation

roncodingenthusiast
Copy link
Contributor

@roncodingenthusiast roncodingenthusiast commented Nov 21, 2023

TODO

  • KV list endpoint
  • KV keys endpoint
  • KV get endpoint
  • Catalog node endpoint
  • Catalog nodes endpoint
  • catalog service endpoint
  • catalog services endpoint
  • Docs

How to test

Note: ensure you have consul running

previous behavior is preserved

  • insert into kv in default namespace and partition
consul kv put "hashi/test" "default"
  • create a file in.tpl insert the following to retrieve that key
{{ key "hashi/test" }}
  • ensure it can still read the key
consul-template -template "in.tpl:out.txt" -once
  • verify: your out.tpl file should have:
default

specify namespace/partition

  • create a new consul namespace/partition (sorry these names aren't too creative :D)
consul partition create -name "partition"
consul namespace create -name "partition-ns" -partition "partition"
  • store kv in the new partition/namespace created above
consul kv put -partition "partition" -namespace "partition-ns" "hashi/test" "part
ition"
  • update your in.tpl to include namespace and partition
{{ key "hashi/test?partition=partition&ns=partition-ns" }}
  • verify: your out.tpl file should have:
partition

@roncodingenthusiast roncodingenthusiast requested review from a team, johnlanda and skpratt and removed request for a team November 21, 2023 14:30
@roncodingenthusiast roncodingenthusiast changed the title [NET-1484] Support for namespaces, partitions and peers in consul endpoints [NET-1484] Support for namespaces, partitions in consul endpoints Nov 21, 2023
@roncodingenthusiast roncodingenthusiast marked this pull request as ready for review November 21, 2023 20:28
@roncodingenthusiast roncodingenthusiast requested a review from a team as a code owner November 21, 2023 20:28
@roncodingenthusiast roncodingenthusiast merged commit ccdb7f9 into main Nov 30, 2023
54 checks passed
@komapa
Copy link

komapa commented Jan 29, 2024

I understand this is "alpha" but this does not work at all with range services and different namespaces. You get the same services as the first range services call because (I am guessing), you did not update the cache key to consider any of the new QUERY params (namespace, partition, peer)

Also, this functionality is pretty useless unless there is also range namespaces implemented. Right now, we need to do something like:

{{- range $k, $n := (plugin "consul" "namespace" "list" "--format" "json" | parseJSON) -}}

@jkirschner-hashicorp
Copy link

Hi @komapa,

It sounds like you have a specific scenario in mind that you need to work, but unfortunately doesn't work with the current implementation. To help get folks aligned on this, can you share that scenario and sample Consul Template syntax you'd hope would work for that scenario?

@komapa
Copy link

komapa commented Jan 30, 2024

We just want to get all services for all namespaces that we care about:

{{- $namespaces := sprig_list "default" -}}
{{- range $k, $n := (plugin "consul" "namespace" "list" "--format" "json" | parseJSON) -}}
    {{- /* TODO: Figure out how we can check for $n.Meta.external-source eq kubernetes, instead */ -}}
    {{ if eq $n.Description "Auto-generated by consul-k8s" }}
        {{- $namespaces = sprig_append $namespaces $n.Name -}}
    {{- end -}}
{{- end -}}

{{- range $k, $namespace := $namespaces }}
{{- range services (printf "?ns=%s&partition=default" $namespace) }}
{{ $namespace }}: {{ .Name }}
{{- end }}
{{- end }}

@jkirschner-hashicorp
Copy link

And based on this comment from before:

Also, this functionality is pretty useless unless there is also range namespaces implemented.

I assume you actually want something like this?

{{- range namespaces "?partition=default"}
{{- range services (printf "?ns=%s&partition=default" .NamespaceName) }}
{{ $namespace }}: {{ .Name }}
{{- end }}
{{- end }}

In other words, is the below an accurate summary?

  • UX Challenge: The top loop in your example is just to construct the list of namespaces to iterate over, which is unnecessary if range namespaces is implemented. This workaround is ugly, but functional.
  • Bug: However, there's a bug that no workaround can resolve: range services is always returning results from the first namespace in the array (index 0 in range namespaces loop).

@komapa
Copy link

komapa commented Jan 31, 2024

And based on this comment from before:

Also, this functionality is pretty useless unless there is also range namespaces implemented.

I assume you actually want something like this?

{{- range namespaces "?partition=default"}
{{- range services (printf "?ns=%s&partition=default" .NamespaceName) }}
{{ $namespace }}: {{ .Name }}
{{- end }}
{{- end }}

In other words, is the below an accurate summary?

  • UX Challenge: The top loop in your example is just to construct the list of namespaces to iterate over, which is unnecessary if range namespaces is implemented. This workaround is ugly, but functional.
  • Bug: However, there's a bug that no workaround can resolve: range services is always returning results from the first namespace in the array (index 0 in range namespaces loop).

Yes, this is perfect summary. Thank you!

@jkirschner-hashicorp
Copy link

Since both Consul and Vault have namespaces, it might make sense to have it be range consulNamespaces or something like that with variable name .ConsulNamespace to make it unambiguous (in case iterating over a Vault namespace tree is ever added, or to just make it clear to the reader that we're talking about a Consul namespace not a Vault one).

@jkirschner-hashicorp
Copy link

@komapa : The Bug described above should be fixed by this PR (to be included in the next Consul Template release): #1874.

The UX Challenge will need to be addressed separately in the future.

@blake blake linked an issue Apr 24, 2024 that may be closed by this pull request
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 this pull request may close these issues.

Support Cluster Peering with Consul Template
4 participants