Skip to content

Commit

Permalink
Added v1alpha3 config
Browse files Browse the repository at this point in the history
I added a v1alpha3 version of the skaffold config, since adding
different buildcontexts to the kaniko builder requires a breaking config
change.

Now, new buildcontexts can be easily added to the KanikoBuildContext type.

I also added a transform function so that 'skaffold fix' can convert
v1alpha2 configs to v1alpha3, and fixed unit tests to pass with the new
config.

This is part 1 of 3 PRs to incorporate more kaniko buildcontexts into
the kaniko builder.
  • Loading branch information
Priya Wadhwa committed Sep 13, 2018
1 parent abe8032 commit e69338b
Show file tree
Hide file tree
Showing 57 changed files with 978 additions and 282 deletions.
20 changes: 10 additions & 10 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -206,23 +206,23 @@ func promptUserForDockerfile(image string, dockerfiles []string) dockerfilePair
}
}

func processBuildArtifacts(pairs []dockerfilePair) v1alpha2.BuildConfig {
var config v1alpha2.BuildConfig
func processBuildArtifacts(pairs []dockerfilePair) v1alpha3.BuildConfig {
var config v1alpha3.BuildConfig

if len(pairs) > 0 {
var artifacts []*v1alpha2.Artifact
var artifacts []*v1alpha3.Artifact
for _, pair := range pairs {
workspace := filepath.Dir(pair.Dockerfile)
dockerfilePath := filepath.Base(pair.Dockerfile)
a := &v1alpha2.Artifact{
a := &v1alpha3.Artifact{
ImageName: pair.ImageName,
}
if workspace != "." {
a.Workspace = workspace
}
if dockerfilePath != constants.DefaultDockerfilePath {
a.ArtifactType = v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
a.ArtifactType = v1alpha3.ArtifactType{
DockerArtifact: &v1alpha3.DockerArtifact{
DockerfilePath: dockerfilePath,
},
}
Expand All @@ -246,9 +246,9 @@ func generateSkaffoldConfig(k8sConfigs []string, dockerfilePairs []dockerfilePai
}
config.Build = processBuildArtifacts(dockerfilePairs)

config.Deploy = v1alpha2.DeployConfig{
DeployType: v1alpha2.DeployType{
KubectlDeploy: &v1alpha2.KubectlDeploy{
config.Deploy = v1alpha3.DeployConfig{
DeployType: v1alpha3.DeployType{
KubectlDeploy: &v1alpha3.KubectlDeploy{
Manifests: k8sConfigs,
},
},
Expand Down
5 changes: 3 additions & 2 deletions examples/annotated-skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
# tagPolicy determines how skaffold is going to tag your images.
Expand Down Expand Up @@ -101,7 +101,8 @@ build:
# See https://github.com/GoogleContainerTools/kaniko#running-kaniko-in-a-kubernetes-cluster
#
# kaniko:
# gcsBucket: k8s-skaffold
# buildContext:
# gcsBucket: k8s-skaffold
# pullSecret: /a/secret/path/serviceaccount.json
# namespace: default
# timeout: 20m
Expand Down
2 changes: 1 addition & 1 deletion examples/bazel/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/getting-started/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/helm-deployment/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
tagPolicy:
Expand Down
45 changes: 45 additions & 0 deletions examples/kaniko/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: v1
kind: Pod
metadata:
name: kaniko
spec:
initContainers:
- name: init-container
image: ubuntu
args: ["sh", "-c", "cp -L -r /kaniko/buildcontext/* /kaniko/emptydir && ls /kaniko/emptydir && stat /kaniko/emptydir/Dockerfile"]
volumeMounts:
- name: build-context
mountPath: /kaniko/buildcontext
- name: empty-dir
mountPath: /kaniko/emptydir
containers:
- name: kaniko
image: gcr.io/kaniko-project/executor:latest
args: ["--dockerfile=Dockerfile",
"--context=dir:///kaniko/emptydir",
"--destination=gcr.io/priya-wadhwa/test:test"]
volumeMounts:
- name: kaniko-secret
mountPath: /secret
- name: build-context
mountPath: /kaniko/buildcontext
- name: empty-dir
mountPath: /kaniko/emptydir
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /secret/kaniko-secret.json
restartPolicy: Never
volumes:
- name: kaniko-secret
secret:
secretName: kaniko
# - name: build-context
# persistentVolumeClaim:
# claimName: pv-claim-kaniko
- name: build-context
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: kaniko-configmap77a0c64f84c48aeb56093119c709807b
- name: empty-dir
emptyDir: {}
5 changes: 3 additions & 2 deletions examples/kaniko/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
artifacts:
- imageName: gcr.io/k8s-skaffold/skaffold-example
kaniko:
gcsBucket: skaffold-kaniko
buildContext:
gcsBucket: skaffold-kaniko
pullSecretName: e2esecret
namespace: default
deploy:
Expand Down
2 changes: 1 addition & 1 deletion examples/kustomize/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
deploy:
kustomize: {}
2 changes: 1 addition & 1 deletion examples/microservices/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
artifacts:
Expand Down
2 changes: 1 addition & 1 deletion examples/tagging-with-environment-variables/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: skaffold/v1alpha2
apiVersion: skaffold/v1alpha3
kind: Config
build:
artifacts:
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"path/filepath"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
)
Expand All @@ -36,7 +36,7 @@ func query(target string) string {

// GetDependencies finds the sources dependencies for the given bazel artifact.
// All paths are relative to the workspace.
func GetDependencies(workspace string, a *v1alpha2.BazelArtifact) ([]string, error) {
func GetDependencies(workspace string, a *v1alpha3.BazelArtifact) ([]string, error) {
cmd := exec.Command("bazel", "query", query(a.BuildTarget), "--noimplicit_deps", "--order_output=no")
cmd.Dir = workspace
stdout, err := util.RunCmdOut(cmd)
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/bazel/bazel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package bazel
import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
)
Expand All @@ -36,7 +36,7 @@ func TestGetDependenciesWithWorkspace(t *testing.T) {
defer cleanup()
tmpDir.Write("WORKSPACE", "")

deps, err := GetDependencies(tmpDir.Root(), &v1alpha2.BazelArtifact{
deps, err := GetDependencies(tmpDir.Root(), &v1alpha3.BazelArtifact{
BuildTarget: "target",
})

Expand All @@ -51,7 +51,7 @@ func TestGetDependenciesWithoutWorkspace(t *testing.T) {
nil,
)

deps, err := GetDependencies(".", &v1alpha2.BazelArtifact{
deps, err := GetDependencies(".", &v1alpha3.BazelArtifact{
BuildTarget: "target2",
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
)

// Artifact is the result corresponding to each successful build.
Expand All @@ -37,5 +37,5 @@ type Artifact struct {
type Builder interface {
Labels() map[string]string

Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]Artifact, error)
Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha3.Artifact) ([]Artifact, error)
}
6 changes: 3 additions & 3 deletions pkg/skaffold/build/gcb/cloud_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
"github.com/pkg/errors"
Expand All @@ -41,11 +41,11 @@ import (
)

// Build builds a list of artifacts with Google Cloud Build.
func (b *Builder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha2.Artifact) ([]build.Artifact, error) {
func (b *Builder) Build(ctx context.Context, out io.Writer, tagger tag.Tagger, artifacts []*v1alpha3.Artifact) ([]build.Artifact, error) {
return build.InParallel(ctx, out, tagger, artifacts, b.buildArtifact)
}

func (b *Builder) buildArtifact(ctx context.Context, out io.Writer, tagger tag.Tagger, artifact *v1alpha2.Artifact) (string, error) {
func (b *Builder) buildArtifact(ctx context.Context, out io.Writer, tagger tag.Tagger, artifact *v1alpha3.Artifact) (string, error) {
client, err := google.DefaultClient(ctx, cloudbuild.CloudPlatformScope)
if err != nil {
return "", errors.Wrap(err, "getting google client")
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/gcb/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package gcb

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func (b *Builder) buildDescription(artifact *v1alpha2.Artifact, bucket, object string) *cloudbuild.Build {
func (b *Builder) buildDescription(artifact *v1alpha3.Artifact, bucket, object string) *cloudbuild.Build {
var steps []*cloudbuild.BuildStep

for _, cacheFrom := range artifact.DockerArtifact.CacheFrom {
Expand Down
18 changes: 9 additions & 9 deletions pkg/skaffold/build/gcb/desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ package gcb
import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func TestBuildDescription(t *testing.T) {
artifact := &v1alpha2.Artifact{
artifact := &v1alpha3.Artifact{
ImageName: "nginx",
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
ArtifactType: v1alpha3.ArtifactType{
DockerArtifact: &v1alpha3.DockerArtifact{
DockerfilePath: "Dockerfile",
BuildArgs: map[string]*string{
"arg1": util.StringPtr("value1"),
Expand All @@ -40,7 +40,7 @@ func TestBuildDescription(t *testing.T) {
}

builder := Builder{
GoogleCloudBuild: &v1alpha2.GoogleCloudBuild{
GoogleCloudBuild: &v1alpha3.GoogleCloudBuild{
DockerImage: "docker/docker",
DiskSizeGb: 100,
MachineType: "n1-standard-1",
Expand Down Expand Up @@ -73,18 +73,18 @@ func TestBuildDescription(t *testing.T) {
}

func TestPullCacheFrom(t *testing.T) {
artifact := &v1alpha2.Artifact{
artifact := &v1alpha3.Artifact{
ImageName: "nginx",
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{
ArtifactType: v1alpha3.ArtifactType{
DockerArtifact: &v1alpha3.DockerArtifact{
DockerfilePath: "Dockerfile",
CacheFrom: []string{"from/image1", "from/image2"},
},
},
}

builder := Builder{
GoogleCloudBuild: &v1alpha2.GoogleCloudBuild{
GoogleCloudBuild: &v1alpha3.GoogleCloudBuild{
DockerImage: "docker/docker",
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/gcb/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"fmt"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/docker/distribution/reference"
"github.com/docker/docker/registry"
"github.com/pkg/errors"
)

func (b *Builder) guessProjectID(artifact *v1alpha2.Artifact) (string, error) {
func (b *Builder) guessProjectID(artifact *v1alpha3.Artifact) (string, error) {
if b.ProjectID != "" {
return b.ProjectID, nil
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/skaffold/build/gcb/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,46 @@ package gcb
import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestGuessProjectID(t *testing.T) {
var tests = []struct {
description string
config *v1alpha2.GoogleCloudBuild
artifact *v1alpha2.Artifact
config *v1alpha3.GoogleCloudBuild
artifact *v1alpha3.Artifact
expected string
shouldErr bool
}{
{
description: "fixed projectId",
config: &v1alpha2.GoogleCloudBuild{ProjectID: "fixed"},
artifact: &v1alpha2.Artifact{ImageName: "any"},
config: &v1alpha3.GoogleCloudBuild{ProjectID: "fixed"},
artifact: &v1alpha3.Artifact{ImageName: "any"},
expected: "fixed",
},
{
description: "gcr.io",
config: &v1alpha2.GoogleCloudBuild{},
artifact: &v1alpha2.Artifact{ImageName: "gcr.io/project/image"},
config: &v1alpha3.GoogleCloudBuild{},
artifact: &v1alpha3.Artifact{ImageName: "gcr.io/project/image"},
expected: "project",
},
{
description: "eu.gcr.io",
config: &v1alpha2.GoogleCloudBuild{},
artifact: &v1alpha2.Artifact{ImageName: "gcr.io/project/image"},
config: &v1alpha3.GoogleCloudBuild{},
artifact: &v1alpha3.Artifact{ImageName: "gcr.io/project/image"},
expected: "project",
},
{
description: "docker hub",
config: &v1alpha2.GoogleCloudBuild{},
artifact: &v1alpha2.Artifact{ImageName: "project/image"},
config: &v1alpha3.GoogleCloudBuild{},
artifact: &v1alpha3.Artifact{ImageName: "project/image"},
shouldErr: true,
},
{
description: "invalid GCR image",
config: &v1alpha2.GoogleCloudBuild{},
artifact: &v1alpha2.Artifact{ImageName: "gcr.io"},
config: &v1alpha3.GoogleCloudBuild{},
artifact: &v1alpha3.Artifact{ImageName: "gcr.io"},
shouldErr: true,
},
}
Expand Down
Loading

0 comments on commit e69338b

Please sign in to comment.