0

currently I have an application set using git generator that defines the destination namespace as:

destination:
   server: https://kubernetes.default.svc
   namespace: '{{index .path.segments 1}}'

That works fine, and all subdirectories in the apps/*/yamls inside git gets their own namespace based on the value of that *.

So:

apps/app1/yaml/ goes to app1 namespace
apps/app2/yaml/ goes to app2 namespace
...

But I would like to make a little change in that behaviour. IF the app name goes like app1:somename, I would like that the somename becomes the namespace name.

apps/app1:somename/yaml/ -> ns would be somename

apps/app2:someothername/yaml/ -> ns would be someothername

apps/app3/yaml/ -> since there is no :name it should fallback to app3 as namespace

Any idea in how to express that in go templating ?

Jose
  • 21

1 Answers1

0

According to the documentation, Helm supports the Sprig template function library, which means we have access to things like the contains and regexSplit functions:

destination:
  server: https://kubernetes.default.svc
  namespace: {{ if contains ":" (index .path.segments 1) -}}
{{ index (regexSplit ":" (index .path.segments 1) -1) 1 -}}
{{ else -}}
{{ index .path.segments 1 -}}
{{ end }}

If we test this out here, then given the input:

path:
  segments: [apps,app1:somename,yaml]

This produces:

destination:
  server: https://kubernetes.default.svc
  namespace: somename

Given the input:

path:
  segments: [apps,app3,yaml]

This produces:

destination:
  server: https://kubernetes.default.svc
  namespace: app3
larsks
  • 47,453