Skip to content

Commit

Permalink
Support args while building from dockerfile (kyma-project#2273)
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel authored Dec 5, 2024
1 parent 5584083 commit ba30d79
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/cmd/alpha/app/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type appPushConfig struct {
image string
dockerfilePath string
dockerfileSrcContext string
dockerfileArgs types.Map
packAppPath string
containerPort types.NullableInt64
istioInject types.NullableBool
Expand Down Expand Up @@ -58,6 +59,7 @@ func NewAppPushCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
// dockerfile flags
cmd.Flags().StringVar(&config.dockerfilePath, "dockerfile", "", "Path to the dockerfile")
cmd.Flags().StringVar(&config.dockerfileSrcContext, "dockerfile-context", "", "Context path for building dockerfile (defaults to current working directory)")
cmd.Flags().Var(&config.dockerfileArgs, "dockerfile-build-arg", "Variables used while building application from dockerfile as args")

// pack flags
cmd.Flags().StringVar(&config.packAppPath, "code-path", "", "Path to the application source code directory")
Expand All @@ -71,6 +73,8 @@ func NewAppPushCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
_ = cmd.MarkFlagRequired("name")
cmd.MarkFlagsMutuallyExclusive("image", "dockerfile", "code-path")
cmd.MarkFlagsMutuallyExclusive("image", "dockerfile-context", "code-path")
cmd.MarkFlagsMutuallyExclusive("dockerfile-build-arg", "image")
cmd.MarkFlagsMutuallyExclusive("dockerfile-build-arg", "code-path")
cmd.MarkFlagsOneRequired("image", "dockerfile", "code-path")

return cmd
Expand Down Expand Up @@ -216,6 +220,7 @@ func buildImage(cfg *appPushConfig) (string, error) {
ImageName: imageName,
BuildContext: cfg.dockerfileSrcContext,
DockerfilePath: cfg.dockerfilePath,
Args: cfg.dockerfileArgs.GetNullableMap(),
})
}

Expand Down
50 changes: 50 additions & 0 deletions internal/cmdcommon/types/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package types

import (
"fmt"
"strings"
)

type Map struct {
Values map[string]string
}

func (em *Map) String() string {
values := make([]string, len(em.Values))

index := 0
for key, value := range em.Values {
values[index] = fmt.Sprintf("%s=%s", key, value)
index++
}

return strings.Join(values, ",")
}

func (em *Map) Set(value string) error {
elems := strings.Split(value, "=")
if len(elems) != 2 {
return fmt.Errorf("failed to parse value '%s', should be in format KEY=VALUE", value)
}

if em.Values == nil {
em.Values = map[string]string{}
}

em.Values[elems[0]] = elems[1]
return nil
}

func (em *Map) Type() string {
return "stringArray"
}

func (em *Map) GetNullableMap() map[string]*string {
nullableMap := map[string]*string{}
for key, value := range em.Values {
v := value
nullableMap[key] = &v
}

return nullableMap
}
40 changes: 40 additions & 0 deletions internal/cmdcommon/types/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/utils/ptr"
)

func TestEnvMap(t *testing.T) {
t.Run("set values", func(t *testing.T) {
envMap := Map{}
require.NoError(t, envMap.Set("TEST1=1"))
require.NoError(t, envMap.Set("TEST2=2"))

expectedMap := map[string]string{
"TEST1": "1",
"TEST2": "2",
}
expectedNullableMap := map[string]*string{
"TEST1": ptr.To("1"),
"TEST2": ptr.To("2"),
}
require.Equal(t, expectedMap, envMap.Values)
require.Equal(t, expectedNullableMap, envMap.GetNullableMap())
require.Equal(t, "TEST1=1,TEST2=2", envMap.String())
})

t.Run("get type", func(t *testing.T) {
envMap := Map{}
require.Equal(t, "stringArray", envMap.Type())
})

t.Run("set values validation error", func(t *testing.T) {
envMap := Map{}

err := envMap.Set("not valid value")
require.ErrorContains(t, err, "failed to parse value 'not valid value', should be in format KEY=VALUE")
})
}
2 changes: 2 additions & 0 deletions internal/dockerfile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BuildOptions struct {
ImageName string
BuildContext string
DockerfilePath string
Args map[string]*string
}

type DockerClient interface {
Expand Down Expand Up @@ -88,6 +89,7 @@ func (b *imageBuilder) do(ctx context.Context, opts *BuildOptions) error {
Dockerfile: dockerFile,
Tags: []string{opts.ImageName},
Platform: "linux/amd64",
BuildArgs: opts.Args,
},
)
if err != nil {
Expand Down

0 comments on commit ba30d79

Please sign in to comment.