use of com.netflix.frigga.Names in project halyard by spinnaker.
the class KubernetesV1DistributedService method getServerGroupDescription.
default Map<String, Object> getServerGroupDescription(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings, List<ConfigSource> configSources) {
DeployKubernetesAtomicOperationDescription description = new DeployKubernetesAtomicOperationDescription();
SpinnakerMonitoringDaemonService monitoringService = getMonitoringDaemonService();
ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
DeploymentEnvironment deploymentEnvironment = details.getDeploymentConfiguration().getDeploymentEnvironment();
String accountName = details.getAccount().getName();
String namespace = getNamespace(settings);
String name = getServiceName();
Names parsedName = Names.parseName(name);
description.setNamespace(namespace);
description.setAccount(accountName);
description.setApplication(parsedName.getApp());
description.setStack(parsedName.getStack());
description.setFreeFormDetails(parsedName.getDetail());
List<KubernetesVolumeSource> volumeSources = new ArrayList<>();
for (ConfigSource configSource : configSources) {
KubernetesVolumeSource volumeSource = new KubernetesVolumeSource();
volumeSource.setName(configSource.getId());
volumeSource.setType(KubernetesVolumeSourceType.Secret);
KubernetesSecretVolumeSource secretVolumeSource = new KubernetesSecretVolumeSource();
secretVolumeSource.setSecretName(configSource.getId());
volumeSource.setSecret(secretVolumeSource);
volumeSources.add(volumeSource);
}
description.setVolumeSources(volumeSources);
description.setPodAnnotations(settings.getKubernetes().getPodAnnotations());
List<String> loadBalancers = new ArrayList<>();
loadBalancers.add(name);
description.setLoadBalancers(loadBalancers);
List<KubernetesContainerDescription> containers = new ArrayList<>();
ServiceSettings serviceSettings = runtimeSettings.getServiceSettings(getService());
KubernetesContainerDescription container = buildContainer(name, serviceSettings, configSources, deploymentEnvironment, description);
containers.add(container);
ServiceSettings monitoringSettings = runtimeSettings.getServiceSettings(monitoringService);
if (monitoringSettings.getEnabled() && serviceSettings.getMonitored()) {
serviceSettings = runtimeSettings.getServiceSettings(monitoringService);
container = buildContainer(monitoringService.getServiceName(), serviceSettings, configSources, deploymentEnvironment, description);
containers.add(container);
}
description.setContainers(containers);
return getObjectMapper().convertValue(description, new TypeReference<Map<String, Object>>() {
});
}
use of com.netflix.frigga.Names in project halyard by spinnaker.
the class KubernetesV1DistributedService method getLoadBalancerDescription.
default Map<String, Object> getLoadBalancerDescription(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
int port = settings.getPort();
String accountName = details.getAccount().getName();
KubernetesLoadBalancerDescription description = new KubernetesLoadBalancerDescription();
String namespace = getNamespace(settings);
String name = getServiceName();
Names parsedName = Names.parseName(name);
description.setApp(parsedName.getApp());
description.setStack(parsedName.getStack());
description.setDetail(parsedName.getDetail());
description.setName(name);
description.setNamespace(namespace);
description.setAccount(accountName);
KubernetesNamedServicePort servicePort = new KubernetesNamedServicePort();
servicePort.setPort(port);
servicePort.setTargetPort(port);
servicePort.setName("http");
servicePort.setProtocol("TCP");
KubernetesNamedServicePort monitoringPort = new KubernetesNamedServicePort();
monitoringPort.setPort(8008);
monitoringPort.setTargetPort(8008);
monitoringPort.setName("monitoring");
monitoringPort.setProtocol("TCP");
List<KubernetesNamedServicePort> servicePorts = new ArrayList<>();
servicePorts.add(servicePort);
servicePorts.add(monitoringPort);
description.setPorts(servicePorts);
return getObjectMapper().convertValue(description, new TypeReference<Map<String, Object>>() {
});
}
use of com.netflix.frigga.Names in project halyard by spinnaker.
the class GoogleDistributedService method getServerGroupDescription.
@Override
default Map<String, Object> getServerGroupDescription(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings, List<ConfigSource> configSources) {
GoogleAccount account = details.getAccount();
RunningServiceDetails runningServiceDetails = getRunningServiceDetails(details, runtimeSettings);
Integer version = runningServiceDetails.getLatestEnabledVersion();
if (version == null) {
version = 0;
} else {
version++;
}
Names name = Names.parseName(getServiceName());
String app = name.getApp();
String stack = name.getStack();
String detail = name.getDetail();
String network = GoogleProviderUtils.getNetworkName();
Map<String, String> metadata = getMetadata(details, runtimeSettings, configSources, version).stream().reduce(new HashMap<String, String>(), (h1, item) -> {
h1.put(item.getKey(), item.getValue());
return h1;
}, (h1, h2) -> {
h1.putAll(h2);
return h1;
});
String serviceAccountEmail = GoogleProviderUtils.defaultServiceAccount(details);
List<String> scopes = getScopes();
String accountName = account.getName();
Map<String, Object> deployDescription = new HashMap<>();
deployDescription.put("application", app);
deployDescription.put("stack", stack);
deployDescription.put("freeFormDetails", detail);
deployDescription.put("network", network);
deployDescription.put("instanceMetadata", metadata);
deployDescription.put("serviceAccountEmail", serviceAccountEmail);
deployDescription.put("authScopes", scopes);
deployDescription.put("accountName", accountName);
deployDescription.put("account", accountName);
return deployDescription;
/* TODO(lwander): Google's credential class cannot be serialized as-is, making this type of construction impossible
BasicGoogleDeployDescription deployDescription = new BasicGoogleDeployDescription();
deployDescription.setApplication(app);
deployDescription.setStack(stack);
deployDescription.setFreeFormDetails(detail);
deployDescription.setNetwork(network);
deployDescription.setInstanceMetadata(metadata);
deployDescription.setServiceAccountEmail(serviceAccountEmail);
deployDescription.setAuthScopes(scopes);
// Google's credentials constructor prevents us from neatly creating a deploy description with only a name supplied
String jsonKey = null;
if (!StringUtils.isEmpty(account.getJsonPath())) {
try {
jsonKey = IOUtils.toString(new FileInputStream(account.getJsonPath()));
} catch (IOException e) {
throw new RuntimeException("Unvalidated json path found during deployment: " + e.getMessage(), e);
}
}
deployDescription.setCredentials(new GoogleNamedAccountCredentials.Builder()
.name(account.getName())
.jsonKey(jsonKey)
.project(account.getProject())
.build()
);
return new ObjectMapper().convertValue(deployDescription, new TypeReference<Map<String, Object>>() { });
*/
}
use of com.netflix.frigga.Names in project halyard by spinnaker.
the class KubernetesV1DistributedService method getRunningServiceDetails.
@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
RunningServiceDetails res = new RunningServiceDetails();
KubernetesClient client = KubernetesV1ProviderUtils.getClient(details);
String name = getServiceName();
String namespace = getNamespace(settings);
RunningServiceDetails.LoadBalancer lb = new RunningServiceDetails.LoadBalancer();
lb.setExists(client.services().inNamespace(namespace).withName(name).get() != null);
res.setLoadBalancer(lb);
List<Pod> pods = client.pods().inNamespace(namespace).withLabel("load-balancer-" + name, "true").list().getItems();
pods.addAll(client.pods().inNamespace(namespace).withLabel("load-balancer-" + name, "false").list().getItems());
Map<Integer, List<Instance>> instances = res.getInstances();
for (Pod pod : pods) {
String podName = pod.getMetadata().getName();
String serverGroupName = podName.substring(0, podName.lastIndexOf("-"));
Names parsedName = Names.parseName(serverGroupName);
Integer version = parsedName.getSequence();
if (version == null) {
throw new IllegalStateException("Server group for service " + getServiceName() + " has unknown sequence (" + serverGroupName + ")");
}
String location = pod.getMetadata().getNamespace();
String id = pod.getMetadata().getName();
Instance instance = new Instance().setId(id).setLocation(location);
List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses();
if (!containerStatuses.isEmpty() && containerStatuses.stream().allMatch(ContainerStatus::getReady)) {
instance.setHealthy(true);
}
if (!containerStatuses.isEmpty() && containerStatuses.stream().allMatch(s -> s.getState().getRunning() != null && s.getState().getTerminated() == null)) {
instance.setRunning(true);
}
List<Instance> knownInstances = instances.getOrDefault(version, new ArrayList<>());
knownInstances.add(instance);
instances.put(version, knownInstances);
}
List<ReplicaSet> replicaSets = client.extensions().replicaSets().inNamespace(settings.getLocation()).list().getItems();
for (ReplicaSet rs : replicaSets) {
String rsName = rs.getMetadata().getName();
Names parsedRsName = Names.parseName(rsName);
if (!parsedRsName.getCluster().equals(getServiceName())) {
continue;
}
instances.computeIfAbsent(parsedRsName.getSequence(), i -> new ArrayList<>());
}
return res;
}
use of com.netflix.frigga.Names in project halyard by spinnaker.
the class GoogleDistributedService method getRunningServiceDetails.
@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<GoogleAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
RunningServiceDetails result = new RunningServiceDetails();
// All GCE load balancing is done via consul
result.setLoadBalancer(new RunningServiceDetails.LoadBalancer().setExists(true));
Compute compute = GoogleProviderUtils.getCompute(details);
GoogleAccount account = details.getAccount();
List<InstanceGroupManager> migs;
try {
migs = compute.instanceGroupManagers().list(account.getProject(), settings.getLocation()).execute().getItems();
if (migs == null) {
migs = Collections.emptyList();
}
} catch (IOException e) {
throw new HalException(FATAL, "Failed to load MIGS: " + e.getMessage(), e);
}
boolean consulEnabled = getSidecars(runtimeSettings).stream().anyMatch(s -> s.getService().getType().equals(SpinnakerService.Type.CONSUL_CLIENT));
Set<String> healthyConsulInstances = consulEnabled ? getConsulServerService().connectToPrimaryService(details, runtimeSettings).serviceHealth(getService().getCanonicalName(), true).stream().map(s -> s != null && s.getNode() != null ? s.getNode().getNodeName() : null).filter(Objects::nonNull).collect(Collectors.toSet()) : new HashSet<>();
String serviceName = getService().getServiceName();
migs = migs.stream().filter(ig -> ig.getName().startsWith(serviceName + "-v")).collect(Collectors.toList());
Map<Integer, List<RunningServiceDetails.Instance>> instances = migs.stream().reduce(new HashMap<>(), (map, mig) -> {
Names names = Names.parseName(mig.getName());
Integer version = names.getSequence();
List<RunningServiceDetails.Instance> computeInstances;
try {
List<ManagedInstance> managedInstances = compute.instanceGroupManagers().listManagedInstances(account.getProject(), settings.getLocation(), mig.getName()).execute().getManagedInstances();
if (managedInstances == null) {
managedInstances = new ArrayList<>();
}
computeInstances = managedInstances.stream().map(i -> {
String instanceUrl = i.getInstance();
String instanceStatus = i.getInstanceStatus();
boolean running = instanceStatus != null && instanceStatus.equalsIgnoreCase("running");
String instanceName = instanceUrl.substring(instanceUrl.lastIndexOf('/') + 1, instanceUrl.length());
return new RunningServiceDetails.Instance().setId(instanceName).setLocation(settings.getLocation()).setRunning(running).setHealthy(!consulEnabled || healthyConsulInstances.contains(instanceName));
}).collect(Collectors.toList());
} catch (IOException e) {
throw new HalException(FATAL, "Failed to load target pools for " + serviceName, e);
}
map.put(version, computeInstances);
return map;
}, (m1, m2) -> {
m1.putAll(m2);
return m1;
});
result.setInstances(instances);
return result;
}
Aggregations