Skip to content

Commit

Permalink
feat(validation): Dynamic validations (#30)
Browse files Browse the repository at this point in the history
* wip

* feat(validation): dynamic validations

* feat(validations): return list of problems

* feat(validations): Rename to HasEnabled and DynamicValidationService
  • Loading branch information
ncknt authored and german-muzquiz committed Nov 19, 2019
1 parent 49c29af commit a6df74e
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

@EqualsAndHashCode(callSuper = true)
@Data
public abstract class ArtifactProvider<A extends ArtifactAccount> extends Node {
public abstract class ArtifactProvider<A extends ArtifactAccount> extends Node
implements HasEnabled {
boolean enabled = false;
List<A> accounts = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public abstract class Ci<T extends CIAccount> extends Node implements Cloneable {
public abstract class Ci<T extends CIAccount> extends Node implements Cloneable, HasEnabled {
boolean enabled;

public abstract List<T> listAccounts();
Expand Down
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@EqualsAndHashCode(callSuper = false)
@Data
public abstract class MetricStore extends Node {
public abstract class MetricStore extends Node implements HasEnabled {
boolean enabled;

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,8 @@ public NodeFilter setTelemetry() {
public NodeFilter() {
withAnyHalconfigFile();
}

public NodeFilter(Class c) {
matchers.add(Node.thisNodeAcceptor(c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public abstract class Notification extends Node implements Cloneable {
public abstract class Notification extends Node implements Cloneable, HasEnabled {
@ValidForSpinnakerVersion(
lowerBound = "1.4.0",
tooLowMessage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.netflix.spinnaker.halyard.config.model.v1.node;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import java.net.URI;
import java.util.Arrays;
Expand All @@ -25,7 +26,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public abstract class PersistentStore extends Node {
public abstract class PersistentStore extends Node implements HasEnabled {
@Override
public String getNodeName() {
return persistentStoreType().getId();
Expand All @@ -40,6 +41,14 @@ public NodeIterator getChildren() {

public void setConnectionInfo(URI uri) {}

@JsonIgnore
public boolean isEnabled() {
if (parent != null && parent instanceof PersistentStorage) {
return ((PersistentStorage) parent).getPersistentStoreType() == persistentStoreType();
}
return false;
}

public enum PersistentStoreType {
AZS("azs"),
GCS("gcs"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public class Plugins extends Node {
public class Plugins extends Node implements HasEnabled {
private List<Plugin> plugins = new ArrayList<>();
private boolean enabled;
private boolean downloadingEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public abstract class Provider<A extends Account> extends Node implements Cloneable {
public abstract class Provider<A extends Account> extends Node implements Cloneable, HasEnabled {
boolean enabled = false;
List<A> accounts = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@Data
@EqualsAndHashCode(callSuper = false)
public abstract class Pubsub<S extends Subscription, P extends Publisher> extends Node
implements Cloneable {
implements Cloneable, HasEnabled {

boolean enabled = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@

@Data
@EqualsAndHashCode(callSuper = false)
public class Pubsubs extends Node implements Cloneable {
public class Pubsubs extends Node implements Cloneable, HasEnabled {
private Boolean enabled;
private GooglePubsub google = new GooglePubsub();

public boolean isEnabled() {
return enabled == null || !enabled.booleanValue();
}

@Override
public String getNodeName() {
return "pubsub";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public abstract class Repository<T extends Search> extends Node implements Cloneable {
public abstract class Repository<T extends Search> extends Node implements Cloneable, HasEnabled {
boolean enabled;
List<T> searches = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

@Data
@EqualsAndHashCode(callSuper = false)
public class Telemetry extends Node {
public class Telemetry extends Node implements HasEnabled {

public static String DEFAULT_TELEMETRY_ENDPOINT = "https://stats.spinnaker.io";

Expand All @@ -31,7 +31,7 @@ public String getNodeName() {
return "telemetry";
}

private Boolean enabled = false;
private boolean enabled = false;
private String endpoint = DEFAULT_TELEMETRY_ENDPOINT;
private String instanceId = new ULID().nextULID();
private String spinnakerVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package com.netflix.spinnaker.halyard.config.validate.v1;

import com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration;
import com.netflix.spinnaker.halyard.config.model.v1.node.Node;
import com.netflix.spinnaker.halyard.config.model.v1.node.ValidForSpinnakerVersion;
import com.netflix.spinnaker.halyard.config.model.v1.node.Validator;
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 com.netflix.spinnaker.halyard.core.registry.v1.Versions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.netflix.spinnaker.halyard.core.error.v1.HalException;
import com.netflix.spinnaker.halyard.core.job.v1.DaemonLocalJobExecutor;
import com.netflix.spinnaker.halyard.core.job.v1.JobExecutor;
import com.netflix.spinnaker.halyard.core.job.v1.JobExecutorLocal;
import com.netflix.spinnaker.halyard.core.problem.v1.ProblemSet;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
Expand Down Expand Up @@ -116,7 +117,7 @@ public static <C, T> DaemonTask<C, T> submitTask(

public static JobExecutor getJobExecutor() {
if (getTask() == null) {
throw new IllegalStateException("Cannot request a job executor from outside a daemon task");
return new JobExecutorLocal();
}

if (jobExecutor == null) {
Expand Down
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();
}
}
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;
}
}
}
Loading

0 comments on commit a6df74e

Please sign in to comment.