1
0
mirror of https://github.com/ko-build/ko.git synced 2025-11-26 22:40:38 +02:00

feat: add image user option

Signed-off-by: Maxime Brunet <max@brnt.mx>
This commit is contained in:
Maxime Brunet
2024-03-23 12:58:03 -07:00
committed by Jason Hall
parent 6541f6e217
commit ac22328979
10 changed files with 41 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -103,6 +103,7 @@ type gobuild struct {
dir string dir string
labels map[string]string labels map[string]string
annotations map[string]string annotations map[string]string
user string
debug bool debug bool
semaphore *semaphore.Weighted semaphore *semaphore.Weighted
@@ -129,6 +130,7 @@ type gobuildOpener struct {
platforms []string platforms []string
labels map[string]string labels map[string]string
annotations map[string]string annotations map[string]string
user string
dir string dir string
jobs int jobs int
debug bool debug bool
@@ -151,6 +153,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
return &gobuild{ return &gobuild{
ctx: gbo.ctx, ctx: gbo.ctx,
getBase: gbo.getBase, getBase: gbo.getBase,
user: gbo.user,
creationTime: gbo.creationTime, creationTime: gbo.creationTime,
kodataCreationTime: gbo.kodataCreationTime, kodataCreationTime: gbo.kodataCreationTime,
build: gbo.build, build: gbo.build,
@@ -1172,6 +1175,10 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
cfg.Config.Labels[k] = v cfg.Config.Labels[k] = v
} }
if g.user != "" {
cfg.Config.User = g.user
}
empty := v1.Time{} empty := v1.Time{}
if g.creationTime != empty { if g.creationTime != empty {
cfg.Created = g.creationTime cfg.Created = g.creationTime

View File

@@ -846,6 +846,7 @@ func TestGoBuild(t *testing.T) {
WithLabel("hello", "world"), WithLabel("hello", "world"),
WithAnnotation("fizz", "buzz"), WithAnnotation("fizz", "buzz"),
WithAnnotation("goodbye", "world"), WithAnnotation("goodbye", "world"),
WithUser("1234:1234"),
WithPlatforms("all"), WithPlatforms("all"),
) )
if err != nil { if err != nil {
@@ -921,6 +922,19 @@ func TestGoBuild(t *testing.T) {
t.Fatalf("Annotations diff (-got,+want): %s", d) t.Fatalf("Annotations diff (-got,+want): %s", d)
} }
}) })
t.Run("check user", func(t *testing.T) {
cfg, err := img.ConfigFile()
if err != nil {
t.Fatalf("ConfigFile() = %v", err)
}
want := "1234:1234"
got := cfg.Config.User
if got != want {
t.Fatalf("User: %s != %s", want, got)
}
})
} }
func TestGoBuild_Defaults(t *testing.T) { func TestGoBuild_Defaults(t *testing.T) {

View File

@@ -153,6 +153,14 @@ func WithAnnotation(k, v string) Option {
} }
} }
// WithUser is a functional option for overriding the user in the image config.
func WithUser(user string) Option {
return func(gbo *gobuildOpener) error {
gbo.user = user
return nil
}
}
// withBuilder is a functional option for overriding the way go binaries // withBuilder is a functional option for overriding the way go binaries
// are built. // are built.
func withBuilder(b builder) Option { func withBuilder(b builder) Option {

View File

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

View File

@@ -127,6 +127,10 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
opts = append(opts, build.WithAnnotation(k, v)) opts = append(opts, build.WithAnnotation(k, v))
} }
if bo.User != "" {
opts = append(opts, build.WithUser(bo.User))
}
if bo.BuildConfigs != nil { if bo.BuildConfigs != nil {
opts = append(opts, build.WithConfig(bo.BuildConfigs)) opts = append(opts, build.WithConfig(bo.BuildConfigs))
} }