woodpecker/yaml/transform/pod.go

95 lines
2.3 KiB
Go
Raw Normal View History

2016-05-09 18:28:49 +00:00
package transform
import (
"encoding/base64"
"fmt"
"github.com/drone/drone/yaml"
"github.com/gorilla/securecookie"
)
2016-08-25 18:35:28 +00:00
type ambassador struct {
image string
entrypoint []string
command []string
}
// default linux amd64 ambassador
var defaultAmbassador = ambassador{
image: "busybox:latest",
entrypoint: []string{"/bin/sleep"},
command: []string{"86400"},
}
// lookup ambassador configuration by architecture and os
var lookupAmbassador = map[string]ambassador{
"linux/amd64": {
image: "busybox:latest",
entrypoint: []string{"/bin/sleep"},
command: []string{"86400"},
},
"linux/arm": {
image: "armhf/alpine:latest",
entrypoint: []string{"/bin/sleep"},
command: []string{"86400"},
},
}
2016-05-09 18:28:49 +00:00
// Pod transforms the containers in the Yaml to use Pod networking, where every
// container shares the localhost connection.
2016-08-25 18:35:28 +00:00
func Pod(c *yaml.Config, platform string) error {
2016-05-09 18:28:49 +00:00
rand := base64.RawURLEncoding.EncodeToString(
securecookie.GenerateRandomKey(8),
)
2016-08-25 18:35:28 +00:00
// choose the ambassador configuration based on os and architecture
conf, ok := lookupAmbassador[platform]
if !ok {
conf = defaultAmbassador
}
2016-05-09 18:28:49 +00:00
ambassador := &yaml.Container{
2016-05-10 05:57:57 +00:00
ID: fmt.Sprintf("drone_ambassador_%s", rand),
Name: "ambassador",
2016-08-25 18:35:28 +00:00
Image: conf.image,
2016-05-10 05:57:57 +00:00
Detached: true,
2016-08-25 18:35:28 +00:00
Entrypoint: conf.entrypoint,
Command: conf.command,
2016-05-10 05:57:57 +00:00
Volumes: []string{c.Workspace.Path, c.Workspace.Base},
Environment: map[string]string{},
2016-05-09 18:28:49 +00:00
}
network := fmt.Sprintf("container:%s", ambassador.ID)
var containers []*yaml.Container
containers = append(containers, c.Pipeline...)
containers = append(containers, c.Services...)
for _, container := range containers {
container.VolumesFrom = append(container.VolumesFrom, ambassador.ID)
if container.Network == "" {
container.Network = network
}
}
c.Services = append([]*yaml.Container{ambassador}, c.Services...)
return nil
}
// func (v *podOp) VisitContainer(node *parse.ContainerNode) error {
// if node.Container.Network == "" {
// parent := fmt.Sprintf("container:%s", v.name)
// node.Container.Network = parent
// }
// node.Container.VolumesFrom = append(node.Container.VolumesFrom, v.name)
// return nil
// }
//
// func (v *podOp) VisitRoot(node *parse.RootNode) error {
//
//
// node.Pod = service
// return nil
// }