forked from CyCoreSystems/dispatchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s.go
105 lines (85 loc) · 1.97 KB
/
k8s.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
)
var setDefinitions SetDefinitions
// SetDefinition describes a kubernetes dispatcher set's parameters
type SetDefinition struct {
id int
namespace string
name string
port string
}
// SetDefinitions represents a set of kubernetes dispatcher set parameter definitions
type SetDefinitions struct {
list []*SetDefinition
}
// String implements flag.Value
func (s *SetDefinitions) String() string {
var list []string
for _, d := range s.list {
list = append(list, d.String())
}
return strings.Join(list, ",")
}
// Set implements flag.Value
func (s *SetDefinitions) Set(raw string) error {
d := new(SetDefinition)
if err := d.Set(raw); err != nil {
return err
}
s.list = append(s.list, d)
return nil
}
func (s *SetDefinition) String() string {
return fmt.Sprintf("%s:%s=%d:%s", s.namespace, s.name, s.id, s.port)
}
// Set configures a kubernetes-derived dispatcher set
func (s *SetDefinition) Set(raw string) (err error) {
// Handle multiple comma-delimited arguments
if strings.Contains(raw, ",") {
args := strings.Split(raw, ",")
for _, n := range args {
if err = s.Set(n); err != nil {
return err
}
}
return nil
}
var id int
ns := "default"
var name string
port := "5060"
if os.Getenv("POD_NAMESPACE") != "" {
ns = os.Getenv("POD_NAMESPACE")
}
pieces := strings.SplitN(raw, "=", 2)
if len(pieces) < 2 {
return fmt.Errorf("failed to parse %s as the form [namespace:]name=index", raw)
}
naming := strings.SplitN(pieces[0], ":", 2)
if len(naming) < 2 {
name = naming[0]
} else {
ns = naming[0]
name = naming[1]
}
idString := pieces[1]
if pieces = strings.Split(pieces[1], ":"); len(pieces) > 1 {
idString = pieces[0]
port = pieces[1]
}
id, err = strconv.Atoi(idString)
if err != nil {
return errors.Wrap(err, "failed to parse index as an integer")
}
s.id = id
s.namespace = ns
s.name = name
s.port = port
return nil
}