-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.go
More file actions
271 lines (248 loc) · 7.04 KB
/
Copy pathplatform.go
File metadata and controls
271 lines (248 loc) · 7.04 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* Package platform provides Deis platform components.
*/
package platform
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"github.com/deis/deis/trireme/k8s"
"github.com/deis/deis/trireme/storage"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/latest"
)
// Component describes a component of the Deis platform.
type Component struct {
Name, Description string
RCs, Pods, Services, Namespaces, Volumes, Secrets []string
Optional bool
}
// InstallPrereqs installs Services, Namespaces, Volumes, and Secrets.
//
// This installs things that are considered prerequisites for the RC or Pod to
// function.
func (c *Component) InstallPrereqs(dir string) error {
for _, ns := range c.Namespaces {
p := filepath.Join(dir, ns)
fmt.Printf("Installed namespace %s\n", p)
if err := k8s.KubectlCreate(p); err != nil {
return err
}
}
for _, s := range c.Services {
p := filepath.Join(dir, s)
fmt.Printf("Installed services %s\n", p)
if err := k8s.KubectlCreate(p); err != nil {
return err
}
}
for _, vol := range c.Volumes {
p := filepath.Join(dir, vol)
fmt.Printf("Installed volume %s\n", p)
if err := k8s.KubectlCreate(p); err != nil {
return err
}
}
for _, sec := range c.Secrets {
p := filepath.Join(dir, sec)
fmt.Printf("Installed secret %s\n", p)
if err := k8s.KubectlCreate(p); err != nil {
return err
}
}
return nil
}
// DeletePrereqs deletes a component's prerequisites.
//
// The order is Secrets, Volumes, Services, Namespaces.
func (c *Component) DeletePrereqs(dir string) error {
for _, x := range c.Secrets {
p := filepath.Join(dir, x)
fmt.Printf("Removing Secret %s\n", p)
if err := k8s.KubectlDelete(p); err != nil {
return err
}
}
for _, x := range c.Volumes {
p := filepath.Join(dir, x)
fmt.Printf("Removing Volume %s\n", p)
if err := k8s.KubectlDelete(p); err != nil {
return err
}
}
for _, x := range c.Services {
p := filepath.Join(dir, x)
fmt.Printf("Removing Service %s\n", p)
if err := k8s.KubectlDelete(p); err != nil {
return err
}
}
for _, x := range c.Namespaces {
p := filepath.Join(dir, x)
fmt.Printf("Removing Namespace %s\n", p)
if err := k8s.KubectlDelete(p); err != nil {
return err
}
}
return nil
}
// Install installs pods and RCs (in that order).
func (c *Component) Install(dir string) error {
for _, pod := range c.Pods {
p := filepath.Join(dir, pod)
fmt.Printf("Installing pod %s\n", p)
if err := k8s.KubectlCreate(p); err != nil {
return err
}
}
for _, rc := range c.RCs {
p := filepath.Join(dir, rc)
fmt.Printf("Installing replication controller %s\n", p)
if err := k8s.KubectlCreate(p); err != nil {
return err
}
}
return nil
}
// Delete removes a component's pods and rcs.
//
// Pods are deleted first, then RCs.
func (c *Component) Delete(dir string) error {
for _, pod := range c.Pods {
p := filepath.Join(dir, pod)
fmt.Printf("Removing Pod %s\n", p)
if err := k8s.KubectlDelete(p); err != nil {
return err
}
}
for _, rc := range c.RCs {
p := filepath.Join(dir, rc)
fmt.Printf("Removing RC %s\n", p)
if err := k8s.KubectlDelete(p); err != nil {
return err
}
}
return nil
}
// InstallAll installs all the components in the given list.
//
// If optional is true, this will install packages marked optional. Otherwise,
// it will only install non-optional components.
func InstallAll(list []*Component, dir string, optional bool) error {
for _, item := range list {
if (optional && item.Optional) || !item.Optional {
if err := item.InstallPrereqs(dir); err != nil {
return err
}
}
}
for _, item := range list {
if (optional && item.Optional) || !item.Optional {
if err := item.Install(dir); err != nil {
return err
}
}
}
return nil
}
// DeleteAll removes the entire platform.
func DeleteAll(list []*Component, dir string) error {
var incomplete bool
for _, item := range list {
if err := item.Delete(dir); err != nil {
fmt.Printf("Error: %s", err)
incomplete = true
}
}
for _, item := range list {
if err := item.DeletePrereqs(dir); err != nil {
fmt.Printf("Error: %s", err)
incomplete = true
}
}
if incomplete {
return errors.New("Incomplete deletion")
}
return nil
}
// filterFunc takes data, a component , and storage and filters the data.
//
// When we import k8s files for rewriting, they are rewritten by filter
// functions. The function is expected to do its own decoding and encoding
// of the data, since these steps sometimes require non-generic information
// about the input.
//
// The returned result ought to be a []byte containing JSON-encoded data.
type filterFunc func([]byte, *Component, storage.Storer) ([]byte, error)
// filterMap maps a Component list type to a function for filtering.
var filterMap = map[string]filterFunc{
"RCs": rcFilter,
}
// rcFilter filters replication controller data.
//
// This will contain Deis-specific logic for replication controller config.
func rcFilter(data []byte, comp *Component, store storage.Storer) ([]byte, error) {
var rc *api.ReplicationController
if o, err := k8s.Decode(data); err != nil {
return data, err
} else {
rc = o.(*api.ReplicationController)
}
// This feels really hacky. I think we should be able to remove it
// at some point.
rc.APIVersion = "v1"
rc.Kind = "ReplicationController"
// Replace the image name with whatever config has been set for it.
kname := rc.Name
val, err := store.Get(kname, "image")
if err == nil && len(val) > 0 {
orig := rc.Spec.Template.Spec.Containers[0].Image
rc.Spec.Template.Spec.Containers[0].Image = val
fmt.Printf("===> Replacing %s with %s\n", orig, val)
}
return latest.Codec.Encode(rc)
}
// RebuildDefs reads a set of files, rebuilds, and writes them.
//
// This is a utility for taking the published definitions and rewriting them
// into customized definitions.
//
// To rebuild, this applies a finite set of hard-coded rules to transform
// a source file via configuration directives into a destination.
func RebuildDefs(src, dest string, comps []*Component, store storage.Storer) error {
rebuild := []string{"Services", "Namespaces", "Volumes", "Secrets", "RCs", "Pods"}
for _, comp := range comps {
cv := reflect.Indirect(reflect.ValueOf(comp))
for _, n := range rebuild {
fv := cv.FieldByName(n)
if fv.Len() > 0 {
for _, file := range fv.Interface().([]string) {
src := filepath.Join(src, file)
dest := filepath.Join(dest, file)
fmt.Printf("Rebuilding file %s to %s\n", src, dest)
// Ensure that the basedir exists in dest
os.MkdirAll(filepath.Dir(dest), 0755)
in, err := ioutil.ReadFile(src)
if err != nil {
return err
}
// If there is a filter function for this, run it through
// the filter function.
if fn, ok := filterMap[n]; ok {
fmt.Printf("Found filter func for %s\n", n)
in, err = fn(in, comp, store)
if err != nil {
return err
}
}
if err := ioutil.WriteFile(dest, in, 0755); err != nil {
return err
}
}
}
}
}
return nil
}