Skip to content

Commit 76c6b74

Browse files
committed
[supervisor] respond PortsStatus with ordering
1 parent ed62ab9 commit 76c6b74

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,26 @@ 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)
746+
}
747+
sort.SliceStable(stateKeys, func(i, j int) bool {
748+
return stateKeys[i] < stateKeys[j]
749+
})
750+
751+
appended := make(map[uint32]struct{})
752+
for _, list := range [][]uint32{pm.configs.workspaceConfigsOrder, stateKeys} {
753+
for _, port := range list {
754+
if _, ok := appended[port]; ok {
755+
continue
756+
}
757+
appended[port] = struct{}{}
758+
res = append(res, pm.getPortStatus(port))
759+
}
744760
}
761+
745762
return res
746763
}
747764

0 commit comments

Comments
 (0)