From 619826abca1f35097bcf48cdf099b37be3f96130 Mon Sep 17 00:00:00 2001 From: Aleksejs Sinicins Date: Tue, 19 Jul 2016 10:10:45 +0300 Subject: [PATCH] Add support for value format string Fixes #1 --- README.md | 5 +++-- main.go | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 57c4676..dcbb9e3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) ``` diff --git a/main.go b/main.go index 18acdb0..a6da0f8 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "net" "os" "os/signal" + "strings" "syscall" "time" ) @@ -16,12 +17,14 @@ import ( 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]) @@ -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) }