forked from spinnaker/halyard
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): Dynamic validations (#30)
* wip * feat(validation): dynamic validations * feat(validations): return list of problems * feat(validations): Rename to HasEnabled and DynamicValidationService
- Loading branch information
1 parent
49c29af
commit a6df74e
Showing
18 changed files
with
283 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...d-config/src/main/java/com/netflix/spinnaker/halyard/config/model/v1/node/HasEnabled.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2019 Armory, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.halyard.config.model.v1.node; | ||
|
||
public interface HasEnabled { | ||
boolean isEnabled(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
.../main/java/com/netflix/spinnaker/halyard/deploy/services/v1/DynamicValidationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2019 Armory, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.halyard.deploy.services.v1; | ||
|
||
import com.netflix.spinnaker.halyard.config.config.v1.HalconfigParser; | ||
import com.netflix.spinnaker.halyard.config.model.v1.node.*; | ||
import com.netflix.spinnaker.halyard.core.problem.v1.Problem; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartHttpServletRequest; | ||
|
||
@Slf4j | ||
@Component | ||
public class DynamicValidationService { | ||
@Autowired HalconfigParser halconfigParser; | ||
@Autowired ApplicationContext applicationContext; | ||
@Autowired List<Validator> allValidators = new ArrayList<>(); | ||
|
||
public List<Problem> validate( | ||
MultipartHttpServletRequest request, List<String> skipValidators, boolean failFast) | ||
throws IOException { | ||
RequestGenerateService fileRequestService = newRequestGenerateService(); | ||
try { | ||
log.info("Preparing Halyard configuration from incoming request"); | ||
fileRequestService.prepare(request); | ||
|
||
log.info("Parsing Halyard config"); | ||
Halconfig halConfig = halconfigParser.getHalconfig(); | ||
|
||
List<Validator> validators = | ||
allValidators.stream() | ||
.filter(v -> !skipValidators.contains(v.getClass().getSimpleName())) | ||
.collect(Collectors.toList()); | ||
|
||
log.info("Running {} validators", validators.size()); | ||
ValidationRun run = | ||
new ValidationRun( | ||
validators, | ||
applicationContext, | ||
halConfig.getDeploymentConfigurations().get(0), | ||
failFast); | ||
return run.run(); | ||
} finally { | ||
fileRequestService.cleanup(); | ||
} | ||
} | ||
|
||
protected RequestGenerateService newRequestGenerateService() { | ||
return new RequestGenerateService(); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...-deploy/src/main/java/com/netflix/spinnaker/halyard/deploy/services/v1/ValidationRun.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2019 Armory, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License") | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.halyard.deploy.services.v1; | ||
|
||
import com.netflix.spinnaker.halyard.config.model.v1.node.*; | ||
import com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder; | ||
import com.netflix.spinnaker.halyard.core.problem.v1.Problem; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.lang.reflect.TypeVariable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
@RequiredArgsConstructor | ||
public class ValidationRun { | ||
private final List<Validator> validators; | ||
private final ApplicationContext applicationContext; | ||
private final DeploymentConfiguration deploymentConfiguration; | ||
private final boolean failFast; | ||
private List<NodeValidator> nodeValidators; | ||
private List<Problem> results = new ArrayList<>(); | ||
|
||
public List<Problem> run() { | ||
nodeValidators = | ||
validators.stream() | ||
.map(v -> new NodeValidator(v, getNodeFilter(v))) | ||
.collect(Collectors.toList()); | ||
visitNode(deploymentConfiguration); | ||
return results; | ||
} | ||
|
||
private boolean visitNode(Node node) { | ||
if (!validateNode(node)) { | ||
return false; | ||
} | ||
|
||
NodeIterator children = node.getChildren(); | ||
Node recurse; | ||
while ((recurse = children.getNext()) != null) { | ||
// Don't visit disabled features | ||
if (recurse instanceof HasEnabled && !((HasEnabled) recurse).isEnabled()) { | ||
continue; | ||
} | ||
if (!visitNode(recurse)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private boolean validateNode(Node node) { | ||
for (NodeValidator nodeValidator : nodeValidators) { | ||
ConfigProblemSetBuilder psBuilder = nodeValidator.validate(node); | ||
if (psBuilder != null) { | ||
List<Problem> problems = psBuilder.build().getProblems(); | ||
results.addAll(problems); | ||
if (failFast | ||
&& problems.stream() | ||
.anyMatch( | ||
p -> | ||
p.getSeverity() == Problem.Severity.ERROR | ||
|| p.getSeverity() == Problem.Severity.FATAL)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private NodeFilter getNodeFilter(Validator validator) { | ||
Type type = | ||
((ParameterizedType) validator.getClass().getGenericSuperclass()) | ||
.getActualTypeArguments()[0]; | ||
if (type instanceof TypeVariable) { | ||
type = ((TypeVariable) type).getBounds()[0]; | ||
} | ||
if (type instanceof Class) { | ||
return new NodeFilter((Class) type); | ||
} | ||
throw new IllegalArgumentException( | ||
"Unable to determine validator type of " + validator.getClass().getName()); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private class NodeValidator<T extends Node> { | ||
private final Validator<T> validator; | ||
private final NodeFilter filter; | ||
|
||
public ConfigProblemSetBuilder validate(T node) { | ||
if (filter.matches(node)) { | ||
ConfigProblemSetBuilder psBuilder = new ConfigProblemSetBuilder(applicationContext); | ||
psBuilder.setNode(node); | ||
validator.validate(psBuilder, node); | ||
return psBuilder; | ||
} | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.