@@ -26,6 +26,7 @@ import (
26
26
"k8s.io/apimachinery/pkg/api/equality"
27
27
"k8s.io/apimachinery/pkg/api/errors"
28
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
+ "k8s.io/apimachinery/pkg/fields"
29
30
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
30
31
"k8s.io/apimachinery/pkg/util/wait"
31
32
coreinformers "k8s.io/client-go/informers/core/v1"
@@ -41,14 +42,24 @@ const (
41
42
authenticationRoleName = "extension-apiserver-authentication-reader"
42
43
)
43
44
45
+ // RequestHeaderAuthRequestProvider a provider that knows how to dynamically fill parts of RequestHeaderConfig struct
46
+ type RequestHeaderAuthRequestProvider interface {
47
+ UsernameHeaders () []string
48
+ GroupHeaders () []string
49
+ ExtraHeaderPrefixes () []string
50
+ AllowedClientNames () []string
51
+ }
52
+
53
+ var _ RequestHeaderAuthRequestProvider = & RequestHeaderAuthRequestController {}
54
+
44
55
type requestHeaderBundle struct {
45
56
UsernameHeaders []string
46
57
GroupHeaders []string
47
58
ExtraHeaderPrefixes []string
48
59
AllowedClientNames []string
49
60
}
50
61
51
- // RequestHeaderAuthRequestController a controller that exposes a set of methods for dynamically filling RequestHeaderConfig struct.
62
+ // RequestHeaderAuthRequestController a controller that exposes a set of methods for dynamically filling parts of RequestHeaderConfig struct.
52
63
// The methods are sourced from the config map which is being monitored by this controller.
53
64
// The controller is primed from the server at the construction time for components that don't want to dynamically react to changes
54
65
// in the config map.
@@ -59,6 +70,7 @@ type RequestHeaderAuthRequestController struct {
59
70
configmapNamespace string
60
71
61
72
configmapLister corev1listers.ConfigMapNamespaceLister
73
+ configmapInformer cache.SharedIndexInformer
62
74
configmapInformerSynced cache.InformerSynced
63
75
64
76
queue workqueue.RateLimitingInterface
@@ -77,7 +89,6 @@ func NewRequestHeaderAuthRequestController(
77
89
cmName string ,
78
90
cmNamespace string ,
79
91
client kubernetes.Interface ,
80
- cmInformer coreinformers.ConfigMapInformer ,
81
92
usernameHeadersKey , groupHeadersKey , extraHeaderPrefixesKey , allowedClientNamesKey string ) (* RequestHeaderAuthRequestController , error ) {
82
93
c := & RequestHeaderAuthRequestController {
83
94
name : "RequestHeaderAuthRequestController" ,
@@ -98,7 +109,12 @@ func NewRequestHeaderAuthRequestController(
98
109
return nil , err
99
110
}
100
111
101
- cmInformer .Informer ().AddEventHandler (cache.FilteringResourceEventHandler {
112
+ // we construct our own informer because we need such a small subset of the information available. Just one namespace.
113
+ c .configmapInformer = coreinformers .NewFilteredConfigMapInformer (client , c .configmapNamespace , 12 * time .Hour , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc }, func (listOptions * metav1.ListOptions ) {
114
+ listOptions .FieldSelector = fields .OneTermEqualSelector ("metadata.name" , c .configmapName ).String ()
115
+ })
116
+
117
+ c .configmapInformer .AddEventHandler (cache.FilteringResourceEventHandler {
102
118
FilterFunc : func (obj interface {}) bool {
103
119
if cast , ok := obj .(* corev1.ConfigMap ); ok {
104
120
return cast .Name == c .configmapName && cast .Namespace == c .configmapNamespace
@@ -125,8 +141,8 @@ func NewRequestHeaderAuthRequestController(
125
141
},
126
142
})
127
143
128
- c .configmapLister = cmInformer . Lister ( ).ConfigMaps (c .configmapNamespace )
129
- c .configmapInformerSynced = cmInformer . Informer () .HasSynced
144
+ c .configmapLister = corev1listers . NewConfigMapLister ( c . configmapInformer . GetIndexer () ).ConfigMaps (c .configmapNamespace )
145
+ c .configmapInformerSynced = c . configmapInformer .HasSynced
130
146
131
147
return c , nil
132
148
}
@@ -155,6 +171,8 @@ func (c *RequestHeaderAuthRequestController) Run(workers int, stopCh <-chan stru
155
171
klog .Infof ("Starting %s" , c .name )
156
172
defer klog .Infof ("Shutting down %s" , c .name )
157
173
174
+ go c .configmapInformer .Run (stopCh )
175
+
158
176
// wait for caches to fill before starting your work
159
177
if ! cache .WaitForNamedCacheSync (c .name , stopCh , c .configmapInformerSynced ) {
160
178
return
@@ -224,6 +242,7 @@ func (c *RequestHeaderAuthRequestController) syncConfigMap(configMap *corev1.Con
224
242
}
225
243
if hasChanged {
226
244
c .exportedRequestHeaderBundle .Store (newRequestHeaderBundle )
245
+ klog .V (2 ).Infof ("Loaded a new request header values for %v" , c .name )
227
246
}
228
247
return nil
229
248
}
0 commit comments