/
resources.go
156 lines (140 loc) · 4.22 KB
/
resources.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package workflow
import (
"fmt"
"sync"
)
type resource struct {
real, link string
noCleanup, deleted bool
creator, deleter *Step
users []*Step
}
type resourceMap struct {
typeName string
m map[string]*resource
mx sync.Mutex
usageRegistrationHook func(name string, s *Step) error
}
func (rm *resourceMap) add(name string, r *resource) {
rm.mx.Lock()
defer rm.mx.Unlock()
if rm.m == nil {
rm.m = map[string]*resource{}
}
rm.m[name] = r
}
func (rm *resourceMap) del(name string) {
rm.mx.Lock()
defer rm.mx.Unlock()
delete(rm.m, name)
}
func (rm *resourceMap) get(name string) (*resource, bool) {
rm.mx.Lock()
defer rm.mx.Unlock()
r, ok := rm.m[name]
return r, ok
}
func (rm *resourceMap) registerCreation(name string, r *resource, s *Step) error {
rm.mx.Lock()
defer rm.mx.Unlock()
// Check: don't create a dupe resource.
if rm.m == nil {
rm.m = map[string]*resource{}
} else if r, ok := rm.m[name]; ok {
return fmt.Errorf("cannot create %s %q; already created by step %q", rm.typeName, name, r.creator.name)
}
r.creator = s
rm.m[name] = r
return nil
}
func (rm *resourceMap) registerDeletion(name string, s *Step) error {
rm.mx.Lock()
defer rm.mx.Unlock()
// Check: don't dupe deletion of a resource.
// Check: delete depends on ALL registered usages and creation of resource.
r, ok := rm.m[name]
if ok && r.creator != nil {
if r.deleter != nil {
return fmt.Errorf("cannot delete %s %q: already deleted by step %q", rm.typeName, name, r.deleter.name)
}
for _, u := range append(r.users, r.creator) {
if !s.depends(u) {
return fmt.Errorf("deleting %s %q MUST transitively depend on step %q which references %q", rm.typeName, name, u.name, name)
}
}
} else {
return fmt.Errorf("missing reference for %s %q", rm.typeName, name)
}
r.deleter = s
return nil
}
func (rm *resourceMap) registerUsage(name string, s *Step) error {
rm.mx.Lock()
defer rm.mx.Unlock()
// Check: usage depends on creation of resource.
// Check: there shouldn't be a deleter yet, usage occurs before deletion.
r, ok := rm.m[name]
if ok && r.creator != nil {
if !s.depends(r.creator) {
return fmt.Errorf("using %s %q MUST transitively depend on step %q which creates %q", rm.typeName, name, r.creator.name, name)
}
if r.deleter != nil {
return fmt.Errorf("using %s %q; step %q deletes %q and MUST transitively depend on this step", rm.typeName, name, r.deleter.name, name)
}
if err := rm.usageRegistrationHook(name, s); err != nil {
return err
}
} else {
return fmt.Errorf("missing reference for %s %q", rm.typeName, name)
}
rm.m[name].users = append(rm.m[name].users, s)
return nil
}
func initWorkflowResources(w *Workflow) {
initDisksMap(w)
initImagesMap(w)
initInstancesMap(w)
w.addCleanupHook(resourceCleanupHook(w))
}
func shareWorkflowResources(giver, taker *Workflow) {
disks[taker] = disks[giver]
images[taker] = images[giver]
instances[taker] = instances[giver]
}
func resourceCleanupHook(w *Workflow) func() error {
return func() error {
resourceCleanupHelper(images[w], func(r *resource) error { return deleteImage(w, r) })
resourceCleanupHelper(instances[w], func(r *resource) error { return deleteInstance(w, r) })
resourceCleanupHelper(disks[w], func(r *resource) error { return deleteDisk(w, r) })
return nil
}
}
func resourceCleanupHelper(rm *resourceMap, deleteFn func(*resource) error) {
var wg sync.WaitGroup
for name, r := range rm.m {
if r.noCleanup || r.deleted {
continue
}
wg.Add(1)
go func(ref string, res *resource) {
defer wg.Done()
if err := deleteFn(res); err != nil {
fmt.Println(err)
}
}(name, r)
}
wg.Wait()
}