2017-04-07 01:14:53 -04:00
|
|
|
package builders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service creates a service with default values.
|
|
|
|
// Any number of service builder functions can be passed to augment it.
|
|
|
|
// Currently, only ServiceName is implemented
|
|
|
|
func Service(builders ...func(*swarm.Service)) *swarm.Service {
|
|
|
|
service := &swarm.Service{
|
|
|
|
ID: "serviceID",
|
|
|
|
Spec: swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: "defaultServiceName",
|
|
|
|
},
|
2017-06-20 14:00:01 -04:00
|
|
|
EndpointSpec: &swarm.EndpointSpec{},
|
2017-04-07 01:14:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, builder := range builders {
|
|
|
|
builder(service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return service
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
// ServiceID sets the service ID
|
|
|
|
func ServiceID(ID string) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.ID = ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:14:53 -04:00
|
|
|
// ServiceName sets the service name
|
|
|
|
func ServiceName(name string) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Annotations.Name = name
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 14:00:01 -04:00
|
|
|
|
|
|
|
// ServiceLabels sets the service's labels
|
|
|
|
func ServiceLabels(labels map[string]string) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Annotations.Labels = labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplicatedService sets the number of replicas for the service
|
|
|
|
func ReplicatedService(replicas uint64) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Mode = swarm.ServiceMode{Replicated: &swarm.ReplicatedService{Replicas: &replicas}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceImage sets the service's image
|
|
|
|
func ServiceImage(image string) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.TaskTemplate = swarm.TaskSpec{ContainerSpec: swarm.ContainerSpec{Image: image}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServicePort sets the service's port
|
|
|
|
func ServicePort(port swarm.PortConfig) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.EndpointSpec.Ports = append(service.Spec.EndpointSpec.Ports, port)
|
2017-07-31 18:45:50 -04:00
|
|
|
|
|
|
|
assignedPort := port
|
|
|
|
if assignedPort.PublishedPort == 0 {
|
|
|
|
assignedPort.PublishedPort = 30000
|
|
|
|
}
|
|
|
|
service.Endpoint.Ports = append(service.Endpoint.Ports, assignedPort)
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
}
|