_images.tpl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. {{/*
  2. Copyright Broadcom, Inc. All Rights Reserved.
  3. SPDX-License-Identifier: APACHE-2.0
  4. */}}
  5. {{/* vim: set filetype=mustache: */}}
  6. {{/*
  7. Return the proper image name.
  8. If image tag and digest are not defined, termination fallbacks to chart appVersion.
  9. {{ include "common.images.image" ( dict "imageRoot" .Values.path.to.the.image "global" .Values.global "chart" .Chart ) }}
  10. */}}
  11. {{- define "common.images.image" -}}
  12. {{- $registryName := default .imageRoot.registry ((.global).imageRegistry) -}}
  13. {{- $repositoryName := .imageRoot.repository -}}
  14. {{- $separator := ":" -}}
  15. {{- $termination := .imageRoot.tag | toString -}}
  16. {{- if not .imageRoot.tag }}
  17. {{- if .chart }}
  18. {{- $termination = .chart.AppVersion | toString -}}
  19. {{- end -}}
  20. {{- end -}}
  21. {{- if .imageRoot.digest }}
  22. {{- $separator = "@" -}}
  23. {{- $termination = .imageRoot.digest | toString -}}
  24. {{- end -}}
  25. {{- if $registryName }}
  26. {{- printf "%s/%s%s%s" $registryName $repositoryName $separator $termination -}}
  27. {{- else -}}
  28. {{- printf "%s%s%s" $repositoryName $separator $termination -}}
  29. {{- end -}}
  30. {{- end -}}
  31. {{/*
  32. Return the proper Docker Image Registry Secret Names (deprecated: use common.images.renderPullSecrets instead)
  33. {{ include "common.images.pullSecrets" ( dict "images" (list .Values.path.to.the.image1, .Values.path.to.the.image2) "global" .Values.global) }}
  34. */}}
  35. {{- define "common.images.pullSecrets" -}}
  36. {{- $pullSecrets := list }}
  37. {{- range ((.global).imagePullSecrets) -}}
  38. {{- if kindIs "map" . -}}
  39. {{- $pullSecrets = append $pullSecrets .name -}}
  40. {{- else -}}
  41. {{- $pullSecrets = append $pullSecrets . -}}
  42. {{- end }}
  43. {{- end -}}
  44. {{- range .images -}}
  45. {{- range .pullSecrets -}}
  46. {{- if kindIs "map" . -}}
  47. {{- $pullSecrets = append $pullSecrets .name -}}
  48. {{- else -}}
  49. {{- $pullSecrets = append $pullSecrets . -}}
  50. {{- end -}}
  51. {{- end -}}
  52. {{- end -}}
  53. {{- if (not (empty $pullSecrets)) -}}
  54. imagePullSecrets:
  55. {{- range $pullSecrets | uniq }}
  56. - name: {{ . }}
  57. {{- end }}
  58. {{- end }}
  59. {{- end -}}
  60. {{/*
  61. Return the proper Docker Image Registry Secret Names evaluating values as templates
  62. {{ include "common.images.renderPullSecrets" ( dict "images" (list .Values.path.to.the.image1, .Values.path.to.the.image2) "context" $) }}
  63. */}}
  64. {{- define "common.images.renderPullSecrets" -}}
  65. {{- $pullSecrets := list }}
  66. {{- $context := .context }}
  67. {{- range (($context.Values.global).imagePullSecrets) -}}
  68. {{- if kindIs "map" . -}}
  69. {{- $pullSecrets = append $pullSecrets (include "common.tplvalues.render" (dict "value" .name "context" $context)) -}}
  70. {{- else -}}
  71. {{- $pullSecrets = append $pullSecrets (include "common.tplvalues.render" (dict "value" . "context" $context)) -}}
  72. {{- end -}}
  73. {{- end -}}
  74. {{- range .images -}}
  75. {{- range .pullSecrets -}}
  76. {{- if kindIs "map" . -}}
  77. {{- $pullSecrets = append $pullSecrets (include "common.tplvalues.render" (dict "value" .name "context" $context)) -}}
  78. {{- else -}}
  79. {{- $pullSecrets = append $pullSecrets (include "common.tplvalues.render" (dict "value" . "context" $context)) -}}
  80. {{- end -}}
  81. {{- end -}}
  82. {{- end -}}
  83. {{- if (not (empty $pullSecrets)) -}}
  84. imagePullSecrets:
  85. {{- range $pullSecrets | uniq }}
  86. - name: {{ . }}
  87. {{- end }}
  88. {{- end }}
  89. {{- end -}}
  90. {{/*
  91. Return the proper image version (ingores image revision/prerelease info & fallbacks to chart appVersion)
  92. {{ include "common.images.version" ( dict "imageRoot" .Values.path.to.the.image "chart" .Chart ) }}
  93. */}}
  94. {{- define "common.images.version" -}}
  95. {{- $imageTag := .imageRoot.tag | toString -}}
  96. {{/* regexp from https://github.com/Masterminds/semver/blob/23f51de38a0866c5ef0bfc42b3f735c73107b700/version.go#L41-L44 */}}
  97. {{- if regexMatch `^([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?$` $imageTag -}}
  98. {{- $version := semver $imageTag -}}
  99. {{- printf "%d.%d.%d" $version.Major $version.Minor $version.Patch -}}
  100. {{- else -}}
  101. {{- print .chart.AppVersion -}}
  102. {{- end -}}
  103. {{- end -}}