@@ -3,7 +3,7 @@ type settingsLike = Record<string, any>;
3
3
export default class SettingsValidator {
4
4
k8sVersions : Array < string > = [ ] ;
5
5
allowedSettings : settingsLike | null = null ;
6
- correctionsTable : settingsLike | null = null ;
6
+ synonymsTable : settingsLike | null = null ;
7
7
8
8
validateSettings ( currentSettings : settingsLike , newSettings : settingsLike ) : [ boolean , string [ ] ] {
9
9
this . allowedSettings ||= {
@@ -28,6 +28,7 @@ export default class SettingsValidator {
28
28
updater : this . checkUnchanged ,
29
29
debug : this . checkUnchanged
30
30
} ;
31
+ this . canonicalizeSynonyms ( newSettings ) ;
31
32
const errors : Array < string > = [ ] ;
32
33
const needToUpdate = this . checkProposedSettings ( this . allowedSettings , currentSettings , newSettings , errors , '' ) ;
33
34
@@ -88,14 +89,9 @@ export default class SettingsValidator {
88
89
}
89
90
90
91
protected checkContainerEngine ( currentValue : string , desiredEngine : string , errors : string [ ] , fqname : string ) : boolean {
91
- switch ( desiredEngine ) {
92
- case 'containerd' :
93
- case 'moby' :
94
- break ;
95
- case 'docker' :
96
- desiredEngine = 'moby' ;
97
- break ;
98
- default :
92
+ if ( ! [ 'containerd' , 'moby' ] . includes ( desiredEngine ) ) {
93
+ // The error message says 'docker' is ok, although it should have been converted to 'moby' by now.
94
+ // But the word "'docker'" is valid in a raw API call.
99
95
errors . push ( `Invalid value for ${ fqname } : <${ desiredEngine } >; must be 'containerd', 'docker', or 'moby'` ) ;
100
96
101
97
return false ;
@@ -106,38 +102,16 @@ export default class SettingsValidator {
106
102
107
103
protected checkEnabled ( currentState : boolean , desiredState : string | boolean , errors : string [ ] , fqname : string ) : boolean {
108
104
if ( typeof ( desiredState ) !== 'boolean' ) {
109
- switch ( desiredState ) {
110
- case 'true' :
111
- desiredState = true ;
112
- break ;
113
- case 'false' :
114
- desiredState = false ;
115
- break ;
116
- default :
117
- errors . push ( `Invalid value for ${ fqname } : <${ desiredState } >` ) ;
118
-
119
- return false ;
120
- }
105
+ errors . push ( `Invalid value for ${ fqname } : <${ desiredState } >` ) ;
106
+
107
+ return false ;
121
108
}
122
109
123
110
return currentState !== desiredState ;
124
111
}
125
112
126
- protected checkKubernetesVersion ( currentValue : string , desiredVersion : string , errors : string [ ] , fqname : string ) : boolean {
127
- const ptn = / ^ v ? ( \d + \. \d + \. \d + ) (?: \+ k 3 s \d + ) ? $ / ;
128
- const m = ptn . exec ( desiredVersion ) ;
129
-
130
- if ( ! m ) {
131
- errors . push ( `Desired kubernetes version not valid: <${ desiredVersion } >` ) ;
132
-
133
- return false ;
134
- }
135
- desiredVersion = m [ 1 ] ;
136
- if ( this . k8sVersions . length === 0 ) {
137
- errors . push ( `Can't check field ${ fqname } : no versions of Kubernetes were found.` ) ;
138
-
139
- return false ;
140
- } else if ( ! this . k8sVersions . includes ( desiredVersion ) ) {
113
+ protected checkKubernetesVersion ( currentValue : string , desiredVersion : string , errors : string [ ] , _ : string ) : boolean {
114
+ if ( ! this . k8sVersions . includes ( desiredVersion ) ) {
141
115
errors . push ( `Kubernetes version ${ desiredVersion } not found.` ) ;
142
116
143
117
return false ;
@@ -180,33 +154,33 @@ export default class SettingsValidator {
180
154
return false ;
181
155
}
182
156
183
- correctSynonymValues ( newSettings : settingsLike ) : void {
184
- this . correctionsTable ||= {
157
+ canonicalizeSynonyms ( newSettings : settingsLike ) : void {
158
+ this . synonymsTable ||= {
185
159
kubernetes : {
186
- version : this . correctKubernetesVersion ,
187
- containerEngine : this . correctContainerEngine ,
188
- enabled : this . correctKubernetesEnabled ,
160
+ version : this . canonicalizeKubernetesVersion ,
161
+ containerEngine : this . canonicalizeContainerEngine ,
162
+ enabled : this . canonicalizeKubernetesEnabled ,
189
163
}
190
164
} ;
191
- this . correctSettings ( this . correctionsTable , newSettings , '' ) ;
165
+ this . canonicalizeSettings ( this . synonymsTable , newSettings , '' ) ;
192
166
}
193
167
194
- protected correctSettings ( correctionsTable : settingsLike , newSettings : settingsLike , prefix : string ) : void {
168
+ protected canonicalizeSettings ( synonymsTable : settingsLike , newSettings : settingsLike , prefix : string ) : void {
195
169
for ( const k in newSettings ) {
196
170
const fqname = prefix ? `${ prefix } .${ k } ` : k ;
197
171
198
- if ( k in correctionsTable ) {
199
- if ( typeof ( correctionsTable [ k ] ) === 'object' ) {
200
- return this . correctSettings ( correctionsTable [ k ] , newSettings [ k ] , fqname ) ;
172
+ if ( k in synonymsTable ) {
173
+ if ( typeof ( synonymsTable [ k ] ) === 'object' ) {
174
+ return this . canonicalizeSettings ( synonymsTable [ k ] , newSettings [ k ] , fqname ) ;
201
175
} else {
202
- correctionsTable [ k ] . call ( this , newSettings , k ) ;
176
+ synonymsTable [ k ] . call ( this , newSettings , k ) ;
203
177
}
204
178
// else: ignore unrecognized fields, because we don't need to change everything
205
179
}
206
180
}
207
181
}
208
182
209
- protected correctKubernetesVersion ( newSettings : settingsLike , index : string ) : void {
183
+ protected canonicalizeKubernetesVersion ( newSettings : settingsLike , index : string ) : void {
210
184
const desiredValue : string = newSettings [ index ] ;
211
185
const ptn = / ^ ( v ? ) ( \d + \. \d + \. \d + ) ( (?: \+ k 3 s \d + ) ? ) $ / ;
212
186
const m = ptn . exec ( desiredValue ) ;
@@ -216,13 +190,13 @@ export default class SettingsValidator {
216
190
}
217
191
}
218
192
219
- protected correctContainerEngine ( newSettings : settingsLike , index : string ) : void {
193
+ protected canonicalizeContainerEngine ( newSettings : settingsLike , index : string ) : void {
220
194
if ( newSettings [ index ] === 'docker' ) {
221
195
newSettings [ index ] = 'moby' ;
222
196
}
223
197
}
224
198
225
- protected correctKubernetesEnabled ( newSettings : settingsLike , index : string ) : void {
199
+ protected canonicalizeKubernetesEnabled ( newSettings : settingsLike , index : string ) : void {
226
200
const desiredValue : boolean | string = newSettings [ index ] ;
227
201
228
202
if ( desiredValue === 'true' ) {
0 commit comments