@@ -11,6 +11,7 @@ import (
1111 "sigs.k8s.io/controller-runtime/pkg/client"
1212
1313 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1415 "k8s.io/apimachinery/pkg/fields"
1516 "k8s.io/apimachinery/pkg/runtime/schema"
1617 "k8s.io/client-go/dynamic"
@@ -130,18 +131,51 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A
130131
131132 // Override the duck type in the status of the resource
132133 statusListKey := "clusters"
133-
134- matchKey := cm .Data ["matchKey" ]
135-
136134 if cm .Data ["statusListKey" ] != "" {
137135 statusListKey = cm .Data ["statusListKey" ]
138136 }
137+
138+ matchKey := cm .Data ["matchKey" ]
139139 if matchKey == "" {
140140 log .WithField ("matchKey" , matchKey ).Warning ("matchKey not found in " + cm .Name )
141141 return nil , nil
142142 }
143143
144+ clusterDecisions := buildClusterDecisions (duckResources , statusListKey )
145+ if len (clusterDecisions ) == 0 {
146+ log .Warningf ("clusterDecisionResource status.%s missing" , statusListKey )
147+ return nil , nil
148+ }
149+
144150 res := []map [string ]any {}
151+ for _ , clusterDecision := range clusterDecisions {
152+ cluster := findCluster (clustersFromArgoCD , clusterDecision , matchKey , statusListKey )
153+ // if no cluster is found, move to the next cluster
154+ if cluster == nil {
155+ continue
156+ }
157+
158+ // generated instance of cluster params
159+ params := map [string ]any {
160+ "name" : cluster .Name ,
161+ "server" : cluster .Server ,
162+ }
163+
164+ for key , value := range clusterDecision .(map [string ]any ) {
165+ params [key ] = value .(string )
166+ }
167+
168+ for key , value := range appSetGenerator .ClusterDecisionResource .Values {
169+ collectParams (appSet , params , key , value )
170+ }
171+
172+ res = append (res , params )
173+ }
174+
175+ return res , nil
176+ }
177+
178+ func buildClusterDecisions (duckResources * unstructured.UnstructuredList , statusListKey string ) []any {
145179 clusterDecisions := []any {}
146180
147181 // Build the decision slice
@@ -158,61 +192,38 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A
158192 clusterDecisions = append (clusterDecisions , duckResource .Object ["status" ].(map [string ]any )[statusListKey ].([]any )... )
159193 }
160194 log .Infof ("Number of decisions found: %v" , len (clusterDecisions ))
195+ return clusterDecisions
196+ }
161197
162- if len (clusterDecisions ) == 0 {
163- log .Warningf ("clusterDecisionResource status.%s missing" , statusListKey )
164- return nil , nil
198+ func findCluster (clustersFromArgoCD []utils.ClusterSpecifier , cluster any , matchKey string , statusListKey string ) * utils.ClusterSpecifier {
199+ log .Infof ("cluster: %v" , cluster )
200+ matchValue := cluster .(map [string ]any )[matchKey ]
201+ if matchValue == nil || matchValue .(string ) == "" {
202+ log .Warningf ("matchKey=%v not found in \" %v\" list: %v\n " , matchKey , statusListKey , cluster .(map [string ]any ))
203+ return nil // no match
165204 }
166- for _ , cluster := range clusterDecisions {
167- // generated instance of cluster params
168- params := map [string ]any {}
169205
170- log .Infof ("cluster: %v" , cluster )
171- matchValue := cluster .(map [string ]any )[matchKey ]
172- if matchValue == nil || matchValue .(string ) == "" {
173- log .Warningf ("matchKey=%v not found in \" %v\" list: %v\n " , matchKey , statusListKey , cluster .(map [string ]any ))
174- continue
175- }
176-
177- strMatchValue := matchValue .(string )
178- log .WithField (matchKey , strMatchValue ).Debug ("validate against ArgoCD" )
206+ strMatchValue := matchValue .(string )
207+ log .WithField (matchKey , strMatchValue ).Debug ("validate against ArgoCD" )
179208
180- found := false
181-
182- for _ , argoCluster := range clustersFromArgoCD {
183- if argoCluster .Name != strMatchValue {
184- continue
185- }
209+ for _ , argoCluster := range clustersFromArgoCD {
210+ if argoCluster .Name == strMatchValue {
186211 log .WithField (matchKey , argoCluster .Name ).Info ("matched cluster in ArgoCD" )
187- params ["name" ] = argoCluster .Name
188- params ["server" ] = argoCluster .Server
189-
190- found = true
191- break // Stop looking
192- }
193-
194- if ! found {
195- log .WithField (matchKey , strMatchValue ).Warning ("unmatched cluster in ArgoCD" )
196- continue
212+ return & argoCluster
197213 }
214+ }
198215
199- for key , value := range cluster .( map [ string ] any ) {
200- params [ key ] = value .( string )
201- }
216+ log . WithField ( matchKey , strMatchValue ). Warning ( "unmatched cluster in ArgoCD" )
217+ return nil
218+ }
202219
203- for key , value := range appSetGenerator .ClusterDecisionResource .Values {
204- if appSet .Spec .GoTemplate {
205- if params ["values" ] == nil {
206- params ["values" ] = map [string ]string {}
207- }
208- params ["values" ].(map [string ]string )[key ] = value
209- } else {
210- params ["values." + key ] = value
211- }
220+ func collectParams (appSet * argoprojiov1alpha1.ApplicationSet , params map [string ]any , key string , value string ) {
221+ if appSet .Spec .GoTemplate {
222+ if params ["values" ] == nil {
223+ params ["values" ] = map [string ]string {}
212224 }
213-
214- res = append (res , params )
225+ params ["values" ].(map [string ]string )[key ] = value
226+ } else {
227+ params ["values." + key ] = value
215228 }
216-
217- return res , nil
218229}
0 commit comments