Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in insecure registries for kaniko #2974

Merged
merged 6 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions integration/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package integration

import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/webhook/kubernetes"
"github.com/GoogleContainerTools/skaffold/testutil"
"github.com/docker/docker/api/types"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -340,3 +342,49 @@ func TestExpectedBuildFailures(t *testing.T) {
})
}
}

func TestBuildKanikoInsecureRegistry(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

// run on GCP as this test requires a load balancer
if !ShouldRunGCPOnlyTests() {
t.Skip("skipping test that is gcp only")
}

ns, k8sClient, cleanupNs := SetupNamespace(t)
defer cleanupNs()

dir := "testdata/kaniko-insecure-registry"

cleanup := deployInsecureRegistry(t, ns.Name, dir)
defer cleanup()

ip := getExternalIP(t, k8sClient, ns.Name)
registry := fmt.Sprintf("%s:5000", ip)

skaffold.Build("--insecure-registry", registry, "-d", registry, "-p", "build-artifact").InDir(dir).InNs(ns.Name).RunOrFailOutput(t)
}

func deployInsecureRegistry(t *testing.T, ns, dir string) func() {
skaffold.Run("-p", "deploy-insecure-registry").InDir(dir).InNs(ns).RunOrFailOutput(t)

cleanup := func() {
skaffold.Delete("-p", "deploy-insecure-registry").InDir(dir).InNs(ns).RunOrFailOutput(t)
}
return cleanup
}

func getExternalIP(t *testing.T, c *NSKubernetesClient, ns string) string {
svc, err := c.client.CoreV1().Services(ns).Get("registry", metav1.GetOptions{})
if err != nil {
t.Fatalf("error getting registry service: %v", err)
}
// Wait for external IP of service
ip, err := kubernetes.GetExternalIP(svc)
if err != nil {
t.Fatalf("error getting external ip: %v", err)
}
return ip
}
7 changes: 7 additions & 0 deletions integration/testdata/kaniko-insecure-registry/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.12.9-alpine3.10 as builder
COPY main.go .
RUN go build -o /app main.go

FROM alpine:3.10
CMD ["./app"]
COPY --from=builder /app .
14 changes: 14 additions & 0 deletions integration/testdata/kaniko-insecure-registry/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
"time"
)

func main() {
for {
fmt.Println("Hello world!")

time.Sleep(time.Second * 1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: skaffold/v1beta15
kind: Config
build:
artifacts:
- image: gcr.io/k8s-skaffold/skaffold-example
deploy:
kubectl:
manifests:
- k8s-*
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM registry:2

ADD config.yaml /etc/docker/registry/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 0.1
log:
fields:
service: registry
storage:
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
http:
addr: 0.0.0.0:5000
headers:
X-Content-Type-Options: [nosniff]
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: v1
kind: Service
metadata:
name: registry
labels:
app: registry
spec:
type: LoadBalancer
ports:
- port: 5000
name: registry
selector:
app: registry
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: registry
labels:
app: registry
spec:
replicas: 1
selector:
matchLabels:
app: registry
template:
metadata:
labels:
app: registry
spec:
containers:
- name: registry
image: gcr.io/k8s-skaffold/devreg
ports:
- containerPort: 50051
24 changes: 24 additions & 0 deletions integration/testdata/kaniko-insecure-registry/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: skaffold/v1beta15
kind: Config
profiles:
- name: build-artifact
build:
artifacts:
- image: kaniko-insecure
context: app
kaniko:
cache: {}
buildContext:
localDir: {}
cluster:
pullSecretName: e2esecret
- name: deploy-insecure-registry
build:
artifacts:
- image: gcr.io/k8s-skaffold/devreg
context: insecure-registry
docker: {}
deploy:
kubectl:
manifests:
- insecure-registry/reg.yaml
8 changes: 6 additions & 2 deletions pkg/skaffold/build/cluster/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (b *Builder) runKanikoBuild(ctx context.Context, out io.Writer, artifact *l
}
defer s.Cleanup(ctx)

args, err := args(artifact.KanikoArtifact, context, tag)
args, err := args(artifact.KanikoArtifact, context, tag, b.insecureRegistries)
if err != nil {
return "", errors.Wrap(err, "building args list")
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (b *Builder) runKanikoBuild(ctx context.Context, out io.Writer, artifact *l
return docker.RemoteDigest(tag, b.insecureRegistries)
}

func args(artifact *latest.KanikoArtifact, context, tag string) ([]string, error) {
func args(artifact *latest.KanikoArtifact, context, tag string, insecureRegistries map[string]bool) ([]string, error) {
// Create pod spec
args := []string{
"--dockerfile", artifact.DockerfilePath,
Expand Down Expand Up @@ -140,6 +140,10 @@ func args(artifact *latest.KanikoArtifact, context, tag string) ([]string, error
args = append(args, "--reproducible")
}

for reg := range insecureRegistries {
args = append(args, "--insecure-registry", reg)
}

if artifact.SkipTLS {
reg, err := artifactRegistry(tag)
if err != nil {
Expand Down
21 changes: 15 additions & 6 deletions pkg/skaffold/build/cluster/kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import (

func TestArgs(t *testing.T) {
tests := []struct {
description string
artifact *latest.KanikoArtifact
tag string
shouldErr bool
expectedArgs []string
description string
artifact *latest.KanikoArtifact
insecureRegistries map[string]bool
tag string
shouldErr bool
expectedArgs []string
}{
{
description: "simple build",
Expand Down Expand Up @@ -105,6 +106,14 @@ func TestArgs(t *testing.T) {
},
shouldErr: true,
},
{
description: "insecure registries",
artifact: &latest.KanikoArtifact{
DockerfilePath: "Dockerfile",
},
insecureRegistries: map[string]bool{"localhost:4000": true},
expectedArgs: []string{"--insecure-registry", "localhost:4000"},
},
{
description: "skip tls",
artifact: &latest.KanikoArtifact{
Expand All @@ -131,7 +140,7 @@ func TestArgs(t *testing.T) {
if test.tag != "" {
tag = test.tag
}
args, err := args(test.artifact, "context", tag)
args, err := args(test.artifact, "context", tag, test.insecureRegistries)

t.CheckError(test.shouldErr, err)
if !test.shouldErr {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (

DefaultKustomizationPath = "."

DefaultKanikoImage = "gcr.io/kaniko-project/executor:v0.10.0@sha256:78d44ec4e9cb5545d7f85c1924695c89503ded86a59f92c7ae658afa3cff5400"
DefaultKanikoImage = "gcr.io/kaniko-project/executor:4ce8b8db817047f0be7a78c0fdffab71f797e8f8@sha256:fe1b5a428273309088fb6df563f4d88ab806fe602a7b0b3e8fbe1d7ee5f9ead0"
DefaultKanikoSecretName = "kaniko-secret"
DefaultKanikoTimeout = "20m"
DefaultKanikoContainerName = "kaniko"
Expand Down