Skip to content

Commit cdc17fe

Browse files
authored
Merge pull request moby#3053 from yyb196/fix_schema1_onbuild
fix bug that cann't recognize ONBUILD in schemaV1 img
2 parents 611137a + 9dd3dfd commit cdc17fe

File tree

5 files changed

+96
-49
lines changed

5 files changed

+96
-49
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package image
2+
3+
import (
4+
"time"
5+
6+
"github.com/docker/docker/api/types/strslice"
7+
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
8+
)
9+
10+
// HealthConfig holds configuration settings for the HEALTHCHECK feature.
11+
type HealthConfig struct {
12+
// Test is the test to perform to check that the container is healthy.
13+
// An empty slice means to inherit the default.
14+
// The options are:
15+
// {} : inherit healthcheck
16+
// {"NONE"} : disable healthcheck
17+
// {"CMD", args...} : exec arguments directly
18+
// {"CMD-SHELL", command} : run command with system's default shell
19+
Test []string `json:",omitempty"`
20+
21+
// Zero means to inherit. Durations are expressed as integer nanoseconds.
22+
Interval time.Duration `json:",omitempty"` // Interval is the time to wait between checks.
23+
Timeout time.Duration `json:",omitempty"` // Timeout is the time to wait before considering the check to have hung.
24+
StartPeriod time.Duration `json:",omitempty"` // The start period for the container to initialize before the retries starts to count down.
25+
26+
// Retries is the number of consecutive failures needed to consider a container as unhealthy.
27+
// Zero means inherit.
28+
Retries int `json:",omitempty"`
29+
}
30+
31+
// ImageConfig is a docker compatible config for an image
32+
type ImageConfig struct {
33+
ocispecs.ImageConfig
34+
35+
Healthcheck *HealthConfig `json:",omitempty"` // Healthcheck describes how to check the container is healthy
36+
ArgsEscaped bool `json:",omitempty"` // True if command is already escaped (Windows specific)
37+
38+
// NetworkDisabled bool `json:",omitempty"` // Is network disabled
39+
// MacAddress string `json:",omitempty"` // Mac Address of the container
40+
OnBuild []string // ONBUILD metadata that were defined on the image Dockerfile
41+
StopTimeout *int `json:",omitempty"` // Timeout (in seconds) to stop a container
42+
Shell strslice.StrSlice `json:",omitempty"` // Shell for shell-form of RUN, CMD, ENTRYPOINT
43+
}
44+
45+
// Image is the JSON structure which describes some basic information about the image.
46+
// This provides the `application/vnd.oci.image.config.v1+json` mediatype when marshalled to JSON.
47+
type Image struct {
48+
ocispecs.Image
49+
50+
// Config defines the execution parameters which should be used as a base when running a container using the image.
51+
Config ImageConfig `json:"config,omitempty"`
52+
53+
// Variant defines platform variant. To be added to OCI.
54+
Variant string `json:"variant,omitempty"`
55+
}

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/go-connections/nat"
2121
"github.com/moby/buildkit/client/llb"
2222
"github.com/moby/buildkit/client/llb/imagemetaresolver"
23+
"github.com/moby/buildkit/exporter/containerimage/image"
2324
"github.com/moby/buildkit/frontend/dockerfile/instructions"
2425
"github.com/moby/buildkit/frontend/dockerfile/parser"
2526
"github.com/moby/buildkit/frontend/dockerfile/shell"
@@ -1280,7 +1281,7 @@ func dispatchEntrypoint(d *dispatchState, c *instructions.EntrypointCommand) err
12801281
}
12811282

12821283
func dispatchHealthcheck(d *dispatchState, c *instructions.HealthCheckCommand) error {
1283-
d.image.Config.Healthcheck = &HealthConfig{
1284+
d.image.Config.Healthcheck = &image.HealthConfig{
12841285
Test: c.Health.Test,
12851286
Interval: c.Health.Interval,
12861287
Timeout: c.Health.Timeout,

frontend/dockerfile/dockerfile2llb/image.go

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,14 @@
11
package dockerfile2llb
22

33
import (
4-
"time"
5-
6-
"github.com/docker/docker/api/types/strslice"
4+
"github.com/moby/buildkit/exporter/containerimage/image"
75
"github.com/moby/buildkit/util/system"
86
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
97
)
108

11-
// HealthConfig holds configuration settings for the HEALTHCHECK feature.
12-
type HealthConfig struct {
13-
// Test is the test to perform to check that the container is healthy.
14-
// An empty slice means to inherit the default.
15-
// The options are:
16-
// {} : inherit healthcheck
17-
// {"NONE"} : disable healthcheck
18-
// {"CMD", args...} : exec arguments directly
19-
// {"CMD-SHELL", command} : run command with system's default shell
20-
Test []string `json:",omitempty"`
21-
22-
// Zero means to inherit. Durations are expressed as integer nanoseconds.
23-
Interval time.Duration `json:",omitempty"` // Interval is the time to wait between checks.
24-
Timeout time.Duration `json:",omitempty"` // Timeout is the time to wait before considering the check to have hung.
25-
StartPeriod time.Duration `json:",omitempty"` // The start period for the container to initialize before the retries starts to count down.
26-
27-
// Retries is the number of consecutive failures needed to consider a container as unhealthy.
28-
// Zero means inherit.
29-
Retries int `json:",omitempty"`
30-
}
31-
32-
// ImageConfig is a docker compatible config for an image
33-
type ImageConfig struct {
34-
ocispecs.ImageConfig
35-
36-
Healthcheck *HealthConfig `json:",omitempty"` // Healthcheck describes how to check the container is healthy
37-
ArgsEscaped bool `json:",omitempty"` // True if command is already escaped (Windows specific)
38-
39-
// NetworkDisabled bool `json:",omitempty"` // Is network disabled
40-
// MacAddress string `json:",omitempty"` // Mac Address of the container
41-
OnBuild []string // ONBUILD metadata that were defined on the image Dockerfile
42-
StopTimeout *int `json:",omitempty"` // Timeout (in seconds) to stop a container
43-
Shell strslice.StrSlice `json:",omitempty"` // Shell for shell-form of RUN, CMD, ENTRYPOINT
44-
}
45-
469
// Image is the JSON structure which describes some basic information about the image.
4710
// This provides the `application/vnd.oci.image.config.v1+json` mediatype when marshalled to JSON.
48-
type Image struct {
49-
ocispecs.Image
50-
51-
// Config defines the execution parameters which should be used as a base when running a container using the image.
52-
Config ImageConfig `json:"config,omitempty"`
53-
54-
// Variant defines platform variant. To be added to OCI.
55-
Variant string `json:"variant,omitempty"`
56-
}
11+
type Image image.Image
5712

5813
func clone(src Image) Image {
5914
img := src

util/imageutil/schema1.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/containerd/containerd/remotes"
11+
"github.com/moby/buildkit/exporter/containerimage/image"
1112
digest "github.com/opencontainers/go-digest"
1213
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1314
"github.com/pkg/errors"
@@ -44,7 +45,7 @@ func convertSchema1ConfigMeta(in []byte) ([]byte, error) {
4445
return nil, errors.Errorf("invalid schema1 manifest")
4546
}
4647

47-
var img ocispecs.Image
48+
var img image.Image
4849
if err := json.Unmarshal([]byte(m.History[0].V1Compatibility), &img); err != nil {
4950
return nil, errors.Wrap(err, "failed to unmarshal image from schema 1 history")
5051
}

util/imageutil/schema1_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package imageutil
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
func TestConvertSchema1ConfigMeta(t *testing.T) {
9+
dt := []byte(`{
10+
"schemaVersion": 1,
11+
"name": "base-global/common",
12+
"tag": "1.0.9.zb.standard",
13+
"architecture": "amd64",
14+
"fsLayers": [
15+
{
16+
"blobSum": "sha256:id1"
17+
}
18+
],
19+
"history": [
20+
{
21+
"v1Compatibility": "{\"id\":\"id2\",\"parent\":\"id3\",\"created\":\"2018-07-26T11:56:23.157525618Z\",\"config\":{\"Hostname\":\"4f3d4451\",\"Env\":[\"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"],\"Cmd\":null,\"Volumes\":{},\"OnBuild\":[\"ARG APP_NAME\",\"COPY ${APP_NAME}.tgz /home/admin/${APP_NAME}/target/${APP_NAME}.tgz\"],\"Labels\":{}},\"architecture\":\"amd64\",\"os\":\"linux\"}"
22+
}
23+
]
24+
}`)
25+
result, err := convertSchema1ConfigMeta(dt)
26+
if err != nil {
27+
t.Errorf("convertSchema1ConfigMeta error %v", err)
28+
return
29+
}
30+
if !bytes.Contains(result, []byte("OnBuild")) {
31+
t.Errorf("convertSchema1ConfigMeta lost onbuild")
32+
} else if !bytes.Contains(result, []byte("COPY ${APP_NAME}.tgz /home/admin/${APP_NAME}/target/${APP_NAME}.tg")) {
33+
t.Errorf("convertSchema1ConfigMeta lost onbuild content")
34+
}
35+
}

0 commit comments

Comments
 (0)