KubernetesTestBase.java 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.kubernetes;

import org.apache.flink.configuration.Configuration;
22
import org.apache.flink.configuration.DeploymentOptionsInternal;
23 24
import org.apache.flink.configuration.JobManagerOptions;
import org.apache.flink.configuration.MemorySize;
25
import org.apache.flink.core.testutils.CommonTestUtils;
26 27 28 29 30
import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
import org.apache.flink.kubernetes.kubeclient.Fabric8FlinkKubeClient;
import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient;
import org.apache.flink.kubernetes.utils.Constants;
import org.apache.flink.runtime.clusterframework.BootstrapTools;
31
import org.apache.flink.runtime.concurrent.Executors;
32 33
import org.apache.flink.util.TestLogger;

34 35 36 37 38 39
import io.fabric8.kubernetes.api.model.Config;
import io.fabric8.kubernetes.api.model.ConfigBuilder;
import io.fabric8.kubernetes.api.model.NamedClusterBuilder;
import io.fabric8.kubernetes.api.model.NamedContextBuilder;
import io.fabric8.kubernetes.client.NamespacedKubernetesClient;
import io.fabric8.kubernetes.client.utils.Serialization;
40
import org.junit.After;
41 42 43 44 45 46 47 48 49
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

50
/** Base test class for Kubernetes. */
51
public class KubernetesTestBase extends TestLogger {
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    protected static final String NAMESPACE = "test";
    protected static final String CLUSTER_ID = "my-flink-cluster1";
    protected static final String CONTAINER_IMAGE = "flink-k8s-test:latest";
    protected static final String KEYTAB_FILE = "keytab";
    protected static final String KRB5_CONF_FILE = "krb5.conf";
    protected static final KubernetesConfigOptions.ImagePullPolicy CONTAINER_IMAGE_PULL_POLICY =
            KubernetesConfigOptions.ImagePullPolicy.IfNotPresent;
    protected static final int JOB_MANAGER_MEMORY = 768;

    @Rule public MixedKubernetesServer server = new MixedKubernetesServer(true, true);

    @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();

    protected File flinkConfDir;

    protected File hadoopConfDir;

    protected File kerberosDir;

    protected final Configuration flinkConfig = new Configuration();

74
    protected NamespacedKubernetesClient kubeClient;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    protected FlinkKubeClient flinkKubeClient;

    protected void setupFlinkConfig() {
        flinkConfig.setString(KubernetesConfigOptions.NAMESPACE, NAMESPACE);
        flinkConfig.setString(KubernetesConfigOptions.CLUSTER_ID, CLUSTER_ID);
        flinkConfig.setString(KubernetesConfigOptions.CONTAINER_IMAGE, CONTAINER_IMAGE);
        flinkConfig.set(
                KubernetesConfigOptions.CONTAINER_IMAGE_PULL_POLICY, CONTAINER_IMAGE_PULL_POLICY);
        flinkConfig.set(
                JobManagerOptions.TOTAL_PROCESS_MEMORY, MemorySize.ofMebiBytes(JOB_MANAGER_MEMORY));
        flinkConfig.set(DeploymentOptionsInternal.CONF_DIR, flinkConfDir.toString());
    }

    protected void onSetup() throws Exception {}

    @Before
    public final void setup() throws Exception {
        flinkConfDir = temporaryFolder.newFolder().getAbsoluteFile();
        hadoopConfDir = temporaryFolder.newFolder().getAbsoluteFile();
        kerberosDir = temporaryFolder.newFolder().getAbsoluteFile();

        setupFlinkConfig();
        writeFlinkConfiguration();

        kubeClient = server.getClient().inNamespace(NAMESPACE);
        flinkKubeClient =
                new Fabric8FlinkKubeClient(
                        flinkConfig, kubeClient, Executors::newDirectExecutorService);

        onSetup();
    }

    @After
    public void tearDown() throws Exception {
        flinkKubeClient.close();
    }

    protected void writeFlinkConfiguration() throws IOException {
        BootstrapTools.writeConfiguration(
                this.flinkConfig, new File(flinkConfDir, "flink-conf.yaml"));
    }

    protected Map<String, String> getCommonLabels() {
        Map<String, String> labels = new HashMap<>();
        labels.put(Constants.LABEL_TYPE_KEY, Constants.LABEL_TYPE_NATIVE_TYPE);
        labels.put(Constants.LABEL_APP_KEY, CLUSTER_ID);
        return labels;
    }

    protected void setHadoopConfDirEnv() {
        Map<String, String> map = new HashMap<>();
        map.put(Constants.ENV_HADOOP_CONF_DIR, hadoopConfDir.toString());
        CommonTestUtils.setEnv(map, false);
    }

    protected void generateHadoopConfFileItems() throws IOException {
        KubernetesTestUtils.createTemporyFile("some data", hadoopConfDir, "core-site.xml");
        KubernetesTestUtils.createTemporyFile("some data", hadoopConfDir, "hdfs-site.xml");
    }

    protected void generateKerberosFileItems() throws IOException {
        KubernetesTestUtils.createTemporyFile("some keytab", kerberosDir, KEYTAB_FILE);
        KubernetesTestUtils.createTemporyFile("some conf", kerberosDir, KRB5_CONF_FILE);
    }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    protected String writeKubeConfigForMockKubernetesServer() throws Exception {
        final Config kubeConfig =
                new ConfigBuilder()
                        .withApiVersion(server.getClient().getApiVersion())
                        .withClusters(
                                new NamedClusterBuilder()
                                        .withName(CLUSTER_ID)
                                        .withNewCluster()
                                        .withNewServer(server.getClient().getMasterUrl().toString())
                                        .withInsecureSkipTlsVerify(true)
                                        .endCluster()
                                        .build())
                        .withContexts(
                                new NamedContextBuilder()
                                        .withName(CLUSTER_ID)
                                        .withNewContext()
                                        .withCluster(CLUSTER_ID)
                                        .withUser(
                                                server.getClient().getConfiguration().getUsername())
                                        .endContext()
                                        .build())
                        .withNewCurrentContext(CLUSTER_ID)
                        .build();
        final File kubeConfigFile = new File(temporaryFolder.newFolder(".kube"), "config");
        Serialization.yamlMapper().writeValue(kubeConfigFile, kubeConfig);
        return kubeConfigFile.getAbsolutePath();
    }
168
}