Skip to content

Commit ac22328

Browse files
maxbrunetimjasonh
authored andcommitted
feat: add image user option
Signed-off-by: Maxime Brunet <[email protected]>
1 parent 6541f6e commit ac22328

File tree

10 files changed

+41
-0
lines changed

10 files changed

+41
-0
lines changed

docs/reference/ko_apply.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ko apply -f FILENAME [flags]
5454
--image-annotation strings Which annotations (key=value[,key=value]) to add to the OCI manifest.
5555
--image-label strings Which labels (key=value[,key=value]) to add to the image.
5656
--image-refs string Path to file where a list of the published image references will be written.
57+
--image-user string The default user the image should be run as.
5758
--insecure-registry Whether to skip TLS verification on the registry
5859
-j, --jobs int The maximum number of concurrent builds (default GOMAXPROCS)
5960
-L, --local Load into images to local docker daemon.

docs/reference/ko_build.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ ko build IMPORTPATH... [flags]
5050
--image-annotation strings Which annotations (key=value[,key=value]) to add to the OCI manifest.
5151
--image-label strings Which labels (key=value[,key=value]) to add to the image.
5252
--image-refs string Path to file where a list of the published image references will be written.
53+
--image-user string The default user the image should be run as.
5354
--insecure-registry Whether to skip TLS verification on the registry
5455
-j, --jobs int The maximum number of concurrent builds (default GOMAXPROCS)
5556
-L, --local Load into images to local docker daemon.

docs/reference/ko_create.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ko create -f FILENAME [flags]
5454
--image-annotation strings Which annotations (key=value[,key=value]) to add to the OCI manifest.
5555
--image-label strings Which labels (key=value[,key=value]) to add to the image.
5656
--image-refs string Path to file where a list of the published image references will be written.
57+
--image-user string The default user the image should be run as.
5758
--insecure-registry Whether to skip TLS verification on the registry
5859
-j, --jobs int The maximum number of concurrent builds (default GOMAXPROCS)
5960
-L, --local Load into images to local docker daemon.

docs/reference/ko_resolve.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ko resolve -f FILENAME [flags]
4747
--image-annotation strings Which annotations (key=value[,key=value]) to add to the OCI manifest.
4848
--image-label strings Which labels (key=value[,key=value]) to add to the image.
4949
--image-refs string Path to file where a list of the published image references will be written.
50+
--image-user string The default user the image should be run as.
5051
--insecure-registry Whether to skip TLS verification on the registry
5152
-j, --jobs int The maximum number of concurrent builds (default GOMAXPROCS)
5253
-L, --local Load into images to local docker daemon.

docs/reference/ko_run.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ ko run IMPORTPATH [flags]
3838
--image-annotation strings Which annotations (key=value[,key=value]) to add to the OCI manifest.
3939
--image-label strings Which labels (key=value[,key=value]) to add to the image.
4040
--image-refs string Path to file where a list of the published image references will be written.
41+
--image-user string The default user the image should be run as.
4142
--insecure-registry Whether to skip TLS verification on the registry
4243
-j, --jobs int The maximum number of concurrent builds (default GOMAXPROCS)
4344
-L, --local Load into images to local docker daemon.

pkg/build/gobuild.go

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type gobuild struct {
103103
dir string
104104
labels map[string]string
105105
annotations map[string]string
106+
user string
106107
debug bool
107108
semaphore *semaphore.Weighted
108109

@@ -129,6 +130,7 @@ type gobuildOpener struct {
129130
platforms []string
130131
labels map[string]string
131132
annotations map[string]string
133+
user string
132134
dir string
133135
jobs int
134136
debug bool
@@ -151,6 +153,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
151153
return &gobuild{
152154
ctx: gbo.ctx,
153155
getBase: gbo.getBase,
156+
user: gbo.user,
154157
creationTime: gbo.creationTime,
155158
kodataCreationTime: gbo.kodataCreationTime,
156159
build: gbo.build,
@@ -1172,6 +1175,10 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
11721175
cfg.Config.Labels[k] = v
11731176
}
11741177

1178+
if g.user != "" {
1179+
cfg.Config.User = g.user
1180+
}
1181+
11751182
empty := v1.Time{}
11761183
if g.creationTime != empty {
11771184
cfg.Created = g.creationTime

pkg/build/gobuild_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ func TestGoBuild(t *testing.T) {
846846
WithLabel("hello", "world"),
847847
WithAnnotation("fizz", "buzz"),
848848
WithAnnotation("goodbye", "world"),
849+
WithUser("1234:1234"),
849850
WithPlatforms("all"),
850851
)
851852
if err != nil {
@@ -921,6 +922,19 @@ func TestGoBuild(t *testing.T) {
921922
t.Fatalf("Annotations diff (-got,+want): %s", d)
922923
}
923924
})
925+
926+
t.Run("check user", func(t *testing.T) {
927+
cfg, err := img.ConfigFile()
928+
if err != nil {
929+
t.Fatalf("ConfigFile() = %v", err)
930+
}
931+
932+
want := "1234:1234"
933+
got := cfg.Config.User
934+
if got != want {
935+
t.Fatalf("User: %s != %s", want, got)
936+
}
937+
})
924938
}
925939

926940
func TestGoBuild_Defaults(t *testing.T) {

pkg/build/options.go

+8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ func WithAnnotation(k, v string) Option {
153153
}
154154
}
155155

156+
// WithUser is a functional option for overriding the user in the image config.
157+
func WithUser(user string) Option {
158+
return func(gbo *gobuildOpener) error {
159+
gbo.user = user
160+
return nil
161+
}
162+
}
163+
156164
// withBuilder is a functional option for overriding the way go binaries
157165
// are built.
158166
func withBuilder(b builder) Option {

pkg/commands/options/build.go

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type BuildOptions struct {
6666
Platforms []string
6767
Labels []string
6868
Annotations []string
69+
User string
6970
Debug bool
7071
// UserAgent enables overriding the default value of the `User-Agent` HTTP
7172
// request header used when retrieving the base image.
@@ -98,6 +99,8 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
9899
"Which labels (key=value[,key=value]) to add to the image.")
99100
cmd.Flags().StringSliceVar(&bo.Annotations, "image-annotation", []string{},
100101
"Which annotations (key=value[,key=value]) to add to the OCI manifest.")
102+
cmd.Flags().StringVar(&bo.User, "image-user", "",
103+
"The default user the image should be run as.")
101104
cmd.Flags().BoolVar(&bo.Debug, "debug", bo.Debug,
102105
"Include Delve debugger into image and wrap around ko-app. This debugger will listen to port 40000.")
103106
bo.Trimpath = true

pkg/commands/resolver.go

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
127127
opts = append(opts, build.WithAnnotation(k, v))
128128
}
129129

130+
if bo.User != "" {
131+
opts = append(opts, build.WithUser(bo.User))
132+
}
133+
130134
if bo.BuildConfigs != nil {
131135
opts = append(opts, build.WithConfig(bo.BuildConfigs))
132136
}

0 commit comments

Comments
 (0)