forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f5_virtualserver.go
213 lines (172 loc) · 6.4 KB
/
f5_virtualserver.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
/*
Copyright 2022 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 source
import (
"context"
"fmt"
"sort"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
f5 "github.com/F5Networks/k8s-bigip-ctlr/v2/config/apis/cis/v1"
"sigs.k8s.io/external-dns/endpoint"
)
var f5VirtualServerGVR = schema.GroupVersionResource{
Group: "cis.f5.com",
Version: "v1",
Resource: "virtualservers",
}
// virtualServerSource is an implementation of Source for F5 VirtualServer objects.
type f5VirtualServerSource struct {
dynamicKubeClient dynamic.Interface
virtualServerInformer informers.GenericInformer
kubeClient kubernetes.Interface
annotationFilter string
namespace string
unstructuredConverter *unstructuredConverter
}
func NewF5VirtualServerSource(
ctx context.Context,
dynamicKubeClient dynamic.Interface,
kubeClient kubernetes.Interface,
namespace string,
annotationFilter string,
) (Source, error) {
informerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynamicKubeClient, 0, namespace, nil)
virtualServerInformer := informerFactory.ForResource(f5VirtualServerGVR)
virtualServerInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
},
},
)
informerFactory.Start(ctx.Done())
// wait for the local cache to be populated.
if err := waitForDynamicCacheSync(context.Background(), informerFactory); err != nil {
return nil, err
}
uc, err := newVSUnstructuredConverter()
if err != nil {
return nil, errors.Wrapf(err, "failed to setup unstructured converter")
}
return &f5VirtualServerSource{
dynamicKubeClient: dynamicKubeClient,
virtualServerInformer: virtualServerInformer,
kubeClient: kubeClient,
namespace: namespace,
annotationFilter: annotationFilter,
unstructuredConverter: uc,
}, nil
}
// Endpoints returns endpoint objects for each host-target combination that should be processed.
// Retrieves all VirtualServers in the source's namespace(s).
func (vs *f5VirtualServerSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
virtualServerObjects, err := vs.virtualServerInformer.Lister().ByNamespace(vs.namespace).List(labels.Everything())
if err != nil {
return nil, err
}
var virtualServers []*f5.VirtualServer
for _, vsObj := range virtualServerObjects {
unstructuredHost, ok := vsObj.(*unstructured.Unstructured)
if !ok {
return nil, errors.New("could not convert")
}
virtualServer := &f5.VirtualServer{}
err := vs.unstructuredConverter.scheme.Convert(unstructuredHost, virtualServer, nil)
if err != nil {
return nil, err
}
virtualServers = append(virtualServers, virtualServer)
}
virtualServers, err = vs.filterByAnnotations(virtualServers)
if err != nil {
return nil, errors.Wrap(err, "failed to filter VirtualServers")
}
endpoints, err := vs.endpointsFromVirtualServers(virtualServers)
if err != nil {
return nil, err
}
// Sort endpoints
for _, ep := range endpoints {
sort.Sort(ep.Targets)
}
return endpoints, nil
}
func (vs *f5VirtualServerSource) AddEventHandler(ctx context.Context, handler func()) {
log.Debug("Adding event handler for VirtualServer")
vs.virtualServerInformer.Informer().AddEventHandler(eventHandlerFunc(handler))
}
// endpointsFromVirtualServers extracts the endpoints from a slice of VirtualServers
func (vs *f5VirtualServerSource) endpointsFromVirtualServers(virtualServers []*f5.VirtualServer) ([]*endpoint.Endpoint, error) {
var endpoints []*endpoint.Endpoint
for _, virtualServer := range virtualServers {
resource := fmt.Sprintf("f5-virtualserver/%s/%s", virtualServer.Namespace, virtualServer.Name)
ttl := getTTLFromAnnotations(virtualServer.Annotations, resource)
targets := getTargetsFromTargetAnnotation(virtualServer.Annotations)
if len(targets) == 0 && virtualServer.Spec.VirtualServerAddress != "" {
targets = append(targets, virtualServer.Spec.VirtualServerAddress)
}
if len(targets) == 0 && virtualServer.Status.VSAddress != "" {
targets = append(targets, virtualServer.Status.VSAddress)
}
endpoints = append(endpoints, endpointsForHostname(virtualServer.Spec.Host, targets, ttl, nil, "", resource)...)
}
return endpoints, nil
}
// newUnstructuredConverter returns a new unstructuredConverter initialized
func newVSUnstructuredConverter() (*unstructuredConverter, error) {
uc := &unstructuredConverter{
scheme: runtime.NewScheme(),
}
// Add the core types we need
uc.scheme.AddKnownTypes(f5VirtualServerGVR.GroupVersion(), &f5.VirtualServer{}, &f5.VirtualServerList{})
if err := scheme.AddToScheme(uc.scheme); err != nil {
return nil, err
}
return uc, nil
}
// filterByAnnotations filters a list of VirtualServers by a given annotation selector.
func (vs *f5VirtualServerSource) filterByAnnotations(virtualServers []*f5.VirtualServer) ([]*f5.VirtualServer, error) {
labelSelector, err := metav1.ParseToLabelSelector(vs.annotationFilter)
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(labelSelector)
if err != nil {
return nil, err
}
// empty filter returns original list
if selector.Empty() {
return virtualServers, nil
}
filteredList := []*f5.VirtualServer{}
for _, vs := range virtualServers {
// convert the VirtualServer's annotations to an equivalent label selector
annotations := labels.Set(vs.Annotations)
// include VirtualServer if its annotations match the selector
if selector.Matches(annotations) {
filteredList = append(filteredList, vs)
}
}
return filteredList, nil
}