@@ -31,11 +31,10 @@ type EgressConfigSource interface {
3131// EgressController is the controller for creating Egress configuration via a
3232// provider.
3333type EgressController struct {
34- interval time.Duration
35- configSource EgressConfigSource
36- configsCache map [provider.Resource ]map [string ]* net.IPNet
37- provider provider.Provider
38- cacheInitialized bool
34+ interval time.Duration
35+ configSource EgressConfigSource
36+ configsCache map [provider.Resource ]map [string ]* net.IPNet
37+ provider provider.Provider
3938}
4039
4140// NewEgressController initializes a new EgressController.
@@ -53,50 +52,47 @@ func (c *EgressController) Run(ctx context.Context) {
5352 log .Info ("Running controller" )
5453
5554 for {
56- if ! c .cacheInitialized {
57- configs , err := c .configSource .ListConfigs (ctx )
58- if err != nil {
59- log .Errorf ("Failed to list Egress configurations: %v" , err )
60- time .Sleep (3 * time .Second )
61- continue
62- }
55+ configs , err := c .configSource .ListConfigs (ctx )
56+ if err != nil {
57+ log .Errorf ("Failed to list Egress configurations: %v" , err )
58+ time .Sleep (3 * time .Second )
59+ continue // retry
60+ }
6361
64- c .cacheInitialized = true
65- for _ , config := range configs {
66- if len (config .IPAddresses ) > 0 {
67- c .configsCache [config .Resource ] = config .IPAddresses
68- }
62+ for _ , config := range configs {
63+ if len (config .IPAddresses ) > 0 {
64+ c .configsCache [config .Resource ] = config .IPAddresses
6965 }
70- continue
7166 }
67+ ensureEgressRules (ctx , c .provider , c .configsCache )
68+ break // successfully initialized cache, move on
69+ }
7270
71+ for {
7372 select {
7473 case <- time .After (c .interval ):
75- err := c .provider .Ensure (ctx , c .configsCache )
76- if err != nil {
77- log .Errorf ("Failed to ensure configuration: %v" , err )
78- continue
79- }
80- // successfully synced
81- lastSyncTimestamp .SetToCurrentTime ()
74+ ensureEgressRules (ctx , c .provider , c .configsCache )
8275 case config := <- c .configSource .Config ():
8376 if len (config .IPAddresses ) == 0 {
8477 delete (c .configsCache , config .Resource )
8578 } else {
8679 log .Infof ("Observed IP Addresses %v for %v" , config .IPAddresses , config .Resource )
8780 c .configsCache [config .Resource ] = config .IPAddresses
8881 }
89-
90- err := c .provider .Ensure (ctx , c .configsCache )
91- if err != nil {
92- log .Errorf ("Failed to ensure configuration: %v" , err )
93- continue
94- }
95- // successfully synced
96- lastSyncTimestamp .SetToCurrentTime ()
82+ ensureEgressRules (ctx , c .provider , c .configsCache )
9783 case <- ctx .Done ():
9884 log .Info ("Terminating controller loop." )
9985 return
10086 }
10187 }
10288}
89+
90+ func ensureEgressRules (ctx context.Context , prov provider.Provider , configsCache map [provider.Resource ]map [string ]* net.IPNet ) {
91+ err := prov .Ensure (ctx , configsCache )
92+ if err != nil {
93+ log .Errorf ("Failed to ensure configuration: %v" , err )
94+ return
95+ }
96+ // successfully synced
97+ lastSyncTimestamp .SetToCurrentTime ()
98+ }
0 commit comments