@@ -82,8 +82,6 @@ func NewController(
82
82
c .eventBroadcaster = broadcaster
83
83
c .eventRecorder = recorder
84
84
85
- c .readyCh = make (chan struct {})
86
-
87
85
return c
88
86
}
89
87
@@ -99,8 +97,6 @@ type Controller struct {
99
97
serviceCIDRLister networkingv1alpha1listers.ServiceCIDRLister
100
98
serviceCIDRsSynced cache.InformerSynced
101
99
102
- readyCh chan struct {} // channel to block until the default ServiceCIDR exists
103
-
104
100
interval time.Duration
105
101
}
106
102
@@ -120,28 +116,40 @@ func (c *Controller) Start(stopCh <-chan struct{}) {
120
116
return
121
117
}
122
118
123
- go wait .Until (c .sync , c .interval , stopCh )
119
+ // derive a context from the stopCh so we can cancel the poll loop
120
+ ctx := wait .ContextForChannel (stopCh )
121
+ // wait until first successfully sync
122
+ // this blocks apiserver startup so poll with a short interval
123
+ err := wait .PollUntilContextCancel (ctx , 100 * time .Millisecond , true , func (ctx context.Context ) (bool , error ) {
124
+ syncErr := c .sync ()
125
+ return syncErr == nil , nil
126
+ })
127
+ if err != nil {
128
+ klog .Infof ("error initializing the default ServiceCIDR: %v" , err )
124
129
125
- select {
126
- case <- stopCh :
127
- case <- c .readyCh :
128
130
}
131
+
132
+ // run the sync loop in the background with the defined interval
133
+ go wait .Until (func () {
134
+ err := c .sync ()
135
+ if err != nil {
136
+ klog .Infof ("error trying to sync the default ServiceCIDR: %v" , err )
137
+ }
138
+ }, c .interval , stopCh )
129
139
}
130
140
131
- func (c * Controller ) sync () {
141
+ func (c * Controller ) sync () error {
132
142
// check if the default ServiceCIDR already exist
133
143
serviceCIDR , err := c .serviceCIDRLister .Get (DefaultServiceCIDRName )
134
144
// if exists
135
145
if err == nil {
136
- c .setReady ()
137
146
c .syncStatus (serviceCIDR )
138
- return
147
+ return nil
139
148
}
140
149
141
150
// unknown error
142
151
if ! apierrors .IsNotFound (err ) {
143
- klog .Infof ("error trying to obtain the default ServiceCIDR: %v" , err )
144
- return
152
+ return err
145
153
}
146
154
147
155
// default ServiceCIDR does not exist
@@ -156,21 +164,11 @@ func (c *Controller) sync() {
156
164
}
157
165
serviceCIDR , err = c .client .NetworkingV1alpha1 ().ServiceCIDRs ().Create (context .Background (), serviceCIDR , metav1.CreateOptions {})
158
166
if err != nil && ! apierrors .IsAlreadyExists (err ) {
159
- klog .Infof ("error creating default ServiceCIDR: %v" , err )
160
167
c .eventRecorder .Eventf (serviceCIDR , v1 .EventTypeWarning , "KubernetesDefaultServiceCIDRError" , "The default ServiceCIDR can not be created" )
161
- return
168
+ return err
162
169
}
163
-
164
- c .setReady ()
165
170
c .syncStatus (serviceCIDR )
166
- }
167
-
168
- func (c * Controller ) setReady () {
169
- select {
170
- case <- c .readyCh :
171
- default :
172
- close (c .readyCh )
173
- }
171
+ return nil
174
172
}
175
173
176
174
func (c * Controller ) syncStatus (serviceCIDR * networkingapiv1alpha1.ServiceCIDR ) {
0 commit comments