use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.
the class GoogleBaseImageValidator method validate.
@Override
public void validate(ConfigProblemSetBuilder p, GoogleBaseImage n) {
String sourceImage = n.getVirtualizationSettings().getSourceImage();
String sourceImageFamily = n.getVirtualizationSettings().getSourceImageFamily();
if (StringUtils.isEmpty(sourceImage) && StringUtils.isEmpty(sourceImageFamily)) {
p.addProblem(Problem.Severity.ERROR, "Either source image or source image family must be specified for " + n.getBaseImage().getId() + ".");
}
if (!StringUtils.isEmpty(sourceImage)) {
int i = 0;
boolean[] foundSourceImageHolder = new boolean[1];
while (!foundSourceImageHolder[0] && i < credentialsList.size()) {
GoogleNamedAccountCredentials credentials = credentialsList.get(i);
List<String> imageProjects = Lists.newArrayList(credentials.getProject());
imageProjects.addAll(credentials.getImageProjects());
imageProjects.addAll(baseImageProjects);
Compute compute = credentials.getCompute();
BatchRequest imageListBatch = buildBatchRequest(compute);
JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
@Override
public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
// No need to look through these images if the requested image was already found.
if (!foundSourceImageHolder[0]) {
if (imageList.getItems() != null) {
foundSourceImageHolder[0] = imageList.getItems().stream().filter(image -> image.getName().equals(sourceImage)).findFirst().isPresent();
}
}
}
};
try {
for (String imageProject : imageProjects) {
compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
}
imageListBatch.execute();
} catch (IOException e) {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
i++;
}
if (!foundSourceImageHolder[0]) {
p.addProblem(Problem.Severity.ERROR, "Image " + sourceImage + " not found via any configured google account.");
}
}
if (!StringUtils.isEmpty(sourceImageFamily)) {
int i = 0;
boolean[] foundSourceImageFamilyHolder = new boolean[1];
while (!foundSourceImageFamilyHolder[0] && i < credentialsList.size()) {
GoogleNamedAccountCredentials credentials = credentialsList.get(i);
List<String> imageProjects = Lists.newArrayList(credentials.getProject());
imageProjects.addAll(credentials.getImageProjects());
imageProjects.addAll(baseImageProjects);
Compute compute = credentials.getCompute();
BatchRequest imageListBatch = buildBatchRequest(compute);
JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
@Override
public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
// No need to look through these images if the requested image family was already found.
if (!foundSourceImageFamilyHolder[0]) {
if (imageList.getItems() != null) {
foundSourceImageFamilyHolder[0] = imageList.getItems().stream().filter(image -> sourceImageFamily.equals(image.getFamily())).findFirst().isPresent();
}
}
}
};
try {
for (String imageProject : imageProjects) {
compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
}
imageListBatch.execute();
} catch (IOException e) {
p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
}
i++;
}
if (!foundSourceImageFamilyHolder[0]) {
p.addProblem(Problem.Severity.ERROR, "Image family " + sourceImageFamily + " not found via any configured google account.");
}
}
if (StringUtils.isEmpty(n.getBaseImage().getPackageType())) {
p.addProblem(Problem.Severity.ERROR, "Package type must be specified for " + n.getBaseImage().getId() + ".");
}
}
use of com.netflix.spinnaker.halyard.config.model.v1.node.Account 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.model.v1.node.Account in project halyard by spinnaker.
the class KubernetesAccountValidator method validate.
@Override
public void validate(ConfigProblemSetBuilder psBuilder, KubernetesAccount account) {
DeploymentConfiguration deploymentConfiguration;
// 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;
validateKindConfig(psBuilder, account);
// TODO(lwander) validate all config with clouddriver's v2 creds
switch(account.getProviderVersion()) {
case V1:
final List<String> dockerRegistryNames = account.getDockerRegistries().stream().map(DockerRegistryReference::getAccountName).collect(Collectors.toList());
validateDockerRegistries(psBuilder, deploymentConfiguration, dockerRegistryNames, Provider.ProviderType.KUBERNETES);
validateKubeconfig(psBuilder, account);
case V2:
break;
default:
throw new IllegalStateException("Unknown provider version " + account.getProviderVersion());
}
}
use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.
the class DistributedDeployer method disableOrcaServerGroups.
private <T extends Account> Set<Integer> disableOrcaServerGroups(AccountDeploymentDetails<T> details, SpinnakerRuntimeSettings runtimeSettings, DistributedService<Orca, T> orcaService, RunningServiceDetails runningOrcaDetails) {
Map<Integer, List<RunningServiceDetails.Instance>> instances = runningOrcaDetails.getInstances();
List<Integer> existingVersions = new ArrayList<>(instances.keySet());
existingVersions.sort(Integer::compareTo);
Map<String, String> disableRequest = new HashMap<>();
Set<Integer> result = new HashSet<>();
disableRequest.put("enabled", "false");
List<Integer> disabledVersions = existingVersions.subList(0, existingVersions.size() - 1);
for (Integer version : disabledVersions) {
try {
for (RunningServiceDetails.Instance instance : instances.get(version)) {
log.info("Disabling instance " + instance.getId());
Orca orca = orcaService.connectToInstance(details, runtimeSettings, orcaService.getService(), instance.getId());
orca.setInstanceStatusEnabled(disableRequest);
}
result.add(version);
} catch (RetrofitError e) {
Response response = e.getResponse();
if (response == null) {
log.warn("Unexpected error disabling orca", e);
} else if (response.getStatus() == 400 && ((Map) e.getBodyAs(Map.class)).containsKey("discovery")) {
log.info("Orca instance is managed by eureka");
result.add(version);
} else {
log.warn("Orca version doesn't support explicit disabling of instances", e);
}
}
}
Set<Integer> unknownVersions = disabledVersions.stream().filter(i -> !result.contains(i)).collect(Collectors.toSet());
if (unknownVersions.size() > 0) {
log.warn("There are existing orca server groups that cannot be explicitly disabled, we will have to wait for these to drain work");
}
return unknownVersions;
}
use of com.netflix.spinnaker.halyard.config.model.v1.node.Account in project halyard by spinnaker.
the class DCOSAccountValidator method validateClusters.
private void validateClusters(final ConfigProblemSetBuilder problems, final DCOSAccount account) {
final NodeIterator children = account.getParent().getChildren();
Node n = children.getNext();
Set<String> definedClusters = new HashSet<>();
while (n != null) {
if (n instanceof DCOSCluster) {
definedClusters.add(((DCOSCluster) n).getName());
}
n = children.getNext();
}
final Set<String> accountClusters = account.getClusters().stream().map(c -> c.getName()).collect(Collectors.toSet());
accountClusters.removeAll(definedClusters);
accountClusters.forEach(c -> problems.addProblem(ERROR, "Cluster \"" + c.toString() + "\" not defined for provider").setRemediation("Add cluster to the provider or remove from the account").setOptions(Lists.newArrayList(definedClusters)));
Set<List<String>> credentials = new HashSet<>();
account.getClusters().forEach(c -> {
final List<String> key = Lists.newArrayList(c.getName(), c.getUid());
if (credentials.contains(key)) {
problems.addProblem(ERROR, "Account contains duplicate credentials for cluster \"" + c.getName() + "\" and user id \"" + c.getUid() + "\".").setRemediation("Remove the duplicate credentials");
} else {
credentials.add(key);
}
// we can connect without a password
if (Strings.isStringEmpty(c.getPassword()) && Strings.isStringEmpty(c.getServiceKeyFile())) {
problems.addProblem(WARNING, "Account has no password or service key. Unless the cluster has security disabled this may be an error").setRemediation("Add a password or service key.");
}
if (!Strings.isStringEmpty(c.getPassword()) && !Strings.isStringEmpty(c.getServiceKeyFile())) {
problems.addProblem(ERROR, "Account has both a password and service key").setRemediation("Remove either the password or service key.");
}
if (!Strings.isStringEmpty(c.getServiceKeyFile())) {
String resolvedServiceKey = ValidatingFileReader.contents(problems, c.getServiceKeyFile());
if (Strings.isStringEmpty(resolvedServiceKey)) {
problems.addProblem(ERROR, "The supplied service key file does not exist or is empty.").setRemediation("Supply a valid service key file.");
}
}
});
}
Aggregations