use of com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder in project halyard by spinnaker.
the class ArtifactService method writeArtifactConfig.
public void writeArtifactConfig(String bomPath, String artifactName, String profilePath) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL, "You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify base-profiles.").build());
}
BillOfMaterials bom;
File profileFile = Paths.get(profilePath).toFile();
String profileContents;
try {
bom = relaxedObjectMapper.convertValue(yamlParser.load(IOUtils.toString(new FileInputStream(bomPath))), BillOfMaterials.class);
} catch (IOException e) {
throw new HalException(new ConfigProblemBuilder(FATAL, "Unable to load Bill of Materials: " + e.getMessage()).build());
}
try {
profileContents = IOUtils.toString(new FileInputStream(profileFile));
} catch (IOException e) {
throw new HalException(new ConfigProblemBuilder(FATAL, "Unable to load profile : " + e.getMessage()).build());
}
googleWriteableProfileRegistry.writeArtifactConfig(bom, artifactName, profileFile.getName(), profileContents);
}
use of com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder in project halyard by spinnaker.
the class ArtifactService method publishVersion.
public void publishVersion(Version version) {
if (googleWriteableProfileRegistry == null) {
throw new HalException(new ConfigProblemBuilder(FATAL, "You need to set the \"spinnaker.config.input.writerEnabled\" property to \"true\" to modify your halconfig bucket contents.").build());
}
Versions versionsCollection = versionsService.getVersions();
deleteVersion(versionsCollection, version.getVersion());
versionsCollection.getVersions().add(version);
googleWriteableProfileRegistry.writeVersions(yamlParser.dump(relaxedObjectMapper.convertValue(versionsCollection, Map.class)));
}
use of com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder in project halyard by spinnaker.
the class DockerRegistryAccountValidator method validate.
@Override
public void validate(ConfigProblemSetBuilder p, DockerRegistryAccount n) {
String resolvedPassword = null;
String password = n.getPassword();
String passwordFile = n.getPasswordFile();
String username = n.getUsername();
boolean passwordProvided = password != null && !password.isEmpty();
boolean passwordFileProvided = passwordFile != null && !passwordFile.isEmpty();
if (passwordProvided && passwordFileProvided) {
p.addProblem(Severity.ERROR, "You have provided both a password and a password file for your docker registry. You can specify at most one.");
return;
}
if (passwordProvided) {
resolvedPassword = password;
} else if (passwordFileProvided) {
resolvedPassword = ValidatingFileReader.contents(p, passwordFile);
if (resolvedPassword == null) {
return;
}
if (resolvedPassword.isEmpty()) {
p.addProblem(Severity.WARNING, "The supplied password file is empty.");
}
} else {
resolvedPassword = "";
}
if (!resolvedPassword.isEmpty()) {
if (username == null || username.isEmpty()) {
p.addProblem(Severity.WARNING, "You have supplied a password but no username.");
}
} else {
if (username != null && !username.isEmpty()) {
p.addProblem(Severity.WARNING, "You have a supplied a username but no password.");
}
}
DockerRegistryNamedAccountCredentials credentials;
try {
credentials = (new DockerRegistryNamedAccountCredentials.Builder()).accountName(n.getName()).address(n.getAddress()).email(n.getEmail()).password(n.getPassword()).passwordFile(n.getPasswordFile()).dockerconfigFile(n.getDockerconfigFile()).username(n.getUsername()).clientTimeoutMillis(n.getClientTimeoutMillis()).cacheThreads(n.getCacheThreads()).paginateSize(n.getPaginateSize()).sortTagsByDate(n.getSortTagsByDate()).trackDigests(n.getTrackDigests()).insecureRegistry(n.getInsecureRegistry()).build();
} catch (Exception e) {
p.addProblem(Severity.ERROR, "Failed to instantiate docker credentials for account \"" + n.getName() + "\".");
return;
}
ConfigProblemBuilder authFailureProblem = null;
if (n.getRepositories() == null || n.getRepositories().size() == 0) {
try {
DockerRegistryCatalog catalog = credentials.getCredentials().getClient().getCatalog();
if (catalog.getRepositories() == null || catalog.getRepositories().size() == 0) {
p.addProblem(Severity.WARNING, "Your docker registry has no repositories specified, and the registry's catalog is empty. Spinnaker will not be able to deploy any images until some are pushed to this registry.").setRemediation("Manually specify some repositories for this docker registry to index.");
}
} catch (Exception e) {
if (n.getAddress().endsWith("gcr.io")) {
p.addProblem(Severity.ERROR, "The GCR service requires the Resource Manager API to be enabled for the catalog endpoint to work.").setRemediation("Visit https://console.developers.google.com/apis/api/cloudresourcemanager.googleapis.com/overview to enable the API.");
}
authFailureProblem = p.addProblem(Severity.ERROR, "Unable to connect the registries catalog endpoint: " + e.getMessage() + ".");
}
} else {
try {
// effectively final
int[] tagCount = new int[1];
tagCount[0] = 0;
n.getRepositories().forEach(r -> tagCount[0] += credentials.getCredentials().getClient().getTags(r).getTags().size());
if (tagCount[0] == 0) {
p.addProblem(Severity.WARNING, "None of your supplied repositories contain any tags. Spinnaker will not be able to deploy anything.").setRemediation("Push some images to your registry.");
}
} catch (Exception e) {
authFailureProblem = p.addProblem(Severity.ERROR, "Unable to reach repository: " + e.getMessage() + ".");
}
}
if (authFailureProblem != null && !StringUtils.isEmpty(resolvedPassword)) {
String message = "Your registry password has %s whitespace; if this is unintentional, this may be the cause of failed authentication.";
if (Character.isWhitespace(resolvedPassword.charAt(0))) {
authFailureProblem.setRemediation(String.format(message, "leading"));
}
char c = resolvedPassword.charAt(resolvedPassword.length() - 1);
if (Character.isWhitespace(c)) {
authFailureProblem.setRemediation(String.format(message, "trailing"));
if (passwordFileProvided && c == '\n')
authFailureProblem.setRemediation("Your password file has a trailing newline; many text editors append a newline to files they open." + " If you think this is causing authentication issues, you can strip the newline with the command:\n\n" + " tr -d '\\n' < PASSWORD_FILE | tee PASSWORD_FILE");
}
}
}
use of com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder in project halyard by spinnaker.
the class KubernetesAccountValidator method validateKubeconfig.
private void validateKubeconfig(ConfigProblemSetBuilder psBuilder, KubernetesAccount account) {
io.fabric8.kubernetes.api.model.Config kubeconfig;
String context = account.getContext();
String kubeconfigFile = account.getKubeconfigFile();
String cluster = account.getCluster();
String user = account.getUser();
List<String> namespaces = account.getNamespaces();
List<String> omitNamespaces = account.getOmitNamespaces();
// This indicates if a first pass at the config looks OK. If we don't see any serious problems, we'll do one last check
// against the requested kubernetes cluster to ensure that we can run spinnaker.
boolean smoketest = true;
boolean namespacesProvided = namespaces != null && !namespaces.isEmpty();
boolean omitNamespacesProvided = omitNamespaces != null && !omitNamespaces.isEmpty();
if (namespacesProvided && omitNamespacesProvided) {
psBuilder.addProblem(ERROR, "At most one of \"namespaces\" and \"omitNamespaces\" can be supplied.");
smoketest = false;
}
// TODO(lwander) find a good resource / list of resources for generating kubeconfig files to link to here.
try {
if (ValidatingFileReader.contents(psBuilder, kubeconfigFile) == null) {
return;
}
File kubeconfigFileOpen = new File(kubeconfigFile);
kubeconfig = KubeConfigUtils.parseConfig(kubeconfigFileOpen);
} catch (IOException e) {
psBuilder.addProblem(ERROR, e.getMessage());
return;
}
System.out.println(context);
if (context != null && !context.isEmpty()) {
Optional<NamedContext> namedContext = kubeconfig.getContexts().stream().filter(c -> c.getName().equals(context)).findFirst();
if (!namedContext.isPresent()) {
psBuilder.addProblem(ERROR, "Context \"" + context + "\" not found in kubeconfig \"" + kubeconfigFile + "\".", "context").setRemediation("Either add this context to your kubeconfig, rely on the default context, or pick another kubeconfig file.");
smoketest = false;
}
} else {
String currentContext = kubeconfig.getCurrentContext();
if (StringUtils.isEmpty(currentContext)) {
psBuilder.addProblem(ERROR, "You have not specified a Kubernetes context, and your kubeconfig \"" + kubeconfigFile + "\" has no current-context.", "context").setRemediation("Either specify a context in your halconfig, or set a current-context in your kubeconfig.");
smoketest = false;
} else {
psBuilder.addProblem(WARNING, "You have not specified a Kubernetes context in your halconfig, Spinnaker will use \"" + currentContext + "\" instead.", "context").setRemediation("We recommend explicitly setting a context in your halconfig, to ensure changes to your kubeconfig won't break your deployment.");
}
}
if (smoketest) {
Config config = KubernetesConfigParser.parse(kubeconfigFile, context, cluster, user, namespaces, false);
try {
KubernetesClient client = new DefaultKubernetesClient(config);
client.namespaces().list();
} catch (Exception e) {
ConfigProblemBuilder pb = psBuilder.addProblem(ERROR, "Unable to communicate with your Kubernetes cluster: " + e.getMessage() + ".");
if (e.getMessage().contains("Token may have expired")) {
pb.setRemediation("If you downloaded these keys with gcloud, it's possible they are in the wrong format. To fix this, run \n\n" + "gcloud config set container/use_client_certificate true\n\ngcloud container clusters get-credentials $CLUSTERNAME");
} else {
pb.setRemediation("Unable to authenticate with your Kubernetes cluster. Try using kubectl to verify your credentials.");
}
}
}
}
use of com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemBuilder in project halyard by spinnaker.
the class DeploymentService method getAllDeploymentConfigurations.
public List<DeploymentConfiguration> getAllDeploymentConfigurations() {
NodeFilter filter = new NodeFilter().withAnyDeployment();
List<DeploymentConfiguration> matching = lookupService.getMatchingNodesOfType(filter, DeploymentConfiguration.class);
if (matching.size() == 0) {
throw new ConfigNotFoundException(new ConfigProblemBuilder(Severity.FATAL, "No deployments could be found in your currently loaded halconfig").build());
} else {
return matching;
}
}
Aggregations