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.
|
|
|
|
func Service(builders ...func(*swarm.Service)) *swarm.Service {
|
2019-10-30 05:52:26 -04:00
|
|
|
service := &swarm.Service{}
|
|
|
|
defaults := []func(*swarm.Service){ServiceID("serviceID"), ServiceName("defaultServiceName")}
|
2017-04-07 01:14:53 -04:00
|
|
|
|
2019-10-30 05:52:26 -04:00
|
|
|
for _, opt := range append(defaults, builders...) {
|
|
|
|
opt(service)
|
2017-04-07 01:14:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return service
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
// ServiceID sets the service ID
|
linting: address assorted issues found by gocritic
internal/test/builders/config.go:36:15: captLocal: `ID' should not be capitalized (gocritic)
func ConfigID(ID string) func(config *swarm.Config) {
^
internal/test/builders/secret.go:45:15: captLocal: `ID' should not be capitalized (gocritic)
func SecretID(ID string) func(secret *swarm.Secret) {
^
internal/test/builders/service.go:21:16: captLocal: `ID' should not be capitalized (gocritic)
func ServiceID(ID string) func(*swarm.Service) {
^
cli/command/image/formatter_history.go:100:15: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.h.CreatedBy, "\t", " ", -1)` (gocritic)
createdBy := strings.Replace(c.h.CreatedBy, "\t", " ", -1)
^
e2e/image/push_test.go:246:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
e2e/image/push_test.go:313:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
cli/config/configfile/file_test.go:185:2: assignOp: replace `c.GetAllCallCount = c.GetAllCallCount + 1` with `c.GetAllCallCount++` (gocritic)
c.GetAllCallCount = c.GetAllCallCount + 1
^
cli/command/context/inspect_test.go:20:58: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.MetadataPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<METADATA_PATH>", strings.Replace(si.MetadataPath, `\`, `\\`, -1), 1)
^
cli/command/context/inspect_test.go:21:53: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.TLSPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<TLS_PATH>", strings.Replace(si.TLSPath, `\`, `\\`, -1), 1)
^
cli/command/container/formatter_stats.go:119:46: captLocal: `Stats' should not be capitalized (gocritic)
func statsFormatWrite(ctx formatter.Context, Stats []StatsEntry, osType string, trunc bool) error {
^
cli/command/container/stats_helpers.go:209:4: assignOp: replace `blkRead = blkRead + bioEntry.Value` with `blkRead += bioEntry.Value` (gocritic)
blkRead = blkRead + bioEntry.Value
^
cli/command/container/stats_helpers.go:211:4: assignOp: replace `blkWrite = blkWrite + bioEntry.Value` with `blkWrite += bioEntry.Value` (gocritic)
blkWrite = blkWrite + bioEntry.Value
^
cli/command/registry/formatter_search.go:67:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.s.Description, "\n", " ", -1)` (gocritic)
desc := strings.Replace(c.s.Description, "\n", " ", -1)
^
cli/command/registry/formatter_search.go:68:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", " ", -1)` (gocritic)
desc = strings.Replace(desc, "\r", " ", -1)
^
cli/command/service/list_test.go:164:5: assignOp: replace `tc.doc = tc.doc + " with quiet"` with `tc.doc += " with quiet"` (gocritic)
tc.doc = tc.doc + " with quiet"
^
cli/command/service/progress/progress.go:274:11: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(errMsg, "\n", " ", -1)` (gocritic)
errMsg = strings.Replace(errMsg, "\n", " ", -1)
^
cli/manifest/store/store.go:153:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(fileName, "/", "_", -1)` (gocritic)
return strings.Replace(fileName, "/", "_", -1)
^
cli/manifest/store/store.go:152:14: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(ref, ":", "-", -1)` (gocritic)
fileName := strings.Replace(ref, ":", "-", -1)
^
cli/command/plugin/formatter.go:79:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.p.Config.Description, "\n", "", -1)` (gocritic)
desc := strings.Replace(c.p.Config.Description, "\n", "", -1)
^
cli/command/plugin/formatter.go:80:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", "", -1)` (gocritic)
desc = strings.Replace(desc, "\r", "", -1)
^
cli/compose/convert/service.go:642:23: captLocal: `DNS' should not be capitalized (gocritic)
func convertDNSConfig(DNS []string, DNSSearch []string) *swarm.DNSConfig {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 07:30:05 -05:00
|
|
|
func ServiceID(id string) func(*swarm.Service) {
|
2017-06-20 14:00:01 -04:00
|
|
|
return func(service *swarm.Service) {
|
linting: address assorted issues found by gocritic
internal/test/builders/config.go:36:15: captLocal: `ID' should not be capitalized (gocritic)
func ConfigID(ID string) func(config *swarm.Config) {
^
internal/test/builders/secret.go:45:15: captLocal: `ID' should not be capitalized (gocritic)
func SecretID(ID string) func(secret *swarm.Secret) {
^
internal/test/builders/service.go:21:16: captLocal: `ID' should not be capitalized (gocritic)
func ServiceID(ID string) func(*swarm.Service) {
^
cli/command/image/formatter_history.go:100:15: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.h.CreatedBy, "\t", " ", -1)` (gocritic)
createdBy := strings.Replace(c.h.CreatedBy, "\t", " ", -1)
^
e2e/image/push_test.go:246:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
e2e/image/push_test.go:313:34: badCall: suspicious Join on 1 argument (gocritic)
assert.NilError(t, os.RemoveAll(filepath.Join(dir.Join("trust"))))
^
cli/config/configfile/file_test.go:185:2: assignOp: replace `c.GetAllCallCount = c.GetAllCallCount + 1` with `c.GetAllCallCount++` (gocritic)
c.GetAllCallCount = c.GetAllCallCount + 1
^
cli/command/context/inspect_test.go:20:58: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.MetadataPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<METADATA_PATH>", strings.Replace(si.MetadataPath, `\`, `\\`, -1), 1)
^
cli/command/context/inspect_test.go:21:53: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(si.TLSPath, `\`, `\\`, -1)` (gocritic)
expected = strings.Replace(expected, "<TLS_PATH>", strings.Replace(si.TLSPath, `\`, `\\`, -1), 1)
^
cli/command/container/formatter_stats.go:119:46: captLocal: `Stats' should not be capitalized (gocritic)
func statsFormatWrite(ctx formatter.Context, Stats []StatsEntry, osType string, trunc bool) error {
^
cli/command/container/stats_helpers.go:209:4: assignOp: replace `blkRead = blkRead + bioEntry.Value` with `blkRead += bioEntry.Value` (gocritic)
blkRead = blkRead + bioEntry.Value
^
cli/command/container/stats_helpers.go:211:4: assignOp: replace `blkWrite = blkWrite + bioEntry.Value` with `blkWrite += bioEntry.Value` (gocritic)
blkWrite = blkWrite + bioEntry.Value
^
cli/command/registry/formatter_search.go:67:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.s.Description, "\n", " ", -1)` (gocritic)
desc := strings.Replace(c.s.Description, "\n", " ", -1)
^
cli/command/registry/formatter_search.go:68:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", " ", -1)` (gocritic)
desc = strings.Replace(desc, "\r", " ", -1)
^
cli/command/service/list_test.go:164:5: assignOp: replace `tc.doc = tc.doc + " with quiet"` with `tc.doc += " with quiet"` (gocritic)
tc.doc = tc.doc + " with quiet"
^
cli/command/service/progress/progress.go:274:11: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(errMsg, "\n", " ", -1)` (gocritic)
errMsg = strings.Replace(errMsg, "\n", " ", -1)
^
cli/manifest/store/store.go:153:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(fileName, "/", "_", -1)` (gocritic)
return strings.Replace(fileName, "/", "_", -1)
^
cli/manifest/store/store.go:152:14: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(ref, ":", "-", -1)` (gocritic)
fileName := strings.Replace(ref, ":", "-", -1)
^
cli/command/plugin/formatter.go:79:10: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(c.p.Config.Description, "\n", "", -1)` (gocritic)
desc := strings.Replace(c.p.Config.Description, "\n", "", -1)
^
cli/command/plugin/formatter.go:80:9: wrapperFunc: use strings.ReplaceAll method in `strings.Replace(desc, "\r", "", -1)` (gocritic)
desc = strings.Replace(desc, "\r", "", -1)
^
cli/compose/convert/service.go:642:23: captLocal: `DNS' should not be capitalized (gocritic)
func convertDNSConfig(DNS []string, DNSSearch []string) *swarm.DNSConfig {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-11-20 07:30:05 -05:00
|
|
|
service.ID = id
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 20:54:22 -04:00
|
|
|
// GlobalService sets the service to use "global" mode
|
|
|
|
func GlobalService() func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Mode = swarm.ServiceMode{Global: &swarm.GlobalService{}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplicatedService sets the service to use "replicated" mode with the specified number of replicas
|
2017-06-20 14:00:01 -04:00
|
|
|
func ReplicatedService(replicas uint64) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.Spec.Mode = swarm.ServiceMode{Replicated: &swarm.ReplicatedService{Replicas: &replicas}}
|
2019-10-27 20:54:22 -04:00
|
|
|
if service.ServiceStatus == nil {
|
|
|
|
service.ServiceStatus = &swarm.ServiceStatus{}
|
|
|
|
}
|
|
|
|
service.ServiceStatus.DesiredTasks = replicas
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceStatus sets the services' ServiceStatus (API v1.41 and above)
|
|
|
|
func ServiceStatus(desired, running uint64) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
|
|
|
service.ServiceStatus = &swarm.ServiceStatus{
|
|
|
|
RunningTasks: running,
|
|
|
|
DesiredTasks: desired,
|
|
|
|
}
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceImage sets the service's image
|
|
|
|
func ServiceImage(image string) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
2019-10-30 05:52:26 -04:00
|
|
|
if service.Spec.TaskTemplate.ContainerSpec == nil {
|
|
|
|
service.Spec.TaskTemplate.ContainerSpec = &swarm.ContainerSpec{}
|
|
|
|
}
|
|
|
|
service.Spec.TaskTemplate.ContainerSpec.Image = image
|
2017-06-20 14:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServicePort sets the service's port
|
|
|
|
func ServicePort(port swarm.PortConfig) func(*swarm.Service) {
|
|
|
|
return func(service *swarm.Service) {
|
2019-10-30 05:52:26 -04:00
|
|
|
if service.Spec.EndpointSpec == nil {
|
|
|
|
service.Spec.EndpointSpec = &swarm.EndpointSpec{}
|
|
|
|
}
|
2017-06-20 14:00:01 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|