use of io.fabric8.annotations.Endpoint in project fabric8 by jboss-fuse.
the class Manager method added.
//
// ListenerHook
//
@SuppressWarnings("unchecked")
public void added(final Collection listenerInfos) {
for (ListenerInfo listenerInfo : (Collection<ListenerInfo>) listenerInfos) {
// Ignore our own listeners or those that don't have any filter
if (listenerInfo.getBundleContext() == bundleContext || listenerInfo.getFilter() == null) {
continue;
}
// Make sure we only import remote services
String filter = "(&" + listenerInfo.getFilter() + "(!(" + ENDPOINT_FRAMEWORK_UUID + "=" + this.uuid + ")))";
SimpleFilter exFilter = SimpleFilter.parse(filter);
listeners.put(listenerInfo, exFilter);
// Iterate through known services and import them if needed
Set<EndpointDescription> matches = remoteEndpoints.match(exFilter);
for (EndpointDescription endpoint : matches) {
doImportService(endpoint, listenerInfo);
}
}
}
use of io.fabric8.annotations.Endpoint in project shinyproxy by openanalytics.
the class KubernetesBackend method doStartProxy.
@Override
protected void doStartProxy(KubernetesContainerProxy proxy) throws Exception {
String kubeNamespace = getProperty(PROPERTY_NAMESPACE, proxy.getApp(), DEFAULT_NAMESPACE);
String apiVersion = getProperty(PROPERTY_API_VERSION, proxy.getApp(), DEFAULT_API_VERSION);
String[] volumeStrings = Optional.ofNullable(proxy.getApp().getDockerVolumes()).orElse(new String[] {});
Volume[] volumes = new Volume[volumeStrings.length];
VolumeMount[] volumeMounts = new VolumeMount[volumeStrings.length];
for (int i = 0; i < volumeStrings.length; i++) {
String[] volume = volumeStrings[i].split(":");
String hostSource = volume[0];
String containerDest = volume[1];
String name = "shinyproxy-volume-" + i;
volumes[i] = new VolumeBuilder().withNewHostPath(hostSource).withName(name).build();
volumeMounts[i] = new VolumeMountBuilder().withMountPath(containerDest).withName(name).build();
}
List<EnvVar> envVars = new ArrayList<>();
for (String envString : buildEnv(proxy.getUserId(), proxy.getApp())) {
int idx = envString.indexOf('=');
if (idx == -1)
log.warn("Invalid environment variable: " + envString);
envVars.add(new EnvVar(envString.substring(0, idx), envString.substring(idx + 1), null));
}
SecurityContext security = new SecurityContextBuilder().withPrivileged(Boolean.valueOf(getProperty(PROPERTY_PRIVILEGED, proxy.getApp(), DEFAULT_PRIVILEGED))).build();
ContainerBuilder containerBuilder = new ContainerBuilder().withImage(proxy.getApp().getDockerImage()).withName("shiny-container").withPorts(new ContainerPortBuilder().withContainerPort(getAppPort(proxy)).build()).withVolumeMounts(volumeMounts).withSecurityContext(security).withEnv(envVars);
String imagePullPolicy = getProperty(PROPERTY_IMG_PULL_POLICY, proxy.getApp(), null);
if (imagePullPolicy != null)
containerBuilder.withImagePullPolicy(imagePullPolicy);
if (proxy.getApp().getDockerCmd() != null)
containerBuilder.withCommand(proxy.getApp().getDockerCmd());
String[] imagePullSecrets = getProperty(PROPERTY_IMG_PULL_SECRETS, proxy.getApp(), String[].class, null);
if (imagePullSecrets == null) {
String imagePullSecret = getProperty(PROPERTY_IMG_PULL_SECRET, proxy.getApp(), null);
if (imagePullSecret != null) {
imagePullSecrets = new String[] { imagePullSecret };
} else {
imagePullSecrets = new String[0];
}
}
Pod pod = kubeClient.pods().inNamespace(kubeNamespace).createNew().withApiVersion(apiVersion).withKind("Pod").withNewMetadata().withName(proxy.getName()).addToLabels("app", proxy.getName()).endMetadata().withNewSpec().withContainers(Collections.singletonList(containerBuilder.build())).withVolumes(volumes).withImagePullSecrets(Arrays.asList(imagePullSecrets).stream().map(LocalObjectReference::new).collect(Collectors.toList())).endSpec().done();
proxy.setPod(kubeClient.resource(pod).waitUntilReady(600, TimeUnit.SECONDS));
if (!isUseInternalNetwork()) {
// If SP runs outside the cluster, a NodePort service is needed to access the pod externally.
Service service = kubeClient.services().inNamespace(kubeNamespace).createNew().withApiVersion(apiVersion).withKind("Service").withNewMetadata().withName(proxy.getName() + "service").endMetadata().withNewSpec().addToSelector("app", proxy.getName()).withType("NodePort").withPorts(new ServicePortBuilder().withPort(getAppPort(proxy)).build()).endSpec().done();
// Retry, because if this is done too fast, an 'endpoint not found' exception will be thrown.
Utils.retry(i -> {
try {
proxy.setService(kubeClient.resource(service).waitUntilReady(600, TimeUnit.SECONDS));
} catch (Exception e) {
return false;
}
return true;
}, 5, 1000);
releasePort(proxy.getPort());
proxy.setPort(proxy.getService().getSpec().getPorts().get(0).getNodePort());
}
}
use of io.fabric8.annotations.Endpoint in project fabric8 by fabric8io.
the class Fabric8Extension method onInjectionPoint.
public <T, X> void onInjectionPoint(@Observes ProcessInjectionPoint<T, X> event, BeanManager beanManager) {
final InjectionPoint injectionPoint = event.getInjectionPoint();
if (isServiceInjectionPoint(injectionPoint)) {
Annotated annotated = injectionPoint.getAnnotated();
ServiceName name = annotated.getAnnotation(ServiceName.class);
Protocol protocol = annotated.getAnnotation(Protocol.class);
PortName port = annotated.getAnnotation(PortName.class);
Path path = annotated.getAnnotation(Path.class);
Alias alias = annotated.getAnnotation(Alias.class);
Endpoint endpoint = annotated.getAnnotation(Endpoint.class);
External external = annotated.getAnnotation(External.class);
String serviceName = name.value();
String serviceProtocol = protocol != null ? protocol.value() : null;
String servicePort = port != null ? port.value() : null;
String servicePath = path != null ? path.value() : null;
String serviceAlias = alias != null ? alias.value() : null;
Boolean serviceExternal = external != null ? external.value() : false;
Boolean serviceEndpoint = endpoint != null ? endpoint.value() : false;
Type type = annotated.getBaseType();
if (type instanceof ParameterizedType && Instance.class.equals(((ParameterizedType) type).getRawType())) {
type = ((ParameterizedType) type).getActualTypeArguments()[0];
}
if (type.equals(String.class)) {
ServiceUrlBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal);
} else if (isGenericOf(type, List.class, String.class)) {
ServiceUrlCollectionBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, Types.LIST_OF_STRINGS);
} else if (isGenericOf(type, List.class, null)) {
// TODO: Integrate with Factories(?)
} else if (isGenericOf(type, Set.class, String.class)) {
ServiceUrlCollectionBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, Types.SET_OF_STRINGS);
} else if (isGenericOf(type, Set.class, null)) {
// TODO: Integrate with Factories(?)
} else if (type instanceof Class) {
ServiceBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, type);
} else {
throw new RuntimeException(String.format(INJECTION_POINT_UNKNOWN_TYPE, injectionPoint.getBean().getBeanClass(), type));
}
if (protocol == null) {
setDefaultProtocol(event);
}
if (port == null) {
setDefaultPort(event);
}
if (path == null) {
setDefaultPath(event);
}
if (endpoint == null) {
setDefaultEndpoint(event);
}
if (external == null) {
setDefaultExternal(event);
}
} else if (isConfigurationInjectionPoint(injectionPoint)) {
Annotated annotated = injectionPoint.getAnnotated();
Configuration configuration = annotated.getAnnotation(Configuration.class);
Type type = injectionPoint.getType();
String configurationId = configuration.value();
ConfigurationBean.getBean(configurationId, type);
}
}
use of io.fabric8.annotations.Endpoint in project strimzi by strimzi.
the class MockKube method buildStatefulSets.
private MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> buildStatefulSets(MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods) {
return new AbstractMockBuilder<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>>(StatefulSet.class, StatefulSetList.class, DoneableStatefulSet.class, castClass(RollableScalableResource.class), ssDb) {
@Override
protected void nameScopedMocks(RollableScalableResource<StatefulSet, DoneableStatefulSet> resource, String resourceName) {
mockGet(resourceName, resource);
// mockCreate("endpoint", endpointDb, resourceName, resource);
mockCascading(resource);
mockPatch(resourceName, resource);
mockDelete(resourceName, resource);
mockIsReady(resourceName, resource);
when(resource.create(any())).thenAnswer(cinvocation -> {
checkNotExists(resourceName);
StatefulSet argument = cinvocation.getArgument(0);
LOGGER.debug("create {} {} -> {}", resourceType, resourceName, argument);
ssDb.put(resourceName, copyResource(argument));
for (int i = 0; i < argument.getSpec().getReplicas(); i++) {
String podName = argument.getMetadata().getName() + "-" + i;
podDb.put(podName, new PodBuilder().withNewMetadata().withNamespace(argument.getMetadata().getNamespace()).withName(podName).endMetadata().build());
}
return argument;
});
EditReplacePatchDeletable<StatefulSet, StatefulSet, DoneableStatefulSet, Boolean> c = mock(EditReplacePatchDeletable.class);
when(resource.cascading(false)).thenReturn(c);
when(c.patch(any())).thenAnswer(patchInvocation -> {
StatefulSet argument = patchInvocation.getArgument(0);
return doPatch(resourceName, argument);
});
when(resource.scale(anyInt(), anyBoolean())).thenAnswer(invocation -> {
checkDoesExist(resourceName);
StatefulSet ss = copyResource(ssDb.get(resourceName));
int newScale = invocation.getArgument(0);
ss.getSpec().setReplicas(newScale);
return doPatch(resourceName, ss);
});
when(resource.scale(anyInt())).thenAnswer(invocation -> {
checkDoesExist(resourceName);
StatefulSet ss = copyResource(ssDb.get(resourceName));
int newScale = invocation.getArgument(0);
ss.getSpec().setReplicas(newScale);
return doPatch(resourceName, ss);
});
when(resource.isReady()).thenAnswer(i -> {
LOGGER.debug("{} {} is ready", resourceType, resourceName);
return true;
});
mockPods.inNamespace(any()).withName(any()).watch(new Watcher<Pod>() {
@Override
public void eventReceived(Action action, Pod resource) {
if (action == Action.DELETED) {
String podName = resource.getMetadata().getName();
String podNamespace = resource.getMetadata().getNamespace();
StatefulSet statefulSet = ssDb.get(resourceName);
if (podName.startsWith(resourceName + "-") && Integer.parseInt(podName.substring(podName.lastIndexOf("-") + 1)) < statefulSet.getSpec().getReplicas()) {
mockPods.inNamespace(podNamespace).withName(podName).create(resource);
}
}
}
@Override
public void onClose(KubernetesClientException e) {
}
});
}
private StatefulSet doPatch(String resourceName, StatefulSet argument) {
int oldScale = ssDb.get(resourceName).getSpec().getReplicas();
int newScale = argument.getSpec().getReplicas();
if (newScale > oldScale) {
LOGGER.debug("scaling up {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
Pod examplePod = mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(argument.getMetadata().getName() + "-0").get();
for (int i = oldScale; i < newScale; i++) {
String newPodName = argument.getMetadata().getName() + "-" + i;
mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).create(new PodBuilder(examplePod).editMetadata().withName(newPodName).endMetadata().build());
}
ssDb.put(resourceName, copyResource(argument));
} else if (newScale < oldScale) {
ssDb.put(resourceName, copyResource(argument));
LOGGER.debug("scaling down {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
for (int i = oldScale - 1; i >= newScale; i--) {
String newPodName = argument.getMetadata().getName() + "-" + i;
mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).delete();
}
} else {
ssDb.put(resourceName, copyResource(argument));
}
return argument;
}
}.build();
}
use of io.fabric8.annotations.Endpoint in project flink by apache.
the class Fabric8FlinkKubeClient method getRestEndPointFromService.
private Optional<Endpoint> getRestEndPointFromService(Service service, int restPort) {
if (service.getStatus() == null) {
return Optional.empty();
}
LoadBalancerStatus loadBalancer = service.getStatus().getLoadBalancer();
boolean hasExternalIP = service.getSpec() != null && service.getSpec().getExternalIPs() != null && !service.getSpec().getExternalIPs().isEmpty();
if (loadBalancer != null) {
return getLoadBalancerRestEndpoint(loadBalancer, restPort);
} else if (hasExternalIP) {
final String address = service.getSpec().getExternalIPs().get(0);
if (address != null && !address.isEmpty()) {
return Optional.of(new Endpoint(address, restPort));
}
}
return Optional.empty();
}
Aggregations