Skip to content

Commit

Permalink
Add support for value format string
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
monder committed Jul 19, 2016
1 parent 29b797d commit 619826a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ rkt run \
--insecure-options=image \
--net=flannel \
docker://redis \
monder.cc/rkt-sidekick:v0.0.1 -- --cidr 10.0.0.0/16 /services/redis-a6f43b/ip
monder.cc/rkt-sidekick:v0.0.2 -- --cidr 10.0.0.0/16 -f '{"host":"$ip", "port":3000}' /services/redis-a6f43b/ip
```

The script above will launch redis and a sidekick in the same pod. The sidekick will enumerate all network interfaces and write the first one matching `10.0.0.0/16` to the key `/services/redis-a6f43b/ip`
The script above will launch redis and a sidekick in the same pod. The sidekick will enumerate all network interfaces and write the first one matching `10.0.0.0/16` to the formatted string to `/services/redis-a6f43b/ip`

Please note how to pass arguments to multiple images: https://coreos.com/rkt/docs/latest/subcommands/run.html#passing-arguments

Expand All @@ -31,6 +31,7 @@ Please note how to pass arguments to multiple images: https://coreos.com/rkt/doc
Flags:
--cidr string cidr to match the ip (default "0.0.0.0/0")
-e, --etcd-endpoint string an etcd address in the cluster (default "http://172.16.28.1:2379")
-f, --format string format of the etcd key value. '$ip' will be replace by container's ip address (default "$ip")
-i, --interval duration refresh interval (default 1m0s)
```

Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import (
"net"
"os"
"os/signal"
"strings"
"syscall"
"time"
)

var flags struct {
etcdAddress string
cidr string
format string
interval time.Duration
}

func init() {
pflag.StringVarP(&flags.etcdAddress, "etcd-endpoint", "e", "http://172.16.28.1:2379", "an etcd address in the cluster")
pflag.StringVar(&flags.cidr, "cidr", "0.0.0.0/0", "cidr to match the ip")
pflag.StringVarP(&flags.format, "format", "f", "$ip", "format of the etcd key value. '$ip' will be replace by container's ip address")
pflag.DurationVarP(&flags.interval, "interval", "i", time.Minute, "refresh interval")
pflag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n %s /key/in/etcd\n\nFlags:\n", os.Args[0])
Expand All @@ -48,7 +51,9 @@ func main() {
log.Fatal(err)
}

_, err = etcd.Set(context.Background(), pflag.Arg(0), ip, &client.SetOptions{TTL: 2 * flags.interval})
value := strings.Replace(flags.format, "$ip", ip, -1)

_, err = etcd.Set(context.Background(), pflag.Arg(0), value, &client.SetOptions{TTL: 2 * flags.interval})
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 619826a

Please sign in to comment.