service_create.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/docker/distribution/reference"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/api/types/swarm"
  8. "github.com/opencontainers/go-digest"
  9. "golang.org/x/net/context"
  10. )
  11. // ServiceCreate creates a new Service.
  12. func (cli *Client) ServiceCreate(ctx context.Context, service swarm.ServiceSpec, options types.ServiceCreateOptions) (types.ServiceCreateResponse, error) {
  13. var distErr error
  14. headers := map[string][]string{
  15. "version": {cli.version},
  16. }
  17. if options.EncodedRegistryAuth != "" {
  18. headers["X-Registry-Auth"] = []string{options.EncodedRegistryAuth}
  19. }
  20. // Contact the registry to retrieve digest and platform information
  21. if options.QueryRegistry {
  22. distributionInspect, err := cli.DistributionInspect(ctx, service.TaskTemplate.ContainerSpec.Image, options.EncodedRegistryAuth)
  23. distErr = err
  24. if err == nil {
  25. // now pin by digest if the image doesn't already contain a digest
  26. img := imageWithDigestString(service.TaskTemplate.ContainerSpec.Image, distributionInspect.Descriptor.Digest)
  27. if img != "" {
  28. service.TaskTemplate.ContainerSpec.Image = img
  29. }
  30. }
  31. }
  32. var response types.ServiceCreateResponse
  33. resp, err := cli.post(ctx, "/services/create", nil, service, headers)
  34. if err != nil {
  35. return response, err
  36. }
  37. err = json.NewDecoder(resp.body).Decode(&response)
  38. if distErr != nil {
  39. response.Warnings = append(response.Warnings, digestWarning(service.TaskTemplate.ContainerSpec.Image))
  40. }
  41. ensureReaderClosed(resp)
  42. return response, err
  43. }
  44. // imageWithDigestString takes an image string and a digest, and updates
  45. // the image string if it didn't originally contain a digest. It assumes
  46. // that the image string is not an image ID
  47. func imageWithDigestString(image string, dgst digest.Digest) string {
  48. ref, err := reference.ParseAnyReference(image)
  49. if err == nil {
  50. if _, isCanonical := ref.(reference.Canonical); !isCanonical {
  51. namedRef, _ := ref.(reference.Named)
  52. img, err := reference.WithDigest(namedRef, dgst)
  53. if err == nil {
  54. return img.String()
  55. }
  56. }
  57. }
  58. return ""
  59. }
  60. // digestWarning constructs a formatted warning string using the
  61. // image name that could not be pinned by digest. The formatting
  62. // is hardcoded, but could me made smarter in the future
  63. func digestWarning(image string) string {
  64. return fmt.Sprintf("image %s could not be accessed on a registry to record\nits digest. Each node will access %s independently,\npossibly leading to different nodes running different\nversions of the image.\n", image, image)
  65. }