Skip to content

Commit 5e8cd42

Browse files
committed
group services w same selectors in oc status
1 parent 751b4fa commit 5e8cd42

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

pkg/oc/cli/describe/projectstatus.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,37 @@ func (d *ProjectStatusDescriber) MakeGraph(namespace string) (osgraph.Graph, set
164164
return g, forbiddenResources, nil
165165
}
166166

167+
func selectorEquals(A, B map[string]string) bool {
168+
if len(A) == 0 && len(B) == 0 {
169+
return true
170+
}
171+
if len(A) == 0 {
172+
return false
173+
}
174+
175+
for Ak, Av := range A {
176+
if Bv, exists := B[Ak]; !exists || Av != Bv {
177+
return false
178+
}
179+
}
180+
181+
return true
182+
}
183+
184+
func containsSelector(selector map[string]string, selectorGroup []map[string]string) bool {
185+
if len(selector) == 0 {
186+
return false
187+
}
188+
189+
for _, grouppedSelector := range selectorGroup {
190+
if selectorEquals(selector, grouppedSelector) {
191+
return true
192+
}
193+
}
194+
195+
return false
196+
}
197+
167198
// Describe returns the description of a project
168199
func (d *ProjectStatusDescriber) Describe(namespace, name string) (string, error) {
169200
var f formatter = namespacedFormatter{}
@@ -195,9 +226,25 @@ func (d *ProjectStatusDescriber) Describe(namespace, name string) (string, error
195226

196227
coveredNodes := graphview.IntSet{}
197228

198-
services, coveredByServices := graphview.AllServiceGroups(g, coveredNodes)
229+
allServices, coveredByServices := graphview.AllServiceGroups(g, coveredNodes)
199230
coveredNodes.Insert(coveredByServices.List()...)
200231

232+
seenSelectors := []map[string]string{}
233+
234+
services := []graphview.ServiceGroup{}
235+
groupedServices := []graphview.ServiceGroup{}
236+
237+
// calculate services with identical selectors
238+
for _, svc := range allServices {
239+
if containsSelector(svc.Service.Spec.Selector, seenSelectors) {
240+
groupedServices = append(groupedServices, svc)
241+
continue
242+
}
243+
244+
services = append(services, svc)
245+
seenSelectors = append(seenSelectors, svc.Service.Spec.Selector)
246+
}
247+
201248
standaloneDCs, coveredByDCs := graphview.AllDeploymentConfigPipelines(g, coveredNodes)
202249
coveredNodes.Insert(coveredByDCs.List()...)
203250

@@ -243,6 +290,22 @@ func (d *ProjectStatusDescriber) Describe(namespace, name string) (string, error
243290
sort.Sort(exposedRoutes(exposes))
244291

245292
fmt.Fprintln(out)
293+
294+
// print services that should be grouped with this service based on matching selectors
295+
for _, groupedSvc := range groupedServices {
296+
if !selectorEquals(groupedSvc.Service.Spec.Selector, service.Service.Spec.Selector) {
297+
continue
298+
}
299+
300+
var grouppedExposes []string
301+
for _, routeNode := range groupedSvc.ExposingRoutes {
302+
grouppedExposes = append(grouppedExposes, describeRouteInServiceGroup(local, routeNode)...)
303+
}
304+
sort.Sort(exposedRoutes(grouppedExposes))
305+
306+
printLines(out, "", 0, describeServiceInServiceGroup(f, groupedSvc, grouppedExposes...)...)
307+
}
308+
246309
printLines(out, "", 0, describeServiceInServiceGroup(f, service, exposes...)...)
247310

248311
for _, dcPipeline := range service.DeploymentConfigPipelines {

0 commit comments

Comments
 (0)