-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
139 lines (125 loc) · 3.08 KB
/
main.go
File metadata and controls
139 lines (125 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
yaml "gopkg.in/yaml.v2"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
)
type miniDeployment struct {
APIVersion string `yaml:"apiVersion"`
Kind string
Metadata struct {
Name string
}
Spec struct {
Template struct {
Spec struct {
Containers []struct {
Name string
Image string
}
}
}
}
}
func patchFile(filename string, oldTag, newTag string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to patch %s - skipping\n", filename)
return
}
var reImage = regexp.MustCompile(fmt.Sprintf("image:\\s*['\"]*%s\\s*['\"]*\n", oldTag))
newData := reImage.ReplaceAllLiteral(data, []byte("image: "+newTag+"\n"))
err = ioutil.WriteFile(filename, newData, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to patch %s - skipping\n", filename)
return
}
}
func investigateFile(t miniDeployment, filename string, image string, newTag string) {
if t.Kind == "Deployment" {
for _, container := range t.Spec.Template.Spec.Containers {
parts := strings.SplitN(container.Image, ":", 2)
if parts[0] == image {
baseName := path.Base(filename)
if container.Image != newTag {
fmt.Fprintf(os.Stderr, "%s (%s/%s) %s -> %s\n", baseName, t.Metadata.Name, container.Name, container.Image, newTag)
patchFile(filename, container.Image, newTag)
}
}
}
}
}
func getLatestTag() string {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
panic(err)
}
for _, image := range images {
if len(image.RepoTags) == 0 || image.RepoTags[0] == "<none>:<none>" {
continue
}
return image.RepoTags[0]
}
return ""
}
func main() {
templatePath := os.Getenv("UPDATE_DOCKER_TAG_PATH")
if templatePath == "" {
templatePath = "."
}
var newTag string
if len(os.Args) >= 2 {
newTag = os.Args[1]
} else {
newTag = getLatestTag()
}
image := strings.SplitN(newTag, ":", 2)[0]
err := filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".yaml") {
f, err := os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read file %s - skipping.\n", path)
} else {
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)
}
}
}
}
}
return nil
})
if err != nil {
panic(err)
}
}