Skip to content

Commit 4857bbc

Browse files
committed
[supervisor] respond PortsStatus with order
1 parent ed62ab9 commit 4857bbc

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

components/supervisor/pkg/ports/ports-config.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ type RangeConfig struct {
2525

2626
// Configs provides access to port configurations.
2727
type Configs struct {
28-
workspaceConfigs map[uint32]*gitpod.PortConfig
29-
instancePortConfigs map[uint32]*gitpod.PortConfig
30-
instanceRangeConfigs []*RangeConfig
28+
workspaceConfigs map[uint32]*gitpod.PortConfig
29+
instancePortConfigs map[uint32]*gitpod.PortConfig
30+
instanceRangeConfigs []*RangeConfig
31+
workspaceConfigsOrder []uint32
3132
}
3233

3334
// ForEach iterates over all configured ports.
@@ -124,7 +125,7 @@ func (service *ConfigService) Observe(ctx context.Context) (<-chan *Configs, <-c
124125
if err != nil {
125126
errorsChan <- err
126127
} else {
127-
current.workspaceConfigs = parseWorkspaceConfigs(info.Workspace.Config.Ports)
128+
current.workspaceConfigs, current.workspaceConfigsOrder = parseWorkspaceConfigs(info.Workspace.Config.Ports)
128129
updatesChan <- &Configs{workspaceConfigs: current.workspaceConfigs}
129130
}
130131
} else {
@@ -144,9 +145,10 @@ func (service *ConfigService) Observe(ctx context.Context) (<-chan *Configs, <-c
144145
continue
145146
}
146147
updatesChan <- &Configs{
147-
workspaceConfigs: current.workspaceConfigs,
148-
instancePortConfigs: current.instancePortConfigs,
149-
instanceRangeConfigs: current.instanceRangeConfigs,
148+
workspaceConfigs: current.workspaceConfigs,
149+
instancePortConfigs: current.instancePortConfigs,
150+
instanceRangeConfigs: current.instanceRangeConfigs,
151+
workspaceConfigsOrder: current.workspaceConfigsOrder,
150152
}
151153
}
152154
}
@@ -168,19 +170,20 @@ func (service *ConfigService) update(config *gitpod.GitpodConfig, current *Confi
168170

169171
var portRangeRegexp = regexp.MustCompile(`^(\d+)[-:](\d+)$`)
170172

171-
func parseWorkspaceConfigs(ports []*gitpod.PortConfig) (portConfigs map[uint32]*gitpod.PortConfig) {
173+
func parseWorkspaceConfigs(ports []*gitpod.PortConfig) (portConfigs map[uint32]*gitpod.PortConfig, portsOrder []uint32) {
172174
if len(ports) == 0 {
173-
return nil
175+
return
174176
}
175177
portConfigs = make(map[uint32]*gitpod.PortConfig)
176178
for _, config := range ports {
177179
port := uint32(config.Port)
178180
_, exists := portConfigs[port]
179181
if !exists {
180182
portConfigs[port] = config
183+
portsOrder = append(portsOrder, port)
181184
}
182185
}
183-
return portConfigs
186+
return
184187
}
185188

186189
func parseInstanceConfigs(ports []*gitpod.PortsItems) (portConfigs map[uint32]*gitpod.PortConfig, rangeConfigs []*RangeConfig) {

components/supervisor/pkg/ports/ports.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,42 @@ func (pm *Manager) Subscribe() (*Subscription, error) {
739739
// Callers are expected to hold mu.
740740
func (pm *Manager) getStatus() []*api.PortsStatus {
741741
res := make([]*api.PortsStatus, 0, len(pm.state))
742+
743+
stateKeys := []uint32{}
742744
for port := range pm.state {
743-
res = append(res, pm.getPortStatus(port))
745+
stateKeys = append(stateKeys, port)
744746
}
747+
sort.SliceStable(stateKeys, func(i, j int) bool {
748+
return stateKeys[i] < stateKeys[j]
749+
})
750+
751+
appended := make(map[uint32]struct{})
752+
appendPort := func(port uint32, ignoreIfNotServed bool) {
753+
if _, ok := appended[port]; ok {
754+
return
755+
}
756+
appended[port] = struct{}{}
757+
status := pm.getPortStatus(port)
758+
if status == nil || (ignoreIfNotServed && !status.Served) {
759+
return
760+
}
761+
res = append(res, status)
762+
}
763+
for _, port := range pm.configs.workspaceConfigsOrder {
764+
appendPort(port, false)
765+
}
766+
for _, port := range stateKeys {
767+
appendPort(port, true)
768+
}
769+
745770
return res
746771
}
747772

748773
func (pm *Manager) getPortStatus(port uint32) *api.PortsStatus {
749774
mp := pm.state[port]
775+
if mp == nil {
776+
return nil
777+
}
750778
ps := &api.PortsStatus{
751779
LocalPort: mp.LocalhostPort,
752780
Served: mp.Served,

0 commit comments

Comments
 (0)