diff --git a/README.md b/README.md index 45a95ff..f972cc0 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Drone kaniko plugin uses [kaniko](https://github.com/GoogleContainerTools/kaniko Plugin images are published with 1.6.0 as well as 1.9.1 kaniko version from 1.5.1 release tag. `plugins/kaniko:` uses 1.6.0 version while `plugins/kaniko:-kaniko1.9.1` uses 1.9.1 version. Similar convention is used for plugins/kaniko-ecr & plugins/kaniko-gcr images as well. Run the following script to install git-leaks support to this repo. + ``` chmod +x ./git-hooks/install.sh ./git-hooks/install.sh @@ -35,7 +36,7 @@ docker build \ --label org.label-schema.build-date=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \ --label org.label-schema.vcs-ref=$(git rev-parse --short HEAD) \ --file docker/docker/Dockerfile.linux.amd64 --tag plugins/kaniko . - + docker build \ --label org.label-schema.build-date=$(date -u +"%Y-%m-%dT%H:%M:%SZ") \ --label org.label-schema.vcs-ref=$(git rev-parse --short HEAD) \ @@ -53,6 +54,33 @@ docker build \ ``` ## Usage + +### Operation Modes + +Default Mode (Build and Push): + +When neither `no_push` nor `push_only` is provided. Plugin builds and pushes the Docker image to a container registry. + +Build-Only Mode (no-push): + +When `no_push` is true and `destination_tar_path` is defined. +Plugin performs only the image build operation and saves the resulting image tarball to the specified `destination_tar_path` +It does not push the image to any registry. + +Push-Only Mode (push-only): + +When `push_only` is true and `source_tar_path` is defined. +Plugin loads an existing image tarball from the specified `source_tar_path` +and pushes the loaded image to a Container Registry. +It skips the build process. + +### Mutually Exclusive Inputs + +If both `no_push` and `push_only` inputs are provided, the plugin will: + +Terminate the operation and +throw an error with the message: "Inputs no-push and push-only cannot be used together. Please define only one." + ### Manual Tagging ```console @@ -79,6 +107,7 @@ docker run --rm \ -w /drone \ plugins/kaniko:linux-amd64 ``` + would both be equivalent to ``` @@ -88,7 +117,7 @@ PLUGIN_TAGS=1,1.2,1.2.3,latest This allows for passing `$DRONE_TAG` directly as a tag for repos that use [semver](https://semver.org) tags. To avoid confusion between repo tags and image tags, `PLUGIN_EXPAND_TAG` also recognizes a semantic version -without the `v` prefix. As such, the following is also equivalent to the above: +without the `v` prefix. As such, the following is also equivalent to the above: ```console docker run --rm \ @@ -100,6 +129,7 @@ docker run --rm \ ``` ### Auto Tagging + The [auto tag feature](https://plugins.drone.io/drone-plugins/drone-docker) of docker plugin is also supported. When auto tagging is enabled, if any of the case is matched below, a docker build will be pushed with auto generated tags. Otherwise the docker build will be skipped. @@ -121,6 +151,7 @@ docker run --rm \ ``` Tags to push: + - 1.2.3 - 1.2 - 1 @@ -141,4 +172,5 @@ docker run --rm \ ``` Tags to push: + - latest diff --git a/cmd/kaniko-docker/main.go b/cmd/kaniko-docker/main.go index c4378bb..699b53f 100644 --- a/cmd/kaniko-docker/main.go +++ b/cmd/kaniko-docker/main.go @@ -190,7 +190,7 @@ func main() { cli.StringFlag{ Name: "tar-path", Usage: "Set this flag to save the image as a tarball at path", - EnvVar: "PLUGIN_TAR_PATH", + EnvVar: "PLUGIN_TAR_PATH, PLUGIN_DESTINATION_TAR_PATH", }, cli.StringFlag{ Name: "verbosity", @@ -363,6 +363,16 @@ func main() { Usage: "Number of retries for downloading base images.", EnvVar: "PLUGIN_IMAGE_DOWNLOAD_RETRY", }, + cli.StringFlag{ + Name: "source-tar-path", + Usage: "Set this flag for the source tarball during push operations.", + EnvVar: "PLUGIN_SOURCE_TAR_PATH", + }, + cli.BoolFlag{ + Name: "push-only", + Usage: "Specify if the operation is push-only", + EnvVar: "PLUGIN_PUSH_ONLY", + }, } if err := app.Run(os.Args); err != nil { @@ -419,6 +429,7 @@ func run(c *cli.Context) error { TarPath: c.String("tar-path"), Verbosity: c.String("verbosity"), Platform: c.String("platform"), + PushOnly: c.Bool("push-only"), SkipUnusedStages: c.Bool("skip-unused-stages"), CacheDir: c.String("cache-dir"), CacheCopyLayers: c.Bool("cache-copy-layers"), @@ -446,6 +457,7 @@ func run(c *cli.Context) error { SkipPushPermissionCheck: c.Bool("skip-push-permission-check"), SkipTLSVerifyPull: c.Bool("skip-tls-verify-pull"), SkipTLSVerifyRegistry: c.Bool("skip-tls-verify-registry"), + SourceTarPath: c.String("source-tar-path"), UseNewRun: c.Bool("use-new-run"), IgnorePath: c.String("ignore-path"), diff --git a/go.mod b/go.mod index cc5fc03..9ed0014 100644 --- a/go.mod +++ b/go.mod @@ -11,12 +11,13 @@ require ( github.com/aws/smithy-go v1.12.0 github.com/coreos/go-semver v0.3.0 github.com/google/go-cmp v0.5.9 + github.com/google/go-containerregistry v0.20.2 github.com/hashicorp/go-version v1.6.0 github.com/joho/godotenv v1.4.0 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.4 - github.com/urfave/cli v1.22.9 + github.com/urfave/cli v1.22.12 golang.org/x/mod v0.17.0 ) @@ -31,19 +32,30 @@ require ( github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.8 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.11.12 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.16.9 // indirect + github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/docker/cli v27.1.1+incompatible // indirect + github.com/docker/distribution v2.8.2+incompatible // indirect + github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/uuid v1.3.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/klauspost/compress v1.16.5 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/kylelemons/godebug v1.1.0 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.1.0-rc3 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect - golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect - golang.org/x/net v0.0.0-20220725212005-46097bf591d3 // indirect + github.com/vbatts/tar-split v0.11.3 // indirect + golang.org/x/crypto v0.14.0 // indirect + golang.org/x/net v0.17.0 // indirect + golang.org/x/sync v0.2.0 // indirect golang.org/x/sys v0.19.0 // indirect - golang.org/x/text v0.3.7 // indirect + golang.org/x/text v0.13.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 3623b20..7a55e5e 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,7 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 h1:jp0dGvZ7ZK0mgqnTSClMxa5 github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w= github.com/AzureAD/microsoft-authentication-library-for-go v0.5.3 h1:TsFCaaF5tR4XN8b4zLVl/J4qMb0nf80Q4CXcpXDNJDY= github.com/AzureAD/microsoft-authentication-library-for-go v0.5.3/go.mod h1:Vt9sXTKwMyGcOxSmLDMnGPgqsUg7m8pe215qMLrDXw4= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/aws/aws-sdk-go v1.44.52 h1:kHLbYJj59C7VrsLM4gm7pxsvaNIvhXCCIDYEFFoQ+VE= github.com/aws/aws-sdk-go v1.44.52/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= github.com/aws/aws-sdk-go-v2 v1.16.7 h1:zfBwXus3u14OszRxGcqCDS4MfMCv10e8SMJ2r8Xm0Ns= @@ -35,16 +35,25 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.16.9 h1:yOfILxyjmtr2ubRkRJldlHDFBhf5 github.com/aws/aws-sdk-go-v2/service/sts v1.16.9/go.mod h1:O1IvkYxr+39hRf960Us6j0x1P8pDqhTX+oXM5kQNl/Y= github.com/aws/smithy-go v1.12.0 h1:gXpeZel/jPoWQ7OEmLIgCUnhkFftqNfwWUwAHSlp1v0= github.com/aws/smithy-go v1.12.0/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= +github.com/containerd/stargz-snapshotter/estargz v0.14.3 h1:OqlDCK3ZVUO6C3B/5FSkDwbkEETK84kQgEeFwDC+62k= +github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNAHhJogcZtrNHdKrA99/FCCRjE3HD36o= github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c= github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko= +github.com/docker/cli v27.1.1+incompatible h1:goaZxOqs4QKxznZjjBWKONQci/MywhtRv2oNn0GkeZE= +github.com/docker/cli v27.1.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= +github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8= +github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker-credential-helpers v0.7.0 h1:xtCHsjxogADNZcdv1pKUHXryefjlVRqWqIhk/uXJp0A= +github.com/docker/docker-credential-helpers v0.7.0/go.mod h1:rETQfLdHNT3foU5kuNkFR1R1V12OJRRO5lzt2D1b5X0= github.com/golang-jwt/jwt v3.2.1+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= @@ -53,6 +62,8 @@ github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzw github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-containerregistry v0.20.2 h1:B1wPJ1SN/S7pB+ZAimcciVD+r+yV/l/DSArMxlbwseo= +github.com/google/go-containerregistry v0.20.2/go.mod h1:z38EKdKh4h7IP2gSfUUqEvalZBqs6AoLeWfUy34nQC8= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -64,52 +75,79 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= +github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.0-rc3 h1:fzg1mXZFj8YdPeNkRXMg+zb88BFV0Ys52cJydRwBkb8= +github.com/opencontainers/image-spec v1.1.0-rc3/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/urfave/cli v1.22.9 h1:cv3/KhXGBGjEXLC4bH0sLuJ9BewaAbpk5oyMOveu4pw= -github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= -golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c= -golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8= +github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8= +github.com/vbatts/tar-split v0.11.3 h1:hLFqsOLQ1SsppQNTMpkpPXClLDfC2A3Zgy9OUU+RVck= +github.com/vbatts/tar-split v0.11.3/go.mod h1:9QlHN18E+fEH7RdG+QAJJcuya3rqT7eXSTY7wGrAokY= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220725212005-46097bf591d3 h1:2yWTtPWWRcISTw3/o+s/Y4UOMnQL71DWyToOANFusCg= -golang.org/x/net v0.0.0-20220725212005-46097bf591d3/go.mod h1:AaygXjzTFtRAg2ttMY5RMuhpJ3cNnI0XpyFJD1iQRSM= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= +gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= diff --git a/kaniko.go b/kaniko.go index 80faa96..56fc4c9 100644 --- a/kaniko.go +++ b/kaniko.go @@ -8,41 +8,46 @@ import ( "path/filepath" "strings" + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/drone/drone-kaniko/pkg/artifact" "github.com/drone/drone-kaniko/pkg/output" "github.com/drone/drone-kaniko/pkg/tagger" + "github.com/google/go-containerregistry/pkg/crane" "golang.org/x/mod/semver" ) type ( // Build defines Docker build parameters. Build struct { - DroneCommitRef string // Drone git commit reference - DroneRepoBranch string // Drone repo branch - Dockerfile string // Docker build Dockerfile - Context string // Docker build context - Tags []string // Docker build tags - AutoTag bool // Set this to auto detect tags from git commits and semver-tagged labels - AutoTagSuffix string // Suffix to append to the auto detect tags - ExpandTag bool // Set this to expand the `Tags` into semver-tagged labels Args []string // Docker build args ArgsNew []string // docker build args with comma seperated values - IsMultipleBuildArgs bool // env variable for fallback for docker build args - Target string // Docker build target - Repo string // Docker build repository - Mirrors []string // Docker repository mirrors - Labels []string // Label map - SkipTlsVerify bool // Docker skip tls certificate verify for registry - SnapshotMode string // Kaniko snapshot mode - EnableCache bool // Whether to enable kaniko cache + AutoTag bool // Set this to auto detect tags from git commits and semver-tagged labels + AutoTagSuffix string // Suffix to append to the auto detect tags CacheRepo string // Remote repository that will be used to store cached layers CacheTTL int // Cache timeout in hours + Context string // Docker build context DigestFile string // Digest file location + Dockerfile string // Docker build Dockerfile + DroneCommitRef string // Drone git commit reference + DroneRepoBranch string // Drone repo branch + EnableCache bool // Whether to enable kaniko cache + ExpandTag bool // Set this to expand the `Tags` into semver-tagged labels + IsMultipleBuildArgs bool // env variable for fallback for docker build args + Labels []string // Label map + Mirrors []string // Docker repository mirrors NoPush bool // Set this flag if you only want to build the image, without pushing to a registry - Verbosity string // Log level Platform string // Allows to build with another default platform than the host, similarly to docker build --platform + PushOnly bool // Specify if the operation is push-only. + Repo string // Docker build repository + SkipTlsVerify bool // Docker skip tls certificate verify for registry SkipUnusedStages bool // Build only used stages + SnapshotMode string // Kaniko snapshot mode + SourceTarPath string // Path to the local tarball to be pushed + Tags []string // Docker build tags TarPath string // Set this flag to save the image as a tarball at path + Target string // Docker build target + Verbosity string // Log level Cache bool // Enable or disable caching during the build process. CacheDir string // Directory to store cached layers. @@ -100,6 +105,10 @@ type ( Build Build // Docker build configuration Artifact Artifact // Artifact file content Output Output // Output file content + + // parameters for UTs to mock crane functionality + LoadImageFromTarball func(string) (v1.Image, error) + PushImageToRegistry func(v1.Image, string) error } ) @@ -170,10 +179,53 @@ func (b Build) AutoTags() (tags []string, err error) { // Exec executes the plugin step func (p Plugin) Exec() error { + + if p.Build.NoPush && p.Build.PushOnly { + return fmt.Errorf("inputs no-push and push-only cannot be used together. please define only one") + } + if !p.Build.NoPush && p.Build.Repo == "" { return fmt.Errorf("repository name to publish image must be specified") } + if p.Build.PushOnly { + // When push-only is set, source_tar_path MUST be provided + if p.Build.SourceTarPath == "" { + return fmt.Errorf("source_tar_path is required when push_only is set. please provide a valid tarball path") + } + + if _, err := os.Stat(p.Build.SourceTarPath); os.IsNotExist(err) { + return fmt.Errorf("image tarball does not exist at path: %s", p.Build.SourceTarPath) + } + + if p.Build.Repo == "" { + return fmt.Errorf("missing required destination repository for push-only operation") + } + + // Load the image from the tarball + img, err := crane.Load(p.Build.SourceTarPath) + if err != nil { + return fmt.Errorf("failed to load image from tarball: %v", err) + } + + // If no tags are specified, use 'latest' + tags := p.Build.Tags + + for _, tag := range tags { + dest := fmt.Sprintf("%s:%s", p.Build.Repo, tag) + + // Push the image to the destination + err := crane.Push(img, dest) + if err != nil { + return fmt.Errorf("failed to push image from tarball [%s] to destination [%s]: %v", p.Build.SourceTarPath, dest, err) + } + + fmt.Printf("Successfully pushed image - '%s'\n to %s\n", dest, p.Build.Repo) + } + + return nil + } + if _, err := os.Stat(p.Build.Dockerfile); os.IsNotExist(err) { return fmt.Errorf("dockerfile does not exist at path: %s", p.Build.Dockerfile) } diff --git a/kaniko_test.go b/kaniko_test.go index 73eb985..0e97196 100644 --- a/kaniko_test.go +++ b/kaniko_test.go @@ -1,6 +1,8 @@ package kaniko import ( + "archive/tar" + "fmt" "os" "path/filepath" "testing" @@ -280,3 +282,173 @@ func TestTarPathValidation(t *testing.T) { }) } } + +func TestSourceTarballPush(t *testing.T) { + tests := []struct { + name string + sourceTarPath string + repo string + autoTag bool + tags []string + commitRef string + repoBranch string + expectedError bool + expectedTags []string + mockLoadErr error + mockPushErr error + }{ + { + name: "empty_repo_fails", + sourceTarPath: "/path/to/image.tar", + repo: "", + expectedError: true, + }, + { + name: "nonexistent_tarball_fails", + sourceTarPath: "/path/that/does/not/exist/image.tar", + repo: "test-repo", + expectedError: true, + }, + { + name: "load_image_fails", + sourceTarPath: createTestTarball(t), + repo: "test-repo", + expectedError: true, + mockLoadErr: fmt.Errorf("load failed"), + }, + { + name: "push_image_fails", + sourceTarPath: createTestTarball(t), + repo: "test-repo", + expectedError: true, + expectedTags: []string{"latest"}, + mockPushErr: fmt.Errorf("push failed"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockPlugin := Plugin{ + Build: Build{ + SourceTarPath: tt.sourceTarPath, + Repo: tt.repo, + Tags: tt.tags, + AutoTag: tt.autoTag, + DroneCommitRef: tt.commitRef, + DroneRepoBranch: tt.repoBranch, + }, + LoadImageFromTarball: MockCraneLoad(tt.sourceTarPath, tt.mockLoadErr), + PushImageToRegistry: MockCranePush(tt.mockPushErr), + } + + err := mockPlugin.Exec() + + if tt.expectedError { + if err == nil { + t.Errorf("Expected an error, but got none") + } + } else { + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + } + }) + } +} + +// Helper function to create a test tarball +func createTestTarball(t *testing.T) string { + // Create a temporary directory for the tarball contents + tmpDir, err := os.MkdirTemp("", "test-tarball-*") + if err != nil { + t.Fatalf("Failed to create temp directory: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Create a minimal `manifest.json` file with a valid hash + manifestPath := filepath.Join(tmpDir, "manifest.json") + manifestContent := `[{ + "Config": "config.json", + "RepoTags": ["test-repo:latest"], + "Layers": ["layer.tar"] + }]` + err = os.WriteFile(manifestPath, []byte(manifestContent), 0644) + if err != nil { + t.Fatalf("Failed to create manifest.json: %v", err) + } + + // Create a valid `config.json` file with a dummy hash + configPath := filepath.Join(tmpDir, "config.json") + configContent := `{ + "architecture": "amd64", + "os": "linux", + "rootfs": { + "type": "layers", + "diff_ids": ["sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"] + } + }` + err = os.WriteFile(configPath, []byte(configContent), 0644) + if err != nil { + t.Fatalf("Failed to create config.json: %v", err) + } + + // Create a dummy `layer.tar` file + layerPath := filepath.Join(tmpDir, "layer.tar") + layerFile, err := os.Create(layerPath) + if err != nil { + t.Fatalf("Failed to create layer.tar: %v", err) + } + defer layerFile.Close() + _, err = layerFile.Write([]byte("dummy layer content")) + if err != nil { + t.Fatalf("Failed to write to layer.tar: %v", err) + } + + // Create a tarball from the temp directory + tarballPath := filepath.Join(os.TempDir(), "test-image.tar") + tarballFile, err := os.Create(tarballPath) + if err != nil { + t.Fatalf("Failed to create tarball: %v", err) + } + defer tarballFile.Close() + + tw := tar.NewWriter(tarballFile) + defer tw.Close() + + err = filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + relPath, _ := filepath.Rel(tmpDir, path) + if relPath == "." { + return nil + } + + header, err := tar.FileInfoHeader(info, path) + if err != nil { + return err + } + header.Name = relPath + + if err := tw.WriteHeader(header); err != nil { + return err + } + + if !info.IsDir() { + fileContent, err := os.ReadFile(path) + if err != nil { + return err + } + _, err = tw.Write(fileContent) + if err != nil { + return err + } + } + return nil + }) + if err != nil { + t.Fatalf("Failed to write tarball: %v", err) + } + + return tarballPath +} diff --git a/mock_crane.go b/mock_crane.go new file mode 100644 index 0000000..32ac6ed --- /dev/null +++ b/mock_crane.go @@ -0,0 +1,71 @@ +package kaniko + +import ( + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/types" +) + +func MockCraneLoad(path string, loadErr error) func(string) (v1.Image, error) { + return func(inputPath string) (v1.Image, error) { + if loadErr != nil { + return nil, loadErr + } + return &mockImage{}, nil + } +} + +func MockCranePush(pushErr error) func(v1.Image, string) error { + return func(img v1.Image, dest string) error { + if pushErr != nil { + return pushErr + } + return nil + } +} + +// mockImage is a mock implementation of v1.Image interface +type mockImage struct{} + +func (m *mockImage) Size() (int64, error) { + return 0, nil +} + +func (m *mockImage) RawConfigFile() ([]byte, error) { + return nil, nil +} + +func (m *mockImage) Digest() (v1.Hash, error) { + return v1.Hash{}, nil +} + +func (m *mockImage) Manifest() (*v1.Manifest, error) { + return nil, nil +} + +func (m *mockImage) RawManifest() ([]byte, error) { + return nil, nil +} + +func (m *mockImage) LayerByDigest(hash v1.Hash) (v1.Layer, error) { + return nil, nil +} + +func (m *mockImage) LayerByDiffID(hash v1.Hash) (v1.Layer, error) { + return nil, nil +} + +func (m *mockImage) Layers() ([]v1.Layer, error) { + return nil, nil +} + +func (m *mockImage) MediaType() (types.MediaType, error) { + return "", nil +} + +func (m *mockImage) ConfigFile() (*v1.ConfigFile, error) { + return nil, nil +} + +func (m *mockImage) ConfigName() (v1.Hash, error) { + return v1.Hash{}, nil +}