Skip to content

Commit

Permalink
generate binary-less dockerfiles
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Keister <jordan@nimblewidget.com>
  • Loading branch information
grokspawn committed Jun 12, 2024
1 parent 7393a82 commit 4a89a44
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
35 changes: 34 additions & 1 deletion alpha/action/generate_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ type GenerateDockerfile struct {
IndexDir string
ExtraLabels map[string]string
Writer io.Writer
Lite bool
}

func (i GenerateDockerfile) Run() error {
if err := i.validate(); err != nil {
return err
}

t, err := template.New("dockerfile").Parse(dockerfileTmpl)
var dockerfileTemplate string
if i.Lite {
dockerfileTemplate = binlessDockerfileTmpl
} else {
dockerfileTemplate = dockerfileTmpl
}

t, err := template.New("dockerfile").Parse(dockerfileTemplate)
if err != nil {
// The template is hardcoded in the binary, so if
// there is a parse error, it was a programmer error.
Expand All @@ -39,6 +47,31 @@ func (i GenerateDockerfile) validate() error {
return nil
}

const binlessDockerfileTmpl = `# The builder image is expected to contain
# /bin/opm (with serve subcommand)
FROM {{.BaseImage}} as builder
# Copy FBC root into image at /configs and pre-populate serve cache
ADD {{.IndexDir}} /configs
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
FROM scratch
COPY --from=builder /configs /configs
COPY --from=builder /tmp/cache /tmp/cache
# Set FBC-specific label for the location of the FBC root directory
# in the image
LABEL ` + containertools.ConfigsLocationLabel + `=/configs
{{- if .ExtraLabels }}
# Set other custom labels
{{- range $key, $value := .ExtraLabels }}
LABEL "{{ $key }}"="{{ $value }}"
{{- end }}
{{- end }}
`

const dockerfileTmpl = `# The base image is expected to contain
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
FROM {{.BaseImage}}
Expand Down
83 changes: 83 additions & 0 deletions alpha/action/generate_dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,89 @@ RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
# in the image
LABEL operators.operatorframework.io.index.configs.v1=/configs
# Set other custom labels
LABEL "key1"="value1"
LABEL "key2"="value2"
`,
},

{
name: "Lite/Fail/EmptyBaseImage",
gen: GenerateDockerfile{
IndexDir: "bar",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Lite: true,
},
expectedErr: "base image is unset",
},
{
name: "Lite/Fail/EmptyFromDir",
gen: GenerateDockerfile{
BaseImage: "foo",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Lite: true,
},
expectedErr: "index directory is unset",
},
{
name: "Lite/Success/WithoutExtraLabels",
gen: GenerateDockerfile{
BaseImage: "foo",
IndexDir: "bar",
Lite: true,
},
expectedDockerfile: `# The builder image is expected to contain
# /bin/opm (with serve subcommand)
FROM foo as builder
# Copy FBC root into image at /configs and pre-populate serve cache
ADD bar /configs
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
FROM scratch
COPY --from=builder /configs /configs
COPY --from=builder /tmp/cache /tmp/cache
# Set FBC-specific label for the location of the FBC root directory
# in the image
LABEL operators.operatorframework.io.index.configs.v1=/configs
`,
},
{
name: "Lite/Success/WithExtraLabels",
gen: GenerateDockerfile{
BaseImage: "foo",
IndexDir: "bar",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Lite: true,
},
expectedDockerfile: `# The builder image is expected to contain
# /bin/opm (with serve subcommand)
FROM foo as builder
# Copy FBC root into image at /configs and pre-populate serve cache
ADD bar /configs
RUN ["/bin/opm", "serve", "/configs", "--cache-dir=/tmp/cache", "--cache-only"]
FROM scratch
COPY --from=builder /configs /configs
COPY --from=builder /tmp/cache /tmp/cache
# Set FBC-specific label for the location of the FBC root directory
# in the image
LABEL operators.operatorframework.io.index.configs.v1=/configs
# Set other custom labels
LABEL "key1"="value1"
LABEL "key2"="value2"
Expand Down
3 changes: 3 additions & 0 deletions cmd/opm/generate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func newDockerfileCmd() *cobra.Command {
var (
baseImage string
extraLabelStrs []string
lite bool
)
cmd := &cobra.Command{
Use: "dockerfile <dcRootDir>",
Expand Down Expand Up @@ -75,6 +76,7 @@ value of each duplicate key will be added to the generated Dockerfile.
IndexDir: indexName,
ExtraLabels: extraLabels,
Writer: f,
Lite: lite,
}
if err := gen.Run(); err != nil {
log.Fatal(err)
Expand All @@ -83,6 +85,7 @@ value of each duplicate key will be added to the generated Dockerfile.
},
}
cmd.Flags().StringVarP(&baseImage, "binary-image", "i", containertools.DefaultBinarySourceImage, "Image in which to build catalog.")
cmd.Flags().BoolVarP(&lite, "lite", "t", false, "Generate a smaller, binary-less Dockerfile.")
cmd.Flags().StringSliceVarP(&extraLabelStrs, "extra-labels", "l", []string{}, "Extra labels to include in the generated Dockerfile. Labels should be of the form 'key=value'.")
return cmd
}
Expand Down

0 comments on commit 4a89a44

Please sign in to comment.