Skip to content

Commit

Permalink
template links for dynamic naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ban committed Oct 2, 2019
1 parent 4fab7d4 commit 0378ac5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/shoobyban/filehelper v0.0.0-20190509122806-291f797b4fe9
github.com/spf13/viper v1.4.0
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -79,12 +81,22 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/recursionpharma/go-csv-map v0.0.0-20160524001940-792523c65ae9 h1:cvht1GrOF8MbAgDvN6flt1sj9Aixv/SokD/XqH6MXIQ=
github.com/recursionpharma/go-csv-map v0.0.0-20160524001940-792523c65ae9/go.mod h1:Voxo2KUk+o0x2taVupAT82GWwNK6Nv7LFwZHyJduxnY=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/shoobyban/filehelper v0.0.0-20190509122806-291f797b4fe9 h1:s3rZ+JzKRIZUHyhgYwgLtQ7TAs2C4wN/baYLrz/3/EI=
github.com/shoobyban/filehelper v0.0.0-20190509122806-291f797b4fe9/go.mod h1:U7tiRw5lgi734CJNsUy/rURFPjnim0ZpmRT3ZXtHCMk=
github.com/shoobyban/mxj v1.8.5 h1:zAsGPjVTcsHO25DwQiX1TBgTTNhnZ0ElJ8tc05u+gZw=
github.com/shoobyban/mxj v1.8.5/go.mod h1:wxi+v21Kjjno03XA3hlnEPxGfZS0Gra+fuXWXwkvsN8=
github.com/shoobyban/slog v0.0.0-20190209173919-7f513f7a44c1 h1:mPLIPkJhNNBl477M8UhiyzmZRgNyBSx9lOeevhm3VYw=
github.com/shoobyban/slog v0.0.0-20190209173919-7f513f7a44c1/go.mod h1:262Gnlo2g3UKb2sfzPx6aWuybU0xilZraiw7SPUXRIU=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.1 h1:qgMbHoJbPbw579P+1zVY+6n4nIFuIchaIjzZ/I/Yq8M=
github.com/spf13/afero v1.2.1/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
Expand All @@ -110,6 +122,7 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190206173232-65e2d4e15006/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
Expand Down
46 changes: 30 additions & 16 deletions traefiker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ import (
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"

"github.com/shoobyban/filehelper"
)

// AuthInfo stores a users' docker registry/hub info
var AuthInfo sync.Map

var running = map[string]string{}

func main() {

log.Println("Reading configuration...")
Expand Down Expand Up @@ -67,6 +71,7 @@ func main() {
old[s.ID] = s.ID
}
log.Println(s.Image)
running[s.Image] = s.Names[0]
}

log.Println("Creating docker container...")
Expand Down Expand Up @@ -115,13 +120,13 @@ func (d *Docker) Run(ctx context.Context, imagename, imageurl string, labels map
io.Copy(os.Stdout, reader)
}

nets, err := d.cli.NetworkList(ctx, types.NetworkListOptions{})
netid := ""
for _, n := range nets {
if n.Name == conf["networks"][0] {
netid = n.ID
}
}
// nets, err := d.cli.NetworkList(ctx, types.NetworkListOptions{})
// netid := ""
// for _, n := range nets {
// if n.Name == conf["networks"][0] {
// netid = n.ID
// }
// }

mm := []mount.Mount{}

Expand Down Expand Up @@ -154,6 +159,16 @@ func (d *Docker) Run(ctx context.Context, imagename, imageurl string, labels map
// }
// hostconfig.PortBindings = nat.PortMap{containerPort: []nat.PortBinding{hostBinding}}
}

links := []string{}
for _, l := range conf["links"] {
l2, err := filehelper.Template(l, running)
if err != nil {
log.Fatalf("Error in parsing link templates: %v", err)
}
links = append(links, l2)
}

cont, err := d.cli.ContainerCreate(
context.Background(),
&container.Config{
Expand All @@ -162,18 +177,17 @@ func (d *Docker) Run(ctx context.Context, imagename, imageurl string, labels map
Labels: labels,
},
hostconfig,
nil,
&network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
conf["networks"][0]: &network.EndpointSettings{
Links: links,
Aliases: []string{imagename},
},
},
},
imagename+"_"+strconv.FormatInt(time.Now().UTC().Unix(), 32))
E(err)
d.cli.ContainerStart(ctx, cont.ID, types.ContainerStartOptions{})
d.cli.NetworkConnect(ctx,
netid,
cont.ID,
&network.EndpointSettings{
Links: conf["links"],
},
)
fmt.Println("Links:", conf["links"])
return cont.ID
}

Expand Down

0 comments on commit 0378ac5

Please sign in to comment.