From 3ee4fdc43aedf7ad7c9b39c03b3687a8f2fed9a0 Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Mon, 10 Jul 2017 14:50:04 +0100 Subject: [PATCH] Error if there are duplicate service names Continue to allow onboot to have duplicates as we do not run simultaneously so that is ok (and we number them anyway), but services are run together so we will get a runtime error if duplicated as this is the containerd/runc id. Signed-off-by: Justin Cormack --- cmd/moby/build.go | 5 ++++- src/moby/config.go | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/moby/build.go b/cmd/moby/build.go index 4daa0504b..f712fb6af 100644 --- a/cmd/moby/build.go +++ b/cmd/moby/build.go @@ -199,7 +199,10 @@ func build(args []string) { if err != nil { log.Fatalf("Invalid config: %v", err) } - m = moby.AppendConfig(m, c) + m, err = moby.AppendConfig(m, c) + if err != nil { + log.Fatalf("Cannot append config files: %v", err) + } } if *buildDisableTrust { diff --git a/src/moby/config.go b/src/moby/config.go index e425fc6e9..6703ff8b3 100644 --- a/src/moby/config.go +++ b/src/moby/config.go @@ -105,6 +105,18 @@ func convert(i interface{}) interface{} { return i } +func uniqueServices(m Moby) error { + // service names must be unique, as they run as simultaneous containers + names := map[string]bool{} + for _, s := range m.Services { + if names[s.Name] { + return fmt.Errorf("duplicate service name: %s", s.Name) + } + names[s.Name] = true + } + return nil +} + // NewConfig parses a config file func NewConfig(config []byte) (Moby, error) { m := Moby{} @@ -140,11 +152,15 @@ func NewConfig(config []byte) (Moby, error) { return m, err } + if err := uniqueServices(m); err != nil { + return m, err + } + return m, nil } // AppendConfig appends two configs. -func AppendConfig(m0, m1 Moby) Moby { +func AppendConfig(m0, m1 Moby) (Moby, error) { moby := m0 if m1.Kernel.Image != "" { moby.Kernel.Image = m1.Kernel.Image @@ -159,7 +175,11 @@ func AppendConfig(m0, m1 Moby) Moby { moby.Trust.Image = append(moby.Trust.Image, m1.Trust.Image...) moby.Trust.Org = append(moby.Trust.Org, m1.Trust.Org...) - return moby + if err := uniqueServices(moby); err != nil { + return moby, err + } + + return moby, nil } // NewImage validates an parses yaml or json for a Image