Search in sources :

Example 1 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class LookupService method getMatchingNodes.

/**
 * @param node is the node whose children we want to find.
 * @param filter is the filter to lookup by.
 * @return the children of the input node matching the filter.
 */
private List<Node> getMatchingNodes(Node node, NodeFilter filter) {
    log.trace("Checking for leaf nodes of node " + node.getNodeName());
    List<Node> result = new ArrayList<>();
    NodeIterator children = node.getChildren();
    Node recurse = children.getNext(filter);
    while (recurse != null) {
        result.addAll(getMatchingNodes(recurse, filter));
        recurse = children.getNext(filter);
    }
    // If we have visited this node, it must have matched the filter.
    result.add(node);
    return result;
}
Also used : NodeIterator(com.netflix.spinnaker.halyard.config.model.v1.node.NodeIterator) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) ArrayList(java.util.ArrayList)

Example 2 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class DCOSAccountValidator method validate.

@Override
public void validate(final ConfigProblemSetBuilder problems, final DCOSAccount account) {
    DeploymentConfiguration deploymentConfiguration;
    /**
     * I have copied
     * the code
     * that was in
     * the KubernetesAccountValidator
     *
     * and which
     * you were planning
     * to refactor
     * with filters
     *
     * Forgive me
     * It did the job
     * And I was lazy
     * so very lazy
     */
    // TODO(lwander) this is still a little messy - I should use the filters to get the necessary docker account
    Node parent = account.getParent();
    while (!(parent instanceof DeploymentConfiguration)) {
        // Note this will crash in the above check if the halconfig representation is corrupted
        // (that's ok, because it indicates a more serious error than we want to validate).
        parent = parent.getParent();
    }
    deploymentConfiguration = (DeploymentConfiguration) parent;
    validateClusters(problems, account);
    if (account.getClusters().isEmpty()) {
        problems.addProblem(ERROR, "Account does not have any clusters configured").setRemediation("Edit the account with either --update-user-credential or --update-service-credential");
    }
    final List<String> dockerRegistryNames = account.getDockerRegistries().stream().map(DockerRegistryReference::getAccountName).collect(Collectors.toList());
    validateDockerRegistries(problems, deploymentConfiguration, dockerRegistryNames, Provider.ProviderType.DCOS);
}
Also used : Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) DeploymentConfiguration(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)

Example 3 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class ValidatorCollection method runAllValidators.

/**
 * Runs every validator defined against the given node.
 *
 * @param psBuilder contains the problems encountered during validation so far.
 * @param node is the node being validated.
 *
 * @return # of validators run (for logging purposes).
 */
public int runAllValidators(ConfigProblemSetBuilder psBuilder, Node node) {
    psBuilder.setNode(node);
    int validatorRuns = 0;
    for (Validator validator : validators) {
        validatorRuns += runMatchingValidators(psBuilder, validator, node, node.getClass()) ? 1 : 0;
    }
    return validatorRuns;
}
Also used : Validator(com.netflix.spinnaker.halyard.config.model.v1.node.Validator)

Example 4 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class Node method serializedNonNodeFields.

private Map<String, Object> serializedNonNodeFields() {
    List<Field> fields = Arrays.stream(this.getClass().getDeclaredFields()).filter(f -> {
        return (!(Node.class.isAssignableFrom(f.getType()) || List.class.isAssignableFrom(f.getType()) || Map.class.isAssignableFrom(f.getType()) || f.getAnnotation(JsonIgnore.class) != null));
    }).collect(Collectors.toList());
    Map<String, Object> res = new HashMap<>();
    for (Field field : fields) {
        field.setAccessible(true);
        try {
            res.put(field.getName(), field.get(this));
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to read field " + field.getName() + " in node " + getNodeName());
        }
        field.setAccessible(false);
    }
    return res;
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) HashMap(java.util.HashMap) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) ArrayList(java.util.ArrayList) REMOVED(com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.REMOVED) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GlobalApplicationOptions(com.netflix.spinnaker.halyard.core.GlobalApplicationOptions) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) EDITED(com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.EDITED) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ADDED(com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.ADDED) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) Paths(java.nio.file.Paths) CRC32(java.util.zip.CRC32) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Node

use of com.netflix.spinnaker.halyard.config.model.v1.node.Node in project halyard by spinnaker.

the class HalconfigParser method cleanLocalFiles.

/**
 * Deletes all files in the staging directory that are not referenced in the hal config.
 */
public void cleanLocalFiles(Path stagingDirectoryPath) {
    if (!GlobalApplicationOptions.getInstance().isUseRemoteDaemon()) {
        return;
    }
    Halconfig halconfig = getHalconfig();
    Set<String> referencedFiles = new HashSet<String>();
    Consumer<Node> fileFinder = n -> referencedFiles.addAll(n.localFiles().stream().map(f -> {
        try {
            f.setAccessible(true);
            return (String) f.get(n);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to clean staging directory: " + e.getMessage(), e);
        } finally {
            f.setAccessible(false);
        }
    }).filter(Objects::nonNull).collect(Collectors.toSet()));
    halconfig.recursiveConsume(fileFinder);
    Set<String> existingStagingFiles = ((List<File>) FileUtils.listFiles(stagingDirectoryPath.toFile(), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)).stream().map(f -> f.getAbsolutePath()).collect(Collectors.toSet());
    existingStagingFiles.removeAll(referencedFiles);
    try {
        for (String f : existingStagingFiles) {
            FileUtils.forceDelete(new File(f));
        }
    } catch (IOException e) {
        throw new HalException(FATAL, "Failed to clean staging directory: " + e.getMessage(), e);
    }
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) Yaml(org.yaml.snakeyaml.Yaml) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ParseConfigException(com.netflix.spinnaker.halyard.config.error.v1.ParseConfigException) Severity(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) Path(java.nio.file.Path) GlobalApplicationOptions(com.netflix.spinnaker.halyard.core.GlobalApplicationOptions) ScannerException(org.yaml.snakeyaml.scanner.ScannerException) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) AtomicFileWriter(com.netflix.spinnaker.halyard.core.AtomicFileWriter) Objects(java.util.Objects) Consumer(java.util.function.Consumer) ConfigProblemBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) FATAL(com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL) Paths(java.nio.file.Paths) ParserException(org.yaml.snakeyaml.parser.ParserException) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) InputStream(java.io.InputStream) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) Node(com.netflix.spinnaker.halyard.config.model.v1.node.Node) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) IOException(java.io.IOException) Objects(java.util.Objects) List(java.util.List) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

Node (com.netflix.spinnaker.halyard.config.model.v1.node.Node)7 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)5 List (java.util.List)5 DeploymentConfiguration (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentConfiguration)4 ConfigProblemSetBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 NodeIterator (com.netflix.spinnaker.halyard.config.model.v1.node.NodeIterator)3 GlobalApplicationOptions (com.netflix.spinnaker.halyard.core.GlobalApplicationOptions)3 FATAL (com.netflix.spinnaker.halyard.core.problem.v1.Problem.Severity.FATAL)3 File (java.io.File)3 IOException (java.io.IOException)3 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ADDED (com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.ADDED)2 EDITED (com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.EDITED)2 REMOVED (com.netflix.spinnaker.halyard.config.model.v1.node.NodeDiff.ChangeType.REMOVED)2 NodeFilter (com.netflix.spinnaker.halyard.config.model.v1.node.NodeFilter)2 Validator (com.netflix.spinnaker.halyard.config.model.v1.node.Validator)2