Skip to content

Commit 2cc946b

Browse files
Addign globals in versions for generic
Signed-off-by: Markos Kandylis <markos_kandylis@hotmail.com>
1 parent a63f387 commit 2cc946b

File tree

3 files changed

+221
-166
lines changed

3 files changed

+221
-166
lines changed

charts/application-sets/examples/addons.yaml

Lines changed: 103 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,71 @@ spec:
1010
goTemplateOptions:
1111
- missingkey=error
1212
generators:
13-
- matrix:
14-
generators:
15-
- matrix:
16-
generators:
17-
- clusters:
18-
selector:
19-
matchLabels:
20-
fleet_member: hub-cluster
21-
values:
22-
chartName: application-sets
23-
chartRepo: "12345678910.dkr.ecr.eu-west-2.amazonaws.com"
24-
chartPath: "charts/application-sets"
25-
applicationSetGroup: "addons"
26-
groupRelease: '{{default "" (index .metadata.labels "addonsRelease")}}'
27-
useSelectors: "false"
28-
useVersionSelectors: "true"
29-
- git:
30-
repoURL: '{{ .metadata.annotations.fleet_repo_url }}'
31-
revision: '{{ .metadata.annotations.fleet_repo_revision }}'
32-
files:
33-
- path: "{{ .metadata.annotations.fleet_repo_basepath }}/bootstrap/versions/applicationSets.yaml"
34-
- list:
35-
elementsYaml: |
36-
{{- $releaseTypes := index .releases .values.applicationSetGroup | toJson | fromJson -}}
37-
{{- $result := list -}}
38-
{{- $defaultVersion := dict -}}
39-
{{- /* Defining the Default Version in case we need to fall back */ -}}
40-
{{- range $releaseType := $releaseTypes -}}
41-
{{- if eq $releaseType.type "default" -}}
42-
{{- $defaultVersion = $releaseType -}}
43-
{{- end -}}
13+
- matrix:
14+
generators:
15+
- matrix:
16+
generators:
17+
- clusters:
18+
selector:
19+
matchLabels:
20+
fleet_member: hub-cluster
21+
values:
22+
chartName: application-sets
23+
chartRepo: "1234567890.dkr.ecr.eu-west-2.amazonaws.com"
24+
chartPath: "charts/application-sets"
25+
applicationSetGroup: "addons"
26+
groupRelease: '{{default "" (index .metadata.labels "addonsRelease")}}'
27+
useSelectors: "false"
28+
useVersionSelectors: "true"
29+
- git:
30+
repoURL: "{{ .metadata.annotations.fleet_repo_url }}"
31+
revision: "{{ .metadata.annotations.fleet_repo_revision }}"
32+
files:
33+
- path: "{{ .metadata.annotations.fleet_repo_basepath }}/bootstrap/versions/applicationSets.yaml"
34+
- list:
35+
elementsYaml: |
36+
{{- $globals := .releases.globals -}}
37+
{{- $releaseNames := index .releases .values.applicationSetGroup | toJson | fromJson -}}
38+
{{- $groupRelease := .values.groupRelease}}
39+
{{- $firstRelease := index $releaseNames 0 -}}
40+
{{- $result := list -}}
41+
{{- /* If the values of the group release is empty or we dont use version selectors then we use only the first element of the list */ -}}
42+
{{- if or (eq $groupRelease "") (eq .values.useVersionSelectors "false") -}}
43+
{{- $mergedRelease := merge $firstRelease $globals -}}
44+
{{- $mergedRelease = merge $mergedRelease (dict "totalReleases" (len $releaseNames)) -}}
45+
{{- $result = append $result $mergedRelease -}}
46+
{{- else -}}
47+
{{- /* We look for the defined releases */ -}}
48+
{{- $found := false -}}
49+
{{- range $releaseName := $releaseNames -}}
50+
{{- if eq $releaseName.releaseName $groupRelease -}}
51+
{{- $found = true -}}
4452
{{- end -}}
45-
{{- /* We look for the defined releases */ -}}
46-
{{- range $releaseType := $releaseTypes -}}
47-
{{- /* Case 1: If selectors is true, include all group releases */ -}}
48-
{{- if eq $.values.useSelectors "true" -}}
49-
{{- $result = append $result $releaseType -}}
50-
{{- /* Case 2: If group version Release value exists, only include matching releases */ -}}
51-
{{- else if $.values.groupRelease -}}
52-
{{- if or (not $releaseType.type) (eq $releaseType.type $.values.groupRelease) -}}
53-
{{- $result = append $result $releaseType -}}
54-
{{- end -}}
55-
{{- /* Case 3: Default case - include version if it's the default type */ -}}
56-
{{- else -}}
57-
{{- if eq $releaseType.type "default" -}}
58-
{{- $result = append $result $releaseType -}}
59-
{{- end -}}
60-
{{- end -}}
53+
{{- end -}}
54+
{{- if $found -}}
55+
{{- range $releaseName := $releaseNames -}}
56+
{{- $mergedReleaseValues := merge $releaseName $globals -}}
57+
{{- $mergedReleaseValues = merge $mergedReleaseValues (dict "totalReleases" (len $releaseNames)) -}}
58+
{{- $result = append $result $mergedReleaseValues -}}
6159
{{- end -}}
62-
{{- /* If no releases were selected, use default */ -}}
63-
{{- if eq (len $result) 0 -}}
64-
{{- $result = append $result $defaultVersion -}}
65-
{{- end -}}
66-
{{ $result | toJson }}
60+
{{- else -}}
61+
{{- $mergedRelease := merge $firstRelease $globals -}}
62+
{{- $mergedRelease = merge $mergedRelease (dict "totalReleases" (len $releaseNames)) -}}
63+
{{- $result = append $result $mergedRelease -}}
64+
{{- end -}}
65+
{{- end -}}
66+
{{ $result | toJson }}
6767
###################################################
6868
#base template (everything common)
6969
###################################################
7070
template:
7171
metadata:
72-
name: 'cluster-{{.values.applicationSetGroup}}-{{.name}}-{{.type | lower }}'
72+
name: "cluster-{{.values.applicationSetGroup}}-{{.name}}-{{.releaseName | lower }}"
7373
spec:
7474
project: default
7575
destination:
7676
namespace: argocd
77-
name: '{{ .name }}'
77+
name: "{{ .name }}"
7878
# syncPolicy is identical for both variants
7979
syncPolicy:
8080
automated:
@@ -90,46 +90,78 @@ spec:
9090
# conditional sources
9191
###################################################
9292
templatePatch: |
93+
{{- $commonValuesPath := printf "%s/%s.yaml" .values.chartName .values.applicationSetGroup -}}
94+
{{- $repoNames := list "addons" -}}
95+
96+
{{- $environment := .metadata.labels.environment -}}
97+
98+
{{- $tenantPath := "" -}}
99+
{{- if and (hasKey . "tenant") .tenant -}}
100+
{{- $tenantPath = printf "%s" .tenant -}}
101+
{{- else if (index .metadata.labels "tenant") -}}
102+
{{- $tenantPath = printf "%s" .metadata.labels.tenant -}}
103+
{{- end -}}
104+
105+
106+
{{- $clusterName := "" -}}
107+
{{- if and (hasKey . "clusterName") .clusterName -}}
108+
{{- $clusterName = .clusterName -}}
109+
{{- else -}}
110+
{{- $clusterName = .name -}}
111+
{{- end -}}
112+
113+
{{- $pathPatterns := list
114+
(printf "%s/defaults" $tenantPath)
115+
(printf "%s/environments/%s/defaults" $tenantPath $environment)
116+
(printf "%s/environments/%s/clusters/%s" $tenantPath $environment $clusterName)
117+
-}}
118+
93119
spec:
94120
sources:
95-
- ref: values
96-
repoURL: '{{ .metadata.annotations.addons_repo_url }}'
97-
targetRevision: '{{ .metadata.annotations.addons_repo_revision }}'
121+
{{- range $repoName := $repoNames }}
122+
- repoURL: '{{default (index $.metadata.annotations (printf "%s_repo_url" $repoName)) (index $ "repoUrl")}}'
123+
targetRevision: '{{default (index $.metadata.annotations (printf "%s_repo_revision" $repoName)) (index $ "targetRevision")}}'
124+
ref: {{$repoName}}Values
125+
{{- end }}
98126
{{- if eq .use_helm_repo_path "false" }}
99127
- repoURL: '{{default .values.chartRepo .chartRepo }}'
100128
chart: '{{ default .values.chartName .ecrChartName }}'
101129
targetRevision: '{{.version}}'
102130
{{- else }}
103-
- repoURL: '{{ .metadata.annotations.addons_repo_url }}'
104-
path: '{{ .values.chartPath }}'
105-
targetRevision: '{{ .metadata.annotations.addons_repo_revision }}'
131+
- repoURL: '{{default (index .metadata.annotations "chartRepoUrl") (index . "chartRepoUrl") }}'
132+
path: '{{ default .values.chartPath (index . "chartRepoPath")}}'
133+
targetRevision: '{{default (index .metadata.annotations "chartRepoRevision") (index . "chartRepoRevision") }}'
106134
{{- end }}
107135
helm:
108136
ignoreMissingValueFiles: true
109137
valuesObject:
110-
useSelectors: false
138+
useSelectors: '{{.values.useSelectors}}'
111139
useVersionSelectors: '{{.values.useVersionSelectors}}'
140+
applicationSetGroup: {{.values.applicationSetGroup}}
141+
groupRelease: '{{.values.groupRelease}}'
112142
# Defining the way to group addons This application set will handly Addons and ACK values
113143
mergeValues:
114144
addons:
115145
use: true
116146
ack:
117147
use: true
118-
releaseType: '{{.type | lower }}'
148+
releaseName: '{{default "" .releaseName | lower }}'
119149
# If we are using version selector we add the version of the releases on the matchlabels
120-
{{- if eq .values.useVersionSelectors "true"}}
150+
{{- if and (eq .values.useVersionSelectors "true") (or (eq .releaseName .values.groupRelease) (gt (int .totalReleases) 1)) }}
121151
releases:
122-
{{.values.applicationSetGroup}}Release: '{{.type | lower}}'
152+
{{.values.applicationSetGroup}}Release: '{{.releaseName | lower}}'
123153
{{- end }}
124154
{{- if eq .values.useSelectors "false"}}
125155
globalSelectors:
126156
fleet_member: hub-cluster
127157
{{- end }}
128-
valueFiles:
129-
- defaults/{{.values.applicationSetGroup}}
130-
- clusters/{{`{{ .nameNormalized }}`}}/{{.values.applicationSetGroup}}
158+
# Those are the Value files to read for the Whole group of applciationsdts
131159
valueFiles:
132-
- $values/{{ .metadata.annotations.addons_repo_basepath }}/bootstrap/defaults/{{.values.applicationSetGroup}}.yaml
133-
- $values/{{ .metadata.annotations.addons_repo_basepath }}/{{ .metadata.labels.tenant }}/defaults/{{ .values.chartName }}/{{.values.applicationSetGroup}}.yaml
134-
- $values/{{ .metadata.annotations.addons_repo_basepath }}/{{ .metadata.labels.tenant }}/clusters/{{ .name }}/{{ .values.chartName }}/{{.values.applicationSetGroup}}.yaml
135-
- $values/{{ .metadata.annotations.addons_repo_basepath }}/{{ .metadata.labels.tenant }}/environments/{{ .metadata.labels.environment }}/{{ .values.chartName }}/{{.values.applicationSetGroup}}.yaml
160+
- $addonsValues/addons/bootstrap/defaults/{{.values.applicationSetGroup}}.yaml
161+
{{- range $repoName := $repoNames }}
162+
{{- $repoRef := printf "%sValues" $repoName }}
163+
{{- $basePath := default (index $.metadata.annotations (printf "%s_repo_basepath" $repoName)) (index $ (printf "%s_repo_basepath" $repoName)) }}
164+
{{- range $pattern := $pathPatterns }}
165+
- ${{ $repoRef }}/{{ $basePath }}/{{ $pattern }}/{{ $commonValuesPath }}
166+
{{- end }}
167+
{{- end }}

0 commit comments

Comments
 (0)