-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplateController.go
More file actions
156 lines (134 loc) · 4.04 KB
/
templateController.go
File metadata and controls
156 lines (134 loc) · 4.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
package main
import (
"log"
)
/*
* The template controller section, this doesn't actually do any template stuff.
* Its only purpose is to manage the complexities of the comms and children.
*/
type templateControllerType struct {
name string
value string
event string
rFchan chan templateControllerType
}
type templateControllerComms struct {
send chan templateControllerType
recv chan templateControllerType
}
func templateControllerHelper(kc kubeCli, parent templateControllerComms, parentId string, name string) {
id := get_myID(parentId, "(TC)")
log.Printf("[%s] New TC\n", id)
childMessage := make(chan templateRendererType, 100)
templates := make(map[string]templateRendererControllerComms)
unUsed := make(map[string]bool)
for {
select {
case recv := <-childMessage:
// yes, zero this is just to notify and shit.
recv = templateRendererType{}
log.Printf("[%s] Received message from child [%s].\n", id, recv)
var mesg templateControllerType
parent.send <- mesg
case recv := <-parent.recv:
switch recv.event {
case "render":
log.Printf("[%s] Received message from parent.\n", id)
if _, ok := templates[recv.name]; !ok {
templates[recv.name] = templateRendererCacheConstructor(kc, id, recv.name, childMessage)
}
var ret templateControllerType
ret.value = templates[recv.name].Render(recv.name, recv.value)
//log.Printf("[%s] TCH[%s]\n", id, ret)
recv.rFchan <- ret
unUsed[recv.name] = false
case "setUnUsed":
log.Printf("[%s]: Updating unused.\n", id)
for item := range unUsed {
unUsed[item] = true
}
recv.rFchan <- recv
case "destroyUnUsed":
log.Printf("[%s]: (1643)Receive destroy unused.\n", id)
for item := range unUsed {
log.Printf("[%s]: Check [%s].\n", id, item)
if unUsed[item] {
log.Printf("[%s]: Sending destroy [%s].\n", id, item)
templates[item].Destroy()
delete(templates, item)
delete(unUsed, item)
}
}
log.Printf("[%s]: Finished Destroy unused.\n", id)
recv.rFchan <- recv
case "destroy":
log.Printf("[%s]: Destroying Children.\n", id)
for item := range unUsed {
templates[item].Destroy()
delete(templates, item)
delete(unUsed, item)
}
log.Printf("[%s]: Finished destroying Children.\n", id)
recv.rFchan <- recv
return
default:
log.Printf("[%s]: Unknown Message [%s].\n", id, recv)
}
}
}
}
func (t templateControllerComms) Render(name string, value string) string {
var mesg templateControllerType
mesg.name = name
mesg.value = value
mesg.event = "render"
mesg.rFchan = make(chan templateControllerType, 100)
t.send <- mesg
response := <-mesg.rFchan
return response.value
}
func (t templateControllerComms) SetUnUsed() string {
var mesg templateControllerType
mesg.event = "setUnUsed"
mesg.rFchan = make(chan templateControllerType, 100)
t.send <- mesg
response := <-mesg.rFchan
return response.event
}
func (t templateControllerComms) DestroyUnUsed() string {
var mesg templateControllerType
log.Printf("[%s]: Sending destroy unused[%s].\n", t)
mesg.event = "destroyUnUsed"
mesg.rFchan = make(chan templateControllerType, 100)
t.send <- mesg
response := <-mesg.rFchan
return response.event
}
func (t templateControllerComms) Destroy() string {
/*
type templateControllerType struct {
name string
value string
event string
rFchan chan templateControllerType
}
*/
log.Printf("[%s]: Sending destroy [%s].\n", t)
var mesg templateControllerType
mesg.event = "destroy"
mesg.rFchan = make(chan templateControllerType, 100)
t.send <- mesg
response := <-mesg.rFchan
return response.event
}
func NewTemplateController(kc kubeCli, parentId string, name string) templateControllerComms {
var child templateControllerComms
var parent templateControllerComms
child.send = make(chan templateControllerType, 100)
child.recv = make(chan templateControllerType, 100)
parent.send = child.recv
parent.recv = child.send
go templateControllerHelper(kc, child, parentId, name)
return parent
}
//