Skip to content

Commit

Permalink
two-flag approach (with cleanup of old dockerfile)
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Keister <[email protected]>
  • Loading branch information
grokspawn committed Jun 12, 2024
1 parent 4a89a44 commit 427e021
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
18 changes: 9 additions & 9 deletions alpha/action/generate_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type GenerateDockerfile struct {
BaseImage string
IndexDir string
ExtraLabels map[string]string
Writer io.Writer
Lite bool
BinaryImage string
BuilderImage string
IndexDir string
ExtraLabels map[string]string
Writer io.Writer
}

func (i GenerateDockerfile) Run() error {
Expand All @@ -22,7 +22,7 @@ func (i GenerateDockerfile) Run() error {
}

var dockerfileTemplate string
if i.Lite {
if i.BinaryImage == containertools.DefaultScratchSourceImage {
dockerfileTemplate = binlessDockerfileTmpl
} else {
dockerfileTemplate = dockerfileTmpl
Expand All @@ -38,7 +38,7 @@ func (i GenerateDockerfile) Run() error {
}

func (i GenerateDockerfile) validate() error {
if i.BaseImage == "" {
if i.BinaryImage == "" {
return fmt.Errorf("base image is unset")
}
if i.IndexDir == "" {
Expand All @@ -49,7 +49,7 @@ func (i GenerateDockerfile) validate() error {

const binlessDockerfileTmpl = `# The builder image is expected to contain
# /bin/opm (with serve subcommand)
FROM {{.BaseImage}} as builder
FROM {{.BuilderImage}} as builder
# Copy FBC root into image at /configs and pre-populate serve cache
ADD {{.IndexDir}} /configs
Expand All @@ -74,7 +74,7 @@ LABEL "{{ $key }}"="{{ $value }}"

const dockerfileTmpl = `# The base image is expected to contain
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
FROM {{.BaseImage}}
FROM {{.BinaryImage}}
# Configure the entrypoint and command
ENTRYPOINT ["/bin/opm"]
Expand Down
36 changes: 18 additions & 18 deletions alpha/action/generate_dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGenerateDockerfile(t *testing.T) {
{
name: "Fail/EmptyFromDir",
gen: GenerateDockerfile{
BaseImage: "foo",
BinaryImage: "foo",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
Expand All @@ -41,8 +41,8 @@ func TestGenerateDockerfile(t *testing.T) {
{
name: "Success/WithoutExtraLabels",
gen: GenerateDockerfile{
BaseImage: "foo",
IndexDir: "bar",
BinaryImage: "foo",
IndexDir: "bar",
},
expectedDockerfile: `# The base image is expected to contain
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
Expand All @@ -64,8 +64,8 @@ LABEL operators.operatorframework.io.index.configs.v1=/configs
{
name: "Success/WithExtraLabels",
gen: GenerateDockerfile{
BaseImage: "foo",
IndexDir: "bar",
BinaryImage: "foo",
IndexDir: "bar",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
Expand Down Expand Up @@ -94,35 +94,35 @@ LABEL "key2"="value2"
},

{
name: "Lite/Fail/EmptyBaseImage",
name: "Scratch/Fail/EmptyBaseImage",
gen: GenerateDockerfile{
IndexDir: "bar",
BuilderImage: "foo",
IndexDir: "bar",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Lite: true,
},
expectedErr: "base image is unset",
},
{
name: "Lite/Fail/EmptyFromDir",
name: "Scratch/Fail/EmptyFromDir",
gen: GenerateDockerfile{
BaseImage: "foo",
BuilderImage: "foo",
BinaryImage: "scratch",
ExtraLabels: map[string]string{
"key1": "value1",
"key2": "value2",
},
Lite: true,
},
expectedErr: "index directory is unset",
},
{
name: "Lite/Success/WithoutExtraLabels",
name: "Scratch/Success/WithoutExtraLabels",
gen: GenerateDockerfile{
BaseImage: "foo",
IndexDir: "bar",
Lite: true,
BuilderImage: "foo",
BinaryImage: "scratch",
IndexDir: "bar",
},
expectedDockerfile: `# The builder image is expected to contain
# /bin/opm (with serve subcommand)
Expand All @@ -145,13 +145,13 @@ LABEL operators.operatorframework.io.index.configs.v1=/configs
{
name: "Lite/Success/WithExtraLabels",
gen: GenerateDockerfile{
BaseImage: "foo",
IndexDir: "bar",
BinaryImage: "scratch",
BuilderImage: "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)
Expand Down
36 changes: 26 additions & 10 deletions cmd/opm/generate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func NewCmd() *cobra.Command {

func newDockerfileCmd() *cobra.Command {
var (
baseImage string
binaryImage string
builderImage string
extraLabelStrs []string
lite bool
)
cmd := &cobra.Command{
Use: "dockerfile <dcRootDir>",
Expand All @@ -43,10 +43,26 @@ Dockerfile with the same name already exists, this command will fail.
When specifying extra labels, note that if duplicate keys exist, only the last
value of each duplicate key will be added to the generated Dockerfile.
If --builder-image is set, this will generate a multi-stage Dockerfile which
will NOT include a serving binary.
If --builder-image is set, then --binary-image must not be set to anything other than "scratch".
Note: '--builder-image' and '--binary-image' are mutually exclusive.
`,
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(inCmd *cobra.Command, args []string) error {
fromDir := filepath.Clean(args[0])

switch {
// user specified both flags ... reject unless they set binary-image to scratch
case inCmd.Flags().Changed("binary-image") && inCmd.Flags().Changed("builder-image"):
if binaryImage != "scratch" {
return fmt.Errorf("invalid flag combination: if specifying --builder-image then --binary-image can only be \"scratch\" or default")
}
// user specifed only the builder image, set binary image to scratch
case inCmd.Flags().Changed("builder-image"):
binaryImage = "scratch"
}

extraLabels, err := parseLabels(extraLabelStrs)
if err != nil {
return err
Expand All @@ -72,20 +88,20 @@ value of each duplicate key will be added to the generated Dockerfile.
defer f.Close()

gen := action.GenerateDockerfile{
BaseImage: baseImage,
IndexDir: indexName,
ExtraLabels: extraLabels,
Writer: f,
Lite: lite,
BinaryImage: binaryImage,
BuilderImage: builderImage,
IndexDir: indexName,
ExtraLabels: extraLabels,
Writer: f,
}
if err := gen.Run(); err != nil {
log.Fatal(err)
}
return nil
},
}
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().StringVarP(&binaryImage, "binary-image", "i", containertools.DefaultBinarySourceImage, "Image in which to build catalog.")
cmd.Flags().StringVarP(&builderImage, "builder-image", "b", containertools.DefaultBinarySourceImage, "Image to use as a build stage.")
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
6 changes: 0 additions & 6 deletions ohio.Dockerfile

This file was deleted.

9 changes: 5 additions & 4 deletions pkg/containertools/dockerfilegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

const (
DefaultBinarySourceImage = "quay.io/operator-framework/opm:latest"
DefaultDbLocation = "/database/index.db"
DbLocationLabel = "operators.operatorframework.io.index.database.v1"
ConfigsLocationLabel = "operators.operatorframework.io.index.configs.v1"
DefaultScratchSourceImage = "scratch"
DefaultBinarySourceImage = "quay.io/operator-framework/opm:latest"
DefaultDbLocation = "/database/index.db"
DbLocationLabel = "operators.operatorframework.io.index.database.v1"
ConfigsLocationLabel = "operators.operatorframework.io.index.configs.v1"
)

// DockerfileGenerator defines functions to generate index dockerfiles
Expand Down

0 comments on commit 427e021

Please sign in to comment.