Skip to content

Commit 6bcd58b

Browse files
committed
two-flag approach (with cleanup of old dockerfile)
Signed-off-by: Jordan Keister <[email protected]>
1 parent 4a89a44 commit 6bcd58b

File tree

5 files changed

+100
-86
lines changed

5 files changed

+100
-86
lines changed

Diff for: alpha/action/generate_dockerfile.go

+23-38
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,19 @@ import (
99
)
1010

1111
type GenerateDockerfile struct {
12-
BaseImage string
13-
IndexDir string
14-
ExtraLabels map[string]string
15-
Writer io.Writer
16-
Lite bool
12+
BaseImage string
13+
BuilderImage string
14+
IndexDir string
15+
ExtraLabels map[string]string
16+
Writer io.Writer
1717
}
1818

1919
func (i GenerateDockerfile) Run() error {
2020
if err := i.validate(); err != nil {
2121
return err
2222
}
2323

24-
var dockerfileTemplate string
25-
if i.Lite {
26-
dockerfileTemplate = binlessDockerfileTmpl
27-
} else {
28-
dockerfileTemplate = dockerfileTmpl
29-
}
30-
31-
t, err := template.New("dockerfile").Parse(dockerfileTemplate)
24+
t, err := template.New("dockerfile").Parse(dockerfileTmpl)
3225
if err != nil {
3326
// The template is hardcoded in the binary, so if
3427
// there is a parse error, it was a programmer error.
@@ -47,44 +40,36 @@ func (i GenerateDockerfile) validate() error {
4740
return nil
4841
}
4942

50-
const binlessDockerfileTmpl = `# The builder image is expected to contain
43+
const dockerfileTmpl = `# The builder image is expected to contain
5144
# /bin/opm (with serve subcommand)
52-
FROM {{.BaseImage}} as builder
45+
FROM {{.BuilderImage}} as builder
5346
5447
# Copy FBC root into image at /configs and pre-populate serve cache
5548
ADD {{.IndexDir}} /configs
5649
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
5750
58-
FROM scratch
59-
60-
COPY --from=builder /configs /configs
61-
COPY --from=builder /tmp/cache /tmp/cache
62-
63-
# Set FBC-specific label for the location of the FBC root directory
64-
# in the image
65-
LABEL ` + containertools.ConfigsLocationLabel + `=/configs
66-
{{- if .ExtraLabels }}
67-
68-
# Set other custom labels
69-
{{- range $key, $value := .ExtraLabels }}
70-
LABEL "{{ $key }}"="{{ $value }}"
71-
{{- end }}
72-
{{- end }}
73-
`
74-
75-
const dockerfileTmpl = `# The base image is expected to contain
76-
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
7751
FROM {{.BaseImage}}
7852
53+
{{- if ne .BaseImage "scratch" }}
54+
# The base image is expected to contain
55+
# /bin/opm (with serve subcommand) and /bin/grpc_health_probe
56+
7957
# Configure the entrypoint and command
8058
ENTRYPOINT ["/bin/opm"]
8159
CMD ["serve", "/configs", "--cache-dir=/tmp/cache"]
60+
{{- else }}
61+
# OLMv0 CatalogSources that use binary-less images must set:
62+
# spec:
63+
# grpcPodConfig:
64+
# extractContent:
65+
# catalogDir: /configs
66+
# cacheDir: /tmp/cache
67+
{{- end }}
8268
83-
# Copy declarative config root into image at /configs and pre-populate serve cache
84-
ADD {{.IndexDir}} /configs
85-
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
69+
COPY --from=builder /configs /configs
70+
COPY --from=builder /tmp/cache /tmp/cache
8671
87-
# Set DC-specific label for the location of the DC root directory
72+
# Set FBC-specific label for the location of the FBC root directory
8873
# in the image
8974
LABEL ` + containertools.ConfigsLocationLabel + `=/configs
9075
{{- if .ExtraLabels }}

Diff for: alpha/action/generate_dockerfile_test.go

+57-29
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,65 @@ func TestGenerateDockerfile(t *testing.T) {
4141
{
4242
name: "Success/WithoutExtraLabels",
4343
gen: GenerateDockerfile{
44-
BaseImage: "foo",
45-
IndexDir: "bar",
44+
BuilderImage: "foo",
45+
BaseImage: "foo",
46+
IndexDir: "bar",
4647
},
47-
expectedDockerfile: `# The base image is expected to contain
48-
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
48+
expectedDockerfile: `# The builder image is expected to contain
49+
# /bin/opm (with serve subcommand)
50+
FROM foo as builder
51+
52+
# Copy FBC root into image at /configs and pre-populate serve cache
53+
ADD bar /configs
54+
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
55+
4956
FROM foo
57+
# The base image is expected to contain
58+
# /bin/opm (with serve subcommand) and /bin/grpc_health_probe
5059
5160
# Configure the entrypoint and command
5261
ENTRYPOINT ["/bin/opm"]
5362
CMD ["serve", "/configs", "--cache-dir=/tmp/cache"]
5463
55-
# Copy declarative config root into image at /configs and pre-populate serve cache
56-
ADD bar /configs
57-
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
64+
COPY --from=builder /configs /configs
65+
COPY --from=builder /tmp/cache /tmp/cache
5866
59-
# Set DC-specific label for the location of the DC root directory
67+
# Set FBC-specific label for the location of the FBC root directory
6068
# in the image
6169
LABEL operators.operatorframework.io.index.configs.v1=/configs
6270
`,
6371
},
6472
{
6573
name: "Success/WithExtraLabels",
6674
gen: GenerateDockerfile{
67-
BaseImage: "foo",
68-
IndexDir: "bar",
75+
BuilderImage: "foo",
76+
BaseImage: "foo",
77+
IndexDir: "bar",
6978
ExtraLabels: map[string]string{
7079
"key1": "value1",
7180
"key2": "value2",
7281
},
7382
},
74-
expectedDockerfile: `# The base image is expected to contain
75-
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
83+
expectedDockerfile: `# The builder image is expected to contain
84+
# /bin/opm (with serve subcommand)
85+
FROM foo as builder
86+
87+
# Copy FBC root into image at /configs and pre-populate serve cache
88+
ADD bar /configs
89+
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
90+
7691
FROM foo
92+
# The base image is expected to contain
93+
# /bin/opm (with serve subcommand) and /bin/grpc_health_probe
7794
7895
# Configure the entrypoint and command
7996
ENTRYPOINT ["/bin/opm"]
8097
CMD ["serve", "/configs", "--cache-dir=/tmp/cache"]
8198
82-
# Copy declarative config root into image at /configs and pre-populate serve cache
83-
ADD bar /configs
84-
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
99+
COPY --from=builder /configs /configs
100+
COPY --from=builder /tmp/cache /tmp/cache
85101
86-
# Set DC-specific label for the location of the DC root directory
102+
# Set FBC-specific label for the location of the FBC root directory
87103
# in the image
88104
LABEL operators.operatorframework.io.index.configs.v1=/configs
89105
@@ -94,35 +110,35 @@ LABEL "key2"="value2"
94110
},
95111

96112
{
97-
name: "Lite/Fail/EmptyBaseImage",
113+
name: "Scratch/Fail/EmptyBaseImage",
98114
gen: GenerateDockerfile{
99-
IndexDir: "bar",
115+
BuilderImage: "foo",
116+
IndexDir: "bar",
100117
ExtraLabels: map[string]string{
101118
"key1": "value1",
102119
"key2": "value2",
103120
},
104-
Lite: true,
105121
},
106122
expectedErr: "base image is unset",
107123
},
108124
{
109-
name: "Lite/Fail/EmptyFromDir",
125+
name: "Scratch/Fail/EmptyFromDir",
110126
gen: GenerateDockerfile{
111-
BaseImage: "foo",
127+
BuilderImage: "foo",
128+
BaseImage: "scratch",
112129
ExtraLabels: map[string]string{
113130
"key1": "value1",
114131
"key2": "value2",
115132
},
116-
Lite: true,
117133
},
118134
expectedErr: "index directory is unset",
119135
},
120136
{
121-
name: "Lite/Success/WithoutExtraLabels",
137+
name: "Scratch/Success/WithoutExtraLabels",
122138
gen: GenerateDockerfile{
123-
BaseImage: "foo",
124-
IndexDir: "bar",
125-
Lite: true,
139+
BuilderImage: "foo",
140+
BaseImage: "scratch",
141+
IndexDir: "bar",
126142
},
127143
expectedDockerfile: `# The builder image is expected to contain
128144
# /bin/opm (with serve subcommand)
@@ -133,6 +149,12 @@ ADD bar /configs
133149
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
134150
135151
FROM scratch
152+
# OLMv0 CatalogSources that use binary-less images must set:
153+
# spec:
154+
# grpcPodConfig:
155+
# extractContent:
156+
# catalogDir: /configs
157+
# cacheDir: /tmp/cache
136158
137159
COPY --from=builder /configs /configs
138160
COPY --from=builder /tmp/cache /tmp/cache
@@ -145,13 +167,13 @@ LABEL operators.operatorframework.io.index.configs.v1=/configs
145167
{
146168
name: "Lite/Success/WithExtraLabels",
147169
gen: GenerateDockerfile{
148-
BaseImage: "foo",
149-
IndexDir: "bar",
170+
BuilderImage: "foo",
171+
BaseImage: "scratch",
172+
IndexDir: "bar",
150173
ExtraLabels: map[string]string{
151174
"key1": "value1",
152175
"key2": "value2",
153176
},
154-
Lite: true,
155177
},
156178
expectedDockerfile: `# The builder image is expected to contain
157179
# /bin/opm (with serve subcommand)
@@ -162,6 +184,12 @@ ADD bar /configs
162184
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
163185
164186
FROM scratch
187+
# OLMv0 CatalogSources that use binary-less images must set:
188+
# spec:
189+
# grpcPodConfig:
190+
# extractContent:
191+
# catalogDir: /configs
192+
# cacheDir: /tmp/cache
165193
166194
COPY --from=builder /configs /configs
167195
COPY --from=builder /tmp/cache /tmp/cache

Diff for: cmd/opm/generate/cmd.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func NewCmd() *cobra.Command {
2727

2828
func newDockerfileCmd() *cobra.Command {
2929
var (
30-
baseImage string
30+
binaryImage string
31+
builderImage string
3132
extraLabelStrs []string
32-
lite bool
3333
)
3434
cmd := &cobra.Command{
3535
Use: "dockerfile <dcRootDir>",
@@ -43,10 +43,16 @@ Dockerfile with the same name already exists, this command will fail.
4343
4444
When specifying extra labels, note that if duplicate keys exist, only the last
4545
value of each duplicate key will be added to the generated Dockerfile.
46+
47+
A separate builder and binary image can be specified. The builder image may not be "scratch".
4648
`,
4749
RunE: func(_ *cobra.Command, args []string) error {
4850
fromDir := filepath.Clean(args[0])
4951

52+
if builderImage == containertools.DefaultScratchSourceImage {
53+
return fmt.Errorf("invalid builder image: %q", builderImage)
54+
}
55+
5056
extraLabels, err := parseLabels(extraLabelStrs)
5157
if err != nil {
5258
return err
@@ -72,20 +78,20 @@ value of each duplicate key will be added to the generated Dockerfile.
7278
defer f.Close()
7379

7480
gen := action.GenerateDockerfile{
75-
BaseImage: baseImage,
76-
IndexDir: indexName,
77-
ExtraLabels: extraLabels,
78-
Writer: f,
79-
Lite: lite,
81+
BaseImage: binaryImage,
82+
BuilderImage: builderImage,
83+
IndexDir: indexName,
84+
ExtraLabels: extraLabels,
85+
Writer: f,
8086
}
8187
if err := gen.Run(); err != nil {
8288
log.Fatal(err)
8389
}
8490
return nil
8591
},
8692
}
87-
cmd.Flags().StringVarP(&baseImage, "binary-image", "i", containertools.DefaultBinarySourceImage, "Image in which to build catalog.")
88-
cmd.Flags().BoolVarP(&lite, "lite", "t", false, "Generate a smaller, binary-less Dockerfile.")
93+
cmd.Flags().StringVarP(&binaryImage, "binary-image", "i", containertools.DefaultBinarySourceImage, "Image in which to build catalog.")
94+
cmd.Flags().StringVarP(&builderImage, "builder-image", "b", containertools.DefaultBinarySourceImage, "Image to use as a build stage.")
8995
cmd.Flags().StringSliceVarP(&extraLabelStrs, "extra-labels", "l", []string{}, "Extra labels to include in the generated Dockerfile. Labels should be of the form 'key=value'.")
9096
return cmd
9197
}

Diff for: ohio.Dockerfile

-6
This file was deleted.

Diff for: pkg/containertools/dockerfilegenerator.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
)
99

1010
const (
11-
DefaultBinarySourceImage = "quay.io/operator-framework/opm:latest"
12-
DefaultDbLocation = "/database/index.db"
13-
DbLocationLabel = "operators.operatorframework.io.index.database.v1"
14-
ConfigsLocationLabel = "operators.operatorframework.io.index.configs.v1"
11+
DefaultScratchSourceImage = "scratch"
12+
DefaultBinarySourceImage = "quay.io/operator-framework/opm:latest"
13+
DefaultDbLocation = "/database/index.db"
14+
DbLocationLabel = "operators.operatorframework.io.index.database.v1"
15+
ConfigsLocationLabel = "operators.operatorframework.io.index.configs.v1"
1516
)
1617

1718
// DockerfileGenerator defines functions to generate index dockerfiles

0 commit comments

Comments
 (0)