Skip to content

Commit

Permalink
Add generate command
Browse files Browse the repository at this point in the history
This change adds the generate command to s2i, where it allows
the user to produce a Dockerfile able to be used by any build
system supporting the format, such as buildah and possibly
others.
  • Loading branch information
Igor Sutton Lopes committed Jan 10, 2020
1 parent 10bded7 commit c8e4429
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 3 deletions.
36 changes: 36 additions & 0 deletions contrib/completions/bash/s2i
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,41 @@ _s2i_create()
noun_aliases=()
}

_s2i_generate()
{
last_command="s2i_generate"
commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--assemble-runtime-user=")
local_nonpersistent_flags+=("--assemble-runtime-user=")
flags+=("--assemble-user=")
local_nonpersistent_flags+=("--assemble-user=")
flags+=("--env=")
two_word_flags+=("-e")
local_nonpersistent_flags+=("--env=")
flags+=("--quiet")
flags+=("-q")
local_nonpersistent_flags+=("--quiet")
flags+=("--ca=")
flags+=("--cert=")
flags+=("--key=")
flags+=("--loglevel=")
flags+=("--tls")
flags+=("--tlsverify")
flags+=("--url=")
two_word_flags+=("-U")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_s2i_rebuild()
{
last_command="s2i_rebuild"
Expand Down Expand Up @@ -519,6 +554,7 @@ _s2i()
commands+=("build")
commands+=("completion")
commands+=("create")
commands+=("generate")
commands+=("rebuild")
commands+=("usage")
commands+=("version")
Expand Down
36 changes: 36 additions & 0 deletions contrib/completions/zsh/s2i
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,41 @@ _s2i_create()
noun_aliases=()
}

_s2i_generate()
{
last_command="s2i_generate"
commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--assemble-runtime-user=")
local_nonpersistent_flags+=("--assemble-runtime-user=")
flags+=("--assemble-user=")
local_nonpersistent_flags+=("--assemble-user=")
flags+=("--env=")
two_word_flags+=("-e")
local_nonpersistent_flags+=("--env=")
flags+=("--quiet")
flags+=("-q")
local_nonpersistent_flags+=("--quiet")
flags+=("--ca=")
flags+=("--cert=")
flags+=("--key=")
flags+=("--loglevel=")
flags+=("--tls")
flags+=("--tlsverify")
flags+=("--url=")
two_word_flags+=("-U")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_s2i_rebuild()
{
last_command="s2i_rebuild"
Expand Down Expand Up @@ -680,6 +715,7 @@ _s2i()
commands+=("build")
commands+=("completion")
commands+=("create")
commands+=("generate")
commands+=("rebuild")
commands+=("usage")
commands+=("version")
Expand Down
9 changes: 9 additions & 0 deletions pkg/build/strategies/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ func (builder *Dockerfile) CreateDockerfile(config *api.Config) error {
for k, v := range config.Labels {
imageLabels[k] = v
}

if len(config.ScriptsURL) > 0 {
imageLabels[constants.ScriptsURLLabel] = config.ScriptsURL
}

if len(config.Destination) > 0 {
imageLabels[constants.DestinationLabel] = config.Destination
}

if len(imageLabels) > 0 {
first := true
buffer.WriteString("LABEL ")
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewCmdCLI() *cobra.Command {
s2iCmd.AddCommand(cmd.NewCmdRebuild(cfg))
s2iCmd.AddCommand(cmd.NewCmdUsage(cfg))
s2iCmd.AddCommand(cmd.NewCmdCreate())
s2iCmd.AddCommand(cmd.NewCmdGenerate(cfg))
cmdutil.SetupLogger(s2iCmd.PersistentFlags())
basename := filepath.Base(os.Args[0])
// Make case-insensitive and strip executable suffix if present
Expand Down
99 changes: 99 additions & 0 deletions pkg/cmd/cli/cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cmd

import (
"context"

"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/types"
"github.com/spf13/cobra"

"github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/api/constants"
"github.com/openshift/source-to-image/pkg/build/strategies/dockerfile"
"github.com/openshift/source-to-image/pkg/util/fs"
)

// getImageLabels attempts to inspect an image existing in a remote registry.
func getImageLabels(ctx context.Context, imageName string) (map[string]string, error) {
ref, err := alltransports.ParseImageName(imageName)
if err != nil {
return nil, err
}

img, err := ref.NewImage(ctx, &types.SystemContext{})
if err != nil {
return nil, err
}

imageMetadata, err := img.Inspect(ctx)
if err != nil {
return nil, err
}

return imageMetadata.Labels, nil
}

// generateDockerfile generates a Dockerfile with the given configuration.
func generateDockerfile(cfg *api.Config) error {
fileSystem := fs.NewFileSystem()
builder, err := dockerfile.New(cfg, fileSystem)
if err != nil {
return err
}

_, err = builder.Build(cfg)
if err != nil {
return err
}

return nil
}

// adjustConfigWithImageLabels adjusts the configuration with given labels.
func adjustConfigWithImageLabels(cfg *api.Config, labels map[string]string) {
if v, ok := labels[constants.ScriptsURLLabel]; ok {
cfg.ScriptsURL = v
}

if v, ok := labels[constants.DestinationLabel]; ok {
cfg.Destination = v
}

}

// NewCmdGenerate implements the S2I cli generate command.
func NewCmdGenerate(cfg *api.Config) *cobra.Command {
generateCmd := &cobra.Command{
Use: "generate <image> <dockerfile>",
Short: "Generate a Dockerfile based on the provided builder image",
Example: `
# Generate a Dockerfile from a builder image:
$ s2i generate docker://quay.io/redhat-developer/app-binding-operator Dockerfile.gen
`,
RunE: func(cmd *cobra.Command, _ []string) error {
if cmd.Flags().NArg() != 2 {
return cmd.Help()
}

cfg.BuilderImage = cmd.Flags().Arg(0)
cfg.AsDockerfile = cmd.Flags().Arg(1)

ctx := context.Background()
var imageLabels map[string]string
var err error
if imageLabels, err = getImageLabels(ctx, cfg.BuilderImage); err != nil {
return err
}

adjustConfigWithImageLabels(cfg, imageLabels)
return generateDockerfile(cfg)
},
}

generateCmd.Flags().BoolVarP(&(cfg.Quiet), "quiet", "q", false, "Operate quietly. Suppress all non-error output.")
generateCmd.Flags().VarP(&(cfg.Environment), "env", "e", "Specify an single environment variable in NAME=VALUE format")
generateCmd.Flags().StringVarP(&(cfg.AssembleUser), "assemble-user", "", "", "Specify the user to run assemble with")
generateCmd.Flags().StringVarP(&(cfg.AssembleRuntimeUser), "assemble-runtime-user", "", "", "Specify the user to run assemble-runtime with")

return generateCmd
}
2 changes: 1 addition & 1 deletion test/integration/docker/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (i integrationTest) InspectImage(name string) (*dockertypes.ImageInspect, e
defer cancel()
resp, _, err := engineClient.ImageInspectWithRaw(ctx, name)
if err != nil {
if dockerapi.IsErrImageNotFound(err) {
if dockerapi.IsErrNotFound(err) {
return nil, fmt.Errorf("no such image :%q", name)
}
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions test/integration/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func TestDockerfileBuildLabels(t *testing.T) {
AssembleUser: "",
ImageWorkDir: "",
Source: git.MustParse("https://github.com/sclorg/nodejs-ex"),
ScriptsURL: "",
ScriptsURL: "image:///scripts",
Injections: api.VolumeList{},
Destination: "",
Destination: "/destination",

Environment: api.EnvironmentList{},
Labels: map[string]string{"label1": "value1",
Expand All @@ -166,6 +166,8 @@ func TestDockerfileBuildLabels(t *testing.T) {
AsDockerfile: filepath.Join(tempdir, "Dockerfile"),
}
expected := []string{
"\"io.openshift.s2i.scripts-url\"=\"image:///scripts\"",
"\"io.openshift.s2i.destination\"=\"/destination\"",
"\"io.openshift.s2i.build.commit.date\"",
"\"io.openshift.s2i.build.commit.id\"",
"\"io.openshift.s2i.build.commit.ref\"",
Expand Down

0 comments on commit c8e4429

Please sign in to comment.