Debian Codegraph @

pkg/util/peerproxy/peerproxy.go

raw
1 /*
2 Copyright 2023 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package peerproxy
18
19 import (
20 "context"
21 "fmt"
22 "net/http"
23 "strings"
24 "sync/atomic"
25 "time"
26
27 "k8s.io/apimachinery/pkg/labels"
28 "k8s.io/apimachinery/pkg/runtime"
29 "k8s.io/apimachinery/pkg/runtime/serializer"
30 "k8s.io/apiserver/pkg/reconcilers"
31 "k8s.io/client-go/discovery"
32 "k8s.io/client-go/rest"
33 "k8s.io/client-go/tools/cache"
34 "k8s.io/client-go/transport"
35 "k8s.io/client-go/util/workqueue"
36 "k8s.io/klog/v2"
37
38 apidiscoveryv2 "k8s.io/api/apidiscovery/v2"
39 v1 "k8s.io/api/coordination/v1"
40 schema "k8s.io/apimachinery/pkg/runtime/schema"
41 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
42 coordinationv1informers "k8s.io/client-go/informers/coordination/v1"
43 )
44
45 // Local discovery cache needs to be refreshed periodically to store
46 // updates made to custom resources or aggregated resource that can
47 // change dynamically.
48 const localDiscoveryRefreshInterval = 30 * time.Minute
49
50 // Interface defines how the Mixed Version Proxy filter interacts with the underlying system.
51 type Interface interface {
52 WrapHandler(handler http.Handler) http.Handler
53 WaitForCacheSync(stopCh <-chan struct{}) error
54 HasFinishedSync() bool
55 RunLocalDiscoveryCacheSync(stopCh <-chan struct{}) error
56 RunPeerDiscoveryCacheSync(ctx context.Context, workers int)
57 }
58
59 // New creates a new instance to implement unknown version proxy
60 // This method is used for an alpha feature UnknownVersionInteroperabilityProxy
61 // and is subject to future modifications.
62 func NewPeerProxyHandler(
63 serverId string,
64 identityLeaseLabelSelector string,
65 leaseInformer coordinationv1informers.LeaseInformer,
66 reconciler reconcilers.PeerEndpointLeaseReconciler,
67 ser runtime.NegotiatedSerializer,
68 loopbackClientConfig *rest.Config,
69 proxyClientConfig *transport.Config,
70 ) (*peerProxyHandler, error) {
71 h := &peerProxyHandler{
72 name: "PeerProxyHandler",
73 serverID: serverId,
74 reconciler: reconciler,
75 serializer: ser,
76 localDiscoveryInfoCache: atomic.Value{},
77 localDiscoveryCacheTicker: time.NewTicker(localDiscoveryRefreshInterval),
78 localDiscoveryInfoCachePopulated: make(chan struct{}),
79 peerDiscoveryInfoCache: atomic.Value{},
80 peerLeaseQueue: workqueue.NewTypedRateLimitingQueueWithConfig(
81 workqueue.DefaultTypedControllerRateLimiter[string](),
82 workqueue.TypedRateLimitingQueueConfig[string]{
83 Name: controllerName,
84 }),
85 apiserverIdentityInformer: leaseInformer,
86 }
87
88 if parts := strings.Split(identityLeaseLabelSelector, "="); len(parts) != 2 {
89 return nil, fmt.Errorf("invalid identityLeaseLabelSelector provided, must be of the form key=value, received: %v", identityLeaseLabelSelector)
90 }
91 selector, err := labels.Parse(identityLeaseLabelSelector)
92 if err != nil {
93 return nil, fmt.Errorf("failed to parse label selector: %w", err)
94 }
95 h.identityLeaseLabelSelector = selector
96
97 discoveryScheme := runtime.NewScheme()
98 utilruntime.Must(apidiscoveryv2.AddToScheme(discoveryScheme))
99 h.discoverySerializer = serializer.NewCodecFactory(discoveryScheme)
100
101 discoveryClient, err := discovery.NewDiscoveryClientForConfig(loopbackClientConfig)
102 if err != nil {
103 return nil, fmt.Errorf("error creating discovery client: %w", err)
104 }
105 h.discoveryClient = discoveryClient
106 h.localDiscoveryInfoCache.Store(map[schema.GroupVersionResource]bool{})
107 h.peerDiscoveryInfoCache.Store(map[string]map[schema.GroupVersionResource]bool{})
108
109 proxyTransport, err := transport.New(proxyClientConfig)
110 if err != nil {
111 return nil, fmt.Errorf("failed to create proxy transport: %w", err)
112 }
113 h.proxyTransport = proxyTransport
114
115 peerDiscoveryRegistration, err := h.apiserverIdentityInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
116 AddFunc: func(obj interface{}) {
117 if lease, ok := h.isValidPeerIdentityLease(obj); ok {
118 h.enqueueLease(lease)
119 }
120 },
121 UpdateFunc: func(oldObj, newObj interface{}) {
122 oldLease, oldLeaseOk := h.isValidPeerIdentityLease(oldObj)
123 newLease, newLeaseOk := h.isValidPeerIdentityLease(newObj)
124 if oldLeaseOk && newLeaseOk &&
125 oldLease.Name == newLease.Name && *oldLease.Spec.HolderIdentity != *newLease.Spec.HolderIdentity {
126 h.enqueueLease(newLease)
127 }
128 },
129 DeleteFunc: func(obj interface{}) {
130 if lease, ok := h.isValidPeerIdentityLease(obj); ok {
131 h.enqueueLease(lease)
132 }
133 },
134 })
135 if err != nil {
136 return nil, err
137 }
138
139 h.leaseRegistration = peerDiscoveryRegistration
140 return h, nil
141 }
142
143 func (h *peerProxyHandler) isValidPeerIdentityLease(obj interface{}) (*v1.Lease, bool) {
144 lease, ok := obj.(*v1.Lease)
145 if !ok {
146 tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
147 if !ok {
148 utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", obj))
149 return nil, false
150 }
151 if lease, ok = tombstone.Obj.(*v1.Lease); !ok {
152 utilruntime.HandleError(fmt.Errorf("unexpected object type: %T", obj))
153 return nil, false
154 }
155 }
156
157 if lease == nil {
158 klog.Error(fmt.Errorf("nil lease object provided"))
159 return nil, false
160 }
161
162 if h.identityLeaseLabelSelector != nil && h.identityLeaseLabelSelector.String() != "" {
163 identityLeaseLabel := strings.Split(h.identityLeaseLabelSelector.String(), "=")
164 if len(identityLeaseLabel) != 2 {
165 klog.Errorf("invalid identityLeaseLabelSelector format: %s", h.identityLeaseLabelSelector.String())
166 return nil, false
167 }
168
169 if lease.Labels == nil || lease.Labels[identityLeaseLabel[0]] != identityLeaseLabel[1] {
170 klog.V(4).Infof("lease %s/%s does not match label selector: %s=%s", lease.Namespace, lease.Name, identityLeaseLabel[0], identityLeaseLabel[1])
171 return nil, false
172 }
173
174 }
175
176 // Ignore self.
177 if lease.Name == h.serverID {
178 return nil, false
179 }
180
181 if lease.Spec.HolderIdentity == nil {
182 klog.Error(fmt.Errorf("invalid lease object provided, missing holderIdentity in lease obj"))
183 return nil, false
184 }
185
186 return lease, true
187 }