2020-11-16 19:50:20 +01:00
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
kaniko "github.com/drone/drone-kaniko"
2021-10-18 13:36:53 +02:00
"github.com/drone/drone-kaniko/pkg/artifact"
2020-11-16 19:50:20 +01:00
)
const (
// GCR JSON key file path
2020-11-26 19:17:00 +01:00
gcrKeyPath string = "/kaniko/config.json"
2020-11-16 20:03:18 +01:00
gcrEnvVariable string = "GOOGLE_APPLICATION_CREDENTIALS"
2021-04-29 12:05:57 +02:00
defaultDigestFile string = "/kaniko/digest-file"
2020-11-16 19:50:20 +01:00
)
var (
version = "unknown"
)
func main ( ) {
// Load env-file if it exists first
if env := os . Getenv ( "PLUGIN_ENV_FILE" ) ; env != "" {
2021-09-01 08:15:50 +02:00
if err := godotenv . Load ( env ) ; err != nil {
logrus . Fatal ( err )
}
2020-11-16 19:50:20 +01:00
}
app := cli . NewApp ( )
app . Name = "kaniko gcr plugin"
app . Usage = "kaniko gcr plugin"
app . Action = run
app . Version = version
app . Flags = [ ] cli . Flag {
cli . StringFlag {
Name : "dockerfile" ,
Usage : "build dockerfile" ,
Value : "Dockerfile" ,
EnvVar : "PLUGIN_DOCKERFILE" ,
} ,
cli . StringFlag {
Name : "context" ,
Usage : "build context" ,
Value : "." ,
EnvVar : "PLUGIN_CONTEXT" ,
} ,
cli . StringSliceFlag {
Name : "tags" ,
Usage : "build tags" ,
Value : & cli . StringSlice { "latest" } ,
EnvVar : "PLUGIN_TAGS" ,
FilePath : ".tags" ,
} ,
2021-10-17 20:35:18 +02:00
cli . BoolFlag {
Name : "auto_tag" ,
Usage : "enable for semver tagging" ,
EnvVar : "PLUGIN_AUTO_TAG" ,
} ,
2020-11-16 19:50:20 +01:00
cli . StringSliceFlag {
Name : "args" ,
Usage : "build args" ,
EnvVar : "PLUGIN_BUILD_ARGS" ,
} ,
cli . StringFlag {
Name : "target" ,
Usage : "build target" ,
EnvVar : "PLUGIN_TARGET" ,
} ,
cli . StringFlag {
Name : "repo" ,
Usage : "gcr repository" ,
EnvVar : "PLUGIN_REPO" ,
} ,
cli . StringSliceFlag {
Name : "custom-labels" ,
Usage : "additional k=v labels" ,
EnvVar : "PLUGIN_CUSTOM_LABELS" ,
} ,
cli . StringFlag {
Name : "registry" ,
Usage : "gcr registry" ,
Value : "gcr.io" ,
EnvVar : "PLUGIN_REGISTRY" ,
} ,
cli . StringFlag {
Name : "json-key" ,
Usage : "docker username" ,
EnvVar : "PLUGIN_JSON_KEY" ,
} ,
2021-03-02 16:45:44 +01:00
cli . StringFlag {
Name : "snapshot-mode" ,
Usage : "Specify one of full, redo or time as snapshot mode" ,
EnvVar : "PLUGIN_SNAPSHOT_MODE" ,
} ,
2021-04-07 20:35:53 +02:00
cli . BoolFlag {
Name : "enable-cache" ,
Usage : "Set this flag to opt into caching with kaniko" ,
EnvVar : "PLUGIN_ENABLE_CACHE" ,
} ,
cli . StringFlag {
Name : "cache-repo" ,
2021-04-21 09:25:13 +02:00
Usage : "Remote repository that will be used to store cached layers. Cache repo should be present in specified registry. enable-cache needs to be set to use this flag" ,
2021-04-07 20:35:53 +02:00
EnvVar : "PLUGIN_CACHE_REPO" ,
} ,
cli . IntFlag {
Name : "cache-ttl" ,
Usage : "Cache timeout in hours. Defaults to two weeks." ,
EnvVar : "PLUGIN_CACHE_TTL" ,
} ,
2021-04-29 12:05:57 +02:00
cli . StringFlag {
Name : "artifact-file" ,
Usage : "Artifact file location that will be generated by the plugin. This file will include information of docker images that are uploaded by the plugin." ,
EnvVar : "PLUGIN_ARTIFACT_FILE" ,
} ,
2021-04-29 17:23:43 +02:00
cli . BoolFlag {
Name : "no-push" ,
Usage : "Set this flag if you only want to build the image, without pushing to a registry" ,
EnvVar : "PLUGIN_NO_PUSH" ,
} ,
2021-08-20 13:08:35 +02:00
cli . StringFlag {
Name : "verbosity" ,
Usage : "Set this flag as --verbosity=<panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info." ,
EnvVar : "PLUGIN_VERBOSITY" ,
} ,
2021-12-01 12:40:28 +01:00
cli . StringFlag {
Name : "platform" ,
Usage : "Allows to build with another default platform than the host, similarly to docker build --platform" ,
EnvVar : "PLUGIN_PLATFORM" ,
} ,
2020-11-16 19:50:20 +01:00
}
if err := app . Run ( os . Args ) ; err != nil {
logrus . Fatal ( err )
}
}
func run ( c * cli . Context ) error {
2021-09-01 08:15:50 +02:00
noPush := c . Bool ( "no-push" )
jsonKey := c . String ( "json-key" )
2020-11-16 19:50:20 +01:00
2021-10-18 13:36:53 +02:00
// JSON key may not be set in the following cases:
// 1. Image does not need to be pushed to GCR.
// 2. Workload identity is set on GKE in which pod will inherit the credentials via service account.
if jsonKey != "" {
2021-09-01 08:15:50 +02:00
if err := setupGCRAuth ( jsonKey ) ; err != nil {
return err
}
2020-11-16 19:50:20 +01:00
}
plugin := kaniko . Plugin {
Build : kaniko . Build {
2021-03-02 16:45:44 +01:00
Dockerfile : c . String ( "dockerfile" ) ,
Context : c . String ( "context" ) ,
Tags : c . StringSlice ( "tags" ) ,
2021-10-17 20:35:18 +02:00
AutoTag : c . Bool ( "auto_tag" ) ,
2021-03-02 16:45:44 +01:00
Args : c . StringSlice ( "args" ) ,
Target : c . String ( "target" ) ,
Repo : fmt . Sprintf ( "%s/%s" , c . String ( "registry" ) , c . String ( "repo" ) ) ,
Labels : c . StringSlice ( "custom-labels" ) ,
SnapshotMode : c . String ( "snapshot-mode" ) ,
2021-04-07 20:35:53 +02:00
EnableCache : c . Bool ( "enable-cache" ) ,
2021-04-21 09:25:13 +02:00
CacheRepo : fmt . Sprintf ( "%s/%s" , c . String ( "registry" ) , c . String ( "cache-repo" ) ) ,
2021-04-07 20:35:53 +02:00
CacheTTL : c . Int ( "cache-ttl" ) ,
2021-04-29 12:05:57 +02:00
DigestFile : defaultDigestFile ,
2021-09-01 08:15:50 +02:00
NoPush : noPush ,
2021-08-20 13:08:35 +02:00
Verbosity : c . String ( "verbosity" ) ,
2021-12-01 12:40:28 +01:00
Platform : c . String ( "platform" ) ,
2021-04-29 12:05:57 +02:00
} ,
Artifact : kaniko . Artifact {
Tags : c . StringSlice ( "tags" ) ,
Repo : c . String ( "repo" ) ,
Registry : c . String ( "registry" ) ,
ArtifactFile : c . String ( "artifact-file" ) ,
RegistryType : artifact . GCR ,
2020-11-16 19:50:20 +01:00
} ,
}
return plugin . Exec ( )
}
func setupGCRAuth ( jsonKey string ) error {
err := ioutil . WriteFile ( gcrKeyPath , [ ] byte ( jsonKey ) , 0644 )
if err != nil {
return errors . Wrap ( err , "failed to write GCR JSON key" )
}
2020-11-16 20:03:18 +01:00
err = os . Setenv ( gcrEnvVariable , gcrKeyPath )
if err != nil {
return errors . Wrap ( err , fmt . Sprintf ( "failed to set %s environment variable" , gcrEnvVariable ) )
}
2020-11-16 19:50:20 +01:00
return nil
}