Skip to content

Commit effac9d

Browse files
Persistent volume caching for base images (GoogleContainerTools#383)
* comments * initial commit for persisent volume caching * cache warmer works * general cleanup * adding some debugging * adding missing files * Fixing up cache retrieval and cleanup * fix tests * removing auth since we only cache public images * simplifying the caching logic * fixing logic * adding volume cache to integration tests. remove auth from cache warmer image. * add building warmer to integration-test * move sample yaml files to examples dir * small test fix
1 parent 03db09e commit effac9d

File tree

18 files changed

+389
-6
lines changed

18 files changed

+389
-6
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ GO_LDFLAGS += -w -s # Drop debugging symbols.
3535
GO_LDFLAGS += '
3636

3737
EXECUTOR_PACKAGE = $(REPOPATH)/cmd/executor
38+
WARMER_PACKAGE = $(REPOPATH)/cmd/warmer
3839
KANIKO_PROJECT = $(REPOPATH)/kaniko
3940

4041
out/executor: $(GO_FILES)
4142
GOARCH=$(GOARCH) GOOS=linux CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -o $@ $(EXECUTOR_PACKAGE)
4243

44+
out/warmer: $(GO_FILES)
45+
GOARCH=$(GOARCH) GOOS=linux CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -o $@ $(WARMER_PACKAGE)
46+
4347
.PHONY: test
4448
test: out/executor
4549
@ ./test.sh
@@ -52,3 +56,4 @@ integration-test:
5256
images:
5357
docker build -t $(REGISTRY)/executor:latest -f deploy/Dockerfile .
5458
docker build -t $(REGISTRY)/executor:debug -f deploy/Dockerfile_debug .
59+
docker build -t $(REGISTRY)/warmer:latest -f deploy/Dockerfile_warmer .

cmd/executor/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func addKanikoOptionsFlags(cmd *cobra.Command) {
9999
RootCmd.PersistentFlags().StringVarP(&opts.Target, "target", "", "", "Set the target build stage to build")
100100
RootCmd.PersistentFlags().BoolVarP(&opts.NoPush, "no-push", "", false, "Do not push the image to the registry")
101101
RootCmd.PersistentFlags().StringVarP(&opts.CacheRepo, "cache-repo", "", "", "Specify a repository to use as a cache, otherwise one will be inferred from the destination provided")
102+
RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "", "/cache", "Specify a local directory to use as a cache.")
102103
RootCmd.PersistentFlags().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image")
103104
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end")
104105
}

cmd/warmer/cmd/root.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2018 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/GoogleContainerTools/kaniko/pkg/cache"
24+
"github.com/GoogleContainerTools/kaniko/pkg/config"
25+
"github.com/GoogleContainerTools/kaniko/pkg/constants"
26+
"github.com/GoogleContainerTools/kaniko/pkg/util"
27+
"github.com/pkg/errors"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
var (
32+
opts = &config.WarmerOptions{}
33+
logLevel string
34+
)
35+
36+
func init() {
37+
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic")
38+
addKanikoOptionsFlags(RootCmd)
39+
addHiddenFlags(RootCmd)
40+
}
41+
42+
var RootCmd = &cobra.Command{
43+
Use: "cache warmer",
44+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
45+
if err := util.ConfigureLogging(logLevel); err != nil {
46+
return err
47+
}
48+
if len(opts.Images) == 0 {
49+
return errors.New("You must select at least one image to cache")
50+
}
51+
return nil
52+
},
53+
Run: func(cmd *cobra.Command, args []string) {
54+
if err := cache.WarmCache(opts); err != nil {
55+
exit(errors.Wrap(err, "Failed warming cache"))
56+
}
57+
},
58+
}
59+
60+
// addKanikoOptionsFlags configures opts
61+
func addKanikoOptionsFlags(cmd *cobra.Command) {
62+
RootCmd.PersistentFlags().VarP(&opts.Images, "image", "i", "Image to cache. Set it repeatedly for multiple images.")
63+
RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "c", "/cache", "Directory of the cache.")
64+
}
65+
66+
// addHiddenFlags marks certain flags as hidden from the executor help text
67+
func addHiddenFlags(cmd *cobra.Command) {
68+
RootCmd.PersistentFlags().MarkHidden("azure-container-registry-config")
69+
}
70+
71+
func exit(err error) {
72+
fmt.Println(err)
73+
os.Exit(1)
74+
}

cmd/warmer/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2018 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"os"
21+
22+
"github.com/GoogleContainerTools/kaniko/cmd/warmer/cmd"
23+
)
24+
25+
func main() {
26+
if err := cmd.RootCmd.Execute(); err != nil {
27+
os.Exit(1)
28+
}
29+
}

deploy/Dockerfile_warmer

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2018 Google, Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Builds the static Go image to execute in a Kubernetes job
16+
17+
FROM golang:1.10
18+
WORKDIR /go/src/github.com/GoogleContainerTools/kaniko
19+
COPY . .
20+
RUN make
21+
22+
FROM scratch
23+
COPY --from=0 /go/src/github.com/GoogleContainerTools/kaniko/out/warmer /kaniko/warmer
24+
COPY files/ca-certificates.crt /kaniko/ssl/certs/
25+
COPY files/config.json /kaniko/.docker/
26+
ENV HOME /root
27+
ENV USER /root
28+
ENV PATH /usr/local/bin:/kaniko
29+
ENV SSL_CERT_DIR=/kaniko/ssl/certs
30+
ENV DOCKER_CONFIG /kaniko/.docker/
31+
WORKDIR /workspace
32+
ENTRYPOINT ["/kaniko/warmer"]

examples/kaniko-cache-claim.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
kind: PersistentVolumeClaim
2+
apiVersion: v1
3+
metadata:
4+
name: kaniko-cache-claim
5+
spec:
6+
storageClassName: manual
7+
accessModes:
8+
- ReadOnlyMany
9+
resources:
10+
requests:
11+
storage: 8Gi

examples/kaniko-cache-volume.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
kind: PersistentVolume
2+
apiVersion: v1
3+
metadata:
4+
name: kaniko-cache-volume
5+
labels:
6+
type: local
7+
spec:
8+
storageClassName: manual
9+
capacity:
10+
storage: 10Gi
11+
accessModes:
12+
- ReadOnlyMany
13+
hostPath:
14+
path: "/tmp/kaniko-cache"

examples/kaniko-test.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: kaniko
5+
spec:
6+
containers:
7+
- name: kaniko
8+
image: gcr.io/kaniko-project/executor:latest
9+
args: ["--dockerfile=<dockerfile>",
10+
"--context=<context>",
11+
"--destination=<destination>",
12+
"--cache",
13+
"--cache-dir=/cache"]
14+
volumeMounts:
15+
- name: kaniko-secret
16+
mountPath: /secret
17+
- name: kaniko-cache
18+
mountPath: /cache
19+
env:
20+
- name: GOOGLE_APPLICATION_CREDENTIALS
21+
value: /secret/kaniko-secret.json
22+
restartPolicy: Never
23+
volumes:
24+
- name: kaniko-secret
25+
secret:
26+
secretName: kaniko-secret
27+
- name: kaniko-cache
28+
persistentVolumeClaim:
29+
claimName: kaniko-cache-claim
30+

examples/kaniko-warmer.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: kaniko-warmer
5+
spec:
6+
containers:
7+
- name: kaniko-warmer
8+
image: gcr.io/kaniko-project/warmer:latest
9+
args: ["--cache-dir=/cache",
10+
"--image=gcr.io/google-appengine/debian9"]
11+
volumeMounts:
12+
- name: kaniko-secret
13+
mountPath: /secret
14+
- name: kaniko-cache
15+
mountPath: /cache
16+
env:
17+
- name: GOOGLE_APPLICATION_CREDENTIALS
18+
value: /secret/kaniko-secret.json
19+
restartPolicy: Never
20+
volumes:
21+
- name: kaniko-secret
22+
secret:
23+
secretName: kaniko-secret
24+
- name: kaniko-cache
25+
persistentVolumeClaim:
26+
claimName: kaniko-cache-claim
27+

integration-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ fi
3434

3535
echo "Running integration tests..."
3636
make out/executor
37+
make out/warmer
3738
pushd integration
3839
go test -v --bucket "${GCS_BUCKET}" --repo "${IMAGE_REPO}" --timeout 30m

0 commit comments

Comments
 (0)