Skip to content

Commit f68c049

Browse files
mustard-mhroboquat
authored andcommitted
[supervisor] remove ports for workspace config
1 parent dc3e193 commit f68c049

File tree

3 files changed

+14
-148
lines changed

3 files changed

+14
-148
lines changed

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

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package ports
66

77
import (
88
"context"
9-
"errors"
109
"fmt"
1110
"reflect"
1211
"regexp"
@@ -34,7 +33,6 @@ type SortConfig struct {
3433

3534
// Configs provides access to port configurations.
3635
type Configs struct {
37-
workspaceConfigs map[uint32]*SortConfig
3836
instancePortConfigs map[uint32]*SortConfig
3937
instanceRangeConfigs []*RangeConfig
4038
}
@@ -45,15 +43,13 @@ func (configs *Configs) ForEach(callback func(port uint32, config *SortConfig))
4543
return
4644
}
4745
visited := make(map[uint32]struct{})
48-
for _, configs := range []map[uint32]*SortConfig{configs.instancePortConfigs, configs.workspaceConfigs} {
49-
for port, config := range configs {
50-
_, exists := visited[port]
51-
if exists {
52-
continue
53-
}
54-
visited[port] = struct{}{}
55-
callback(port, config)
46+
for port, config := range configs.instancePortConfigs {
47+
_, exists := visited[port]
48+
if exists {
49+
continue
5650
}
51+
visited[port] = struct{}{}
52+
callback(port, config)
5753
}
5854
}
5955

@@ -76,10 +72,6 @@ func (configs *Configs) Get(port uint32) (*SortConfig, ConfigKind, bool) {
7672
if exists {
7773
return config, PortConfigKind, true
7874
}
79-
config, exists = configs.workspaceConfigs[port]
80-
if exists {
81-
return config, PortConfigKind, true
82-
}
8375
for _, rangeConfig := range configs.instanceRangeConfigs {
8476
if rangeConfig.Start <= port && port <= rangeConfig.End {
8577
return &SortConfig{
@@ -131,17 +123,6 @@ func (service *ConfigService) Observe(ctx context.Context) (<-chan *Configs, <-c
131123
configs := service.configService.Observe(ctx)
132124

133125
current := &Configs{}
134-
if service.gitpodAPI != nil {
135-
info, err := service.gitpodAPI.GetWorkspace(ctx, service.workspaceID)
136-
if err != nil {
137-
errorsChan <- err
138-
} else {
139-
current.workspaceConfigs = parseWorkspaceConfigs(info.Workspace.Config.Ports)
140-
updatesChan <- &Configs{workspaceConfigs: current.workspaceConfigs}
141-
}
142-
} else {
143-
errorsChan <- errors.New("could not connect to Gitpod API to fetch workspace port configs")
144-
}
145126

146127
for {
147128
select {
@@ -156,7 +137,6 @@ func (service *ConfigService) Observe(ctx context.Context) (<-chan *Configs, <-c
156137
continue
157138
}
158139
updatesChan <- &Configs{
159-
workspaceConfigs: current.workspaceConfigs,
160140
instancePortConfigs: current.instancePortConfigs,
161141
instanceRangeConfigs: current.instanceRangeConfigs,
162142
}
@@ -180,25 +160,6 @@ func (service *ConfigService) update(config *gitpod.GitpodConfig, current *Confi
180160

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

183-
func parseWorkspaceConfigs(ports []*gitpod.PortConfig) (portConfigs map[uint32]*SortConfig) {
184-
if len(ports) == 0 {
185-
return nil
186-
}
187-
portConfigs = make(map[uint32]*SortConfig)
188-
for _, config := range ports {
189-
port := uint32(config.Port)
190-
_, exists := portConfigs[port]
191-
if !exists {
192-
portConfigs[port] = &SortConfig{
193-
PortConfig: *config,
194-
// We don't care about workspace configs but instance config
195-
Sort: NON_CONFIGED_BASIC_SCORE - 1,
196-
}
197-
}
198-
}
199-
return portConfigs
200-
}
201-
202163
func parseInstanceConfigs(ports []*gitpod.PortsItems) (portConfigs map[uint32]*SortConfig, rangeConfigs []*RangeConfig) {
203164
for index, config := range ports {
204165
if config == nil {

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

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,14 @@ import (
1616

1717
func TestPortsConfig(t *testing.T) {
1818
tests := []struct {
19-
Desc string
20-
WorkspacePorts []*gitpod.PortConfig
21-
GitpodConfig *gitpod.GitpodConfig
22-
Expectation *PortConfigTestExpectations
19+
Desc string
20+
GitpodConfig *gitpod.GitpodConfig
21+
Expectation *PortConfigTestExpectations
2322
}{
2423
{
2524
Desc: "no configs",
2625
Expectation: &PortConfigTestExpectations{},
2726
},
28-
{
29-
Desc: "workspace port config",
30-
WorkspacePorts: []*gitpod.PortConfig{
31-
{
32-
Port: 9229,
33-
OnOpen: "ignore",
34-
Visibility: "public",
35-
Name: "Nice Port Name",
36-
Description: "Nice Port Description",
37-
},
38-
},
39-
Expectation: &PortConfigTestExpectations{
40-
WorkspaceConfigs: []*gitpod.PortConfig{
41-
{
42-
Port: 9229,
43-
OnOpen: "ignore",
44-
Visibility: "public",
45-
Name: "Nice Port Name",
46-
Description: "Nice Port Description",
47-
},
48-
},
49-
},
50-
},
5127
{
5228
Desc: "instance port config",
5329
GitpodConfig: &gitpod.GitpodConfig{
@@ -119,26 +95,11 @@ func TestPortsConfig(t *testing.T) {
11995
defer ctrl.Finish()
12096

12197
gitpodAPI := gitpod.NewMockAPIInterface(ctrl)
122-
gitpodAPI.EXPECT().GetWorkspace(context, workspaceID).Times(1).Return(&gitpod.WorkspaceInfo{
123-
Workspace: &gitpod.Workspace{
124-
Config: &gitpod.WorkspaceConfig{
125-
Ports: test.WorkspacePorts,
126-
},
127-
},
128-
}, nil)
12998

13099
service := NewConfigService(workspaceID, configService, gitpodAPI)
131100
updates, errors := service.Observe(context)
132101

133102
actual := &PortConfigTestExpectations{}
134-
select {
135-
case err := <-errors:
136-
t.Fatal(err)
137-
case change := <-updates:
138-
for _, config := range change.workspaceConfigs {
139-
actual.WorkspaceConfigs = append(actual.WorkspaceConfigs, &config.PortConfig)
140-
}
141-
}
142103

143104
if test.GitpodConfig != nil {
144105
go func() {
@@ -163,7 +124,6 @@ func TestPortsConfig(t *testing.T) {
163124
}
164125

165126
type PortConfigTestExpectations struct {
166-
WorkspaceConfigs []*gitpod.PortConfig
167127
InstancePortConfigs []*gitpod.PortConfig
168128
InstanceRangeConfigs []*RangeConfig
169129
}

components/supervisor/pkg/ports/ports_test.go

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ func TestPortsUpdateState(t *testing.T) {
2525
type ExposureExpectation []ExposedPort
2626
type UpdateExpectation [][]*api.PortsStatus
2727
type ConfigChange struct {
28-
workspace []*gitpod.PortConfig
29-
instance []*gitpod.PortsItems
28+
instance []*gitpod.PortsItems
3029
}
3130
type Change struct {
3231
Config *ConfigChange
@@ -109,47 +108,6 @@ func TestPortsUpdateState(t *testing.T) {
109108
ExpectedExposure: ExposureExpectation(nil),
110109
ExpectedUpdates: UpdateExpectation{{}},
111110
},
112-
{
113-
Desc: "serving configured workspace port",
114-
Changes: []Change{
115-
{Config: &ConfigChange{
116-
workspace: []*gitpod.PortConfig{
117-
{Port: 8080, OnOpen: "open-browser"},
118-
{Port: 9229, OnOpen: "ignore", Visibility: "private"},
119-
},
120-
}},
121-
{
122-
Exposed: []ExposedPort{
123-
{LocalPort: 8080, Public: true, URL: "8080-foobar"},
124-
{LocalPort: 9229, Public: false, URL: "9229-foobar"},
125-
},
126-
},
127-
{
128-
Served: []ServedPort{
129-
{net.IPv4zero, 8080, false},
130-
{net.IPv4(127, 0, 0, 1), 9229, true},
131-
},
132-
},
133-
},
134-
ExpectedExposure: []ExposedPort{
135-
{LocalPort: 8080},
136-
{LocalPort: 9229},
137-
},
138-
ExpectedUpdates: UpdateExpectation{
139-
{},
140-
[]*api.PortsStatus{
141-
{LocalPort: 8080, OnOpen: api.PortsStatus_open_browser},
142-
{LocalPort: 9229, OnOpen: api.PortsStatus_ignore}},
143-
[]*api.PortsStatus{
144-
{LocalPort: 8080, OnOpen: api.PortsStatus_open_browser, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_public, Url: "8080-foobar", OnExposed: api.OnPortExposedAction_open_browser}},
145-
{LocalPort: 9229, OnOpen: api.PortsStatus_ignore, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_private, Url: "9229-foobar", OnExposed: api.OnPortExposedAction_ignore}},
146-
},
147-
[]*api.PortsStatus{
148-
{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_open_browser, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_public, Url: "8080-foobar", OnExposed: api.OnPortExposedAction_open_browser}},
149-
{LocalPort: 9229, Served: true, OnOpen: api.PortsStatus_ignore, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_private, Url: "9229-foobar", OnExposed: api.OnPortExposedAction_ignore}},
150-
},
151-
},
152-
},
153111
{
154112
Desc: "serving port from the configured port range",
155113
Changes: []Change{
@@ -182,7 +140,7 @@ func TestPortsUpdateState(t *testing.T) {
182140
Desc: "auto expose configured ports",
183141
Changes: []Change{
184142
{
185-
Config: &ConfigChange{workspace: []*gitpod.PortConfig{
143+
Config: &ConfigChange{instance: []*gitpod.PortsItems{
186144
{Port: 8080, Visibility: "private"},
187145
}},
188146
},
@@ -244,7 +202,7 @@ func TestPortsUpdateState(t *testing.T) {
244202
Desc: "served between auto exposing configured and exposed update",
245203
Changes: []Change{
246204
{
247-
Config: &ConfigChange{workspace: []*gitpod.PortConfig{
205+
Config: &ConfigChange{instance: []*gitpod.PortsItems{
248206
{Port: 8080, Visibility: "private"},
249207
}},
250208
},
@@ -451,7 +409,7 @@ func TestPortsUpdateState(t *testing.T) {
451409
Desc: "port status has description set as soon as the port gets exposed, if there was a description configured",
452410
Changes: []Change{
453411
{
454-
Config: &ConfigChange{workspace: []*gitpod.PortConfig{
412+
Config: &ConfigChange{instance: []*gitpod.PortsItems{
455413
{Port: 8080, Visibility: "private", Description: "Development server"},
456414
}},
457415
},
@@ -476,7 +434,7 @@ func TestPortsUpdateState(t *testing.T) {
476434
Desc: "port status has the name attribute set as soon as the port gets exposed, if there was a name configured in Gitpod's Workspace",
477435
Changes: []Change{
478436
{
479-
Config: &ConfigChange{workspace: []*gitpod.PortConfig{
437+
Config: &ConfigChange{instance: []*gitpod.PortsItems{
480438
{Port: 3000, Visibility: "private", Name: "react"},
481439
}},
482440
},
@@ -582,9 +540,6 @@ func TestPortsUpdateState(t *testing.T) {
582540
Changes: []Change{
583541
{
584542
Config: &ConfigChange{
585-
workspace: []*gitpod.PortConfig{
586-
{Port: 3000, Visibility: "private", Name: "react"},
587-
},
588543
instance: []*gitpod.PortsItems{
589544
{Port: 3001, Visibility: "private", Name: "react"},
590545
{Port: 3000, Visibility: "private", Name: "react"},
@@ -593,9 +548,6 @@ func TestPortsUpdateState(t *testing.T) {
593548
},
594549
{
595550
Config: &ConfigChange{
596-
workspace: []*gitpod.PortConfig{
597-
{Port: 3000, Visibility: "private", Name: "react"},
598-
},
599551
instance: []*gitpod.PortsItems{
600552
{Port: 3003, Visibility: "private", Name: "react"},
601553
{Port: 3001, Visibility: "private", Name: "react"},
@@ -612,9 +564,6 @@ func TestPortsUpdateState(t *testing.T) {
612564
},
613565
{
614566
Config: &ConfigChange{
615-
workspace: []*gitpod.PortConfig{
616-
{Port: 3000, Visibility: "private", Name: "react"},
617-
},
618567
instance: []*gitpod.PortsItems{
619568
{Port: 3003, Visibility: "private", Name: "react"},
620569
{Port: 3000, Visibility: "private", Name: "react"},
@@ -623,9 +572,6 @@ func TestPortsUpdateState(t *testing.T) {
623572
},
624573
{
625574
Config: &ConfigChange{
626-
workspace: []*gitpod.PortConfig{
627-
{Port: 3000, Visibility: "private", Name: "react"},
628-
},
629575
instance: []*gitpod.PortsItems{
630576
{Port: "3001-3005", Visibility: "private", Name: "react"},
631577
{Port: 3003, Visibility: "private", Name: "react"},
@@ -738,7 +684,6 @@ func TestPortsUpdateState(t *testing.T) {
738684
for _, c := range test.Changes {
739685
if c.Config != nil {
740686
change := &Configs{}
741-
change.workspaceConfigs = parseWorkspaceConfigs(c.Config.workspace)
742687
portConfigs, rangeConfigs := parseInstanceConfigs(c.Config.instance)
743688
change.instancePortConfigs = portConfigs
744689
change.instanceRangeConfigs = rangeConfigs

0 commit comments

Comments
 (0)