Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

build: Add ability to override images with YAML #96

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/moby/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func build(args []string) {
}
m = moby.AppendConfig(m, c)
}
m = moby.ApplyOverride(m)

if *buildDisableTrust {
log.Debugf("Disabling content trust checks for this build")
Expand Down
36 changes: 36 additions & 0 deletions src/moby/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type Moby struct {
Optional bool
Mode string
}
Overrides []struct {
Source string
Substitute string
}
}

// TrustConfig is the type of a content trust config
Expand Down Expand Up @@ -149,6 +153,7 @@ func AppendConfig(m0, m1 Moby) Moby {
moby.Files = append(moby.Files, m1.Files...)
moby.Trust.Image = append(moby.Trust.Image, m1.Trust.Image...)
moby.Trust.Org = append(moby.Trust.Org, m1.Trust.Org...)
moby.Overrides = append(moby.Overrides, m1.Overrides...)

return moby
}
Expand Down Expand Up @@ -776,3 +781,34 @@ func ConfigInspectToOCI(yaml Image, inspect types.ImageInspect) (specs.Spec, err

return oci, nil
}

// ApplyOverride applies an override to a given config
func ApplyOverride(config Moby) Moby {
for _, o := range config.Overrides {
if strings.Contains(config.Kernel.Image, o.Source) {
log.Debugf("Replacing kernel image with %s", o.Substitute)
config.Kernel.Image = o.Substitute
}

for i, img := range config.Init {
if strings.Contains(img, o.Source) {
log.Debugf("Replacing init image with %s", o.Substitute)
config.Init[i] = o.Substitute
}
}
for i, img := range config.Onboot {
if strings.Contains(img.Image, o.Source) {
log.Debugf("Replacing onboot image with %s", o.Substitute)
config.Onboot[i].Image = o.Substitute
}
}

for i, img := range config.Services {
if strings.Contains(img.Image, o.Source) {
log.Debugf("Replacing services image with %s", o.Substitute)
config.Services[i].Image = o.Substitute
}
}
}
return config
}
30 changes: 30 additions & 0 deletions src/moby/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,33 @@ func TestInvalidCap(t *testing.T) {
t.Error("expected error, got valid OCI config")
}
}

func TestApplyOverride(t *testing.T) {
config, err := NewConfig([]byte(`
kernel:
image: "foo/bar:foo"
init:
- foo/bar:foo
onboot:
- name: foo
image: foo/bar:foo
overrides:
- source: foo/bar
substitute: foo/bar:quux
`))
if err != nil {
t.Fatal(err)
}

c := ApplyOverride(config)

if c.Kernel.Image != "foo/bar:quux" {
t.Fatalf("No override!")
}
if c.Init[0] != "foo/bar:quux" {
t.Fatalf("No override1!")
}
if c.Onboot[0].Image != "foo/bar:quux" {
t.Fatalf("No override2!")
}
}
14 changes: 13 additions & 1 deletion src/moby/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ var schema = string(`
"images": {
"type": "array",
"items": { "$ref": "#/definitions/image" }
},
"override" : {
"type": "object",
"properties": {
"destination": { "type": "string" },
"source": { "type": "string" }
}
},
"overrides": {
"type": "array",
"items": { "$ref": "#/definitions/override" }
}
},
"properties": {
Expand All @@ -108,7 +119,8 @@ var schema = string(`
"onboot": { "$ref": "#/definitions/images" },
"services": { "$ref": "#/definitions/images" },
"trust": { "$ref": "#/definitions/trust" },
"files": { "$ref": "#/definitions/files" }
"files": { "$ref": "#/definitions/files" },
"overrides": { "$ref": "#/definitions/overrides" }
}
}
`)