-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
132 lines (116 loc) · 3.47 KB
/
config.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
// config.go - config functions
// Copyright 2020-2022 F5 Inc.
// Licensed under the BSD 3-clause license; see LICENSE.md for more information.
import (
"fmt"
"os"
"strconv"
"strings"
)
var version string
type config struct {
AllRegions []string
ID string // PIPEFITTER_ID
PLAllowedPeers []string // PIPEFITTER_PL_ALLOWED_PEERS
PLRegions []string // PIPEFITTER_PL_REGIONS
TargetPort int64 // PIPEFITTER_TARGET_PORT
TargetRegions []string // PIPEFITTER_TARGET_REGIONS
TargetTag string // PIPEFITTER_TARGET_TAG
TargetValue string // PIPEFITTER_TARGET_VALUE
UpdateAllIPs bool // PIPEFITTER_UPDATE_ALL_IPS
SatelliteRegions []string
}
func buildConfig() config {
rawID := os.Getenv("PIPEFITTER_ID")
rawPLAllowedPeers := os.Getenv("PIPEFITTER_PL_ALLOWED_PEERS")
rawPLRegions := os.Getenv("PIPEFITTER_PL_REGIONS")
rawTargetPort := os.Getenv("PIPEFITTER_TARGET_PORT")
targetPort, err := strconv.ParseInt(rawTargetPort, 10, 64)
if err != nil {
fmt.Printf("PIPEFITTER_TARGET_PORT is not a number\n")
os.Exit(1)
}
if targetPort < 1 || targetPort > 65535 {
fmt.Printf("PIPEFITTER_TARGET_PORT not between 1-65535 (I got %d)\n", targetPort)
os.Exit(1)
}
rawTargetRegions := os.Getenv("PIPEFITTER_TARGET_REGIONS")
rawTargetTag := os.Getenv("PIPEFITTER_TARGET_TAG")
rawTargetValue := os.Getenv("PIPEFITTER_TARGET_VALUE")
rawUpdateAllIPs, err := strconv.ParseBool(os.Getenv("PIPEFITTER_UPDATE_ALL_IPS"))
if err != nil {
rawUpdateAllIPs = false
}
var missing []string
if rawID == "" {
missing = append(missing, "PIPEFITTER_ID")
}
if rawPLAllowedPeers == "" {
missing = append(missing, "PIPEFITTER_PL_ALLOWED_PEERS")
}
if rawPLRegions == "" {
missing = append(missing, "PIPEFITTER_PL_REGIONS")
}
if rawTargetPort == "" {
missing = append(missing, "PIPEFITTER_TARGET_PORT")
}
if rawTargetRegions == "" {
missing = append(missing, "PIPEFITTER_TARGET_REGIONS")
}
if rawTargetTag == "" {
missing = append(missing, "PIPEFITTER_TARGET_TAG")
}
if rawTargetValue == "" {
missing = append(missing, "PIPEFITTER_TARGET_VALUE")
}
if len(missing) > 0 {
fmt.Printf("Missing configuration environment vars: %v\n", missing)
os.Exit(1)
}
plRegions := strings.Split(rawPLRegions, ",")
targetRegions := strings.Split(rawTargetRegions, ",")
var allRegions []string
allRegions = append(allRegions, plRegions...)
allRegions = append(allRegions, targetRegions...)
allRegions = uniq(allRegions)
// Satellite Regions are ones where there's a PL presence but not a
// TargetHost presence.
var satelliteRegions []string
for _, region := range allRegions {
if !contains(targetRegions, region) {
satelliteRegions = append(satelliteRegions, region)
}
}
return config{
AllRegions: allRegions,
ID: rawID,
PLAllowedPeers: strings.Split(rawPLAllowedPeers, ","),
PLRegions: plRegions,
SatelliteRegions: satelliteRegions,
TargetPort: targetPort,
TargetRegions: targetRegions,
TargetTag: fmt.Sprintf("tag:%s", rawTargetTag),
TargetValue: rawTargetValue,
UpdateAllIPs: rawUpdateAllIPs,
}
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func uniq(elements []string) []string {
encountered := map[string]bool{}
for v := range elements {
encountered[elements[v]] = true
}
result := []string{}
for key := range encountered {
result = append(result, key)
}
return result
}