forked from kubernetes-retired/kubefed
/
servicedns.go
255 lines (212 loc) · 9.72 KB
/
servicedns.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
/*
Copyright 2018 The Kubernetes Authors.
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 e2e
import (
"context"
"fmt"
"github.com/pkg/errors"
"sort"
"strings"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
fedv1a1 "sigs.k8s.io/kubefed/pkg/apis/core/v1alpha1"
dnsv1a1 "sigs.k8s.io/kubefed/pkg/apis/multiclusterdns/v1alpha1"
genericclient "sigs.k8s.io/kubefed/pkg/client/generic"
"sigs.k8s.io/kubefed/pkg/controller/dnsendpoint"
"sigs.k8s.io/kubefed/test/common"
"sigs.k8s.io/kubefed/test/e2e/framework"
. "github.com/onsi/ginkgo"
)
var _ = Describe("ServiceDNS", func() {
f := framework.NewFederationFramework("multicluster-service-dns")
tl := framework.NewE2ELogger()
const userAgent = "test-service-dns"
const baseName = "test-service-dns-"
const federationPrefix = "galactic"
const Domain = "example.com"
var client genericclient.Client
var clusterRegionZones map[string]fedv1a1.KubefedClusterStatus
var federation string
var namespace string
objectGetter := func(namespace, name string) (pkgruntime.Object, error) {
serviceDNSRecords := &dnsv1a1.ServiceDNSRecord{}
err := client.Get(context.TODO(), serviceDNSRecords, namespace, name)
return serviceDNSRecords, err
}
BeforeEach(func() {
client = f.Client(userAgent)
namespace = f.TestNamespaceName()
clusterRegionZones = ensureClustersHaveRegionZoneAttributes(tl, client, f.KubefedSystemNamespace())
if framework.TestContext.RunControllers() {
fixture := framework.NewServiceDNSControllerFixture(tl, f.ControllerConfig())
f.RegisterFixture(fixture)
}
f.EnsureTestNamespacePropagation()
domainObj := common.NewDomainObject(federationPrefix, Domain)
domainObj.Namespace = f.KubefedSystemNamespace()
err := client.Create(context.TODO(), domainObj)
framework.ExpectNoError(err, "Error creating Domain object")
federation = domainObj.Name
})
AfterEach(func() {
domainObj := &dnsv1a1.Domain{}
err := client.Delete(context.TODO(), domainObj, f.KubefedSystemNamespace(), federation)
framework.ExpectNoError(err, "Error deleting Domain object")
})
It("ServiceDNS object status should be updated correctly when there are no service and/or endpoint in member clusters", func() {
By("Creating the ServiceDNS object")
serviceDNS := common.NewServiceDNSObject(baseName, namespace)
serviceDNS.Spec.DomainRef = federation
err := client.Create(context.TODO(), serviceDNS)
framework.ExpectNoError(err, "Error creating ServiceDNS object: %v", serviceDNS)
serviceDNSStatus := dnsv1a1.ServiceDNSRecordStatus{Domain: Domain, DNS: []dnsv1a1.ClusterDNS{}}
for _, clusterName := range f.ClusterNames(userAgent) {
serviceDNSStatus.DNS = append(serviceDNSStatus.DNS, dnsv1a1.ClusterDNS{
Cluster: clusterName,
Region: clusterRegionZones[clusterName].Region,
Zones: clusterRegionZones[clusterName].Zones,
})
}
sort.Slice(serviceDNSStatus.DNS, func(i, j int) bool {
return serviceDNSStatus.DNS[i].Cluster < serviceDNSStatus.DNS[j].Cluster
})
serviceDNS.Status = serviceDNSStatus
By("Waiting for the ServiceDNS object to have correct status")
common.WaitForObject(tl, namespace, serviceDNS.Name, objectGetter, serviceDNS, framework.PollInterval, framework.TestContext.SingleCallTimeout)
})
Context("When ServiceDNS object is created", func() {
const (
RecordTypeA = "A"
RecordTTL = 300
)
It("DNSEndpoint object should be created with correct status when ServiceDNS object is created", func() {
By("Creating the ServiceDNS object")
serviceDNS := common.NewServiceDNSObject(baseName, namespace)
serviceDNS.Spec.DomainRef = federation
serviceDNS.Spec.RecordTTL = RecordTTL
err := client.Create(context.TODO(), serviceDNS)
framework.ExpectNoError(err, "Error creating ServiceDNS object %v", serviceDNS)
name := serviceDNS.Name
By("Creating corresponding service and endpoint for the ServiceDNS object in member clusters")
serviceDNSStatus := createClusterServiceAndEndpoints(f, name, namespace, Domain, clusterRegionZones)
serviceDNS.Status = *serviceDNSStatus
By("Waiting for the ServiceDNS object to have correct status")
common.WaitForObject(tl, namespace, name, objectGetter, serviceDNS, framework.PollInterval, framework.TestContext.SingleCallTimeout)
By("Waiting for the DNSEndpoint object to be created")
endpointObjectGetter := func(namespace, name string) (pkgruntime.Object, error) {
dnsEndpoint := &dnsv1a1.DNSEndpoint{}
err := client.Get(context.TODO(), dnsEndpoint, namespace, name)
return dnsEndpoint, err
}
endpoints := []*dnsv1a1.Endpoint{}
for _, cluster := range serviceDNS.Status.DNS {
zones := clusterRegionZones[cluster.Cluster].Zones
region := clusterRegionZones[cluster.Cluster].Region
lbs := dnsendpoint.ExtractLoadBalancerTargets(cluster.LoadBalancer)
endpoint := common.NewDNSEndpoint(
strings.Join([]string{name, namespace, federation, "svc", zones[0], region, Domain}, "."),
lbs, RecordTypeA, RecordTTL)
endpoints = append(endpoints, endpoint)
endpoint = common.NewDNSEndpoint(
strings.Join([]string{name, namespace, federation, "svc", region, Domain}, "."),
lbs, RecordTypeA, RecordTTL)
endpoints = append(endpoints, endpoint)
endpoint = common.NewDNSEndpoint(
strings.Join([]string{name, namespace, federation, "svc", Domain}, "."),
lbs, RecordTypeA, RecordTTL)
endpoints = append(endpoints, endpoint)
}
desiredDNSEndpoint := &dnsv1a1.DNSEndpoint{
ObjectMeta: metav1.ObjectMeta{
Name: "service-" + name,
Namespace: namespace,
},
Spec: dnsv1a1.DNSEndpointSpec{
Endpoints: dnsendpoint.DedupeAndMergeEndpoints(endpoints),
},
}
common.WaitForObject(tl, namespace, "service-"+name, endpointObjectGetter, desiredDNSEndpoint, framework.PollInterval, framework.TestContext.SingleCallTimeout)
})
})
})
func createClusterServiceAndEndpoints(f framework.FederationFramework, name, namespace string, domain string,
clusterRegionZones map[string]fedv1a1.KubefedClusterStatus) *dnsv1a1.ServiceDNSRecordStatus {
const userAgent = "test-service-dns"
service := common.NewServiceObject(name, namespace)
endpoint := common.NewEndpointObject(name, namespace)
lbsuffix := 1
serviceDNSStatus := &dnsv1a1.ServiceDNSRecordStatus{Domain: domain, DNS: []dnsv1a1.ClusterDNS{}}
for clusterName, client := range f.ClusterKubeClients(userAgent) {
clusterLb := fmt.Sprintf("10.20.30.%d", lbsuffix)
lbsuffix++
loadbalancerStatus := apiv1.LoadBalancerStatus{Ingress: []apiv1.LoadBalancerIngress{{IP: clusterLb}}}
serviceDNSStatus.DNS = append(serviceDNSStatus.DNS, dnsv1a1.ClusterDNS{
Cluster: clusterName,
LoadBalancer: loadbalancerStatus,
Region: clusterRegionZones[clusterName].Region,
Zones: clusterRegionZones[clusterName].Zones,
})
common.WaitForNamespaceOrDie(framework.NewE2ELogger(), client, clusterName, namespace,
framework.PollInterval, framework.TestContext.SingleCallTimeout)
createdService, err := client.CoreV1().Services(namespace).Create(service)
framework.ExpectNoError(err, "Error creating service in cluster %q", clusterName)
createdService.Status = apiv1.ServiceStatus{
LoadBalancer: loadbalancerStatus,
}
// Fake out provisioning LoadBalancer by updating the service status in member cluster.
_, err = client.CoreV1().Services(namespace).UpdateStatus(createdService)
framework.ExpectNoError(err, "Error updating service status in cluster %q", clusterName)
// Fake out pods backing service by creating endpoint in member cluster.
_, err = client.CoreV1().Endpoints(namespace).Create(endpoint)
framework.ExpectNoError(err, "Error creating endpoint in cluster %q", clusterName)
}
sort.Slice(serviceDNSStatus.DNS, func(i, j int) bool {
return serviceDNSStatus.DNS[i].Cluster < serviceDNSStatus.DNS[j].Cluster
})
return serviceDNSStatus
}
func ensureClustersHaveRegionZoneAttributes(tl common.TestLogger, client genericclient.Client, clusterNamespace string) map[string]fedv1a1.KubefedClusterStatus {
clusters := &fedv1a1.KubefedClusterList{}
err := client.List(context.TODO(), clusters, clusterNamespace)
framework.ExpectNoError(err, "Error listing federated clusters")
clusterRegionZones := make(map[string]fedv1a1.KubefedClusterStatus)
for i, cluster := range clusters.Items {
err := wait.PollImmediate(framework.PollInterval, framework.TestContext.SingleCallTimeout, func() (bool, error) {
cluster.Status.Region = fmt.Sprintf("r%d", i)
cluster.Status.Zones = []string{fmt.Sprintf("z%d", i)}
err := client.UpdateStatus(context.TODO(), &cluster)
if apierrors.IsConflict(err) {
clusterName := cluster.Name
tl.Logf("Failed to update status for federated cluster %q: %v", clusterName, err)
err = client.Get(context.TODO(), &cluster, clusterNamespace, clusterName)
if err != nil {
return false, errors.Wrapf(err, "failed to retrieve cluster object")
}
return false, nil
}
if err != nil {
return false, err
}
return true, nil
})
framework.ExpectNoError(err, "Error updating federated cluster status")
clusterRegionZones[cluster.Name] = fedv1a1.KubefedClusterStatus{
Region: cluster.Status.Region,
Zones: cluster.Status.Zones,
}
}
return clusterRegionZones
}