-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
portmap.go
55 lines (43 loc) · 1.18 KB
/
portmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package scheduler_k3s
import (
"encoding/json"
"fmt"
"github.com/dokku/dokku/plugins/common"
)
type PortMap struct {
ContainerPort int32 `json:"container_port"`
HostPort int32 `json:"host_port"`
Scheme string `json:"scheme"`
}
func (p PortMap) String() string {
return fmt.Sprintf("%s-%d-%d", p.Scheme, p.HostPort, p.ContainerPort)
}
func (p PortMap) IsAllowedHttp() bool {
return p.Scheme == "http" || p.ContainerPort == 80
}
func (p PortMap) IsAllowedHttps() bool {
return p.Scheme == "https" || p.ContainerPort == 443
}
func getPortMaps(appName string) (map[string]PortMap, error) {
portMaps := []PortMap{}
allowedMappings := map[string]PortMap{}
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "ports-get",
Args: []string{appName, "json"},
})
if err != nil {
return allowedMappings, err
}
err = json.Unmarshal([]byte(results.StdoutContents()), &portMaps)
if err != nil {
return allowedMappings, err
}
for _, portMap := range portMaps {
if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() {
// todo: log warning
continue
}
allowedMappings[portMap.String()] = portMap
}
return allowedMappings, nil
}