-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathports.go
97 lines (80 loc) · 1.79 KB
/
ports.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package k8s
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
)
const (
portDefinitionsEnv = "PORT_DEFINITIONS"
probeLabel = "probe"
serviceLabel = "service"
consulLabel = "consul"
)
type portDefinitions []portDefinition
func (pds portDefinitions) HasServicePortDefined() bool {
for _, pd := range pds {
if pd.isService() {
return true
}
}
return false
}
type portDefinition struct {
Port int `json:"port"`
Labels label `json:"labels"`
}
type label map[string]string
func (pd portDefinition) getTags() []string {
var tags []string
for key, value := range pd.Labels {
if value == "tag" {
tags = append(tags, key)
}
}
return tags
}
func (pd portDefinition) isService() bool {
return pd.hasServiceLabel()
}
func (pd portDefinition) isProbe() bool {
return pd.hasProbeLabel()
}
func (pd portDefinition) labelForConsul() string {
if pd.hasConsulLabel() {
return pd.Labels[consulLabel]
}
return ""
}
func (pd portDefinition) hasConsulLabel() bool {
if _, ok := pd.Labels[consulLabel]; ok {
return true
}
return false
}
func (pd portDefinition) hasServiceLabel() bool {
if val, ok := pd.Labels[serviceLabel]; ok && val == "true" {
return true
}
return false
}
func (pd portDefinition) hasProbeLabel() bool {
if val, ok := pd.Labels[probeLabel]; ok && val == "true" {
return true
}
return false
}
func getPortDefinitions() (*portDefinitions, error) {
portConfig := os.Getenv(portDefinitionsEnv)
if portConfig == "" {
log.Printf("no port configuration (%s)", portDefinitionsEnv)
return nil, nil
}
portDefinitions := &portDefinitions{}
err := json.Unmarshal([]byte(strings.Trim(portConfig, "'")), &portDefinitions)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal env data: %s", err)
}
return portDefinitions, nil
}