Skip to content

Commit 617ba98

Browse files
Merge pull request #124 from dinhxuanvu/bundle-build-fix
Bug 1771273: Fix bundle build command with Docker and update documentation
2 parents da90640 + 662aca5 commit 617ba98

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

docs/design/operator-bundle.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ The `--package` or `-p` is the name of package fo the operator such as `etcd` wh
159159

160160
All information in `annotations.yaml` is also existed in `LABEL` section of `Dockerfile`.
161161

162+
The `Dockerfile` can be used manually to build the bundle image using container image tools such as Docker, Podman or Buildah. For example, the Docker build command would be:
163+
164+
```bash
165+
$ docker build -f /path/to/Dockerfile -t quay.io/test/test-operator:latest /path/to/manifests/
166+
```
167+
162168
### Build Bundle Image
163169

164170
Operator bundle image can be built from provided operator manifests using `build` command (see *Notes* below). The overall `bundle build` command usage is:
@@ -196,4 +202,3 @@ The `--package` or `-p` is the name of package fo the operator such as `etcd` wh
196202
*Notes:*
197203
* If there is `Dockerfile` existing in the directory, it will be overwritten.
198204
* If there is an existing `annotations.yaml` in `/metadata` directory, the cli will attempt to validate it and returns any found errors. If the ``annotations.yaml`` is valid, it will be used as a part of build process. The optional boolean `--overwrite/-o` flag can be enabled (false by default) to allow cli to overwrite the `annotations.yaml` if existed.
199-
* The directory where the operator manifests are located must be inside the context of the build which in this case is inside the directory where you run the command.

pkg/lib/bundle/build.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ func BuildBundleImage(directory, imageTag, imageBuilder string) (*exec.Cmd, erro
1717

1818
switch imageBuilder {
1919
case "docker", "podman":
20-
args = append(args, "build", "-f", dockerfilePath, "-t", imageTag, ".")
20+
args = append(args, "build", "-f", dockerfilePath, "-t", imageTag, directory)
2121
case "buildah":
22-
args = append(args, "bud", "--format=docker", "-f", dockerfilePath, "-t", imageTag, ".")
22+
args = append(args, "bud", "--format=docker", "-f", dockerfilePath, "-t", imageTag, directory)
2323
default:
2424
return nil, fmt.Errorf("%s is not supported image builder", imageBuilder)
2525
}
@@ -54,8 +54,13 @@ func ExecuteCommand(cmd *exec.Cmd) error {
5454
// @channelDefault: The default channel for the bundle image
5555
// @overwrite: Boolean flag to enable overwriting annotations.yaml locally if existed
5656
func BuildFunc(directory, imageTag, imageBuilder, packageName, channels, channelDefault string, overwrite bool) error {
57+
_, err := os.Stat(directory)
58+
if os.IsNotExist(err) {
59+
return err
60+
}
61+
5762
// Generate annotations.yaml and Dockerfile
58-
err := GenerateFunc(directory, packageName, channels, channelDefault, overwrite)
63+
err = GenerateFunc(directory, packageName, channels, channelDefault, overwrite)
5964
if err != nil {
6065
return err
6166
}

pkg/lib/bundle/build_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ func TestBuildBundleImage(t *testing.T) {
2222
testOperatorDir,
2323
"test",
2424
"docker",
25-
"docker build -f /test-operator/Dockerfile -t test .",
25+
"docker build -f /test-operator/Dockerfile -t test /test-operator",
2626
"",
2727
},
2828
{
2929
testOperatorDir,
3030
"test",
3131
"buildah",
32-
"buildah bud --format=docker -f /test-operator/Dockerfile -t test .",
32+
"buildah bud --format=docker -f /test-operator/Dockerfile -t test /test-operator",
3333
"",
3434
},
3535
{
3636
testOperatorDir,
3737
"test",
3838
"podman",
39-
"podman build -f /test-operator/Dockerfile -t test .",
39+
"podman build -f /test-operator/Dockerfile -t test /test-operator",
4040
"",
4141
},
4242
{

pkg/lib/bundle/generate.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ type AnnotationMetadata struct {
4343
// @channelDefault: The default channel for the bundle image
4444
// @overwrite: Boolean flag to enable overwriting annotations.yaml locally if existed
4545
func GenerateFunc(directory, packageName, channels, channelDefault string, overwrite bool) error {
46-
var mediaType string
46+
_, err := os.Stat(directory)
47+
if os.IsNotExist(err) {
48+
return err
49+
}
4750

4851
// Determine mediaType
4952
mediaType, err := GetMediaType(directory)
@@ -77,7 +80,7 @@ func GenerateFunc(directory, packageName, channels, channelDefault string, overw
7780
log.Info("Building Dockerfile")
7881

7982
// Generate Dockerfile
80-
content, err = GenerateDockerfile(directory, mediaType, ManifestsDir, MetadataDir, packageName, channels, channelDefault)
83+
content, err = GenerateDockerfile(mediaType, ManifestsDir, MetadataDir, packageName, channels, channelDefault)
8184
if err != nil {
8285
return err
8386
}
@@ -226,7 +229,7 @@ func GenerateAnnotations(mediaType, manifests, metadata, packageName, channels,
226229
// GenerateDockerfile builds Dockerfile with mediatype, manifests &
227230
// metadata directories in bundle image, package name, channels and default
228231
// channels information in LABEL section.
229-
func GenerateDockerfile(directory, mediaType, manifests, metadata, packageName, channels, channelDefault string) ([]byte, error) {
232+
func GenerateDockerfile(mediaType, manifests, metadata, packageName, channels, channelDefault string) ([]byte, error) {
230233
var fileContent string
231234

232235
chanDefault, err := ValidateChannelDefault(channels, channelDefault)
@@ -246,8 +249,8 @@ func GenerateDockerfile(directory, mediaType, manifests, metadata, packageName,
246249
fileContent += fmt.Sprintf("LABEL %s=%s\n\n", ChannelDefaultLabel, chanDefault)
247250

248251
// CONTENT
249-
fileContent += fmt.Sprintf("ADD %s %s\n", filepath.Join(directory, "*.yaml"), "/manifests/")
250-
fileContent += fmt.Sprintf("ADD %s %s%s\n", filepath.Join(directory, metadata, AnnotationsFile), "/metadata/", AnnotationsFile)
252+
fileContent += fmt.Sprintf("COPY %s %s\n", "/*.yaml", "/manifests/")
253+
fileContent += fmt.Sprintf("COPY %s %s%s\n", filepath.Join("/", metadata, AnnotationsFile), "/metadata/", AnnotationsFile)
251254

252255
return []byte(fileContent), nil
253256
}

pkg/lib/bundle/generate_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ func TestGenerateDockerfileFunc(t *testing.T) {
212212
"LABEL operators.operatorframework.io.bundle.package.v1=test4\n"+
213213
"LABEL operators.operatorframework.io.bundle.channels.v1=test5\n"+
214214
"LABEL operators.operatorframework.io.bundle.channel.default.v1=test5\n\n"+
215-
"ADD %s/*.yaml /manifests/\n"+
216-
"ADD %s/annotations.yaml /metadata/annotations.yaml\n", MetadataDir, getTestDir(),
217-
filepath.Join(getTestDir(), MetadataDir))
215+
"COPY /*.yaml /manifests/\n"+
216+
"COPY %s/annotations.yaml /metadata/annotations.yaml\n", MetadataDir,
217+
filepath.Join("/", MetadataDir))
218218

219-
content, err := GenerateDockerfile(getTestDir(), "test1", "test2", MetadataDir, "test4", "test5", "")
219+
content, err := GenerateDockerfile("test1", "test2", MetadataDir, "test4", "test5", "")
220220
require.NoError(t, err)
221221
require.Equal(t, output, string(content))
222222
}

0 commit comments

Comments
 (0)