2017-04-15 14:46:44 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2024-07-31 14:20:04 +00:00
|
|
|
dockertypes "github.com/docker/docker/api/types"
|
2017-07-06 14:28:13 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-04-15 14:46:44 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
|
|
|
)
|
|
|
|
|
2024-07-31 14:20:04 +00:00
|
|
|
func containerJSON(ops ...func(*dockertypes.ContainerJSON)) dockertypes.ContainerJSON {
|
|
|
|
c := &dockertypes.ContainerJSON{
|
|
|
|
ContainerJSONBase: &dockertypes.ContainerJSONBase{
|
2017-04-15 14:46:44 +00:00
|
|
|
Name: "fake",
|
|
|
|
HostConfig: &container.HostConfig{},
|
|
|
|
},
|
|
|
|
Config: &container.Config{},
|
2024-07-31 14:20:04 +00:00
|
|
|
NetworkSettings: &dockertypes.NetworkSettings{
|
|
|
|
NetworkSettingsBase: dockertypes.NetworkSettingsBase{},
|
2017-04-15 14:46:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *c
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:20:04 +00:00
|
|
|
func name(name string) func(*dockertypes.ContainerJSON) {
|
|
|
|
return func(c *dockertypes.ContainerJSON) {
|
2017-04-15 14:46:44 +00:00
|
|
|
c.ContainerJSONBase.Name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:20:04 +00:00
|
|
|
func networkMode(mode string) func(*dockertypes.ContainerJSON) {
|
|
|
|
return func(c *dockertypes.ContainerJSON) {
|
2017-04-15 14:46:44 +00:00
|
|
|
c.ContainerJSONBase.HostConfig.NetworkMode = container.NetworkMode(mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:20:04 +00:00
|
|
|
func nodeIP(ip string) func(*dockertypes.ContainerJSON) {
|
|
|
|
return func(c *dockertypes.ContainerJSON) {
|
|
|
|
c.ContainerJSONBase.Node = &dockertypes.ContainerNode{
|
2017-10-25 18:20:03 +00:00
|
|
|
IPAddress: ip,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:20:04 +00:00
|
|
|
func ports(portMap nat.PortMap) func(*dockertypes.ContainerJSON) {
|
|
|
|
return func(c *dockertypes.ContainerJSON) {
|
2017-04-15 14:46:44 +00:00
|
|
|
c.NetworkSettings.NetworkSettingsBase.Ports = portMap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:20:04 +00:00
|
|
|
func withNetwork(name string, ops ...func(*network.EndpointSettings)) func(*dockertypes.ContainerJSON) {
|
|
|
|
return func(c *dockertypes.ContainerJSON) {
|
2017-04-15 14:46:44 +00:00
|
|
|
if c.NetworkSettings.Networks == nil {
|
|
|
|
c.NetworkSettings.Networks = map[string]*network.EndpointSettings{}
|
|
|
|
}
|
|
|
|
c.NetworkSettings.Networks[name] = &network.EndpointSettings{}
|
|
|
|
for _, op := range ops {
|
|
|
|
op(c.NetworkSettings.Networks[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipv4(ip string) func(*network.EndpointSettings) {
|
|
|
|
return func(s *network.EndpointSettings) {
|
|
|
|
s.IPAddress = ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 09:20:09 +00:00
|
|
|
func ipv6(ip string) func(*network.EndpointSettings) {
|
|
|
|
return func(s *network.EndpointSettings) {
|
|
|
|
s.GlobalIPv6Address = ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 14:46:44 +00:00
|
|
|
func swarmTask(id string, ops ...func(*swarm.Task)) swarm.Task {
|
|
|
|
task := &swarm.Task{
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(task)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *task
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskSlot(slot int) func(*swarm.Task) {
|
|
|
|
return func(task *swarm.Task) {
|
|
|
|
task.Slot = slot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:42:03 +00:00
|
|
|
func taskNetworkAttachment(id, name, driver string, addresses []string) func(*swarm.Task) {
|
2018-02-12 16:50:05 +00:00
|
|
|
return func(task *swarm.Task) {
|
|
|
|
task.NetworksAttachments = append(task.NetworksAttachments, swarm.NetworkAttachment{
|
|
|
|
Network: swarm.Network{
|
|
|
|
ID: id,
|
|
|
|
Spec: swarm.NetworkSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
DriverConfiguration: &swarm.Driver{
|
|
|
|
Name: driver,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Addresses: addresses,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 14:46:44 +00:00
|
|
|
func taskStatus(ops ...func(*swarm.TaskStatus)) func(*swarm.Task) {
|
|
|
|
return func(task *swarm.Task) {
|
|
|
|
status := &swarm.TaskStatus{}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
task.Status = *status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskState(state swarm.TaskState) func(*swarm.TaskStatus) {
|
|
|
|
return func(status *swarm.TaskStatus) {
|
|
|
|
status.State = state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:50:05 +00:00
|
|
|
func taskContainerStatus(id string) func(*swarm.TaskStatus) {
|
|
|
|
return func(status *swarm.TaskStatus) {
|
2019-08-05 16:24:03 +00:00
|
|
|
status.ContainerStatus = &swarm.ContainerStatus{
|
2018-02-12 16:50:05 +00:00
|
|
|
ContainerID: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-15 14:46:44 +00:00
|
|
|
func swarmService(ops ...func(*swarm.Service)) swarm.Service {
|
|
|
|
service := &swarm.Service{
|
|
|
|
ID: "serviceID",
|
|
|
|
Spec: swarm.ServiceSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
|
|
|
Name: "defaultServiceName",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *service
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceName(name string) func(service *swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Annotations.Name = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceLabels(labels map[string]string) func(service *swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Annotations.Labels = labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withEndpoint(ops ...func(*swarm.Endpoint)) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
endpoint := &swarm.Endpoint{}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Endpoint = *endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func virtualIP(networkID, addr string) func(*swarm.Endpoint) {
|
|
|
|
return func(endpoint *swarm.Endpoint) {
|
|
|
|
if endpoint.VirtualIPs == nil {
|
|
|
|
endpoint.VirtualIPs = []swarm.EndpointVirtualIP{}
|
|
|
|
}
|
|
|
|
endpoint.VirtualIPs = append(endpoint.VirtualIPs, swarm.EndpointVirtualIP{
|
|
|
|
NetworkID: networkID,
|
|
|
|
Addr: addr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withEndpointSpec(ops ...func(*swarm.EndpointSpec)) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
endpointSpec := &swarm.EndpointSpec{}
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
op(endpointSpec)
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Spec.EndpointSpec = endpointSpec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 09:40:04 +00:00
|
|
|
func modeDNSRR(spec *swarm.EndpointSpec) {
|
2017-04-15 14:46:44 +00:00
|
|
|
spec.Mode = swarm.ResolutionModeDNSRR
|
|
|
|
}
|
|
|
|
|
|
|
|
func modeVIP(spec *swarm.EndpointSpec) {
|
|
|
|
spec.Mode = swarm.ResolutionModeVIP
|
|
|
|
}
|