rackpeek.tmpl 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. {{/* default rackpeek configuration template */}}
  2. {{/* Generate Services based on filtered containers */}}
  3. {{/* Optional RUNS_ON environment variable used for runsOn*/}}
  4. {{/* If ports are exposed, the first one is used */}}
  5. {{/* If ports are exposed, the first one is used */}}
  6. {{/* Use index function to access map keys with dots in them https://pkg.go.dev/text/template#hdr-Functions */}}
  7. {{ range $index, $value := . }}
  8. {{ $addrLen := len $value.Addresses }}
  9. - kind: Service
  10. {{/* Name priority rackpeek.name -> homepage.name -> org.opencontainers.image.title -> container_name */}}
  11. {{ if contains $value.Labels "rackpeek.name" }}
  12. name: {{ index $value.Labels "rackpeek.name" }}
  13. {{ else if contains $value.Labels "homepage.name" }}
  14. name: {{ index $value.Labels "homepage.name" }}
  15. {{ else if contains $value.Labels "org.opencontainers.image.title" }}
  16. name: {{ index $value.Labels "org.opencontainers.image.title" }}
  17. {{ else }}
  18. name: {{ $value.Name }}
  19. {{ end }}
  20. {{/* only include network section if there is a url in labels or published ports with HOST_IP env set */}}
  21. {{ if or (contains $value.Labels "homepage.href") (contains $value.Labels "rackpeek.href") (contains $value.Labels "caddy") (contains $value.Labels "swag_url") (and ($.Env.HOST_IP) (gt $addrLen 0)) }}
  22. network:
  23. {{/* URL priority caddy -> homepage.name -> rackpeek.href */}}
  24. {{/* cant get url from traefik due to route rule complexity */}}
  25. {{ if contains $value.Labels "rackpeek.href" }}
  26. url: {{ index $value.Labels "rackpeek.href" }}
  27. {{ else if contains $value.Labels "homepage.href" }}
  28. url: {{ index $value.Labels "homepage.href" }}
  29. {{ else if contains $value.Labels "caddy" }}
  30. url: {{ index $value.Labels "caddy" }}
  31. {{end }}
  32. {{/* only generate ip:port if HOST_IP env is set */}}
  33. {{/* -- if rackpeek supports multiple IPs per service in the future this can be modified */}}
  34. {{ if $.Env.HOST_IP }}
  35. {{ $hostIps := split $.Env.HOST_IP "," }}
  36. {{ $httpsIsh := where $value.Addresses "Port" "443"}}
  37. {{ $httpIsh := where $value.Addresses "Port" "80"}}
  38. {{/* try to get ports by known web ports first */}}
  39. {{ if or (gt (len $httpsIsh) 0) (gt (len $httpIsh) 0)}}
  40. {{ range $i, $address := $value.Addresses }}
  41. {{/* only use this if container is binding on all addresses or a known host, and using known web port */}}
  42. {{ if and (or (eq $address.HostIP "0.0.0.0") (contains $hostIps $address.HostIP)) (or (eq $address.Port "443") (eq $address.Port "80")) }}
  43. ip: {{ index $hostIps 0 }}
  44. port: {{ $address.Port }}
  45. protocol: TCP
  46. {{ break }}
  47. {{ end }}
  48. {{ end }}
  49. {{/* otherwise, use first port */}}
  50. {{ else }}
  51. {{ range $i, $address := $value.Addresses }}
  52. {{/* only use this if container is binding on all addresses or a known host port */}}
  53. {{ if or (eq $address.HostIP "0.0.0.0") (contains $hostIps $address.HostIP) }}
  54. ip: {{ index $hostIps 0 }}
  55. port: {{ $address.Port }}
  56. protocol: TCP
  57. {{ break }}
  58. {{ end }}
  59. {{ end }}
  60. {{ end }}
  61. {{ end }}
  62. {{/* end network loop */}}
  63. {{ end }}
  64. {{ if $.Env.RUNS_ON }}
  65. runsOn:
  66. - {{ $.Env.RUNS_ON }}
  67. {{ end }}
  68. {{/* end containers loop */}}
  69. {{ end }}