Search in sources :

Example 6 with ContainerBuilder

use of io.fabric8.kubernetes.api.model.ContainerBuilder 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());
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) IOException(java.io.IOException) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) SecurityContextBuilder(io.fabric8.kubernetes.api.model.SecurityContextBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) SecurityContext(io.fabric8.kubernetes.api.model.SecurityContext) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 7 with ContainerBuilder

use of io.fabric8.kubernetes.api.model.ContainerBuilder in project flink by apache.

the class HadoopConfMountDecorator method decorateFlinkPod.

@Override
public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
    Volume hadoopConfVolume;
    final Optional<String> existingConfigMap = kubernetesParameters.getExistingHadoopConfigurationConfigMap();
    if (existingConfigMap.isPresent()) {
        hadoopConfVolume = new VolumeBuilder().withName(Constants.HADOOP_CONF_VOLUME).withNewConfigMap().withName(existingConfigMap.get()).endConfigMap().build();
    } else {
        final Optional<String> localHadoopConfigurationDirectory = kubernetesParameters.getLocalHadoopConfigurationDirectory();
        if (!localHadoopConfigurationDirectory.isPresent()) {
            return flinkPod;
        }
        final List<File> hadoopConfigurationFileItems = getHadoopConfigurationFileItems(localHadoopConfigurationDirectory.get());
        if (hadoopConfigurationFileItems.isEmpty()) {
            LOG.warn("Found 0 files in directory {}, skip to mount the Hadoop Configuration ConfigMap.", localHadoopConfigurationDirectory.get());
            return flinkPod;
        }
        final List<KeyToPath> keyToPaths = hadoopConfigurationFileItems.stream().map(file -> new KeyToPathBuilder().withKey(file.getName()).withPath(file.getName()).build()).collect(Collectors.toList());
        hadoopConfVolume = new VolumeBuilder().withName(Constants.HADOOP_CONF_VOLUME).withNewConfigMap().withName(getHadoopConfConfigMapName(kubernetesParameters.getClusterId())).withItems(keyToPaths).endConfigMap().build();
    }
    final Pod podWithHadoopConf = new PodBuilder(flinkPod.getPodWithoutMainContainer()).editOrNewSpec().addNewVolumeLike(hadoopConfVolume).endVolume().endSpec().build();
    final Container containerWithHadoopConf = new ContainerBuilder(flinkPod.getMainContainer()).addNewVolumeMount().withName(Constants.HADOOP_CONF_VOLUME).withMountPath(Constants.HADOOP_CONF_DIR_IN_POD).endVolumeMount().addNewEnv().withName(Constants.ENV_HADOOP_CONF_DIR).withValue(Constants.HADOOP_CONF_DIR_IN_POD).endEnv().build();
    return new FlinkPod.Builder(flinkPod).withPod(podWithHadoopConf).withMainContainer(containerWithHadoopConf).build();
}
Also used : Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) FileUtils(org.apache.flink.util.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractKubernetesParameters(org.apache.flink.kubernetes.kubeclient.parameters.AbstractKubernetesParameters) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) KeyToPathBuilder(io.fabric8.kubernetes.api.model.KeyToPathBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) Volume(io.fabric8.kubernetes.api.model.Volume) KeyToPath(io.fabric8.kubernetes.api.model.KeyToPath) Logger(org.slf4j.Logger) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) File(java.io.File) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) List(java.util.List) Optional(java.util.Optional) Constants(org.apache.flink.kubernetes.utils.Constants) Collections(java.util.Collections) Pod(io.fabric8.kubernetes.api.model.Pod) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) KeyToPath(io.fabric8.kubernetes.api.model.KeyToPath) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) KeyToPathBuilder(io.fabric8.kubernetes.api.model.KeyToPathBuilder) File(java.io.File)

Example 8 with ContainerBuilder

use of io.fabric8.kubernetes.api.model.ContainerBuilder in project flink by apache.

the class InitTaskManagerDecorator method decorateMainContainer.

private Container decorateMainContainer(Container container) {
    final ContainerBuilder mainContainerBuilder = new ContainerBuilder(container);
    // Overwrite fields
    final ResourceRequirements requirementsInPodTemplate = container.getResources() == null ? new ResourceRequirements() : container.getResources();
    final ResourceRequirements resourceRequirements = KubernetesUtils.getResourceRequirements(requirementsInPodTemplate, kubernetesTaskManagerParameters.getTaskManagerMemoryMB(), kubernetesTaskManagerParameters.getTaskManagerMemoryLimitFactor(), kubernetesTaskManagerParameters.getTaskManagerCPU(), kubernetesTaskManagerParameters.getTaskManagerCPULimitFactor(), kubernetesTaskManagerParameters.getTaskManagerExternalResources(), kubernetesTaskManagerParameters.getTaskManagerExternalResourceConfigKeys());
    final String image = KubernetesUtils.resolveUserDefinedValue(flinkConfig, KubernetesConfigOptions.CONTAINER_IMAGE, kubernetesTaskManagerParameters.getImage(), container.getImage(), "main container image");
    final String imagePullPolicy = KubernetesUtils.resolveUserDefinedValue(flinkConfig, KubernetesConfigOptions.CONTAINER_IMAGE_PULL_POLICY, kubernetesTaskManagerParameters.getImagePullPolicy().name(), container.getImagePullPolicy(), "main container image pull policy");
    mainContainerBuilder.withName(Constants.MAIN_CONTAINER_NAME).withImage(image).withImagePullPolicy(imagePullPolicy).withResources(resourceRequirements);
    // Merge fields
    mainContainerBuilder.addAllToPorts(getContainerPorts()).addAllToEnv(getCustomizedEnvs());
    getFlinkLogDirEnv().ifPresent(mainContainerBuilder::addToEnv);
    return mainContainerBuilder.build();
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements)

Example 9 with ContainerBuilder

use of io.fabric8.kubernetes.api.model.ContainerBuilder in project flink by apache.

the class KerberosMountDecorator method decorateFlinkPod.

@Override
public FlinkPod decorateFlinkPod(FlinkPod flinkPod) {
    PodBuilder podBuilder = new PodBuilder(flinkPod.getPodWithoutMainContainer());
    ContainerBuilder containerBuilder = new ContainerBuilder(flinkPod.getMainContainer());
    if (!StringUtils.isNullOrWhitespaceOnly(securityConfig.getKeytab()) && !StringUtils.isNullOrWhitespaceOnly(securityConfig.getPrincipal())) {
        podBuilder = podBuilder.editOrNewSpec().addNewVolume().withName(Constants.KERBEROS_KEYTAB_VOLUME).withNewSecret().withSecretName(getKerberosKeytabSecretName(kubernetesParameters.getClusterId())).endSecret().endVolume().endSpec();
        containerBuilder = containerBuilder.addNewVolumeMount().withName(Constants.KERBEROS_KEYTAB_VOLUME).withMountPath(Constants.KERBEROS_KEYTAB_MOUNT_POINT).endVolumeMount();
    }
    if (!StringUtils.isNullOrWhitespaceOnly(kubernetesParameters.getFlinkConfiguration().get(SecurityOptions.KERBEROS_KRB5_PATH))) {
        final File krb5Conf = new File(kubernetesParameters.getFlinkConfiguration().get(SecurityOptions.KERBEROS_KRB5_PATH));
        podBuilder = podBuilder.editOrNewSpec().addNewVolume().withName(Constants.KERBEROS_KRB5CONF_VOLUME).withNewConfigMap().withName(getKerberosKrb5confConfigMapName(kubernetesParameters.getClusterId())).withItems(new KeyToPathBuilder().withKey(krb5Conf.getName()).withPath(KERBEROS_KRB5CONF_FILE).build()).endConfigMap().endVolume().endSpec();
        containerBuilder = containerBuilder.addNewVolumeMount().withName(Constants.KERBEROS_KRB5CONF_VOLUME).withMountPath(Constants.KERBEROS_KRB5CONF_MOUNT_DIR + "/" + KERBEROS_KRB5CONF_FILE).withSubPath(KERBEROS_KRB5CONF_FILE).endVolumeMount();
    }
    return new FlinkPod(podBuilder.build(), containerBuilder.build());
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) KeyToPathBuilder(io.fabric8.kubernetes.api.model.KeyToPathBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) File(java.io.File)

Example 10 with ContainerBuilder

use of io.fabric8.kubernetes.api.model.ContainerBuilder in project flink by apache.

the class InitJobManagerDecorator method decorateMainContainer.

private Container decorateMainContainer(Container container) {
    final ContainerBuilder mainContainerBuilder = new ContainerBuilder(container);
    // Overwrite fields
    final String image = KubernetesUtils.resolveUserDefinedValue(flinkConfig, KubernetesConfigOptions.CONTAINER_IMAGE, kubernetesJobManagerParameters.getImage(), container.getImage(), "main container image");
    final String imagePullPolicy = KubernetesUtils.resolveUserDefinedValue(flinkConfig, KubernetesConfigOptions.CONTAINER_IMAGE_PULL_POLICY, kubernetesJobManagerParameters.getImagePullPolicy().name(), container.getImagePullPolicy(), "main container image pull policy");
    final ResourceRequirements requirementsInPodTemplate = container.getResources() == null ? new ResourceRequirements() : container.getResources();
    final ResourceRequirements requirements = KubernetesUtils.getResourceRequirements(requirementsInPodTemplate, kubernetesJobManagerParameters.getJobManagerMemoryMB(), kubernetesJobManagerParameters.getJobManagerMemoryLimitFactor(), kubernetesJobManagerParameters.getJobManagerCPU(), kubernetesJobManagerParameters.getJobManagerCPULimitFactor(), Collections.emptyMap(), Collections.emptyMap());
    mainContainerBuilder.withName(Constants.MAIN_CONTAINER_NAME).withImage(image).withImagePullPolicy(imagePullPolicy).withResources(requirements);
    // Merge fields
    mainContainerBuilder.addAllToPorts(getContainerPorts()).addAllToEnv(getCustomizedEnvs()).addNewEnv().withName(ENV_FLINK_POD_IP_ADDRESS).withValueFrom(new EnvVarSourceBuilder().withNewFieldRef(API_VERSION, POD_IP_FIELD_PATH).build()).endEnv();
    getFlinkLogDirEnv().ifPresent(mainContainerBuilder::addToEnv);
    return mainContainerBuilder.build();
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements)

Aggregations

ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)21 Container (io.fabric8.kubernetes.api.model.Container)13 ArrayList (java.util.ArrayList)9 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)7 Pod (io.fabric8.kubernetes.api.model.Pod)7 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)5 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)5 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)5 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)5 List (java.util.List)5 Map (java.util.Map)5 FlinkPod (org.apache.flink.kubernetes.kubeclient.FlinkPod)5 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)4 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)4 Volume (io.fabric8.kubernetes.api.model.Volume)4 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)4 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)4 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 Service (io.fabric8.kubernetes.api.model.Service)3