Skip to content

Commit

Permalink
Kompose will read input from stdin
Browse files Browse the repository at this point in the history
Resolves issue #870

for example,

```
$ cat docker-compose.yaml | kompose convert -f -
INFO Kubernetes file "frontend-service.yaml" created
INFO Kubernetes file "redis-master-service.yaml" created
INFO Kubernetes file "redis-slave-service.yaml" created
INFO Kubernetes file "frontend-deployment.yaml" created
INFO Kubernetes file "redis-master-deployment.yaml" created
INFO Kubernetes file "redis-slave-deployment.yaml" created
```

Added integration test for the same.
`
  • Loading branch information
surajnarwade committed Dec 12, 2017
1 parent e4adfef commit 02da34b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ install:
- script/test/cmd/fix_detached_head.sh

script:
- make bin
- make test
# Only cross-compile once
- if [ "$CROSS_COMPILE" == "yes" ]; then make cross; fi
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ test-openshift:
.PHONY: test-cmd
test-cmd:
./script/test/cmd/tests.sh
go test -v github.com/kubernetes/kompose/script/test/cmd


# run all validation tests
.PHONY: validate
Expand Down
17 changes: 13 additions & 4 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (

yaml "gopkg.in/yaml.v2"

"bufio"
"os"

"github.com/docker/libcompose/project"
"github.com/fatih/structs"
"github.com/kubernetes/kompose/pkg/kobject"
Expand Down Expand Up @@ -199,10 +202,16 @@ func getVersionFromFile(file string) (string, error) {
Version string `json:"version"` // This affects YAML as well
}
var version ComposeVersion

loadedFile, err := ioutil.ReadFile(file)
if err != nil {
return "", err
var loadedFile []byte
var err error
if file == "-" {
data := bufio.NewScanner(os.Stdin)
loadedFile = data.Bytes()
} else {
loadedFile, err = ioutil.ReadFile(file)
if err != nil {
return "", err
}
}

err = yaml.Unmarshal(loadedFile, &version)
Expand Down
30 changes: 30 additions & 0 deletions script/test/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
)

var Fixtures = os.ExpandEnv("$GOPATH/src/github.com/kubernetes/kompose/script/test/fixtures/")
var ProjectPath = "$GOPATH/src/github.com/kubernetes/kompose/"
var BinaryLocation = os.ExpandEnv(ProjectPath + "kompose")

func Test_stdin(t *testing.T) {

kjson := `{"version": "2","services": {"redis": {"image": "redis:3.0","ports": ["6379"]}}}`
cmdStr := fmt.Sprintf("%s convert --stdout -f - <<EOF\n%s\nEOF\n", BinaryLocation, kjson)
subproc := exec.Command("/bin/sh", "-c", cmdStr)
output, err := subproc.Output()
if err != nil {
fmt.Println("error", err)
}
g, err := ioutil.ReadFile(Fixtures + "stdin/output.yml")
if !bytes.Equal(output, g) {
t.Errorf("Test Failed")
}
}

53 changes: 53 additions & 0 deletions script/test/fixtures/stdin/output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apiVersion: v1
items:
- apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: /home/snarwade/go/src/github.com/kubernetes/kompose/kompose convert
--stdout -f -
kompose.version: 1.6.0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
ports:
- name: "6379"
port: 6379
targetPort: 6379
selector:
io.kompose.service: redis
status:
loadBalancer: {}
- apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
kompose.cmd: /home/snarwade/go/src/github.com/kubernetes/kompose/kompose convert
--stdout -f -
kompose.version: 1.6.0 (HEAD)
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
replicas: 1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
spec:
containers:
- image: redis:3.0
name: redis
ports:
- containerPort: 6379
resources: {}
restartPolicy: Always
status: {}
kind: List
metadata: {}

7 changes: 7 additions & 0 deletions script/test_k8s/kubernetes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ test_k8s() {
./kompose down -f $f --controller=replicationcontroller

done

echo -e "\nTesting stdin to kompose\n"
echo -e "\n${RED}cat examples/docker-compose.yaml | ./kompose up -f -${NC}\n"
cat examples/docker-compose.yaml | ./kompose up -f -
sleep 2 # Sleep for k8s to catch up to deployment
echo -e "\n${RED}cat examples/docker-compose.yaml | ./kompose down -f - ${NC}\n"
cat examples/docker-compose.yaml | ./kompose down -f -
}

if [[ $1 == "start" ]]; then
Expand Down

0 comments on commit 02da34b

Please sign in to comment.