-
Notifications
You must be signed in to change notification settings - Fork 680
/
kubeapply.go
227 lines (199 loc) · 5.22 KB
/
kubeapply.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package kubeapply
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/datawire/ambassador/pkg/k8s"
)
// errorDeadlineExceeded is returned from YAMLCollection.applyAndWait
// if the deadline is exceeded.
var errorDeadlineExceeded = errors.New("timeout exceeded")
// Kubeapply applies the supplied manifests to the kubernetes cluster
// indicated via the kubeinfo argument. If kubeinfo is nil, it will
// look in the standard default places for cluster configuration. If
// any phase takes longer than perPhaseTimeout to become ready, then
// it returns early with an error.
func Kubeapply(kubeinfo *k8s.KubeInfo, perPhaseTimeout time.Duration, debug, dryRun bool, files ...string) error {
collection, err := CollectYAML(files...)
if err != nil {
return err
}
if err = collection.ApplyAndWait(kubeinfo, perPhaseTimeout, debug, dryRun); err != nil {
return err
}
return nil
}
// A YAMLCollection is a collection of YAML files to later be applied.
type YAMLCollection map[string][]string
// CollectYAML takes several file or directory paths, and returns a
// collection of the YAML files in them.
func CollectYAML(paths ...string) (YAMLCollection, error) {
ret := make(YAMLCollection)
for _, path := range paths {
err := filepath.Walk(path, func(filename string, fileinfo os.FileInfo, err error) error {
if err != nil {
return err
}
if fileinfo.IsDir() {
return nil
}
if strings.HasSuffix(filename, ".yaml") {
ret.addFile(filename)
}
return nil
})
if err != nil {
return nil, err
}
}
return ret, nil
}
func hasNumberPrefix(filepart string) bool {
if len(filepart) < 3 {
return false
}
return '0' <= filepart[0] && filepart[0] <= '9' &&
'0' <= filepart[1] && filepart[1] <= '9' &&
filepart[2] == '-'
}
func (collection YAMLCollection) addFile(path string) {
_, notdir := filepath.Split(path)
phaseName := "last" // all letters sort after all numbers; "last" is after all numbered phases
if hasNumberPrefix(notdir) {
phaseName = notdir[:2]
}
collection[phaseName] = append(collection[phaseName], path)
}
// ApplyAndWait applies the collection of YAML, and waits for all
// Resources described in it to be ready. If any phase takes longer
// than perPhaseTimeout to become ready, then it returns early with an
// error.
func (collection YAMLCollection) ApplyAndWait(
kubeinfo *k8s.KubeInfo,
perPhaseTimeout time.Duration,
debug, dryRun bool,
) error {
if kubeinfo == nil {
kubeinfo = k8s.NewKubeInfo("", "", "")
}
phaseNames := make([]string, 0, len(collection))
for phaseName := range collection {
phaseNames = append(phaseNames, phaseName)
}
sort.Strings(phaseNames)
for _, phaseName := range phaseNames {
deadline := time.Now().Add(perPhaseTimeout)
err := applyAndWait(kubeinfo, deadline, debug, dryRun, collection[phaseName])
if err != nil {
if err == errorDeadlineExceeded {
err = errors.Errorf("phase %q not ready after %v", phaseName, perPhaseTimeout)
}
return err
}
}
return nil
}
func applyAndWait(kubeinfo *k8s.KubeInfo, deadline time.Time, debug, dryRun bool, filenames []string) error {
expanded, err := expand(filenames)
if err != nil {
return err
}
cli, err := k8s.NewClient(kubeinfo)
if err != nil {
return errors.Wrapf(err, "kubeapply: error connecting to cluster %v", kubeinfo)
}
waiter, err := NewWaiter(cli.Watcher())
if err != nil {
return err
}
valid := make(map[string]bool)
var msgs []string
for _, n := range expanded {
err := waiter.Scan(n)
if err != nil {
msgs = append(msgs, fmt.Sprintf("%s: %v\n", n, err))
valid[n] = false
} else {
valid[n] = true
}
}
if len(msgs) == 0 {
err = kubectlApply(kubeinfo, dryRun, expanded)
}
if !debug {
for _, n := range expanded {
if valid[n] {
err := os.Remove(n)
if err != nil {
log.Print(err)
}
}
}
}
if err != nil {
return err
}
if len(msgs) > 0 {
return errors.Errorf("errors expanding templates:\n %s", strings.Join(msgs, "\n "))
}
if !waiter.Wait(deadline) {
return errorDeadlineExceeded
}
return nil
}
func expand(names []string) ([]string, error) {
fmt.Printf("expanding %s\n", strings.Join(names, " "))
var result []string
for _, n := range names {
resources, err := LoadResources(n)
if err != nil {
return nil, err
}
out := n + ".o"
err = SaveResources(out, resources)
if err != nil {
return nil, err
}
result = append(result, out)
}
return result, nil
}
func kubectlApply(info *k8s.KubeInfo, dryRun bool, filenames []string) error {
args := []string{"apply"}
if dryRun {
args = append(args, "--dry-run")
}
for _, filename := range filenames {
// https://github.com/datawire/ambassador/issues/77
filehandle, err := os.Open(filename)
if err != nil {
return err
}
defer filehandle.Close()
if err := syscall.Flock(int(filehandle.Fd()), syscall.LOCK_EX); err != nil {
return err
}
args = append(args, "-f", filename)
}
kargs, err := info.GetKubectlArray(args...)
if err != nil {
return err
}
fmt.Printf("kubectl %s\n", strings.Join(kargs, " "))
/* #nosec */
cmd := exec.Command("kubectl", kargs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
return nil
}