Compare commits

..

2 Commits

Author SHA1 Message Date
Shubham Agrawal 3a8625ec32 Use relative name for cache repo 2021-04-21 12:47:22 +05:30
Shubham Agrawal b708f2c84c Allow remote caching of docker layers in kaniko build 2021-04-08 00:03:57 +05:30
13 changed files with 15 additions and 272 deletions
-76
View File
@@ -1,76 +0,0 @@
package artifact
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
const (
dockerArtifactV1 string = "docker/v1"
)
type RegistryTypeEnum string
const (
Docker RegistryTypeEnum = "Docker"
ECR RegistryTypeEnum = "ECR"
GCR RegistryTypeEnum = "GCR"
)
type (
Image struct {
Image string `json:"image"`
Digest string `json:"digest"`
}
Data struct {
RegistryType RegistryTypeEnum `json:"registryType"`
RegistryUrl string `json:"registryUrl"`
Images []Image `json:"images"`
}
DockerArtifact struct {
Kind string `json:"kind"`
Data Data `json:"data"`
}
)
func WritePluginArtifactFile(registryType RegistryTypeEnum, artifactFilePath, registryUrl, imageName, digest string, tags []string) error {
var images []Image
for _, tag := range tags {
images = append(images, Image{
Image: fmt.Sprintf("%s:%s", imageName, tag),
Digest: digest,
})
}
data := Data{
RegistryType: registryType,
RegistryUrl: registryUrl,
Images: images,
}
dockerArtifact := DockerArtifact{
Kind: dockerArtifactV1,
Data: data,
}
b, err := json.MarshalIndent(dockerArtifact, "", "\t")
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to marshal output %+v", dockerArtifact))
}
dir := filepath.Dir(artifactFilePath)
err = os.MkdirAll(dir, 0644)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory for artifact file", dir))
}
err = ioutil.WriteFile(artifactFilePath, b, 0644)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to write artifact to artifact file %s", artifactFilePath))
}
return nil
}
-17
View File
@@ -1,17 +0,0 @@
{
"kind": "docker/v1",
"data": {
"registryType": "Docker",
"registryUrl": "https://index.docker.io/",
"images": [
{
"image": "image:a1",
"digest": "sha256:22332233"
},
{
"image": "image:latest",
"digest": "sha256:22332233"
}
]
}
}
-38
View File
@@ -1,38 +0,0 @@
package artifact
import (
"io/ioutil"
"testing"
)
func TestWritePluginArtifactFile(t *testing.T) {
testFile := t.TempDir() + "got.json"
err := WritePluginArtifactFile(Docker, testFile, "https://index.docker.io/", "image", "sha256:22332233", []string{"a1", "latest"})
if err != nil {
t.Error(err)
t.FailNow()
}
gotBytes, err := ioutil.ReadFile(testFile)
if err != nil {
t.Error(err)
t.FailNow()
}
wantBytes, err := ioutil.ReadFile("./artifact.json")
if err != nil {
t.Error(err)
t.FailNow()
}
got := string(gotBytes)
want := string(wantBytes)
if got != want {
t.Logf("got:%s", got)
t.Logf("want:%s", want)
t.FailNow()
}
}
-28
View File
@@ -12,7 +12,6 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
kaniko "github.com/drone/drone-kaniko" kaniko "github.com/drone/drone-kaniko"
"github.com/drone/drone-kaniko/cmd/artifact"
) )
const ( const (
@@ -22,8 +21,6 @@ const (
v1Registry string = "https://index.docker.io/v1/" // Default registry v1Registry string = "https://index.docker.io/v1/" // Default registry
v2Registry string = "https://index.docker.io/v2/" // v2 registry is not supported v2Registry string = "https://index.docker.io/v2/" // v2 registry is not supported
defaultDigestFile string = "/kaniko/digest-file"
) )
var ( var (
@@ -122,21 +119,6 @@ func main() {
Usage: "Cache timeout in hours. Defaults to two weeks.", Usage: "Cache timeout in hours. Defaults to two weeks.",
EnvVar: "PLUGIN_CACHE_TTL", EnvVar: "PLUGIN_CACHE_TTL",
}, },
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",
},
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",
},
cli.StringFlag{
Name: "verbosity",
Usage: "Set this flag with value as oneof <panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info.",
EnvVar: "PLUGIN_VERBOSITY",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@@ -164,16 +146,6 @@ func run(c *cli.Context) error {
EnableCache: c.Bool("enable-cache"), EnableCache: c.Bool("enable-cache"),
CacheRepo: c.String("cache-repo"), CacheRepo: c.String("cache-repo"),
CacheTTL: c.Int("cache-ttl"), CacheTTL: c.Int("cache-ttl"),
DigestFile: defaultDigestFile,
NoPush: c.Bool("no-push"),
Verbosity: c.String("verbosity"),
},
Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"),
Repo: c.String("repo"),
Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"),
RegistryType: artifact.Docker,
}, },
} }
return plugin.Exec() return plugin.Exec()
+2 -30
View File
@@ -11,15 +11,12 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
kaniko "github.com/drone/drone-kaniko" kaniko "github.com/drone/drone-kaniko"
"github.com/drone/drone-kaniko/cmd/artifact"
) )
const ( const (
accessKeyEnv string = "AWS_ACCESS_KEY_ID" accessKeyEnv string = "AWS_ACCESS_KEY_ID"
secretKeyEnv string = "AWS_SECRET_ACCESS_KEY" secretKeyEnv string = "AWS_SECRET_ACCESS_KEY"
dockerConfigPath string = "/kaniko/.docker/config.json" dockerConfigPath string = "/kaniko/.docker/config.json"
defaultDigestFile string = "/kaniko/digest-file"
) )
var ( var (
@@ -112,21 +109,6 @@ func main() {
Usage: "Cache timeout in hours. Defaults to two weeks.", Usage: "Cache timeout in hours. Defaults to two weeks.",
EnvVar: "PLUGIN_CACHE_TTL", EnvVar: "PLUGIN_CACHE_TTL",
}, },
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",
},
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",
},
cli.StringFlag{
Name: "verbosity",
Usage: "Set this flag with value as oneof <panic|fatal|error|warn|info|debug|trace> to set the logging level for kaniko. Defaults to info.",
EnvVar: "PLUGIN_VERBOSITY",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@@ -153,16 +135,6 @@ func run(c *cli.Context) error {
EnableCache: c.Bool("enable-cache"), EnableCache: c.Bool("enable-cache"),
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")), CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
CacheTTL: c.Int("cache-ttl"), CacheTTL: c.Int("cache-ttl"),
DigestFile: defaultDigestFile,
NoPush: c.Bool("no-push"),
Verbosity: c.String("verbosity"),
},
Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"),
Repo: c.String("repo"),
Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"),
RegistryType: artifact.ECR,
}, },
} }
return plugin.Exec() return plugin.Exec()
@@ -170,7 +142,7 @@ func run(c *cli.Context) error {
func setupECRAuth(accessKey, secretKey, registry string) error { func setupECRAuth(accessKey, secretKey, registry string) error {
if registry == "" { if registry == "" {
return fmt.Errorf("registry must be specified") return fmt.Errorf("Registry must be specified")
} }
// If IAM role is used, access key & secret key are not required // If IAM role is used, access key & secret key are not required
@@ -186,7 +158,7 @@ func setupECRAuth(accessKey, secretKey, registry string) error {
} }
} }
jsonBytes := []byte(fmt.Sprintf(`{"credStore": "ecr-login", "credHelpers": {"public.ecr.aws": "ecr-login", "%s": "ecr-login"}}`, registry)) jsonBytes := []byte(fmt.Sprintf(`{"credStore": "ecr-login", "credHelpers": {"%s": "ecr-login"}}`, registry))
err := ioutil.WriteFile(dockerConfigPath, jsonBytes, 0644) err := ioutil.WriteFile(dockerConfigPath, jsonBytes, 0644)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create docker config file") return errors.Wrap(err, "failed to create docker config file")
-28
View File
@@ -11,15 +11,12 @@ import (
"github.com/urfave/cli" "github.com/urfave/cli"
kaniko "github.com/drone/drone-kaniko" kaniko "github.com/drone/drone-kaniko"
"github.com/drone/drone-kaniko/cmd/artifact"
) )
const ( const (
// GCR JSON key file path // GCR JSON key file path
gcrKeyPath string = "/kaniko/config.json" gcrKeyPath string = "/kaniko/config.json"
gcrEnvVariable string = "GOOGLE_APPLICATION_CREDENTIALS" gcrEnvVariable string = "GOOGLE_APPLICATION_CREDENTIALS"
defaultDigestFile string = "/kaniko/digest-file"
) )
var ( var (
@@ -108,21 +105,6 @@ func main() {
Usage: "Cache timeout in hours. Defaults to two weeks.", Usage: "Cache timeout in hours. Defaults to two weeks.",
EnvVar: "PLUGIN_CACHE_TTL", EnvVar: "PLUGIN_CACHE_TTL",
}, },
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",
},
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",
},
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",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@@ -153,16 +135,6 @@ func run(c *cli.Context) error {
EnableCache: c.Bool("enable-cache"), EnableCache: c.Bool("enable-cache"),
CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")), CacheRepo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("cache-repo")),
CacheTTL: c.Int("cache-ttl"), CacheTTL: c.Int("cache-ttl"),
DigestFile: defaultDigestFile,
NoPush: c.Bool("no-push"),
Verbosity: c.String("verbosity"),
},
Artifact: kaniko.Artifact{
Tags: c.StringSlice("tags"),
Repo: c.String("repo"),
Registry: c.String("registry"),
ArtifactFile: c.String("artifact-file"),
RegistryType: artifact.GCR,
}, },
} }
return plugin.Exec() return plugin.Exec()
+1 -1
View File
@@ -1,4 +1,4 @@
FROM gcr.io/kaniko-project/executor:v1.5.2 FROM gcr.io/kaniko-project/executor:v1.3.0
ADD release/linux/amd64/kaniko-docker /kaniko/ ADD release/linux/amd64/kaniko-docker /kaniko/
ENTRYPOINT ["/kaniko/kaniko-docker"] ENTRYPOINT ["/kaniko/kaniko-docker"]
+1 -1
View File
@@ -1,4 +1,4 @@
FROM gcr.io/kaniko-project/executor:arm64-v1.5.2 FROM gcr.io/kaniko-project/executor:arm64-v1.3.0
ENV HOME /root ENV HOME /root
ENV USER root ENV USER root
+1 -1
View File
@@ -1,4 +1,4 @@
FROM gcr.io/kaniko-project/executor:v1.5.2 FROM gcr.io/kaniko-project/executor:v1.3.0
ADD release/linux/amd64/kaniko-ecr /kaniko/ ADD release/linux/amd64/kaniko-ecr /kaniko/
ENTRYPOINT ["/kaniko/kaniko-ecr"] ENTRYPOINT ["/kaniko/kaniko-ecr"]
+1 -1
View File
@@ -1,4 +1,4 @@
FROM gcr.io/kaniko-project/executor:arm64-v1.5.2 FROM gcr.io/kaniko-project/executor:arm64-v1.3.0
ENV HOME /root ENV HOME /root
ENV USER root ENV USER root
+2 -2
View File
@@ -1,4 +1,4 @@
FROM gcr.io/kaniko-project/executor:v1.5.2 FROM gcr.io/kaniko-project/executor:v1.3.0
ADD release/linux/amd64/kaniko-gcr /kaniko/ ADD release/linux/amd64/kaniko-gcr /kaniko/
ENTRYPOINT ["/kaniko/kaniko-gcr"] ENTRYPOINT ["/kaniko/kaniko-gcr"]
+2 -2
View File
@@ -1,7 +1,7 @@
FROM gcr.io/kaniko-project/executor:arm64-v1.5.2 FROM gcr.io/kaniko-project/executor:arm64-v1.3.0
ENV HOME /root ENV HOME /root
ENV USER root ENV USER root
ADD release/linux/arm64/kaniko-gcr /kaniko/ ADD release/linux/arm64/kaniko-gcr /kaniko/
ENTRYPOINT ["/kaniko/kaniko-gcr"] ENTRYPOINT ["/kaniko/kaniko-gcr"]
+5 -47
View File
@@ -2,12 +2,9 @@ package kaniko
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"github.com/drone/drone-kaniko/cmd/artifact"
) )
type ( type (
@@ -25,23 +22,11 @@ type (
EnableCache bool // Whether to enable kaniko cache EnableCache bool // Whether to enable kaniko cache
CacheRepo string // Remote repository that will be used to store cached layers CacheRepo string // Remote repository that will be used to store cached layers
CacheTTL int // Cache timeout in hours CacheTTL int // Cache timeout in hours
DigestFile string // Digest file location
NoPush bool // Set this flag if you only want to build the image, without pushing to a registry
Verbosity string // Log level
}
// Artifact defines content of artifact file
Artifact struct {
Tags []string // Docker artifact tags
Repo string // Docker artifact repository
Registry string // Docker artifact registry
RegistryType artifact.RegistryTypeEnum // Rocker artifact registry type
ArtifactFile string // Artifact file location
} }
// Plugin defines the Docker plugin parameters. // Plugin defines the Docker plugin parameters.
Plugin struct { Plugin struct {
Build Build // Docker build configuration Build Build // Docker build configuration
Artifact Artifact // Artifact file content
} }
) )
@@ -87,50 +72,23 @@ func (p Plugin) Exec() error {
if p.Build.EnableCache == true { if p.Build.EnableCache == true {
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache=true")) cmdArgs = append(cmdArgs, fmt.Sprintf("--cache=true"))
}
if p.Build.CacheRepo != "" { if p.Build.CacheRepo != "" {
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-repo=%s", p.Build.CacheRepo)) cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-repo=%s", p.Build.CacheRepo))
}
} }
if p.Build.CacheTTL != 0 { if p.Build.CacheTTL != 0 {
cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-ttl=%d", p.Build.CacheTTL)) cmdArgs = append(cmdArgs, fmt.Sprintf("--cache-ttl=%d", p.Build.CacheTTL))
} }
if p.Build.DigestFile != "" {
cmdArgs = append(cmdArgs, fmt.Sprintf("--digest-file=%s", p.Build.DigestFile))
}
if p.Build.NoPush {
cmdArgs = append(cmdArgs, fmt.Sprintf("--no-push"))
}
if p.Build.Verbosity != "" {
cmdArgs = append(cmdArgs, fmt.Sprintf("--verbosity=%s", p.Build.Verbosity))
}
cmd := exec.Command("/kaniko/executor", cmdArgs...) cmd := exec.Command("/kaniko/executor", cmdArgs...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
trace(cmd) trace(cmd)
err := cmd.Run() err := cmd.Run()
if err != nil { return err
return err
}
if p.Build.DigestFile != "" && p.Artifact.ArtifactFile != "" {
content, err := ioutil.ReadFile(p.Build.DigestFile)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read digest file contents at path: %s with error: %s\n", p.Build.DigestFile, err)
}
err = artifact.WritePluginArtifactFile(p.Artifact.RegistryType, p.Artifact.ArtifactFile, p.Artifact.Registry, p.Artifact.Repo, string(content), p.Artifact.Tags)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write plugin artifact file at path: %s with error: %s\n", p.Artifact.ArtifactFile, err)
}
}
return nil
} }
// trace writes each command to stdout with the command wrapped in an xml // trace writes each command to stdout with the command wrapped in an xml