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

Add --compose-file option to skaffold init #1282

Merged
merged 3 commits into from
Nov 16, 2018
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
12 changes: 12 additions & 0 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

Expand All @@ -45,6 +46,7 @@ import (
const NoDockerfile = "None (image not built from these sources)"

var (
composeFile string
cliArtifacts []string
skipBuild bool
force bool
Expand All @@ -63,13 +65,23 @@ func NewCmdInit(out io.Writer) *cobra.Command {
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&skipBuild, "skip-build", false, "Skip generating build artifacts in skaffold config")
cmd.Flags().BoolVar(&force, "force", false, "Force the generation of the skaffold config")
cmd.Flags().StringVar(&composeFile, "compose-file", "", "Initialize from a docker-compose file")
cmd.Flags().StringArrayVarP(&cliArtifacts, "artifact", "a", nil, "'='-delimited dockerfile/image pair to generate build artifact\n(example: --artifact=/web/Dockerfile.web=gcr.io/web-project/image)")
return cmd
}

func doInit(out io.Writer) error {
rootDir := "."

if composeFile != "" {
// run kompose first to generate k8s manifests, then run skaffold init
logrus.Infof("running 'kompose convert' for file %s", composeFile)
komposeCmd := exec.Command("kompose", "convert", "-f", composeFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a preference around supported version for kompose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just added it to the dockerfile with a pinned version. I only tried with the latest (v1.17.0), but I'm assuming any older version that does what it's supposed to do will work with skaffold.

if err := util.RunCmd(komposeCmd); err != nil {
return errors.Wrap(err, "running kompose")
}
}

var potentialConfigs, k8sConfigs, dockerfiles, images []string
err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error {
if f.IsDir() {
Expand Down
5 changes: 5 additions & 0 deletions deploy/skaffold/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ RUN curl -LO https://github.com/kubernetes-sigs/kustomize/releases/download/v${K
chmod +x kustomize_${KUSTOMIZE_VERSION}_linux_amd64 && \
mv kustomize_${KUSTOMIZE_VERSION}_linux_amd64 /usr/local/bin/kustomize

ENV KOMPOSE_VERSION=1.17.0
RUN curl -L https://github.com/kubernetes/kompose/releases/download/v${KOMPOSE_VERSION}/kompose-linux-amd64 -o kompose && \
chmod +x kompose && \
mv kompose /usr/local/bin

RUN echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list \
&& curl https://bazel.build/bazel-release.pub.gpg | apt-key add -

Expand Down
7 changes: 7 additions & 0 deletions examples/compose/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.10.1-alpine3.7 as builder
COPY main.go .
RUN go build -o /app main.go

FROM alpine:3.7
CMD ["./app"]
COPY --from=builder /app .
10 changes: 10 additions & 0 deletions examples/compose/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== Example: Running skaffold with docker-compose files
:icons: font

This example provides a simple application set up to run with
https://docs.docker.com/compose/[docker-compose].
Notice there is no skaffold configuration present: to run this example,
first run `skaffold init --compose-file docker-compose.yaml`, which will
invoke the https://github.com/kubernetes/kompose[kompose] binary to generate
kubernetes manifests based off of the docker-compose configuration, and then run
`skaffold init` to generate the skaffold configuration.
7 changes: 7 additions & 0 deletions examples/compose/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'
services:
compose:
build: .
image: "gcr.io/k8s-skaffold/compose"
ports:
- "5000:5000"
13 changes: 13 additions & 0 deletions examples/compose/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
"time"
)

func main() {
for {
fmt.Println("Hello world!")
time.Sleep(time.Second * 1)
}
}
7 changes: 7 additions & 0 deletions integration/examples/compose/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM golang:1.10.1-alpine3.7 as builder
COPY main.go .
RUN go build -o /app main.go

FROM alpine:3.7
CMD ["./app"]
COPY --from=builder /app .
10 changes: 10 additions & 0 deletions integration/examples/compose/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== Example: Running skaffold with docker-compose files
:icons: font

This example provides a simple application set up to run with
https://docs.docker.com/compose/[docker-compose].
Notice there is no skaffold configuration present: to run this example,
first run `skaffold init --compose-file docker-compose.yaml`, which will
invoke the https://github.com/kubernetes/kompose[kompose] binary to generate
kubernetes manifests based off of the docker-compose configuration, and then run
`skaffold init` to generate the skaffold configuration.
7 changes: 7 additions & 0 deletions integration/examples/compose/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'
services:
compose:
build: .
image: "gcr.io/k8s-skaffold/compose"
ports:
- "5000:5000"
13 changes: 13 additions & 0 deletions integration/examples/compose/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"fmt"
"time"
)

func main() {
for {
fmt.Println("Hello world!")
time.Sleep(time.Second * 1)
}
}
25 changes: 17 additions & 8 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,10 @@ func TestListConfig(t *testing.T) {

func TestInit(t *testing.T) {
type testCase struct {
name string
dir string
args []string
name string
dir string
args []string
skipSkaffoldYaml bool
}

tests := []testCase{
Expand All @@ -426,16 +427,24 @@ func TestInit(t *testing.T) {
"-a", "leeroy-web/Dockerfile=gcr.io/k8s-skaffold/leeroy-web",
},
},
{
name: "compose",
dir: "../examples/compose",
args: []string{"--compose-file", "docker-compose.yaml"},
skipSkaffoldYaml: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
oldYamlPath := filepath.Join(test.dir, "skaffold.yaml")
oldYaml, err := removeOldSkaffoldYaml(oldYamlPath)
if err != nil {
t.Fatalf("removing original skaffold.yaml: %s", err)
if !test.skipSkaffoldYaml {
oldYamlPath := filepath.Join(test.dir, "skaffold.yaml")
oldYaml, err := removeOldSkaffoldYaml(oldYamlPath)
if err != nil {
t.Fatalf("removing original skaffold.yaml: %s", err)
}
defer restoreOldSkaffoldYaml(oldYaml, oldYamlPath)
}
defer restoreOldSkaffoldYaml(oldYaml, oldYamlPath)

generatedYaml := "skaffold.yaml.out"
defer func() {
Expand Down