Skip to content

Commit

Permalink
Add support for multi-document YAML files
Browse files Browse the repository at this point in the history
  • Loading branch information
sel committed Dec 10, 2017
1 parent 3e14ce2 commit e595416
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 25 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/docker/docker/client"

yaml "gopkg.in/yaml.v2"

utilyaml "k8s.io/apimachinery/pkg/util/yaml"
)

type miniDeployment struct {
Expand Down Expand Up @@ -95,18 +98,32 @@ func main() {

image := strings.SplitN(newTag, ":", 2)[0]

err := filepath.Walk(templatePath, func(path string, f os.FileInfo, err error) error {
err := filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".yaml") {
data, err := ioutil.ReadFile(path)
f, err := os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read file %s - skipping.\n", path)
} else {
t := miniDeployment{}
err = yaml.Unmarshal(data, &t)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse YAML in %s - skipping.\n", path)
} else {
investigateFile(t, path, image, newTag)
defer f.Close()
data := make([]byte, 4096)
decoder := utilyaml.NewDocumentDecoder(f)
for {
n, err := decoder.Read(data)
if err != nil {
if err != io.EOF {
fmt.Fprintf(os.Stderr, "Failed to parse YAML in %s - skipping.\n", path)
}
break
} else {
fmt.Fprintf(os.Stderr, "Read %d byte YAML document from %s.\n", n, path)
t := miniDeployment{}
err = yaml.Unmarshal(data[:n], &t)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse YAML in %s [%v]- skipping.\n", path, err)
} else {
investigateFile(t, path, image, newTag)
}
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions testdata/nginx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
app: nginx
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.10
ports:
- containerPort: 80

0 comments on commit e595416

Please sign in to comment.