From 3ff6579fec9b086d90827e69c5b8a99d83f1c01e Mon Sep 17 00:00:00 2001 From: Nishant Totla Date: Tue, 16 May 2017 16:09:53 -0700 Subject: [PATCH 1/2] Adding compatible platforms to service spec Signed-off-by: Nishant Totla Upstream-commit: 587d07cca89be978b9f9ca3203ad28b7179e49fd Component: engine --- components/engine/client/service_create.go | 19 +++++++ .../engine/client/service_create_test.go | 54 +++++++++++++++++++ components/engine/client/service_update.go | 2 + 3 files changed, 75 insertions(+) diff --git a/components/engine/client/service_create.go b/components/engine/client/service_create.go index 90082d150f..73fc68fb33 100644 --- a/components/engine/client/service_create.go +++ b/components/engine/client/service_create.go @@ -6,6 +6,7 @@ import ( "github.com/docker/distribution/reference" "github.com/docker/docker/api/types" + registrytypes "github.com/docker/docker/api/types/registry" "github.com/docker/docker/api/types/swarm" "github.com/opencontainers/go-digest" "golang.org/x/net/context" @@ -33,6 +34,8 @@ func (cli *Client) ServiceCreate(ctx context.Context, service swarm.ServiceSpec, if img != "" { service.TaskTemplate.ContainerSpec.Image = img } + // add platforms that are compatible with the service + service.TaskTemplate.Placement = updateServicePlatforms(service.TaskTemplate.Placement, distributionInspect) } } var response types.ServiceCreateResponse @@ -71,6 +74,22 @@ func imageWithDigestString(image string, dgst digest.Digest) string { return "" } +// updateServicePlatforms updates the Platforms in swarm.Placement to list +// all compatible platforms for the service, as found in distributionInspect +// and returns a pointer to the new or updated swarm.Placement struct +func updateServicePlatforms(placement *swarm.Placement, distributionInspect registrytypes.DistributionInspect) *swarm.Placement { + if placement == nil { + placement = &swarm.Placement{} + } + for _, p := range distributionInspect.Platforms { + placement.Platforms = append(placement.Platforms, swarm.Platform{ + Architecture: p.Architecture, + OS: p.OS, + }) + } + return placement +} + // digestWarning constructs a formatted warning string using the // image name that could not be pinned by digest. The formatting // is hardcoded, but could me made smarter in the future diff --git a/components/engine/client/service_create_test.go b/components/engine/client/service_create_test.go index 1e07382870..89b2edd936 100644 --- a/components/engine/client/service_create_test.go +++ b/components/engine/client/service_create_test.go @@ -10,7 +10,9 @@ import ( "testing" "github.com/docker/docker/api/types" + registrytypes "github.com/docker/docker/api/types/registry" "github.com/docker/docker/api/types/swarm" + "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/net/context" ) @@ -55,3 +57,55 @@ func TestServiceCreate(t *testing.T) { t.Fatalf("expected `service_id`, got %s", r.ID) } } + +func TestServiceCreateCompatiblePlatforms(t *testing.T) { + var platforms []v1.Platform + client := &Client{ + client: newMockClient(func(req *http.Request) (*http.Response, error) { + if strings.HasPrefix(req.URL.Path, "/services/create") { + // platforms should have been resolved by now + if len(platforms) != 1 || platforms[0].Architecture != "amd64" || platforms[0].OS != "linux" { + return nil, fmt.Errorf("incorrect platform information") + } + b, err := json.Marshal(types.ServiceCreateResponse{ + ID: "service_" + platforms[0].Architecture, + }) + if err != nil { + return nil, err + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader(b)), + }, nil + } else if strings.HasPrefix(req.URL.Path, "/distribution/") { + platforms = []v1.Platform{ + { + Architecture: "amd64", + OS: "linux", + }, + } + b, err := json.Marshal(registrytypes.DistributionInspect{ + Descriptor: v1.Descriptor{}, + Platforms: platforms, + }) + if err != nil { + return nil, err + } + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader(b)), + }, nil + } else { + return nil, fmt.Errorf("unexpected URL '%s'", req.URL.Path) + } + }), + } + + r, err := client.ServiceCreate(context.Background(), swarm.ServiceSpec{}, types.ServiceCreateOptions{QueryRegistry: true}) + if err != nil { + t.Fatal(err) + } + if r.ID != "service_amd64" { + t.Fatalf("expected `service_amd64`, got %s", r.ID) + } +} diff --git a/components/engine/client/service_update.go b/components/engine/client/service_update.go index 8c7776aad7..0cb35a1991 100644 --- a/components/engine/client/service_update.go +++ b/components/engine/client/service_update.go @@ -46,6 +46,8 @@ func (cli *Client) ServiceUpdate(ctx context.Context, serviceID string, version if img != "" { service.TaskTemplate.ContainerSpec.Image = img } + // add platforms that are compatible with the service + service.TaskTemplate.Placement = updateServicePlatforms(service.TaskTemplate.Placement, distributionInspect) } } From 5e539a93fd6704bed916fc7cd6a3c4ff14c9c28d Mon Sep 17 00:00:00 2001 From: Nishant Totla Date: Wed, 17 May 2017 16:58:54 -0700 Subject: [PATCH 2/2] Updating test for compatible platforms to test unmarshal body Signed-off-by: Nishant Totla Upstream-commit: 7d4b8fb3b56168326f36157064741539f34490b7 Component: engine --- .../engine/client/service_create_test.go | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/components/engine/client/service_create_test.go b/components/engine/client/service_create_test.go index 89b2edd936..2ece62b993 100644 --- a/components/engine/client/service_create_test.go +++ b/components/engine/client/service_create_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "strings" @@ -59,14 +60,24 @@ func TestServiceCreate(t *testing.T) { } func TestServiceCreateCompatiblePlatforms(t *testing.T) { - var platforms []v1.Platform + var ( + platforms []v1.Platform + distributionInspectBody io.ReadCloser + distributionInspect registrytypes.DistributionInspect + ) + client := &Client{ client: newMockClient(func(req *http.Request) (*http.Response, error) { if strings.HasPrefix(req.URL.Path, "/services/create") { - // platforms should have been resolved by now - if len(platforms) != 1 || platforms[0].Architecture != "amd64" || platforms[0].OS != "linux" { - return nil, fmt.Errorf("incorrect platform information") + // check if the /distribution endpoint returned correct output + err := json.NewDecoder(distributionInspectBody).Decode(&distributionInspect) + if err != nil { + return nil, err } + if len(distributionInspect.Platforms) == 0 || distributionInspect.Platforms[0].Architecture != platforms[0].Architecture || distributionInspect.Platforms[0].OS != platforms[0].OS { + return nil, fmt.Errorf("received incorrect platform information from registry") + } + b, err := json.Marshal(types.ServiceCreateResponse{ ID: "service_" + platforms[0].Architecture, }) @@ -91,6 +102,7 @@ func TestServiceCreateCompatiblePlatforms(t *testing.T) { if err != nil { return nil, err } + distributionInspectBody = ioutil.NopCloser(bytes.NewReader(b)) return &http.Response{ StatusCode: http.StatusOK, Body: ioutil.NopCloser(bytes.NewReader(b)),