Skip to content

Commit ca611d8

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

File tree

4 files changed

+110
-89
lines changed

4 files changed

+110
-89
lines changed

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 }}

alpha/action/generate_dockerfile_test.go

+58-30
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
@@ -143,15 +165,15 @@ LABEL operators.operatorframework.io.index.configs.v1=/configs
143165
`,
144166
},
145167
{
146-
name: "Lite/Success/WithExtraLabels",
168+
name: "Scratch/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

cmd/opm/generate/cmd.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func NewCmd() *cobra.Command {
1818
cmd := &cobra.Command{
1919
Use: "generate",
20-
Short: "Generate various artifacts for declarative config indexes",
20+
Short: "Generate various artifacts for file-based catalogs",
2121
}
2222
cmd.AddCommand(
2323
newDockerfileCmd(),
@@ -28,25 +28,36 @@ func NewCmd() *cobra.Command {
2828
func newDockerfileCmd() *cobra.Command {
2929
var (
3030
baseImage string
31+
builderImage string
3132
extraLabelStrs []string
32-
lite bool
3333
)
3434
cmd := &cobra.Command{
35-
Use: "dockerfile <dcRootDir>",
35+
Use: "dockerfile <fbcRootDir>",
3636
Args: cobra.ExactArgs(1),
37-
Short: "Generate a Dockerfile for a declarative config index",
38-
Long: `Generate a Dockerfile for a declarative config index.
37+
Short: "Generate a Dockerfile for a file-based catalog",
38+
Long: `Generate a Dockerfile for a file-based catalog.
3939
40-
This command creates a Dockerfile in the same directory as the <dcRootDir>
41-
(named <dcDirName>.Dockerfile) that can be used to build the index. If a
40+
This command creates a Dockerfile in the same directory as the <fbcRootDir>
41+
(named <fbcRootDir>.Dockerfile) that can be used to build the index. If a
4242
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 base image can be specified. The builder image may not be "scratch".
4648
`,
47-
RunE: func(_ *cobra.Command, args []string) error {
49+
RunE: func(inCmd *cobra.Command, args []string) error {
4850
fromDir := filepath.Clean(args[0])
4951

52+
if builderImage == "scratch" {
53+
return fmt.Errorf("invalid builder image: %q", builderImage)
54+
}
55+
56+
// preserving old behavior, if binary-image is set but not builder-image, set builder-image to binary-image
57+
if inCmd.Flags().Changed("binary-image") && !inCmd.Flags().Changed("builder-image") {
58+
builderImage = baseImage
59+
}
60+
5061
extraLabels, err := parseLabels(extraLabelStrs)
5162
if err != nil {
5263
return err
@@ -72,21 +83,24 @@ value of each duplicate key will be added to the generated Dockerfile.
7283
defer f.Close()
7384

7485
gen := action.GenerateDockerfile{
75-
BaseImage: baseImage,
76-
IndexDir: indexName,
77-
ExtraLabels: extraLabels,
78-
Writer: f,
79-
Lite: lite,
86+
BaseImage: baseImage,
87+
BuilderImage: builderImage,
88+
IndexDir: indexName,
89+
ExtraLabels: extraLabels,
90+
Writer: f,
8091
}
8192
if err := gen.Run(); err != nil {
8293
log.Fatal(err)
8394
}
8495
return nil
8596
},
8697
}
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.")
98+
cmd.Flags().StringVar(&baseImage, "binary-image", containertools.DefaultBinarySourceImage, "Image in which to build catalog.")
99+
cmd.Flags().StringVarP(&baseImage, "base-image", "i", containertools.DefaultBinarySourceImage, "Image base to use to build catalog.")
100+
cmd.Flags().StringVarP(&builderImage, "builder-image", "b", containertools.DefaultBinarySourceImage, "Image to use as a build stage.")
89101
cmd.Flags().StringSliceVarP(&extraLabelStrs, "extra-labels", "l", []string{}, "Extra labels to include in the generated Dockerfile. Labels should be of the form 'key=value'.")
102+
cmd.Flags().MarkDeprecated("binary-image", "use --base-image instead")
103+
cmd.MarkFlagsMutuallyExclusive("binary-image", "base-image")
90104
return cmd
91105
}
92106

ohio.Dockerfile

-6
This file was deleted.

0 commit comments

Comments
 (0)