use of io.fabric8.kubernetes.api.model.EnvVar in project zalenium by zalando.
the class KubernetesContainerClient method registerNode.
@Override
public ContainerClientRegistration registerNode(String zaleniumContainerName, URL remoteHost) {
String podIpAddress = remoteHost.getHost();
// The only way to lookup a pod name by IP address is by looking at all pods in the namespace it seems.
PodList list = client.pods().withLabels(createdByZaleniumMap).list();
String containerId = null;
Pod currentPod = null;
for (Pod pod : list.getItems()) {
if (podIpAddress.equals(pod.getStatus().getPodIP())) {
containerId = pod.getMetadata().getName();
currentPod = pod;
break;
}
}
if (containerId == null) {
throw new IllegalStateException("Unable to locate pod by ip address, registration will fail");
}
ContainerClientRegistration registration = new ContainerClientRegistration();
List<EnvVar> podEnvironmentVariables = currentPod.getSpec().getContainers().get(0).getEnv();
Optional<EnvVar> noVncPort = podEnvironmentVariables.stream().filter(env -> "NOVNC_PORT".equals(env.getName())).findFirst();
if (noVncPort.isPresent()) {
Integer noVncPortInt = Integer.decode(noVncPort.get().getValue());
registration.setNoVncPort(noVncPortInt);
} else {
logger.warn(String.format("%s Couldn't find NOVNC_PORT, live preview will not work.", containerId));
}
registration.setIpAddress(currentPod.getStatus().getPodIP());
registration.setContainerId(containerId);
return registration;
}
use of io.fabric8.kubernetes.api.model.EnvVar 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.kubernetes.api.model.EnvVar in project fabric8 by fabric8io.
the class ListEnvVarBuilder method withEnvVar.
public void withEnvVar(String name, String value) {
if (Strings.isNotBlank(name) && value != null) {
EnvVar envVar = new EnvVar();
envVar.setName(name);
envVar.setValue(value);
envVars.add(envVar);
}
}
use of io.fabric8.kubernetes.api.model.EnvVar in project flink by apache.
the class KubernetesClusterDescriptorTest method testDeployHighAvailabilitySessionCluster.
@Test
public void testDeployHighAvailabilitySessionCluster() throws ClusterDeploymentException {
flinkConfig.set(DeploymentOptions.TARGET, KubernetesDeploymentTarget.SESSION.getName());
flinkConfig.setString(HighAvailabilityOptions.HA_MODE, HighAvailabilityMode.ZOOKEEPER.toString());
final ClusterClient<String> clusterClient = deploySessionCluster().getClusterClient();
checkClusterClient(clusterClient);
final Container jmContainer = kubeClient.apps().deployments().list().getItems().get(0).getSpec().getTemplate().getSpec().getContainers().get(0);
assertTrue("Environment " + ENV_FLINK_POD_IP_ADDRESS + " should be set.", jmContainer.getEnv().stream().map(EnvVar::getName).collect(Collectors.toList()).contains(ENV_FLINK_POD_IP_ADDRESS));
clusterClient.close();
}
use of io.fabric8.kubernetes.api.model.EnvVar in project flink by apache.
the class KubernetesTaskManagerFactoryTest method testContainer.
@Test
public void testContainer() {
final List<Container> resultContainers = this.resultPod.getSpec().getContainers();
assertEquals(1, resultContainers.size());
final Container resultMainContainer = resultContainers.get(0);
assertEquals(Constants.MAIN_CONTAINER_NAME, resultMainContainer.getName());
assertEquals(CONTAINER_IMAGE, resultMainContainer.getImage());
assertEquals(CONTAINER_IMAGE_PULL_POLICY.name(), resultMainContainer.getImagePullPolicy());
assertEquals(4, resultMainContainer.getEnv().size());
assertTrue(resultMainContainer.getEnv().stream().anyMatch(envVar -> envVar.getName().equals("key1")));
assertEquals(1, resultMainContainer.getPorts().size());
assertEquals(1, resultMainContainer.getCommand().size());
// The args list is [bash, -c, 'java -classpath $FLINK_CLASSPATH ...'].
assertEquals(3, resultMainContainer.getArgs().size());
assertEquals(4, resultMainContainer.getVolumeMounts().size());
}
Aggregations