/
routes.go
163 lines (134 loc) · 4.79 KB
/
routes.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
package mdns
import (
"context"
"fmt"
"strings"
"time"
"github.com/openshift/microshift/pkg/mdns/server"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)
const defaultResyncTime = time.Hour * 1
func (c *MicroShiftmDNSController) restConfig() (*rest.Config, error) {
return clientcmd.BuildConfigFromFlags("", c.KubeConfig)
}
func (c *MicroShiftmDNSController) startRouteInformer(stopCh chan struct{}) error {
klog.Infof("Starting MicroShift mDNS route watcher")
cfg, err := c.restConfig()
if err != nil {
return fmt.Errorf("failed to create rest config for route informer: %w", err)
}
dc, err := dynamic.NewForConfig(cfg)
if err != nil {
return fmt.Errorf("failed to create dynamic informer for route controller: %w", err)
}
return c.run(stopCh, dc)
}
func (c *MicroShiftmDNSController) run(stopCh chan struct{}, dc dynamic.Interface) error {
informerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dc, defaultResyncTime, v1.NamespaceAll, nil)
routersGVR, _ := schema.ParseResourceArg("routes.v1.route.openshift.io")
informer := informerFactory.ForResource(*routersGVR).Informer()
c.waitForRouterAPI(dc, routersGVR)
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: c.addedRoute,
UpdateFunc: c.updatedRoute,
DeleteFunc: c.deletedRoute,
}
if _, err := informer.AddEventHandler(handlers); err != nil {
return fmt.Errorf("failed to initialize event handler for route controller: %w", err)
}
informer.Run(stopCh)
return nil
}
// TODO: The need for this indicates that the openshift-default-scc-manager is declaring itself ready before
// it really is. If we don't wait for the route API the informer will start throwing ugly errors.
func (c *MicroShiftmDNSController) waitForRouterAPI(dc dynamic.Interface, routersGVR *schema.GroupVersionResource) {
backoff := wait.Backoff{
Cap: 3 * time.Minute,
Duration: 10 * time.Second,
Factor: 1.5,
Steps: 24,
}
klog.Infof("mDNS: waiting for route API to be ready")
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
_, err := dc.Resource(*routersGVR).List(context.TODO(), metav1.ListOptions{})
if err == nil {
return true, nil
}
return false, nil
})
if err != nil {
// This is a soft error, the watcher down the line will keep waiting, but will emit
// less nice errors
klog.Errorf("mDNS: Failed waiting for the route.openshift.io/v1 API to come up: %s", err)
} else {
klog.Infof("mDNS: Route API ready, watching routers")
}
}
func (c *MicroShiftmDNSController) addedRoute(obj interface{}) {
u := obj.(*unstructured.Unstructured)
host, found, err := unstructured.NestedString(u.UnstructuredContent(), "spec", "host")
if !found || err != nil {
klog.Errorf("mDNS: spec.host not found: %s", err)
return
}
c.exposeHost(host)
}
func (c *MicroShiftmDNSController) updatedRoute(oldObj, newObj interface{}) {
oldU := oldObj.(*unstructured.Unstructured)
oldHost, _, _ := unstructured.NestedString(oldU.UnstructuredContent(), "spec", "host")
newU := newObj.(*unstructured.Unstructured)
newHost, _, _ := unstructured.NestedString(newU.UnstructuredContent(), "spec", "host")
if oldHost != newHost {
klog.Infof("mDNS: Updating route host from %q to %q", oldHost, newHost)
c.unexposeHost(oldHost)
c.exposeHost(newHost)
}
}
func (c *MicroShiftmDNSController) deletedRoute(obj interface{}) {
u := obj.(*unstructured.Unstructured)
host, found, err := unstructured.NestedString(u.UnstructuredContent(), "spec", "host")
if !found || err != nil {
klog.Errorf("mDNS: spec.host not found: %s", err)
return
}
c.unexposeHost(host)
}
func (c *MicroShiftmDNSController) exposeHost(host string) {
if !strings.HasSuffix(host, server.DefaultmDNSTLD) {
klog.V(2).Infof("mDNS: Ignoring host %q without mDNS suffix", host)
return
}
klog.Infof("mDNS: Route found for host %q on IPs %q", host, c.myIPs)
// TODO(multi-node) look up for the exact router service Endpoints instead of assuming our own IP (ok for single-node)
c.incHost(host)
c.resolver.AddDomain(host+".", c.myIPs)
}
func (c *MicroShiftmDNSController) unexposeHost(oldHost string) {
if c.decHost(oldHost) == 0 {
klog.Infof("mDNS: Removing host %q, no more router references", oldHost)
c.resolver.DeleteDomain(oldHost + ".")
}
}
func (c *MicroShiftmDNSController) incHost(name string) {
c.Lock()
defer c.Unlock()
c.hostCount[name]++
}
func (c *MicroShiftmDNSController) decHost(name string) int {
c.Lock()
defer c.Unlock()
if c.hostCount[name] > 0 {
c.hostCount[name]--
}
return c.hostCount[name]
}