@@ -20,11 +20,12 @@ type resourcesOrError struct {
2020}
2121
2222type gridService struct {
23- clusters map [string ]resourcesOrError
24- accessor repository.ClusterAwareAccessor [* repository.Resources ]
25- stores map [string ]stores
26- lock sync.RWMutex
27- addressMap map [string ]string
23+ clusters map [string ]resourcesOrError
24+ accessor repository.ClusterAwareAccessor [* repository.Resources ]
25+ stores map [string ]stores
26+ lock sync.RWMutex
27+ addressMap map [string ]string
28+ environments []string
2829}
2930
3031func (g * gridService ) updateClusters (ctx context.Context ) {
@@ -95,6 +96,7 @@ type gridRow struct {
9596 level int
9697 group string
9798 cells map [string ]* gridCell
99+ name string
98100}
99101
100102func (g * gridRow ) expand () []* deploygrid.Component {
@@ -106,9 +108,8 @@ func (g *gridRow) expand() []*deploygrid.Component {
106108 }
107109
108110 type withParent struct {
109- parentPath string
110- currentPath string
111- node * []* deploygrid.Component
111+ parentPath string
112+ node * []* deploygrid.Component
112113 }
113114
114115 pathMap := map [string ]bool {}
@@ -148,9 +149,8 @@ func (g *gridRow) expand() []*deploygrid.Component {
148149 var res []* deploygrid.Component
149150
150151 cur := & withParent {
151- parentPath : "" ,
152- currentPath : "" ,
153- node : & res ,
152+ parentPath : "" ,
153+ node : & res ,
154154 }
155155 st .Push (cur )
156156
@@ -179,43 +179,82 @@ func (g *gridRow) expand() []*deploygrid.Component {
179179 }
180180
181181 * cur .node = append (* cur .node , comp )
182+ st .Push (cur )
182183 cur = & withParent {
183- parentPath : cur .currentPath ,
184- currentPath : path ,
185- node : & comp .Children ,
184+ parentPath : path ,
185+ node : & comp .Children ,
186186 }
187187 st .Push (cur )
188188 }
189189
190190 return res
191191}
192192
193- func newGridRow (group string ) * gridRow {
193+ func newGridRow (group string , name string ) * gridRow {
194194 return & gridRow {
195+ name : name ,
195196 group : group ,
196197 cells : map [string ]* gridCell {},
197198 }
198199}
199200
200- func buildGrid (grid * deploygrid.Grid , rows map [string ]* gridRow ) {
201+ func buildGrid (grid * deploygrid.Grid , rows map [string ]* gridRow , columns [] string ) {
201202
202203 envs := map [string ]bool {}
203- var comps []* deploygrid.Component
204+
205+ var sorted []* gridRow
204206
205207 for _ , rv := range rows {
208+ sorted = append (sorted , rv )
209+ }
210+
211+ slices .SortFunc (sorted , func (a , b * gridRow ) int {
212+ return strings .Compare (a .name , b .name )
213+ })
214+
215+ grouped := map [string ]* deploygrid.Component {}
216+
217+ for _ , rv := range sorted {
206218 for ck , _ := range rv .cells {
207219 envs [ck ] = true
208220 }
209- comps = append (comps , rv .expand ()... )
221+
222+ groupName := rv .group
223+ if groupName == "" {
224+ groupName = "Default"
225+ }
226+ comps := rv .expand ()
227+
228+ if grp , ok := grouped [groupName ]; ok {
229+ grp .Children = append (grp .Children , comps ... )
230+ } else {
231+ grp = & deploygrid.Component {
232+ Name : groupName ,
233+ ComponentType : "Group" ,
234+ Children : comps ,
235+ }
236+ grouped [groupName ] = grp
237+ }
238+
210239 }
211240
212- grid .Components = comps
213- for k , _ := range envs {
241+ for _ , c := range columns {
214242 grid .Environments = append (grid .Environments , & deploygrid.Environment {
215- Name : k ,
243+ Name : c ,
216244 })
217245 }
218246
247+ var comps []* deploygrid.Component
248+
249+ for _ , v := range grouped {
250+ comps = append (comps , v )
251+ }
252+
253+ slices .SortFunc (comps , func (a , b * deploygrid.Component ) int {
254+ return strings .Compare (a .Name , b .Name )
255+ })
256+
257+ grid .Components = comps
219258}
220259
221260func (g * gridService ) Init () {
@@ -263,40 +302,41 @@ func (g *gridService) Get(ctx context.Context) (*deploygrid.Grid, error) {
263302 // TODO - first encountered group defines the group container - this should change
264303 for _ , v := range data {
265304 for _ , e := range v .entries {
266- group := e .Labels [ LabelDeployGridGroup ]
305+ group := e .Annotations [ AnnotationDeployGridGroup ]
267306
268307 if group == "" {
269308 group = GroupNoGroup
270309 }
271310
272- name , nameOk := e .Labels [ LabelDeployGridName ]
273- env , envOk := e .Labels [ LabelDeployGridEnvironment ]
311+ name , nameOk := e .Annotations [ AnnotationDeployGridName ]
312+ envs , envsOk := e .Annotations [ AnnotationDeployGridEnvironment ]
274313
275- if nameOk && envOk && e .Parent == "" {
314+ if nameOk && envsOk && e .Parent == "" {
276315
277316 row , ok := rowMap [name ]
278317 if ! ok {
279- row = newGridRow (group )
318+ row = newGridRow (group , name )
280319 rowMap [name ] = row
281320 }
282321
283- cell , ok := row .cells [env ]
284- if ! ok {
285- cell = newGridCell ()
286- row .cells [env ] = cell
287- }
288-
289- err := g .buildNodes (ctx , data , & cell .nodes , & e .Components )
322+ for _ , env := range strings .Split (envs , "," ) {
323+ cell , ok := row .cells [env ]
324+ if ! ok {
325+ cell = newGridCell ()
326+ row .cells [env ] = cell
327+ }
290328
291- if err != nil {
292- return nil , err
329+ err := g .buildNodes (ctx , data , & cell .nodes , & e .Components )
330+ if err != nil {
331+ return nil , err
332+ }
293333 }
294334
295335 }
296336 }
297337 }
298338
299- buildGrid (res , rowMap )
339+ buildGrid (res , rowMap , g . environments )
300340
301341 return res , nil
302342}
@@ -353,9 +393,10 @@ func NewGridService(params GridServiceParams) GridService {
353393 }
354394 }
355395 return & gridService {
356- accessor : params .Accessor ,
357- addressMap : addressMap ,
358- clusters : map [string ]resourcesOrError {},
359- stores : map [string ]stores {},
396+ accessor : params .Accessor ,
397+ addressMap : addressMap ,
398+ environments : params .ClustersConfig .Environments ,
399+ clusters : map [string ]resourcesOrError {},
400+ stores : map [string ]stores {},
360401 }
361402}
0 commit comments