MCPcopy Create free account
hub / github.com/docker/cli / ToService

Method ToService

cli/command/service/opts.go:712–818  ·  view source on GitHub ↗

ToService takes the set of flags passed to the command and converts them into a service spec. Takes an API client as the second argument in order to resolve network names from the flags into network IDs. Returns an error if any flags are invalid or contradictory.

(ctx context.Context, apiClient client.NetworkAPIClient, flags *pflag.FlagSet)

Source from the content-addressed store, hash-verified

710//
711// Returns an error if any flags are invalid or contradictory.
712func (options *serviceOptions) ToService(ctx context.Context, apiClient client.NetworkAPIClient, flags *pflag.FlagSet) (swarm.ServiceSpec, error) {
713 var service swarm.ServiceSpec
714
715 currentEnv, err := options.makeEnv()
716 if err != nil {
717 return service, err
718 }
719
720 healthConfig, err := options.healthcheck.toHealthConfig()
721 if err != nil {
722 return service, err
723 }
724
725 serviceMode, err := options.ToServiceMode()
726 if err != nil {
727 return service, err
728 }
729
730 updateConfig := options.update.updateConfig(flags)
731 rollbackConfig := options.rollback.rollbackConfig(flags)
732
733 // update and rollback configuration is not supported for jobs. If these
734 // flags are not set, then the values will be nil. If they are non-nil,
735 // then return an error.
736 if (serviceMode.ReplicatedJob != nil || serviceMode.GlobalJob != nil) && (updateConfig != nil || rollbackConfig != nil) {
737 return service, errors.New("update and rollback configuration is not supported for jobs")
738 }
739
740 networks := convertNetworks(options.networks)
741 for i, net := range networks {
742 nwID, err := resolveNetworkID(ctx, apiClient, net.Target)
743 if err != nil {
744 return service, err
745 }
746 networks[i].Target = nwID
747 }
748 sort.Slice(networks, func(i, j int) bool {
749 return networks[i].Target < networks[j].Target
750 })
751
752 resources, err := options.resources.ToResourceRequirements(flags)
753 if err != nil {
754 return service, err
755 }
756
757 capAdd, capDrop := opts.EffectiveCapAddCapDrop(options.capAdd.GetSlice(), options.capDrop.GetSlice())
758
759 service = swarm.ServiceSpec{
760 Annotations: swarm.Annotations{
761 Name: options.name,
762 Labels: opts.ConvertKVStringsToMap(options.labels.GetSlice()),
763 },
764 TaskTemplate: swarm.TaskSpec{
765 ContainerSpec: &swarm.ContainerSpec{
766 Image: options.image,
767 Args: options.args,
768 Command: options.entrypoint.Value(),
769 Env: currentEnv,

Callers 6

TestToServiceNetworkFunction · 0.80
TestToServicePidsLimitFunction · 0.80
TestToServiceSysCtlsFunction · 0.80
runCreateFunction · 0.80

Calls 15

makeEnvMethod · 0.95
ToServiceModeMethod · 0.95
ToStopGracePeriodMethod · 0.95
convertNetworksFunction · 0.85
resolveNetworkIDFunction · 0.85
toHealthConfigMethod · 0.80
updateConfigMethod · 0.80
rollbackConfigMethod · 0.80
GetSliceMethod · 0.80
ToRestartPolicyMethod · 0.80

Tested by 5

TestToServiceNetworkFunction · 0.64
TestToServicePidsLimitFunction · 0.64
TestToServiceSysCtlsFunction · 0.64