-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
228 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var staticSetDefinitions StaticSetDefinitions | ||
|
||
// StaticSetMember defines the parameters of a member of a static dispatcher set | ||
type StaticSetMember struct { | ||
Host string | ||
Port string | ||
} | ||
|
||
func (s *StaticSetMember) String() string { | ||
return fmt.Sprintf("%s:%s", s.Host, s.Port) | ||
} | ||
|
||
// StaticSetDefinition defines a static dispatcher set | ||
type StaticSetDefinition struct { | ||
id int | ||
members []*StaticSetMember | ||
} | ||
|
||
// Set configures a static dispatcher set | ||
func (s *StaticSetDefinition) Set(raw string) (err error) { | ||
pieces := strings.Split(raw, "=") | ||
if len(pieces) != 2 { | ||
return errors.New("failed to parse static set definition") | ||
} | ||
|
||
s.id, err = strconv.Atoi(pieces[0]) | ||
if err != nil { | ||
return errors.Errorf("failed to parse %s as an integer", pieces[0]) | ||
} | ||
|
||
// Handle multiple comma-delimited arguments | ||
hostList := strings.Split(pieces[1], ",") | ||
for _, h := range hostList { | ||
hostPieces := strings.Split(h, ":") | ||
switch len(hostPieces) { | ||
case 1: | ||
s.members = append(s.members, &StaticSetMember{ | ||
Host: hostPieces[0], | ||
Port: "5060", | ||
}) | ||
case 2: | ||
s.members = append(s.members, &StaticSetMember{ | ||
Host: hostPieces[0], | ||
Port: hostPieces[1], | ||
}) | ||
default: | ||
return errors.Errorf("failed to parse static set member %s", h) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *StaticSetDefinition) String() string { | ||
return fmt.Sprintf("%d=%s", s.id, strings.Join(s.Members(), ",")) | ||
} | ||
|
||
// Members returns the list of set members, formatted for direct inclusion in the dispatcher set | ||
func (s *StaticSetDefinition) Members() (list []string) { | ||
for _, m := range s.members { | ||
list = append(list, m.String()) | ||
} | ||
return | ||
} | ||
|
||
// StaticSetDefinitions is a list of static dispatcher sets | ||
type StaticSetDefinitions struct { | ||
list []*StaticSetDefinition | ||
} | ||
|
||
// String implements flag.Value | ||
func (s *StaticSetDefinitions) String() string { | ||
var list []string | ||
for _, s := range s.list { | ||
list = append(list, s.String()) | ||
} | ||
return strings.Join(list, ",") | ||
} | ||
|
||
// Set implements flag.Value | ||
func (s *StaticSetDefinitions) Set(raw string) error { | ||
d := new(StaticSetDefinition) | ||
|
||
if err := d.Set(raw); err != nil { | ||
return err | ||
} | ||
|
||
s.list = append(s.list, d) | ||
return nil | ||
} |