-
Notifications
You must be signed in to change notification settings - Fork 444
/
snapshot_writer.go
232 lines (214 loc) · 8.22 KB
/
snapshot_writer.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
228
229
230
231
232
package helpers
import (
"time"
errorsBuiltIn "errors"
"github.com/avast/retry-go"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/gloosnapshot"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/errors"
)
var _ SnapshotWriter = new(snapshotWriterImpl)
type SnapshotWriter interface {
WriteSnapshot(snapshot *gloosnapshot.ApiSnapshot, writeOptions clients.WriteOpts) error
DeleteSnapshot(snapshot *gloosnapshot.ApiSnapshot, deleteOptions clients.DeleteOpts) error
}
type snapshotWriterImpl struct {
ResourceClientSet
retryOptions []retry.Option
}
func NewSnapshotWriter(clientSet ResourceClientSet, retryOptions []retry.Option) *snapshotWriterImpl {
defaultRetryOptions := []retry.Option{
retry.Attempts(3),
retry.RetryIf(func(err error) bool {
return err != nil
}),
retry.LastErrorOnly(true),
retry.Delay(time.Second),
retry.DelayType(retry.BackOffDelay),
}
return &snapshotWriterImpl{
ResourceClientSet: clientSet,
retryOptions: append(defaultRetryOptions, retryOptions...),
}
}
// WriteSnapshot writes all resources in the ApiSnapshot to the cache
func (s snapshotWriterImpl) WriteSnapshot(snapshot *gloosnapshot.ApiSnapshot, writeOptions clients.WriteOpts) error {
return retry.Do(func() error {
if writeOptions.Ctx.Err() != nil {
// intentionally return early if context is already done
// this is a backoff loop; by the time we get here ctx may be done
return nil
}
return s.doWriteSnapshot(snapshot, writeOptions)
}, s.retryOptions...)
}
// WriteSnapshot writes all resources in the ApiSnapshot to the cache
func (s snapshotWriterImpl) doWriteSnapshot(snapshot *gloosnapshot.ApiSnapshot, writeOptions clients.WriteOpts) error {
// We intentionally create child resources first to avoid having the validation webhook reject
// the parent resource
for _, secret := range snapshot.Secrets {
secretClient := s.SecretClient()
if secretClient == nil {
return errorsBuiltIn.New("SecretClient on current snapshotWriterImpl is nil")
}
if _, writeErr := secretClient.Write(secret, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, artifact := range snapshot.Artifacts {
artifactClient := s.ArtifactClient()
if artifactClient == nil {
return errorsBuiltIn.New("ArtifactClient on current snapshotWriterImpl is nil")
}
if _, writeErr := artifactClient.Write(artifact, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, us := range snapshot.Upstreams {
if _, writeErr := s.UpstreamClient().Write(us, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, usGroup := range snapshot.UpstreamGroups {
if _, writeErr := s.UpstreamGroupClient().Write(usGroup, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, vhOpt := range snapshot.VirtualHostOptions {
if _, writeErr := s.VirtualHostOptionClient().Write(vhOpt, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, rtOpt := range snapshot.RouteOptions {
if _, writeErr := s.RouteOptionClient().Write(rtOpt, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, rlc := range snapshot.Ratelimitconfigs {
if _, writeErr := s.RateLimitConfigClient().Write(rlc, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, rt := range snapshot.RouteTables {
if _, writeErr := s.RouteTableClient().Write(rt, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, vs := range snapshot.VirtualServices {
if _, writeErr := s.VirtualServiceClient().Write(vs, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, hgw := range snapshot.HttpGateways {
if _, writeErr := s.HttpGatewayClient().Write(hgw, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, gw := range snapshot.Gateways {
if _, writeErr := s.GatewayClient().Write(gw, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
for _, proxy := range snapshot.Proxies {
if _, writeErr := s.ProxyClient().Write(proxy, writeOptions); !s.isContinuableWriteError(writeErr) {
return writeErr
}
}
return nil
}
func (s snapshotWriterImpl) isContinuableWriteError(writeError error) bool {
if writeError == nil {
return true
}
// When we apply a Snapshot, parents resources may fail due to child resources still being created
// To get around this we retry applying the entire snapshot, but some resources may already exist
return errors.IsExist(writeError)
}
// DeleteSnapshot deletes all resources in the ApiSnapshot from the cache
func (s snapshotWriterImpl) DeleteSnapshot(snapshot *gloosnapshot.ApiSnapshot, deleteOptions clients.DeleteOpts) error {
// We intentionally delete resources in the reverse order that we create resources
// If we delete child resources first, the validation webhook may reject the change
for _, gw := range snapshot.Gateways {
gwNamespace, gwName := gw.GetMetadata().Ref().Strings()
if deleteErr := s.GatewayClient().Delete(gwNamespace, gwName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, hgw := range snapshot.HttpGateways {
hgwNamespace, hgwName := hgw.GetMetadata().Ref().Strings()
if deleteErr := s.HttpGatewayClient().Delete(hgwNamespace, hgwName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, vs := range snapshot.VirtualServices {
vsNamespace, vsName := vs.GetMetadata().Ref().Strings()
if deleteErr := s.VirtualServiceClient().Delete(vsNamespace, vsName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, rt := range snapshot.RouteTables {
rtNamespace, rtName := rt.GetMetadata().Ref().Strings()
if deleteErr := s.RouteTableClient().Delete(rtNamespace, rtName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, rlc := range snapshot.Ratelimitconfigs {
rlcNamespace, rlcName := rlc.GetMetadata().Ref().Strings()
if deleteErr := s.RateLimitConfigClient().Delete(rlcNamespace, rlcName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, vhOpt := range snapshot.VirtualHostOptions {
vhOptNamespace, vhOptName := vhOpt.GetMetadata().Ref().Strings()
if deleteErr := s.VirtualHostOptionClient().Delete(vhOptNamespace, vhOptName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, rtOpt := range snapshot.RouteOptions {
rtOptNamespace, rtOptName := rtOpt.GetMetadata().Ref().Strings()
if deleteErr := s.RouteOptionClient().Delete(rtOptNamespace, rtOptName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, usGroup := range snapshot.UpstreamGroups {
usGroupNamespace, usGroupName := usGroup.GetMetadata().Ref().Strings()
if deleteErr := s.UpstreamGroupClient().Delete(usGroupNamespace, usGroupName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, us := range snapshot.Upstreams {
usNamespace, usName := us.GetMetadata().Ref().Strings()
if deleteErr := s.UpstreamClient().Delete(usNamespace, usName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, secret := range snapshot.Secrets {
secretNamespace, secretName := secret.GetMetadata().Ref().Strings()
secretClient := s.SecretClient()
if secretClient == nil {
return errorsBuiltIn.New("SecretClient on current snapshotWriterImpl is nil")
}
if deleteErr := secretClient.Delete(secretNamespace, secretName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
for _, artifact := range snapshot.Artifacts {
artifactNamespace, artifactName := artifact.GetMetadata().Ref().Strings()
artifactClient := s.ArtifactClient()
if artifactClient == nil {
return errorsBuiltIn.New("ArtifactClient on current snapshotWriterImpl is nil")
}
if deleteErr := artifactClient.Delete(artifactNamespace, artifactName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
// Proxies are auto generated by Gateway resources
// Therefore we delete Proxies after we have deleted the resources that may regenerate a Proxy
for _, proxy := range snapshot.Proxies {
proxyNamespace, proxyName := proxy.GetMetadata().Ref().Strings()
if deleteErr := s.ProxyClient().Delete(proxyNamespace, proxyName, deleteOptions); deleteErr != nil {
return deleteErr
}
}
return nil
}