Skip to content

Commit

Permalink
provide an optional base image flag for generated dockerfiles from bu…
Browse files Browse the repository at this point in the history
…ndle build commands

Signed-off-by: Jordan Keister <[email protected]>
  • Loading branch information
grokspawn committed Dec 12, 2024
1 parent dc23843 commit fe99e6f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
5 changes: 5 additions & 0 deletions cmd/opm/alpha/bundle/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
defaultChannel string
outputDir string
overwrite bool
baseImage string
)

// newBundleBuildCmd returns a command that will build operator bundle image.
Expand Down Expand Up @@ -76,6 +77,9 @@ Note:
bundleBuildCmd.Flags().StringVarP(&outputDir, "output-dir", "u", "",
"Optional output directory for operator manifests")

bundleBuildCmd.Flags().StringVar(&baseImage, "base-image", "scratch",
"Use a custom image pullspec as the base bundle image")

return bundleBuildCmd
}

Expand All @@ -89,5 +93,6 @@ func buildFunc(cmd *cobra.Command, _ []string) error {
channels,
defaultChannel,
overwrite,
baseImage,
)
}
2 changes: 2 additions & 0 deletions cmd/opm/alpha/bundle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Note:
bundleGenerateCmd.Flags().StringVarP(&outputDir, "output-dir", "u", "",
"Optional output directory for operator manifests")

bundleGenerateCmd.Flags().StringVar(&baseImage, "base-image", "scratch", "Use a custom image pullspec as the base bundle image")
return bundleGenerateCmd
}

Expand All @@ -56,5 +57,6 @@ func generateFunc(cmd *cobra.Command, _ []string) error {
channels,
defaultChannel,
true,
baseImage,
)
}
4 changes: 2 additions & 2 deletions pkg/lib/bundle/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ func ExecuteCommand(cmd *exec.Cmd) error {
// @channelDefault: The default channel for the bundle image
// @overwrite: Boolean flag to enable overwriting annotations.yaml locally if existed
func BuildFunc(directory, outputDir, imageTag, imageBuilder, packageName, channels, channelDefault string,
overwrite bool) error {
overwrite bool, baseImage string) error {
_, err := os.Stat(directory)
if os.IsNotExist(err) {
return err
}

// Generate annotations.yaml and Dockerfile
err = GenerateFunc(directory, outputDir, packageName, channels, channelDefault, overwrite)
err = GenerateFunc(directory, outputDir, packageName, channels, channelDefault, overwrite, baseImage)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/lib/bundle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type AnnotationMetadata struct {
// @channels: The list of channels that bundle image belongs to
// @channelDefault: The default channel for the bundle image
// @overwrite: Boolean flag to enable overwriting annotations.yaml locally if existed
func GenerateFunc(directory, outputDir, packageName, channels, channelDefault string, overwrite bool) error {
func GenerateFunc(directory, outputDir, packageName, channels, channelDefault string, overwrite bool, baseImage string) error {
// clean the input so that we know the absolute paths of input directories
directory, err := filepath.Abs(directory)
if err != nil {
Expand Down Expand Up @@ -132,7 +132,7 @@ func GenerateFunc(directory, outputDir, packageName, channels, channelDefault st
log.Info("Building Dockerfile")

// Generate Dockerfile
content, err = GenerateDockerfile(mediaType, ManifestsDir, MetadataDir, outManifestDir, outMetadataDir, workingDir, packageName, channels, channelDefault)
content, err = GenerateDockerfile(mediaType, ManifestsDir, MetadataDir, outManifestDir, outMetadataDir, workingDir, packageName, channels, channelDefault, baseImage)
if err != nil {
return err
}
Expand Down Expand Up @@ -319,7 +319,7 @@ func GenerateAnnotations(mediaType, manifests, metadata, packageName, channels,
// GenerateDockerfile builds Dockerfile with mediatype, manifests &
// metadata directories in bundle image, package name, channels and default
// channels information in LABEL section.
func GenerateDockerfile(mediaType, manifests, metadata, copyManifestDir, copyMetadataDir, workingDir, packageName, channels, channelDefault string) ([]byte, error) {
func GenerateDockerfile(mediaType, manifests, metadata, copyManifestDir, copyMetadataDir, workingDir, packageName, channels, channelDefault string, baseImage string) ([]byte, error) {
var fileContent string

relativeManifestDirectory, err := filepath.Rel(workingDir, copyManifestDir)
Expand All @@ -335,7 +335,7 @@ func GenerateDockerfile(mediaType, manifests, metadata, copyManifestDir, copyMet
relativeMetadataDirectory = filepath.ToSlash(relativeMetadataDirectory)

// FROM
fileContent += "FROM scratch\n\n"
fileContent += fmt.Sprintf("FROM %s\n\n", baseImage)

// LABEL
fileContent += fmt.Sprintf("LABEL %s=%s\n", MediatypeLabel, mediaType)
Expand Down
21 changes: 16 additions & 5 deletions pkg/lib/bundle/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestGenerateAnnotationsFunc(t *testing.T) {
}

func TestGenerateDockerfile(t *testing.T) {
expected := `FROM scratch
expected := `FROM %s
LABEL operators.operatorframework.io.bundle.mediatype.v1=test1
LABEL operators.operatorframework.io.bundle.manifests.v1=test2
Expand All @@ -174,9 +174,20 @@ COPY a/b/c /manifests/
COPY x/y/z /metadata/
`

actual, err := GenerateDockerfile("test1", "test2", "metadata/", filepath.Join("a", "b", "c"), filepath.Join("x", "y", "z"), "./", "test4", "test5", "")
require.NoError(t, err)
require.Equal(t, expected, string(actual))
type DockerfileTest struct {
baseImage string
}
tests := []DockerfileTest{
{baseImage: "scratch"},
{baseImage: "registry/group/image@tag"},
}

for _, tt := range tests {
tt_expected := fmt.Sprintf(expected, tt.baseImage)
actual, err := GenerateDockerfile("test1", "test2", "metadata/", filepath.Join("a", "b", "c"), filepath.Join("x", "y", "z"), "./", "test4", "test5", "", tt.baseImage)
require.NoError(t, err)
require.Equal(t, tt_expected, string(actual))
}
}

func TestCopyYamlOutput(t *testing.T) {
Expand Down Expand Up @@ -248,7 +259,7 @@ func TestGenerateFunc(t *testing.T) {
etcdPkgPath := "./testdata/etcd"
outputPath := "./testdata/tmp_output"
defer os.RemoveAll(outputPath)
err := GenerateFunc(filepath.Join(etcdPkgPath, "0.6.1"), outputPath, "", "", "", true)
err := GenerateFunc(filepath.Join(etcdPkgPath, "0.6.1"), outputPath, "", "", "", true, "scratch")
require.NoError(t, err)
os.Remove(filepath.Join("./", DockerFile))

Expand Down

0 comments on commit fe99e6f

Please sign in to comment.