Skip to content

Commit 5d6f3cf

Browse files
committed
Automatically detect expose ports from dockerfile
1 parent 98c346b commit 5d6f3cf

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

cmd/init.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/covexo/devspace/pkg/devspace/config/generated"
1010
"github.com/covexo/devspace/pkg/devspace/kubectl"
1111

12+
"github.com/covexo/devspace/pkg/util/dockerfile"
1213
"github.com/covexo/devspace/pkg/util/kubeconfig"
1314

1415
"github.com/covexo/devspace/pkg/devspace/cloud"
@@ -186,7 +187,9 @@ func (cmd *InitCmd) Run(cobraCmd *cobra.Command, args []string) {
186187
cmd.configureDevSpace()
187188
}
188189

190+
cmd.addDefaultPorts()
189191
cmd.addDefaultSyncConfig()
192+
190193
cmd.configureRegistry()
191194

192195
err := configutil.SaveConfig()
@@ -308,6 +311,36 @@ func (cmd *InitCmd) configureDevSpace() {
308311
config.Cluster.Namespace = namespace
309312
}
310313

314+
func (cmd *InitCmd) addDefaultPorts() {
315+
dockerfilePath := filepath.Join(cmd.workdir, "Dockerfile")
316+
ports, err := dockerfile.GetPorts(dockerfilePath)
317+
if err != nil {
318+
log.Warnf("Error parsing dockerfile %s: %v", dockerfilePath, err)
319+
return
320+
}
321+
if len(ports) == 0 {
322+
return
323+
}
324+
325+
portMappings := []*v1.PortMapping{}
326+
for _, port := range ports {
327+
portMappings = append(portMappings, &v1.PortMapping{
328+
LocalPort: &port,
329+
RemotePort: &port,
330+
})
331+
}
332+
333+
config := configutil.GetConfig()
334+
config.DevSpace.Ports = &[]*v1.PortForwardingConfig{
335+
{
336+
LabelSelector: &map[string]*string{
337+
"release": configutil.String(configutil.DefaultDevspaceDeploymentName),
338+
},
339+
PortMappings: &portMappings,
340+
},
341+
}
342+
}
343+
311344
func (cmd *InitCmd) addDefaultSyncConfig() {
312345
config := configutil.GetConfig()
313346

cmd/up.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ func (cmd *UpCmd) buildAndDeploy() {
332332
// Build image if necessary
333333
mustRedeploy := cmd.buildImages(generatedConfig)
334334

335+
// Save Config
336+
err = generated.SaveConfig(generatedConfig)
337+
if err != nil {
338+
log.Fatalf("Error saving config: %v", err)
339+
}
340+
335341
// Deploy all defined deployments
336342
if config.DevSpace.Deployments != nil {
337343
for _, deployConfig := range *config.DevSpace.Deployments {

pkg/util/dockerfile/get.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dockerfile
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"regexp"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
var findExposePortsRegEx = regexp.MustCompile("$EXPOSE\\s(.*)^")
12+
13+
// GetPorts retrieves all the exported ports from a dockerfile
14+
func GetPorts(filename string) ([]int, error) {
15+
data, err := ioutil.ReadFile(filename)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
data = NormalizeNewlines(data)
21+
lines := strings.Split(string(data), "\n")
22+
ports := []int{}
23+
24+
for _, line := range lines {
25+
match := findExposePortsRegEx.FindStringSubmatch(line)
26+
if match == nil || len(match) != 2 {
27+
continue
28+
}
29+
30+
portStrings := strings.Split(match[1], " ")
31+
32+
OUTER:
33+
for _, port := range portStrings {
34+
if port == "" {
35+
continue
36+
}
37+
38+
intPort, err := strconv.Atoi(strings.Split(port, "/")[0])
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
// Check if port already exists
44+
for _, existingPort := range ports {
45+
if existingPort == intPort {
46+
continue OUTER
47+
}
48+
}
49+
50+
ports = append(ports, intPort)
51+
}
52+
}
53+
54+
return ports, nil
55+
}
56+
57+
// NormalizeNewlines normalizes \r\n (windows) and \r (mac)
58+
// into \n (unix)
59+
func NormalizeNewlines(d []byte) []byte {
60+
// replace CR LF \r\n (windows) with LF \n (unix)
61+
d = bytes.Replace(d, []byte{13, 10}, []byte{10}, -1)
62+
// replace CF \r (mac) with LF \n (unix)
63+
d = bytes.Replace(d, []byte{13}, []byte{10}, -1)
64+
return d
65+
}

0 commit comments

Comments
 (0)