This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
targetproxysyncer.go
303 lines (281 loc) · 12.2 KB
/
targetproxysyncer.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2017 Google Inc.
//
// 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 targetproxy
import (
"fmt"
"net/http"
"reflect"
"github.com/golang/glog"
multierror "github.com/hashicorp/go-multierror"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"k8s.io/apimachinery/pkg/util/diff"
ingresslb "k8s.io/ingress-gce/pkg/loadbalancers"
"k8s.io/ingress-gce/pkg/utils"
utilsnamer "github.com/GoogleCloudPlatform/k8s-multicluster-ingress/app/kubemci/pkg/gcp/namer"
)
// Syncer manages GCP target proxies for multicluster GCP L7 load balancers.
type Syncer struct {
namer *utilsnamer.Namer
// Target proxies provider to call GCP APIs to manipulate target proxies.
tpp ingresslb.LoadBalancers
}
// NewTargetProxySyncer returns a new instance of syncer.
func NewTargetProxySyncer(namer *utilsnamer.Namer, tpp ingresslb.LoadBalancers) SyncerInterface {
return &Syncer{
namer: namer,
tpp: tpp,
}
}
// Ensure this implements SyncerInterface.
var _ SyncerInterface = &Syncer{}
// EnsureHTTPTargetProxy ensures that the required http target proxy exists for the given url map.
// Does nothing if it exists already, else creates a new one.
func (s *Syncer) EnsureHTTPTargetProxy(lbName, umLink string, forceUpdate bool) (string, error) {
fmt.Println("Ensuring http target proxy.")
var err error
tpLink, httpProxyErr := s.ensureHTTPProxy(lbName, umLink, forceUpdate)
if httpProxyErr != nil {
httpProxyErr = fmt.Errorf("Error in ensuring http target proxy: %s", httpProxyErr)
err = multierror.Append(err, httpProxyErr)
}
return tpLink, err
}
// EnsureHTTPSTargetProxy ensures that the required https target proxy exists for the given url map.
// Does nothing if it exists already, else creates a new one.
func (s *Syncer) EnsureHTTPSTargetProxy(lbName, umLink, certLink string, forceUpdate bool) (string, error) {
fmt.Println("Ensuring http target proxy.")
var err error
tpLink, proxyErr := s.ensureHTTPSProxy(lbName, umLink, certLink, forceUpdate)
if proxyErr != nil {
proxyErr = fmt.Errorf("Error in ensuring https target proxy: %s", proxyErr)
err = multierror.Append(err, proxyErr)
}
return tpLink, err
}
// DeleteTargetProxies deletes the target proxies that EnsureHTTPTargetProxy and EnsureHTTPSTargetProxy would have created.
// See interface comments for details.
func (s *Syncer) DeleteTargetProxies() error {
var err error
httpName := s.namer.TargetHTTPProxyName()
fmt.Println("Deleting target HTTP proxy", httpName)
httpErr := s.tpp.DeleteTargetHttpProxy(httpName)
if httpErr != nil {
if utils.IsHTTPErrorCode(httpErr, http.StatusNotFound) {
fmt.Println("Target HTTP proxy", httpName, "does not exist. Nothing to delete")
} else {
httpErr = fmt.Errorf("error in deleting target HTTP proxy %s: %s", httpName, httpErr)
fmt.Println(httpErr)
err = multierror.Append(err, httpErr)
}
} else {
fmt.Println("Target HTTP proxy", httpName, "deleted successfully")
}
httpsName := s.namer.TargetHTTPSProxyName()
fmt.Println("Deleting target HTTPS proxy", httpsName)
httpsErr := s.tpp.DeleteTargetHttpsProxy(httpsName)
if httpsErr != nil {
if utils.IsHTTPErrorCode(httpsErr, http.StatusNotFound) {
fmt.Println("Target HTTPS proxy", httpsName, "does not exist. Nothing to delete")
} else {
httpsErr = fmt.Errorf("error in deleting target HTTPS proxy %s: %s", httpsName, httpsErr)
fmt.Println(httpsErr)
err = multierror.Append(err, httpsErr)
}
} else {
fmt.Println("Target HTTPS proxy", httpsName, "deleted successfully")
}
return err
}
// ensureHTTPProxy ensures that the required target proxy exists for the given port.
// Does nothing if it exists already, else creates a new one.
// Returns the self link for the ensured http proxy.
func (s *Syncer) ensureHTTPProxy(lbName, umLink string, forceUpdate bool) (string, error) {
fmt.Println("Ensuring target http proxy. UrlMap:", umLink)
desiredHTTPProxy := s.desiredHTTPTargetProxy(lbName, umLink)
glog.Infof("desiredHTTPTargetProxy:%+v", desiredHTTPProxy)
name := desiredHTTPProxy.Name
// Check if target proxy already exists.
existingHTTPProxy, err := s.tpp.GetTargetHttpProxy(name)
if err == nil {
fmt.Println("Target HTTP proxy", name, "exists already. Checking if it matches our desired target proxy")
glog.V(5).Infof("Existing target HTTP proxy: %+v", existingHTTPProxy)
// Target proxy with that name exists already. Check if it matches what we want.
if targetHTTPProxyMatches(*desiredHTTPProxy, *existingHTTPProxy) {
// Nothing to do. Desired target proxy exists already.
fmt.Println("Desired target HTTP proxy", name, "exists already.")
return existingHTTPProxy.SelfLink, nil
}
if forceUpdate {
return s.updateHTTPTargetProxy(desiredHTTPProxy)
}
fmt.Println("Will not overwrite this differing Target HTTP Proxy without the --force flag")
return "", fmt.Errorf("Will not overwrite Target HTTP Proxy without --force")
}
glog.V(5).Infof("Got error %s while trying to get existing target HTTP proxy %s", err, name)
// TODO(nikhiljindal): Handle non NotFound errors. We should create only if the error is NotFound.
// Create the target proxy.
fmt.Println("Creating target HTTP proxy", name)
glog.V(5).Infof("Creating target HTTP proxy %v", *desiredHTTPProxy)
return s.createHTTPTargetProxy(desiredHTTPProxy)
}
func (s *Syncer) updateHTTPTargetProxy(desiredHTTPProxy *compute.TargetHttpProxy) (string, error) {
name := desiredHTTPProxy.Name
fmt.Println("Updating existing target http proxy", name, "to match the desired state")
// There is no UpdateTargetHTTPProxy method.
// Apart from name, UrlMap is the only field that can be different. We update that field directly.
// TODO(nikhiljindal): Handle description field differences:
// https://github.com/GoogleCloudPlatform/k8s-multicluster-ingress/issues/94.
urlMap := &compute.UrlMap{SelfLink: desiredHTTPProxy.UrlMap}
glog.Infof("Setting URL Map to:%+v", urlMap)
err := s.tpp.SetUrlMapForTargetHttpProxy(desiredHTTPProxy, urlMap)
if err != nil {
fmt.Println("Error setting URL Map:", err)
return "", err
}
fmt.Println("Target http proxy", name, "updated successfully")
existing, err := s.tpp.GetTargetHttpProxy(name)
if err != nil {
fmt.Println("Error getting target HTTP Proxy:", err)
return "", err
}
return existing.SelfLink, nil
}
func (s *Syncer) createHTTPTargetProxy(desiredHTTPProxy *compute.TargetHttpProxy) (string, error) {
name := desiredHTTPProxy.Name
fmt.Println("Creating target http proxy", name)
glog.V(5).Infof("Creating target http proxy %v", desiredHTTPProxy)
err := s.tpp.CreateTargetHttpProxy(desiredHTTPProxy)
if err != nil {
return "", err
}
fmt.Println("Target http proxy", name, "created successfully")
existing, err := s.tpp.GetTargetHttpProxy(name)
if err != nil {
return "", err
}
return existing.SelfLink, nil
}
func targetHTTPProxyMatches(desiredHTTPProxy, existingHTTPProxy compute.TargetHttpProxy) bool {
existingHTTPProxy.CreationTimestamp = ""
existingHTTPProxy.Id = 0
existingHTTPProxy.Kind = ""
existingHTTPProxy.SelfLink = ""
existingHTTPProxy.ServerResponse = googleapi.ServerResponse{}
equal := reflect.DeepEqual(existingHTTPProxy, desiredHTTPProxy)
if !equal {
glog.V(2).Infof("TargetHTTPProxies differ.")
glog.V(0).Infof("Diff: %v", diff.ObjectDiff(desiredHTTPProxy, existingHTTPProxy))
} else {
glog.V(2).Infof("TargetHTTPProxies match.")
}
return equal
}
func (s *Syncer) desiredHTTPTargetProxy(lbName, umLink string) *compute.TargetHttpProxy {
// Compute the desired target http proxy.
return &compute.TargetHttpProxy{
Name: s.namer.TargetHTTPProxyName(),
Description: fmt.Sprintf("Target http proxy for kubernetes multicluster loadbalancer %s", lbName),
UrlMap: umLink,
}
}
// ensureHTTPSProxy ensures that the required target proxy exists for the given port.
// Does nothing if it exists already, else creates a new one.
// Returns the self link for the ensured https proxy.
func (s *Syncer) ensureHTTPSProxy(lbName, umLink, certLink string, forceUpdate bool) (string, error) {
fmt.Println("Ensuring target https proxy")
desiredHTTPSProxy := s.desiredHTTPSTargetProxy(lbName, umLink, certLink)
name := desiredHTTPSProxy.Name
// Check if target proxy already exists.
existingHTTPSProxy, err := s.tpp.GetTargetHttpsProxy(name)
if err == nil {
fmt.Println("Target HTTPS proxy", name, "exists already. Checking if it matches our desired target proxy")
glog.V(5).Infof("Existing target HTTPS proxy: %+v", existingHTTPSProxy)
// Target proxy with that name exists already. Check if it matches what we want.
if targetHTTPSProxyMatches(*desiredHTTPSProxy, *existingHTTPSProxy) {
// Nothing to do. Desired target proxy exists already.
fmt.Println("Desired target HTTPS proxy", name, "exists already.")
return existingHTTPSProxy.SelfLink, nil
}
if forceUpdate {
fmt.Println("Updating existing target HTTPS proxy", name, "to match the desired state")
return s.updateHTTPSTargetProxy(desiredHTTPSProxy)
}
fmt.Println("Will not overwrite this differing Target HTTPS Proxy without the --force flag")
return "", fmt.Errorf("Will not overwrite Target HTTPS Proxy without --force")
}
glog.V(5).Infof("Got error %s while trying to get existing target HTTPS proxy %s", err, name)
// TODO(nikhiljindal): Handle non NotFound errors. We should create only if the error is NotFound.
// Create the target proxy.
fmt.Println("Creating target HTTPS proxy", name)
glog.V(5).Infof("Creating target HTTPS proxy %v", *desiredHTTPSProxy)
return s.createHTTPSTargetProxy(desiredHTTPSProxy)
}
func (s *Syncer) updateHTTPSTargetProxy(desiredHTTPSProxy *compute.TargetHttpsProxy) (string, error) {
name := desiredHTTPSProxy.Name
fmt.Println("Updating existing target https proxy", name, "to match the desired state")
// There is no UpdateTargetHTTPSProxy method.
// Apart from name, UrlMap is the only field that can be different. We update that field directly.
// TODO(nikhiljindal): Handle description field differences:
// https://github.com/GoogleCloudPlatform/k8s-multicluster-ingress/issues/94.
err := s.tpp.SetUrlMapForTargetHttpsProxy(desiredHTTPSProxy, &compute.UrlMap{SelfLink: desiredHTTPSProxy.UrlMap})
if err != nil {
return "", err
}
fmt.Println("Target https proxy", name, "updated successfully")
existing, err := s.tpp.GetTargetHttpsProxy(name)
if err != nil {
return "", err
}
return existing.SelfLink, nil
}
func (s *Syncer) createHTTPSTargetProxy(desiredHTTPSProxy *compute.TargetHttpsProxy) (string, error) {
name := desiredHTTPSProxy.Name
fmt.Println("Creating target https proxy", name)
glog.V(5).Infof("Creating target https proxy %v", desiredHTTPSProxy)
err := s.tpp.CreateTargetHttpsProxy(desiredHTTPSProxy)
if err != nil {
return "", err
}
fmt.Println("Target https proxy", name, "created successfully")
existing, err := s.tpp.GetTargetHttpsProxy(name)
if err != nil {
return "", err
}
return existing.SelfLink, nil
}
func targetHTTPSProxyMatches(desiredHTTPSProxy, existingHTTPSProxy compute.TargetHttpsProxy) bool {
existingHTTPSProxy.CreationTimestamp = ""
existingHTTPSProxy.Id = 0
existingHTTPSProxy.Kind = ""
existingHTTPSProxy.SelfLink = ""
existingHTTPSProxy.ServerResponse = googleapi.ServerResponse{}
equal := reflect.DeepEqual(existingHTTPSProxy, desiredHTTPSProxy)
if !equal {
glog.V(2).Infof("TargetHTTPSProxies differ.")
glog.V(3).Infof("Diff: %v", diff.ObjectDiff(desiredHTTPSProxy, existingHTTPSProxy))
} else {
glog.V(2).Infof("TargetHTTPSProxies match.")
}
return equal
}
func (s *Syncer) desiredHTTPSTargetProxy(lbName, umLink, certLink string) *compute.TargetHttpsProxy {
// Compute the desired target https proxy.
return &compute.TargetHttpsProxy{
Name: s.namer.TargetHTTPSProxyName(),
Description: fmt.Sprintf("Target https proxy for kubernetes multicluster loadbalancer %s", lbName),
UrlMap: umLink,
SslCertificates: []string{certLink},
}
}