66package io .stackgres .operator .validation .script ;
77
88import java .util .List ;
9- import java .util .Map ;
10- import java .util .Objects ;
119import java .util .Optional ;
12- import java .util .Set ;
1310import java .util .stream .Collectors ;
1411
15- import com .google .common .collect .ImmutableSet ;
16- import io .fabric8 .kubernetes .api .model .ConfigMap ;
17- import io .fabric8 .kubernetes .api .model .ConfigMapKeySelector ;
18- import io .fabric8 .kubernetes .api .model .Secret ;
19- import io .fabric8 .kubernetes .api .model .SecretKeySelector ;
2012import io .stackgres .common .ErrorType ;
2113import io .stackgres .common .crd .sgscript .StackGresScript ;
2214import io .stackgres .common .crd .sgscript .StackGresScriptEntry ;
2315import io .stackgres .common .crd .sgscript .StackGresScriptEntryStatus ;
24- import io .stackgres .common .crd .sgscript .StackGresScriptFrom ;
2516import io .stackgres .common .crd .sgscript .StackGresScriptSpec ;
2617import io .stackgres .common .crd .sgscript .StackGresScriptStatus ;
27- import io .stackgres .common .resource .ResourceFinder ;
2818import io .stackgres .operator .common .StackGresScriptReview ;
2919import io .stackgres .operator .validation .ValidationType ;
3020import io .stackgres .operatorframework .admissionwebhook .Operation ;
@@ -39,19 +29,8 @@ public class ScriptsConfigValidator implements ScriptValidator {
3929 private final String constraintViolationUri = ErrorType
4030 .getErrorTypeUri (ErrorType .CONSTRAINT_VIOLATION );
4131
42- private final String invalidReference = ErrorType
43- .getErrorTypeUri (ErrorType .INVALID_CR_REFERENCE );
44-
45- private final ResourceFinder <Secret > secretFinder ;
46-
47- private final ResourceFinder <ConfigMap > configMapFinder ;
48-
4932 @ Inject
50- public ScriptsConfigValidator (
51- ResourceFinder <Secret > secretFinder ,
52- ResourceFinder <ConfigMap > configMapFinder ) {
53- this .secretFinder = secretFinder ;
54- this .configMapFinder = configMapFinder ;
33+ public ScriptsConfigValidator () {
5534 }
5635
5736 @ Override
@@ -68,8 +47,6 @@ public void validate(StackGresScriptReview review) throws ValidationFailed {
6847 .orElse (List .of ());
6948 checkIdsUniqueness (scripts );
7049 checkStatusIdsCorrelation (scripts , scriptsStatuses );
71- checkSecretKeySelectors (review , scripts );
72- checkConfigMapsSelectors (review , scripts );
7350 }
7451 }
7552
@@ -103,72 +80,4 @@ private void checkStatusIdsCorrelation(List<StackGresScriptEntry> scripts,
10380 }
10481 }
10582
106- private void checkConfigMapsSelectors (StackGresScriptReview review ,
107- List <StackGresScriptEntry > scripts )
108- throws ValidationFailed {
109- String clusterNamespace = review .getRequest ().getObject ().getMetadata ().getNamespace ();
110-
111- List <ConfigMapKeySelector > configMapsSelectors = scripts .stream ()
112- .map (StackGresScriptEntry ::getScriptFrom )
113- .filter (Objects ::nonNull )
114- .<ConfigMapKeySelector >map (StackGresScriptFrom ::getConfigMapKeyRef )
115- .filter (Objects ::nonNull )
116- .toList ();
117-
118- for (ConfigMapKeySelector configMapRef : configMapsSelectors ) {
119- Optional <ConfigMap > scriptConfigMap = configMapFinder
120- .findByNameAndNamespace (configMapRef .getName (), clusterNamespace );
121-
122- if (scriptConfigMap .isEmpty ()) {
123- fail (invalidReference ,
124- "Referenced ConfigMap " + configMapRef .getName ()
125- + " does not exists in namespace " + clusterNamespace );
126- } else {
127- Set <String > configMapKeys = ImmutableSet .<String >builder ()
128- .addAll (Optional .ofNullable (scriptConfigMap .get ().getData ())
129- .map (Map ::keySet )
130- .orElse (Set .of ()))
131- .addAll (Optional .ofNullable (scriptConfigMap .get ().getBinaryData ())
132- .map (Map ::keySet )
133- .orElse (Set .of ()))
134- .build ();
135-
136- if (!configMapKeys .contains (configMapRef .getKey ())) {
137- fail (invalidReference , "Key " + configMapRef .getKey ()
138- + " does not exists in ConfigMap " + configMapRef .getName ());
139- }
140- }
141-
142- }
143-
144- }
145-
146- private void checkSecretKeySelectors (StackGresScriptReview review ,
147- List <StackGresScriptEntry > scripts )
148- throws ValidationFailed {
149- String clusterNamespace = review .getRequest ().getObject ().getMetadata ().getNamespace ();
150-
151- List <SecretKeySelector > secretKeySelectors = scripts .stream ()
152- .map (StackGresScriptEntry ::getScriptFrom )
153- .filter (Objects ::nonNull )
154- .<SecretKeySelector >map (StackGresScriptFrom ::getSecretKeyRef )
155- .filter (Objects ::nonNull )
156- .toList ();
157-
158- for (SecretKeySelector secretRef : secretKeySelectors ) {
159- Optional <Secret > scriptSecret = secretFinder
160- .findByNameAndNamespace (secretRef .getName (), clusterNamespace );
161-
162- if (scriptSecret .isEmpty ()) {
163- fail (invalidReference ,
164- "Referenced Secret " + secretRef .getName ()
165- + " does not exists in namespace " + clusterNamespace );
166- } else if (!scriptSecret .get ().getData ().containsKey (secretRef .getKey ())) {
167- fail (invalidReference ,
168- "Key " + secretRef .getKey ()
169- + " does not exists in Secret " + secretRef .getName ());
170- }
171- }
172- }
173-
17483}
0 commit comments