forked from openshift/external-dns-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
375 lines (338 loc) · 11.9 KB
/
util.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//go:build e2e
// +build e2e
package e2e
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"math/rand"
"net/http"
"reflect"
"testing"
"time"
ibclient "github.com/infobloxopen/infoblox-go-client"
"github.com/miekg/dns"
"sigs.k8s.io/controller-runtime/pkg/client"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/utils/ptr"
operatorv1alpha1 "github.com/openshift/external-dns-operator/api/v1alpha1"
operatorv1beta1 "github.com/openshift/external-dns-operator/api/v1beta1"
"github.com/openshift/external-dns-operator/pkg/utils"
)
const (
infobloxDNSProvider = "INFOBLOX"
)
type providerTestHelper interface {
ensureHostedZone(string) (string, []string, error)
deleteHostedZone(string, string) error
platform() string
makeCredentialsSecret(namespace string) *corev1.Secret
buildExternalDNS(name, zoneID, zoneDomain string, credsSecret *corev1.Secret) operatorv1beta1.ExternalDNS
buildOpenShiftExternalDNS(name, zoneID, zoneDomain, routeName string, credsSecret *corev1.Secret) operatorv1beta1.ExternalDNS
buildOpenShiftExternalDNSV1Alpha1(name, zoneID, zoneDomain, routeName string, credsSecret *corev1.Secret) operatorv1alpha1.ExternalDNS
}
func randomString(n int) string {
var chars = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
str := make([]rune, n)
for i := range str {
str[i] = chars[rand.Intn(len(chars))]
}
return string(str)
}
func testRoute(name, namespace, host, svcName string) *routev1.Route {
return &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Labels: map[string]string{
"external-dns.mydomain.org/publish": "yes",
},
},
Spec: routev1.RouteSpec{
Host: host,
To: routev1.RouteTargetReference{
Name: svcName,
},
},
}
}
func deploymentConditionMap(conditions ...appsv1.DeploymentCondition) map[string]string {
conds := map[string]string{}
for _, cond := range conditions {
conds[string(cond.Type)] = string(cond.Status)
}
return conds
}
func waitForOperatorDeploymentStatusCondition(ctx context.Context, t *testing.T, cl client.Client, conditions ...appsv1.DeploymentCondition) error {
t.Helper()
return wait.PollUntilContextTimeout(ctx, 2*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
dep := &appsv1.Deployment{}
depNamespacedName := types.NamespacedName{
Name: "external-dns-operator",
Namespace: "external-dns-operator",
}
if err := cl.Get(ctx, depNamespacedName, dep); err != nil {
t.Logf("failed to get deployment %s: %v", depNamespacedName.Name, err)
return false, nil
}
expected := deploymentConditionMap(conditions...)
current := deploymentConditionMap(dep.Status.Conditions...)
return conditionsMatchExpected(expected, current), nil
})
}
func conditionsMatchExpected(expected, actual map[string]string) bool {
filtered := map[string]string{}
for k := range actual {
if _, comparable := expected[k]; comparable {
filtered[k] = actual[k]
}
}
return reflect.DeepEqual(expected, filtered)
}
func defaultExternalDNS(name, zoneID, zoneDomain string) operatorv1beta1.ExternalDNS {
return operatorv1beta1.ExternalDNS{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: operatorv1beta1.ExternalDNSSpec{
Zones: []string{zoneID},
Source: operatorv1beta1.ExternalDNSSource{
ExternalDNSSourceUnion: operatorv1beta1.ExternalDNSSourceUnion{
Type: operatorv1beta1.SourceTypeService,
Service: &operatorv1beta1.ExternalDNSServiceSourceOptions{
ServiceType: []corev1.ServiceType{
corev1.ServiceTypeLoadBalancer,
corev1.ServiceTypeClusterIP,
},
},
LabelFilter: utils.MustParseLabelSelector("external-dns.mydomain.org/publish=yes"),
},
HostnameAnnotationPolicy: "Ignore",
FQDNTemplate: []string{fmt.Sprintf("{{.Name}}.%s", zoneDomain)},
},
},
}
}
func routeExternalDNS(name, zoneID, zoneDomain, routerName string) operatorv1beta1.ExternalDNS {
extDns := operatorv1beta1.ExternalDNS{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: operatorv1beta1.ExternalDNSSpec{
Zones: []string{zoneID},
Source: operatorv1beta1.ExternalDNSSource{
ExternalDNSSourceUnion: operatorv1beta1.ExternalDNSSourceUnion{
Type: operatorv1beta1.SourceTypeRoute,
LabelFilter: utils.MustParseLabelSelector("external-dns.mydomain.org/publish=yes"),
},
HostnameAnnotationPolicy: operatorv1beta1.HostnameAnnotationPolicyIgnore,
},
},
}
// this additional check can be removed with latest external-dns image (>v0.10.1)
// instantiate the route additional information at ExternalDNS initiation level.
if routerName != "" {
extDns.Spec.Source.ExternalDNSSourceUnion.OpenShiftRoute = &operatorv1beta1.ExternalDNSOpenShiftRouteOptions{
RouterName: routerName,
}
}
return extDns
}
func routeExternalDNSV1Alpha1(name, zoneID, zoneDomain, routerName string) operatorv1alpha1.ExternalDNS {
extDns := operatorv1alpha1.ExternalDNS{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: operatorv1alpha1.ExternalDNSSpec{
Zones: []string{zoneID},
Source: operatorv1alpha1.ExternalDNSSource{
ExternalDNSSourceUnion: operatorv1alpha1.ExternalDNSSourceUnion{
Type: operatorv1alpha1.SourceTypeRoute,
LabelFilter: utils.MustParseLabelSelector("external-dns.mydomain.org/publish=yes"),
},
HostnameAnnotationPolicy: operatorv1alpha1.HostnameAnnotationPolicyIgnore,
},
},
}
// this additional check can be removed with latest external-dns image (>v0.10.1)
// instantiate the route additional information at ExternalDNS initiation level.
if routerName != "" {
extDns.Spec.Source.ExternalDNSSourceUnion.OpenShiftRoute = &operatorv1alpha1.ExternalDNSOpenShiftRouteOptions{
RouterName: routerName,
}
}
return extDns
}
// lookupCNAME retrieves the first canonical name of the given host.
// This function is different from net.LookupCNAME.
// net.LookupCNAME assumes the nameserver used is the recursive resolver (https://github.com/golang/go/blob/master/src/net/dnsclient_unix.go#L637).
// Therefore CNAME is tried to be resolved to its last canonical name, the quote from doc:
// "A canonical name is the final name after following zero or more CNAME records."
// This may be a problem if the default nameserver (from host /etc/resolv.conf, default lookup order is files,dns)
// is replaced (custom net.Resolver with overridden Dial function) with not recursive resolver
// and the other CNAMEs down to the last one are not known to this replaced nameserver.
// This may result in "no such host" error.
func lookupCNAME(host, server string) (string, error) {
dnsClient := dns.Client{}
message := &dns.Msg{}
message.SetQuestion(dns.Fqdn(host), dns.TypeCNAME)
r, _, err := dnsClient.Exchange(message, fmt.Sprintf("%s:53", server))
if err != nil {
return "", err
}
if len(r.Answer) == 0 {
return "", fmt.Errorf("not found")
}
cname, ok := r.Answer[0].(*dns.CNAME)
if !ok {
return "", fmt.Errorf("not a CNAME record")
}
return cname.Target, nil
}
func equalFQDN(name1, name2 string) bool {
return dns.Fqdn(name1) == dns.Fqdn(name2)
}
func newHostNetworkController(name types.NamespacedName, domain string) *operatorv1.IngressController {
return &operatorv1.IngressController{
ObjectMeta: metav1.ObjectMeta{
Namespace: name.Namespace,
Name: name.Name,
},
Spec: operatorv1.IngressControllerSpec{
Domain: domain,
Replicas: ptr.To[int32](1),
EndpointPublishingStrategy: &operatorv1.EndpointPublishingStrategy{
Type: operatorv1.HostNetworkStrategyType,
},
},
}
}
// enhancedIBClient provides enhancements not implemented in Infoblox golang client.
// https://pkg.go.dev/github.com/infobloxopen/infoblox-go-client
type enhancedIBClient struct {
*ibclient.Connector
httpClient *http.Client
}
func newEnhancedIBClient(hostConfig ibclient.HostConfig, transportConfig ibclient.TransportConfig) (*enhancedIBClient, error) {
ibcli, err := ibclient.NewConnector(hostConfig, transportConfig, &ibclient.WapiRequestBuilder{}, &ibclient.WapiHttpRequestor{})
if err != nil {
return nil, err
}
return &enhancedIBClient{
Connector: ibcli,
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !transportConfig.SslVerify,
},
},
},
}, nil
}
// addNameServer uses NIOS REST API to add the Grid host as the nameserver for the given DNS zone.
// Infoblox golang client has the interface for creating DNS zones.
// However these zones don't have NS/SOA records added by default.
func (c *enhancedIBClient) addNameServer(zoneRef, nameserver string) error {
payload := fmt.Sprintf(`{"grid_primary":[{"name":"%s"}]}`, nameserver)
qparams := map[string]string{
"_return_fields+": "fqdn,grid_primary",
"_return_as_object": "1",
}
_, err := c.doHTTPRequest(context.TODO(), "PUT", "https://"+c.HostConfig.Host+"/wapi/v"+c.HostConfig.Version+"/"+zoneRef, qparams, []byte(payload))
return err
}
// restartServices uses NIOS REST API to restart all Grid services.
// Some configurations don't take effect until the corresponding service is not restarted,
// see the doc: https://docs.infoblox.com/display/nios85/Configurations+Requiring+Service+Restart
func (c *enhancedIBClient) restartServices() error {
respJSON, err := c.doHTTPRequest(context.TODO(), "GET", "https://"+c.HostConfig.Host+"/wapi/v"+c.HostConfig.Version+"/grid", nil, nil)
if err != nil {
return err
}
type Ref struct {
Ref string `json:"_ref"`
}
resp := &[]Ref{}
if err = json.Unmarshal(respJSON, resp); err != nil {
return err
}
payload := `{"member_order" : "SIMULTANEOUSLY","service_option": "ALL"}`
qparams := map[string]string{
"_function": "restartservices",
}
_, err = c.doHTTPRequest(context.TODO(), "POST", "https://"+c.HostConfig.Host+"/wapi/v"+c.HostConfig.Version+"/"+(*resp)[0].Ref, qparams, []byte(payload))
return err
}
func (c *enhancedIBClient) doHTTPRequest(ctx context.Context, method, url string, queryParams map[string]string, body []byte) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("failed to create http request: %s", err)
}
// set query parameters
query := req.URL.Query()
for k, v := range queryParams {
query.Add(k, v)
}
req.URL.RawQuery = query.Encode()
// set headers
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(c.HostConfig.Username, c.HostConfig.Password)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed: %s", err)
}
defer resp.Body.Close()
// 2xx
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("failure http status code received: %d (%s)", resp.StatusCode, http.StatusText(resp.StatusCode))
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read http response body: %s", err)
}
return respBody, nil
}
// readServerTLSCert returns PEM encoded TLS certificate of the given server.
func readServerTLSCert(addr string, selfSigned bool) ([]byte, error) {
tlsConf := &tls.Config{
InsecureSkipVerify: selfSigned,
}
conn, err := tls.Dial("tcp", addr, tlsConf)
if err != nil {
return nil, err
}
defer conn.Close()
derCertsRaw := []byte{}
certs := conn.ConnectionState().PeerCertificates
for _, cert := range certs {
derCertsRaw = append(derCertsRaw, cert.Raw...)
}
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: derCertsRaw,
}
return pem.EncodeToMemory(block), nil
}
// ensureEnvVar ensures the environment variable is present in the given list.
func ensureEnvVar(vars []corev1.EnvVar, v corev1.EnvVar) []corev1.EnvVar {
if vars == nil {
return []corev1.EnvVar{v}
}
for i := range vars {
if vars[i].Name == v.Name {
vars[i].Value = v.Value
return vars
}
}
return append(vars, v)
}