forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
142 lines (121 loc) · 3.24 KB
/
context.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
package fi
import (
"fmt"
"github.com/golang/glog"
"io/ioutil"
"k8s.io/kops/util/pkg/vfs"
"os"
"reflect"
"strings"
)
type Context struct {
Tmpdir string
Target Target
Cloud Cloud
CAStore CAStore
SecretStore SecretStore
ClusterConfigBase vfs.Path
CheckExisting bool
tasks map[string]Task
}
func NewContext(target Target, cloud Cloud, castore CAStore, secretStore SecretStore, clusterConfigBase vfs.Path, checkExisting bool, tasks map[string]Task) (*Context, error) {
c := &Context{
Cloud: cloud,
Target: target,
CAStore: castore,
SecretStore: secretStore,
ClusterConfigBase: clusterConfigBase,
CheckExisting: checkExisting,
tasks: tasks,
}
t, err := ioutil.TempDir("", "deploy")
if err != nil {
return nil, fmt.Errorf("error creating temporary directory: %v", err)
}
c.Tmpdir = t
return c, nil
}
func (c *Context) AllTasks() map[string]Task {
return c.tasks
}
func (c *Context) RunTasks(maxAttemptsWithNoProgress int) error {
e := &executor{
context: c,
}
return e.RunTasks(c.tasks, maxAttemptsWithNoProgress)
}
func (c *Context) Close() {
glog.V(2).Infof("deleting temp dir: %q", c.Tmpdir)
if c.Tmpdir != "" {
err := os.RemoveAll(c.Tmpdir)
if err != nil {
glog.Warningf("unable to delete temporary directory %q: %v", c.Tmpdir, err)
}
}
}
//func (c *Context) MergeOptions(options Options) error {
// return c.Options.Merge(options)
//}
func (c *Context) NewTempDir(prefix string) (string, error) {
t, err := ioutil.TempDir(c.Tmpdir, prefix)
if err != nil {
return "", fmt.Errorf("error creating temporary directory: %v", err)
}
return t, nil
}
var typeContextPtr = reflect.TypeOf((*Context)(nil))
func (c *Context) Render(a, e, changes Task) error {
if _, ok := c.Target.(*DryRunTarget); ok {
return c.Target.(*DryRunTarget).Render(a, e, changes)
}
v := reflect.ValueOf(e)
vType := v.Type()
targetType := reflect.ValueOf(c.Target).Type()
var renderer *reflect.Method
var rendererArgs []reflect.Value
for i := 0; i < vType.NumMethod(); i++ {
method := vType.Method(i)
if !strings.HasPrefix(method.Name, "Render") {
continue
}
match := true
var args []reflect.Value
for j := 0; j < method.Type.NumIn(); j++ {
arg := method.Type.In(j)
if arg.ConvertibleTo(vType) {
continue
}
if arg.ConvertibleTo(typeContextPtr) {
args = append(args, reflect.ValueOf(c))
continue
}
if arg.ConvertibleTo(targetType) {
args = append(args, reflect.ValueOf(c.Target))
continue
}
match = false
break
}
if match {
if renderer != nil {
return fmt.Errorf("Found multiple Render methods that could be invokved on %T", e)
}
renderer = &method
rendererArgs = args
}
}
if renderer == nil {
return fmt.Errorf("Could not find Render method on type %T (target %T)", e, c.Target)
}
rendererArgs = append(rendererArgs, reflect.ValueOf(a))
rendererArgs = append(rendererArgs, reflect.ValueOf(e))
rendererArgs = append(rendererArgs, reflect.ValueOf(changes))
glog.V(11).Infof("Calling method %s on %T", renderer.Name, e)
m := v.MethodByName(renderer.Name)
rv := m.Call(rendererArgs)
var rvErr error
if !rv[0].IsNil() {
rvErr = rv[0].Interface().(error)
}
return rvErr
}