Skip to content

Commit

Permalink
Merge pull request #1656 from priyawadhwa/local
Browse files Browse the repository at this point in the history
 Added local execution environment to docker builder plugin
  • Loading branch information
priyawadhwa authored Feb 22, 2019
2 parents 0e175e2 + 3d3b628 commit 218b0ec
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.10.1-alpine3.7 as builder
COPY app.go .
RUN go build -o /app .

FROM alpine:3.7
CMD ["./app"]
COPY --from=builder /app .
17 changes: 17 additions & 0 deletions integration/examples/test-plugin/local/docker/leeroy-app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"log"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "leeroooooy app!!\n")
}

func main() {
log.Print("leeroy app server ready")
http.HandleFunc("/", handler)
http.ListenAndServe(":50051", nil)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: v1
kind: Service
metadata:
name: leeroy-app
labels:
app: leeroy-app
spec:
clusterIP: None
ports:
- port: 50051
name: leeroy-app
selector:
app: leeroy-app
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: leeroy-app
labels:
app: leeroy-app
spec:
replicas: 1
selector:
matchLabels:
app: leeroy-app
template:
metadata:
labels:
app: leeroy-app
spec:
containers:
- name: leeroy-app
image: gcr.io/k8s-skaffold/leeroy-app
ports:
- containerPort: 50051
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.10.1-alpine3.7 as builder
COPY web.go .
RUN go build -o /web .

FROM alpine:3.7
CMD ["./web"]
COPY --from=builder /web .
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: v1
kind: Service
metadata:
name: leeroy-web
labels:
app: leeroy-web
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
protocol: TCP
name: leeroy-web
selector:
app: leeroy-web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: leeroy-web
labels:
app: leeroy-web
spec:
replicas: 1
selector:
matchLabels:
app: leeroy-web
template:
metadata:
labels:
app: leeroy-web
spec:
containers:
- name: leeroy-web
image: gcr.io/k8s-skaffold/leeroy-web
ports:
- containerPort: 8080
26 changes: 26 additions & 0 deletions integration/examples/test-plugin/local/docker/leeroy-web/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"io"
"net/http"

"log"
)

func handler(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("http://leeroy-app:50051")
if err != nil {
panic(err)
}
defer resp.Body.Close()
if _, err := io.Copy(w, resp.Body); err != nil {
panic(err)
}

}

func main() {
log.Print("leeroy web server ready")
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
19 changes: 19 additions & 0 deletions integration/examples/test-plugin/local/docker/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: skaffold/v1beta5
kind: Config
build:
artifacts:
- image: gcr.io/k8s-skaffold/leeroy-web
context: ./leeroy-web/
plugin:
name: docker
- image: gcr.io/k8s-skaffold/leeroy-app
context: ./leeroy-app/
plugin:
name: docker
executionEnvironment:
name: local
deploy:
kubectl:
manifests:
- ./leeroy-web/kubernetes/*
- ./leeroy-app/kubernetes/*
4 changes: 4 additions & 0 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func TestRun(t *testing.T) {
description: "bazel plugin in local exec environment",
dir: "examples/test-plugin/local/bazel",
pods: []string{"bazel"},
}, {
description: "docker plugin in local exec environment",
dir: "examples/test-plugin/local/docker",
deployments: []string{"leeroy-app", "leeroy-web"},
},
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/skaffold/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/local"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/environments/gcb"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/defaults"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -75,11 +77,37 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, tags tag.ImageTags,
switch b.env.Name {
case constants.GoogleCloudBuild:
return b.googleCloudBuild(ctx, out, tags, artifacts)
case constants.Local:
return b.local(ctx, out, tags, artifacts)
default:
return nil, errors.Errorf("%s is not a supported environment for builder docker", b.env.Name)
}
}

func (b *Builder) local(ctx context.Context, out io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
var l *latest.LocalBuild
if err := util.CloneThroughJSON(b.env.Properties, &l); err != nil {
return nil, errors.Wrap(err, "converting execution env to localBuild struct")
}
if l == nil {
l = &latest.LocalBuild{}
}
kubeContext, err := kubectx.CurrentContext()
if err != nil {
return nil, errors.Wrap(err, "getting current cluster context")
}
builder, err := local.NewBuilder(l, kubeContext, b.opts.SkipTests)
if err != nil {
return nil, errors.Wrap(err, "getting local builder")
}
for _, a := range artifacts {
if err := setArtifact(a); err != nil {
return nil, err
}
}
return builder.Build(ctx, out, tags, artifacts)
}

// googleCloudBuild sets any necessary defaults and then builds artifacts with docker in GCB
func (b *Builder) googleCloudBuild(ctx context.Context, out io.Writer, tags tag.ImageTags, artifacts []*latest.Artifact) ([]build.Artifact, error) {
var g *latest.GoogleCloudBuild
Expand Down

0 comments on commit 218b0ec

Please sign in to comment.