@@ -3,7 +3,7 @@ type settingsLike = Record<string, any>;
33export default class SettingsValidator {
44 k8sVersions : Array < string > = [ ] ;
55 allowedSettings : settingsLike | null = null ;
6- correctionsTable : settingsLike | null = null ;
6+ synonymsTable : settingsLike | null = null ;
77
88 validateSettings ( currentSettings : settingsLike , newSettings : settingsLike ) : [ boolean , string [ ] ] {
99 this . allowedSettings ||= {
@@ -28,6 +28,7 @@ export default class SettingsValidator {
2828 updater : this . checkUnchanged ,
2929 debug : this . checkUnchanged
3030 } ;
31+ this . canonicalizeSynonyms ( newSettings ) ;
3132 const errors : Array < string > = [ ] ;
3233 const needToUpdate = this . checkProposedSettings ( this . allowedSettings , currentSettings , newSettings , errors , '' ) ;
3334
@@ -88,14 +89,9 @@ export default class SettingsValidator {
8889 }
8990
9091 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.
9995 errors . push ( `Invalid value for ${ fqname } : <${ desiredEngine } >; must be 'containerd', 'docker', or 'moby'` ) ;
10096
10197 return false ;
@@ -106,38 +102,16 @@ export default class SettingsValidator {
106102
107103 protected checkEnabled ( currentState : boolean , desiredState : string | boolean , errors : string [ ] , fqname : string ) : boolean {
108104 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 ;
121108 }
122109
123110 return currentState !== desiredState ;
124111 }
125112
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 ) ) {
141115 errors . push ( `Kubernetes version ${ desiredVersion } not found.` ) ;
142116
143117 return false ;
@@ -180,33 +154,33 @@ export default class SettingsValidator {
180154 return false ;
181155 }
182156
183- correctSynonymValues ( newSettings : settingsLike ) : void {
184- this . correctionsTable ||= {
157+ canonicalizeSynonyms ( newSettings : settingsLike ) : void {
158+ this . synonymsTable ||= {
185159 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 ,
189163 }
190164 } ;
191- this . correctSettings ( this . correctionsTable , newSettings , '' ) ;
165+ this . canonicalizeSettings ( this . synonymsTable , newSettings , '' ) ;
192166 }
193167
194- protected correctSettings ( correctionsTable : settingsLike , newSettings : settingsLike , prefix : string ) : void {
168+ protected canonicalizeSettings ( synonymsTable : settingsLike , newSettings : settingsLike , prefix : string ) : void {
195169 for ( const k in newSettings ) {
196170 const fqname = prefix ? `${ prefix } .${ k } ` : k ;
197171
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 ) ;
201175 } else {
202- correctionsTable [ k ] . call ( this , newSettings , k ) ;
176+ synonymsTable [ k ] . call ( this , newSettings , k ) ;
203177 }
204178 // else: ignore unrecognized fields, because we don't need to change everything
205179 }
206180 }
207181 }
208182
209- protected correctKubernetesVersion ( newSettings : settingsLike , index : string ) : void {
183+ protected canonicalizeKubernetesVersion ( newSettings : settingsLike , index : string ) : void {
210184 const desiredValue : string = newSettings [ index ] ;
211185 const ptn = / ^ ( v ? ) ( \d + \. \d + \. \d + ) ( (?: \+ k 3 s \d + ) ? ) $ / ;
212186 const m = ptn . exec ( desiredValue ) ;
@@ -216,13 +190,13 @@ export default class SettingsValidator {
216190 }
217191 }
218192
219- protected correctContainerEngine ( newSettings : settingsLike , index : string ) : void {
193+ protected canonicalizeContainerEngine ( newSettings : settingsLike , index : string ) : void {
220194 if ( newSettings [ index ] === 'docker' ) {
221195 newSettings [ index ] = 'moby' ;
222196 }
223197 }
224198
225- protected correctKubernetesEnabled ( newSettings : settingsLike , index : string ) : void {
199+ protected canonicalizeKubernetesEnabled ( newSettings : settingsLike , index : string ) : void {
226200 const desiredValue : boolean | string = newSettings [ index ] ;
227201
228202 if ( desiredValue === 'true' ) {
0 commit comments