@@ -4,14 +4,10 @@ import (
4
4
"fmt"
5
5
"reflect"
6
6
7
- "github.com/pkg/errors"
8
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
- "k8s.io/apimachinery/pkg/fields"
10
7
"k8s.io/client-go/tools/cache"
11
8
12
9
configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
13
- configClientset "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
14
- configInformers "github.com/openservicemesh/osm/pkg/gen/client/config/informers/externalversions"
10
+ "github.com/openservicemesh/osm/pkg/k8s/informers"
15
11
16
12
"github.com/openservicemesh/osm/pkg/announcements"
17
13
"github.com/openservicemesh/osm/pkg/errcode"
@@ -21,44 +17,9 @@ import (
21
17
)
22
18
23
19
// NewConfigurator implements configurator.Configurator and creates the Kubernetes client to manage namespaces.
24
- func NewConfigurator (configClient configClientset.Interface , stop <- chan struct {}, osmNamespace , meshConfigName string ,
25
- msgBroker * messaging.Broker ) (Configurator , error ) {
26
- return newConfigurator (configClient , stop , osmNamespace , meshConfigName , msgBroker )
27
- }
28
-
29
- func newConfigurator (configClient configClientset.Interface , stop <- chan struct {}, osmNamespace string , meshConfigName string ,
30
- msgBroker * messaging.Broker ) (* client , error ) {
31
- listOption := configInformers .WithTweakListOptions (func (opt * metav1.ListOptions ) {
32
- opt .FieldSelector = fields .OneTermEqualSelector (metav1 .ObjectNameField , meshConfigName ).String ()
33
- })
34
-
35
- meshConfigInformerFactory := configInformers .NewSharedInformerFactoryWithOptions (
36
- configClient ,
37
- k8s .DefaultKubeEventResyncInterval ,
38
- configInformers .WithNamespace (osmNamespace ),
39
- listOption ,
40
- )
41
-
42
- // informerFactory without listOptions
43
- configInformerFactory := configInformers .NewSharedInformerFactoryWithOptions (
44
- configClient ,
45
- k8s .DefaultKubeEventResyncInterval ,
46
- configInformers .WithNamespace (osmNamespace ),
47
- )
48
-
49
- informerCollection := informerCollection {
50
- meshConfig : meshConfigInformerFactory .Config ().V1alpha2 ().MeshConfigs ().Informer (),
51
- meshRootCertificate : configInformerFactory .Config ().V1alpha2 ().MeshRootCertificates ().Informer (),
52
- }
53
-
54
- cacheCollection := cacheCollection {
55
- meshConfig : informerCollection .meshConfig .GetStore (),
56
- meshRootCertificate : informerCollection .meshRootCertificate .GetStore (),
57
- }
58
-
59
- c := & client {
60
- informers : & informerCollection ,
61
- caches : & cacheCollection ,
20
+ func NewConfigurator (informerCollection * informers.InformerCollection , osmNamespace , meshConfigName string , msgBroker * messaging.Broker ) * Client {
21
+ c := & Client {
22
+ informers : informerCollection ,
62
23
osmNamespace : osmNamespace ,
63
24
meshConfigName : meshConfigName ,
64
25
}
@@ -69,70 +30,30 @@ func newConfigurator(configClient configClientset.Interface, stop <-chan struct{
69
30
Update : announcements .MeshConfigUpdated ,
70
31
Delete : announcements .MeshConfigDeleted ,
71
32
}
72
- informerCollection .meshConfig .AddEventHandler (k8s .GetEventHandlerFuncs (nil , meshConfigEventTypes , msgBroker ))
73
- informerCollection .meshConfig .AddEventHandler (c .metricsHandler ())
33
+
34
+ informerCollection .AddEventHandler (informers .InformerKeyMeshConfig , k8s .GetEventHandlerFuncs (nil , meshConfigEventTypes , msgBroker ))
35
+ informerCollection .AddEventHandler (informers .InformerKeyMeshConfig , c .metricsHandler ())
74
36
75
37
meshRootCertificateEventTypes := k8s.EventTypes {
76
38
Add : announcements .MeshRootCertificateAdded ,
77
39
Update : announcements .MeshRootCertificateUpdated ,
78
40
Delete : announcements .MeshRootCertificateDeleted ,
79
41
}
80
- informerCollection .meshRootCertificate .AddEventHandler (k8s .GetEventHandlerFuncs (nil , meshRootCertificateEventTypes , msgBroker ))
81
-
82
- err := c .run (stop )
83
- if err != nil {
84
- return c , errors .Errorf ("Could not start %s informer clients: %s" , configv1alpha2 .SchemeGroupVersion , err )
85
- }
86
-
87
- return c , nil
88
- }
89
-
90
- func (c * client ) run (stop <- chan struct {}) error {
91
- log .Info ().Msgf ("Starting informer clients for API group %s" , configv1alpha2 .SchemeGroupVersion )
92
-
93
- if c .informers == nil {
94
- return errors .New ("config.openservicemesh.io informers not initialized" )
95
- }
96
-
97
- sharedInformers := map [string ]cache.SharedInformer {
98
- "MeshConfig" : c .informers .meshConfig ,
99
- "MeshRootCertificate" : c .informers .meshRootCertificate ,
100
- }
101
-
102
- var informerNames []string
103
- var hasSynced []cache.InformerSynced
104
- for name , informer := range sharedInformers {
105
- if informer == nil {
106
- log .Error ().Msgf ("Informer for '%s' not initialized, ignoring it" , name ) // TODO: log with errcode
107
- continue
108
- }
109
- informerNames = append (informerNames , name )
110
- log .Info ().Msgf ("Starting informer: %s" , name )
111
- go informer .Run (stop )
112
- hasSynced = append (hasSynced , informer .HasSynced )
113
- }
114
-
115
- log .Info ().Msgf ("Waiting for informers %v caches to sync" , informerNames )
116
- if ! cache .WaitForCacheSync (stop , hasSynced ... ) {
117
- // TODO(#3962): metric might not be scraped before process restart resulting from this error
118
- log .Error ().Str (errcode .Kind , errcode .GetErrCodeWithMetric (errcode .ErrConfigInformerInitCache )).Msg ("Failed initial cache sync for config.openservicemesh.io informers" )
119
- return errors .New ("Failed initial cache sync for config.openservicemesh.io informers" )
120
- }
42
+ informerCollection .AddEventHandler (informers .InformerKeyMeshRootCertificate , k8s .GetEventHandlerFuncs (nil , meshRootCertificateEventTypes , msgBroker ))
121
43
122
- log .Info ().Msgf ("Cache sync finished for %v informers in API group %s" , informerNames , configv1alpha2 .SchemeGroupVersion )
123
- return nil
44
+ return c
124
45
}
125
46
126
- func (c * client ) getMeshConfigCacheKey () string {
47
+ func (c * Client ) getMeshConfigCacheKey () string {
127
48
return fmt .Sprintf ("%s/%s" , c .osmNamespace , c .meshConfigName )
128
49
}
129
50
130
51
// Returns the current MeshConfig
131
- func (c * client ) getMeshConfig () configv1alpha2.MeshConfig {
52
+ func (c * Client ) getMeshConfig () configv1alpha2.MeshConfig {
132
53
var meshConfig configv1alpha2.MeshConfig
133
54
134
55
meshConfigCacheKey := c .getMeshConfigCacheKey ()
135
- item , exists , err := c .caches . meshConfig . GetByKey (meshConfigCacheKey )
56
+ item , exists , err := c .informers . GetByKey (informers . InformerKeyMeshConfig , meshConfigCacheKey )
136
57
if err != nil {
137
58
log .Error ().Err (err ).Str (errcode .Kind , errcode .GetErrCodeWithMetric (errcode .ErrMeshConfigFetchFromCache )).Msgf ("Error getting MeshConfig from cache with key %s" , meshConfigCacheKey )
138
59
return meshConfig
@@ -147,7 +68,7 @@ func (c *client) getMeshConfig() configv1alpha2.MeshConfig {
147
68
return meshConfig
148
69
}
149
70
150
- func (c * client ) metricsHandler () cache.ResourceEventHandlerFuncs {
71
+ func (c * Client ) metricsHandler () cache.ResourceEventHandlerFuncs {
151
72
handleMetrics := func (obj interface {}) {
152
73
config := obj .(* configv1alpha2.MeshConfig )
153
74
0 commit comments