@@ -21,6 +21,7 @@ import (
21
21
"time"
22
22
23
23
flowcontrolv1alpha1 "k8s.io/api/flowcontrol/v1alpha1"
24
+ "k8s.io/apimachinery/pkg/api/equality"
24
25
apierrors "k8s.io/apimachinery/pkg/api/errors"
25
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
27
"k8s.io/apimachinery/pkg/util/wait"
@@ -33,6 +34,7 @@ import (
33
34
"k8s.io/klog"
34
35
"k8s.io/kubernetes/pkg/api/legacyscheme"
35
36
"k8s.io/kubernetes/pkg/apis/flowcontrol"
37
+ flowcontrolapisv1alpha1 "k8s.io/kubernetes/pkg/apis/flowcontrol/v1alpha1"
36
38
flowschemastore "k8s.io/kubernetes/pkg/registry/flowcontrol/flowschema/storage"
37
39
prioritylevelconfigurationstore "k8s.io/kubernetes/pkg/registry/flowcontrol/prioritylevelconfiguration/storage"
38
40
)
@@ -94,20 +96,21 @@ func (p RESTStorageProvider) PostStartHook() (string, genericapiserver.PostStart
94
96
_ = wait .PollImmediateUntil (
95
97
retryCreatingSuggestedSettingsInterval ,
96
98
func () (bool , error ) {
97
- shouldEnsureSuggested , err := shouldEnsureAllPredefined (flowcontrolClientSet )
99
+ shouldEnsureSuggested , err := lastMandatoryExists (flowcontrolClientSet )
98
100
if err != nil {
99
101
klog .Errorf ("failed getting exempt flow-schema, will retry later: %v" , err )
100
102
return false , nil
101
103
}
102
- if shouldEnsureSuggested {
103
- err := ensure (
104
- flowcontrolClientSet ,
105
- flowcontrolbootstrap .SuggestedFlowSchemas ,
106
- flowcontrolbootstrap .SuggestedPriorityLevelConfigurations )
107
- if err != nil {
108
- klog .Errorf ("failed ensuring suggested settings, will retry later: %v" , err )
109
- return false , nil
110
- }
104
+ if ! shouldEnsureSuggested {
105
+ return true , nil
106
+ }
107
+ err = ensure (
108
+ flowcontrolClientSet ,
109
+ flowcontrolbootstrap .SuggestedFlowSchemas ,
110
+ flowcontrolbootstrap .SuggestedPriorityLevelConfigurations )
111
+ if err != nil {
112
+ klog .Errorf ("failed ensuring suggested settings, will retry later: %v" , err )
113
+ return false , nil
111
114
}
112
115
return true , nil
113
116
},
@@ -124,7 +127,7 @@ func (p RESTStorageProvider) PostStartHook() (string, genericapiserver.PostStart
124
127
// the full initial set of objects from being created.
125
128
flowcontrolbootstrap .MandatoryPriorityLevelConfigurations ,
126
129
); err != nil {
127
- klog .Errorf ("failed creating default flowcontrol settings: %v" , err )
130
+ klog .Errorf ("failed creating mandatory flowcontrol settings: %v" , err )
128
131
return false , nil
129
132
}
130
133
return false , nil // always retry
@@ -138,7 +141,7 @@ func (p RESTStorageProvider) PostStartHook() (string, genericapiserver.PostStart
138
141
139
142
// Returns false if there's a "exempt" priority-level existing in the cluster, otherwise returns a true
140
143
// if the "exempt" priority-level is not found.
141
- func shouldEnsureAllPredefined (flowcontrolClientSet flowcontrolclient.FlowcontrolV1alpha1Interface ) (bool , error ) {
144
+ func lastMandatoryExists (flowcontrolClientSet flowcontrolclient.FlowcontrolV1alpha1Interface ) (bool , error ) {
142
145
if _ , err := flowcontrolClientSet .PriorityLevelConfigurations ().Get (flowcontrol .PriorityLevelConfigurationNameExempt , metav1.GetOptions {}); err != nil {
143
146
if apierrors .IsNotFound (err ) {
144
147
return true , nil
@@ -175,39 +178,75 @@ func ensure(flowcontrolClientSet flowcontrolclient.FlowcontrolV1alpha1Interface,
175
178
}
176
179
177
180
func upgrade (flowcontrolClientSet flowcontrolclient.FlowcontrolV1alpha1Interface , flowSchemas []* flowcontrolv1alpha1.FlowSchema , priorityLevels []* flowcontrolv1alpha1.PriorityLevelConfiguration ) error {
178
- for _ , flowSchema := range flowSchemas {
179
- _ , err := flowcontrolClientSet .FlowSchemas ().Get (flowSchema .Name , metav1.GetOptions {})
180
- if err != nil {
181
- return fmt .Errorf ("failed getting FlowSchema %s due to %v, will retry later" , flowSchema .Name , err )
181
+ for _ , expectedFlowSchema := range flowSchemas {
182
+ actualFlowSchema , err := flowcontrolClientSet .FlowSchemas ().Get (expectedFlowSchema .Name , metav1.GetOptions {})
183
+ if err == nil {
184
+ // TODO(yue9944882): extract existing version from label and compare
185
+ // TODO(yue9944882): create w/ version string attached
186
+ identical , err := flowSchemaHasWrongSpec (expectedFlowSchema , actualFlowSchema )
187
+ if err != nil {
188
+ return fmt .Errorf ("failed checking if mandatory FlowSchema %s is up-to-date due to %v, will retry later" , expectedFlowSchema .Name , err )
189
+ }
190
+ if ! identical {
191
+ if _ , err := flowcontrolClientSet .FlowSchemas ().Update (expectedFlowSchema ); err != nil {
192
+ return fmt .Errorf ("failed upgrading mandatory FlowSchema %s due to %v, will retry later" , expectedFlowSchema .Name , err )
193
+ }
194
+ }
195
+ continue
196
+ }
197
+ if ! apierrors .IsNotFound (err ) {
198
+ return fmt .Errorf ("failed getting FlowSchema %s due to %v, will retry later" , expectedFlowSchema .Name , err )
182
199
}
183
- // TODO(yue9944882): extract existing version from label and compare
184
- // TODO(yue9944882): create w/ version string attached
185
- _ , err = flowcontrolClientSet .FlowSchemas ().Create (flowSchema )
200
+ _ , err = flowcontrolClientSet .FlowSchemas ().Create (expectedFlowSchema )
186
201
if apierrors .IsAlreadyExists (err ) {
187
- klog .V (3 ).Infof ("system preset FlowSchema %s already exists, skipping creating" , flowSchema .Name )
202
+ klog .V (3 ).Infof ("system preset FlowSchema %s already exists, skipping creating" , expectedFlowSchema .Name )
188
203
continue
189
204
}
190
205
if err != nil {
191
- return fmt .Errorf ("cannot create FlowSchema %s due to %v" , flowSchema .Name , err )
206
+ return fmt .Errorf ("cannot create FlowSchema %s due to %v" , expectedFlowSchema .Name , err )
192
207
}
193
- klog .V (3 ).Infof ("created system preset FlowSchema %s" , flowSchema .Name )
208
+ klog .V (3 ).Infof ("created system preset FlowSchema %s" , expectedFlowSchema .Name )
194
209
}
195
- for _ , priorityLevelConfiguration := range priorityLevels {
196
- _ , err := flowcontrolClientSet .FlowSchemas ().Get (priorityLevelConfiguration .Name , metav1.GetOptions {})
197
- if err != nil {
198
- return fmt .Errorf ("failed getting PriorityLevelConfiguration %s due to %v, will retry later" , priorityLevelConfiguration .Name , err )
210
+ for _ , expectedPriorityLevelConfiguration := range priorityLevels {
211
+ actualPriorityLevelConfiguration , err := flowcontrolClientSet .PriorityLevelConfigurations ().Get (expectedPriorityLevelConfiguration .Name , metav1.GetOptions {})
212
+ if err == nil {
213
+ // TODO(yue9944882): extract existing version from label and compare
214
+ // TODO(yue9944882): create w/ version string attached
215
+ identical , err := priorityLevelHasWrongSpec (expectedPriorityLevelConfiguration , actualPriorityLevelConfiguration )
216
+ if err != nil {
217
+ return fmt .Errorf ("failed checking if mandatory PriorityLevelConfiguration %s is up-to-date due to %v, will retry later" , expectedPriorityLevelConfiguration .Name , err )
218
+ }
219
+ if ! identical {
220
+ if _ , err := flowcontrolClientSet .PriorityLevelConfigurations ().Update (expectedPriorityLevelConfiguration ); err != nil {
221
+ return fmt .Errorf ("failed upgrading mandatory PriorityLevelConfiguration %s due to %v, will retry later" , expectedPriorityLevelConfiguration .Name , err )
222
+ }
223
+ }
224
+ continue
199
225
}
200
- // TODO(yue9944882): extract existing version from label and compare
201
- // TODO(yue9944882): create w/ version string attached
202
- _ , err = flowcontrolClientSet .PriorityLevelConfigurations ().Create (priorityLevelConfiguration )
226
+ if ! apierrors .IsNotFound (err ) {
227
+ return fmt .Errorf ("failed getting PriorityLevelConfiguration %s due to %v, will retry later" , expectedPriorityLevelConfiguration .Name , err )
228
+ }
229
+ _ , err = flowcontrolClientSet .PriorityLevelConfigurations ().Create (expectedPriorityLevelConfiguration )
203
230
if apierrors .IsAlreadyExists (err ) {
204
- klog .V (3 ).Infof ("system preset PriorityLevelConfiguration %s already exists, skipping creating" , priorityLevelConfiguration .Name )
231
+ klog .V (3 ).Infof ("system preset PriorityLevelConfiguration %s already exists, skipping creating" , expectedPriorityLevelConfiguration .Name )
205
232
continue
206
233
}
207
234
if err != nil {
208
- return fmt .Errorf ("cannot create PriorityLevelConfiguration %s due to %v" , priorityLevelConfiguration .Name , err )
235
+ return fmt .Errorf ("cannot create PriorityLevelConfiguration %s due to %v" , expectedPriorityLevelConfiguration .Name , err )
209
236
}
210
- klog .V (3 ).Infof ("created system preset PriorityLevelConfiguration %s" , priorityLevelConfiguration .Name )
237
+ klog .V (3 ).Infof ("created system preset PriorityLevelConfiguration %s" , expectedPriorityLevelConfiguration .Name )
211
238
}
212
239
return nil
213
240
}
241
+
242
+ func flowSchemaHasWrongSpec (expected , actual * flowcontrolv1alpha1.FlowSchema ) (bool , error ) {
243
+ copiedExpectedFlowSchema := expected .DeepCopy ()
244
+ flowcontrolapisv1alpha1 .SetObjectDefaults_FlowSchema (copiedExpectedFlowSchema )
245
+ return ! equality .Semantic .DeepEqual (copiedExpectedFlowSchema .Spec , actual .Spec ), nil
246
+ }
247
+
248
+ func priorityLevelHasWrongSpec (expected , actual * flowcontrolv1alpha1.PriorityLevelConfiguration ) (bool , error ) {
249
+ copiedExpectedPriorityLevel := expected .DeepCopy ()
250
+ flowcontrolapisv1alpha1 .SetObjectDefaults_PriorityLevelConfiguration (copiedExpectedPriorityLevel )
251
+ return ! equality .Semantic .DeepEqual (copiedExpectedPriorityLevel .Spec , actual .Spec ), nil
252
+ }
0 commit comments