Unverified Commit f3f4d225 authored by gaoyan's avatar gaoyan Committed by GitHub

fix k8s template bug (#2660)

* fix k8s template bug * fix k8s template bug
parent 824a3e80
......@@ -64,4 +64,7 @@ public class K8sConfig {
example = "tm-pod-template.yaml",
notes = "YAML file containing the pod template for TaskManagers in Flink jobs")
private String tmPodTemplate;
@ApiModelProperty(value = "KubeConfig", dataType = "String", example = "kubeconfig.yaml", notes = "KubeConfig file")
private String kubeConfig;
}
......@@ -91,8 +91,7 @@ public class KubernetesApplicationGateway extends KubernetesGateway {
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
client.close();
kubernetesClient.close();
close();
}
}
......
......@@ -30,6 +30,7 @@ import org.dinky.gateway.result.TestResult;
import org.dinky.utils.TextUtil;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.CoreOptions;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.configuration.PipelineOptions;
......@@ -42,8 +43,8 @@ import org.apache.flink.kubernetes.kubeclient.FlinkKubeClientFactory;
import org.apache.http.util.TextUtils;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.UUID;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
......@@ -54,12 +55,15 @@ import io.fabric8.kubernetes.client.KubernetesClient;
/**
* KubernetesGateway
*
*/
public abstract class KubernetesGateway extends AbstractGateway {
protected FlinkKubeClient client;
protected KubernetesClient kubernetesClient;
protected String flinkConfigPath;
protected FlinkConfig flinkConfig;
protected K8sConfig k8sConfig;
protected String tmpConfDir;
public KubernetesGateway() {}
......@@ -69,10 +73,12 @@ public abstract class KubernetesGateway extends AbstractGateway {
}
private void initConfig() {
String flinkConfigPath = config.getClusterConfig().getFlinkConfigPath();
FlinkConfig flinkConfig = config.getFlinkConfig();
K8sConfig k8sConfig = config.getKubernetesConfig();
tmpConfDir = String.format("%s/tmp/kubernets/%s", System.getProperty("user.dir"), UUID.randomUUID());
flinkConfigPath = config.getClusterConfig().getFlinkConfigPath();
flinkConfig = config.getFlinkConfig();
k8sConfig = config.getKubernetesConfig();
configuration.set(CoreOptions.CLASSLOADER_RESOLVE_ORDER, "parent-first");
try {
addConfigParas(
GlobalConfiguration.loadConfiguration(flinkConfigPath).toMap());
......@@ -91,6 +97,7 @@ public abstract class KubernetesGateway extends AbstractGateway {
preparPodTemplate(k8sConfig.getPodTemplate(), KubernetesConfigOptions.KUBERNETES_POD_TEMPLATE);
preparPodTemplate(k8sConfig.getJmPodTemplate(), KubernetesConfigOptions.JOB_MANAGER_POD_TEMPLATE);
preparPodTemplate(k8sConfig.getTmPodTemplate(), KubernetesConfigOptions.TASK_MANAGER_POD_TEMPLATE);
preparPodTemplate(k8sConfig.getKubeConfig(), KubernetesConfigOptions.KUBE_CONFIG_FILE);
if (getType().isApplicationMode()) {
resetCheckpointInApplicationMode(flinkConfig.getJobName());
......@@ -98,27 +105,22 @@ public abstract class KubernetesGateway extends AbstractGateway {
}
private void preparPodTemplate(String podTemplate, ConfigOption<String> option) {
if (TextUtil.isEmpty(podTemplate)) {
return;
}
String filePath = String.format(
"%s/tmp/Kubernets/%s.yaml",
System.getProperty("user.dir"), config.getFlinkConfig().getJobName());
if (FileUtil.exist(filePath)) {
Assert.isTrue(FileUtil.del(filePath));
if (!TextUtil.isEmpty(podTemplate)) {
String filePath = String.format("%s/%s.yaml", tmpConfDir, option.key());
if (FileUtil.exist(filePath)) {
Assert.isTrue(FileUtil.del(filePath));
}
FileUtil.writeUtf8String(podTemplate, filePath);
addConfigParas(option, filePath);
}
FileUtil.writeUtf8String(podTemplate, filePath);
addConfigParas(option, filePath);
}
private void initKubeClient() {
client = FlinkKubeClientFactory.getInstance().fromConfiguration(configuration, "client");
String kubeFile = configuration.getString(KubernetesConfigOptions.KUBE_CONFIG_FILE);
if (TextUtils.isEmpty(kubeFile)) {
if (TextUtils.isEmpty(k8sConfig.getKubeConfig())) {
kubernetesClient = new DefaultKubernetesClient();
} else {
String kubeStr = FileUtil.readString(kubeFile, StandardCharsets.UTF_8);
kubernetesClient = DefaultKubernetesClient.fromConfig(kubeStr);
kubernetesClient = DefaultKubernetesClient.fromConfig(k8sConfig.getKubeConfig());
}
}
......@@ -166,6 +168,8 @@ public abstract class KubernetesGateway extends AbstractGateway {
public TestResult test() {
try {
initConfig();
// Test mode no jobName, use uuid .
addConfigParas(KubernetesConfigOptions.CLUSTER_ID, UUID.randomUUID().toString());
initKubeClient();
if (client instanceof Fabric8FlinkKubeClient) {
Object internalClient = ReflectUtil.getFieldValue(client, "internalClient");
......@@ -181,6 +185,8 @@ public abstract class KubernetesGateway extends AbstractGateway {
logger.error(Status.GAETWAY_KUBERNETS_TEST_FAILED.getMessage(), e);
return TestResult.fail(
StrFormatter.format("{}:{}", Status.GAETWAY_KUBERNETS_TEST_FAILED.getMessage(), e.getMessage()));
} finally {
close();
}
}
......@@ -205,4 +211,14 @@ public abstract class KubernetesGateway extends AbstractGateway {
logger.error(e.getMessage(), e);
}
}
public void close() {
if (client != null) {
client.close();
}
if (kubernetesClient != null) {
kubernetesClient.close();
}
FileUtil.del(tmpConfDir);
}
}
......@@ -35,6 +35,7 @@ import org.dinky.gateway.result.TestResult;
import org.dinky.utils.TextUtil;
import org.apache.flink.configuration.CoreOptions;
import org.apache.http.util.TextUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
......@@ -61,16 +62,12 @@ public abstract class KubernetsOperatorGateway extends AbstractGateway {
private FlinkDeployment flinkDeployment = new FlinkDeployment();
private FlinkDeploymentSpec flinkDeploymentSpec = new FlinkDeploymentSpec();
private KubernetesClient kubernetesClient;
// TODO 改为ProcessStep注释
private static final Logger logger = LoggerFactory.getLogger(KubernetsOperatorGateway.class);
@Override
protected void init() {
kubernetsConfiguration = config.getKubernetesConfig().getConfiguration();
flinkConfig = config.getFlinkConfig();
k8sConfig = config.getKubernetesConfig();
kubernetesClient = new DefaultKubernetesClient();
initKubeClient();
initBase();
initMetadata();
initSpec();
......@@ -78,13 +75,24 @@ public abstract class KubernetsOperatorGateway extends AbstractGateway {
initJob();
}
private void initKubeClient() {
kubernetsConfiguration = config.getKubernetesConfig().getConfiguration();
flinkConfig = config.getFlinkConfig();
k8sConfig = config.getKubernetesConfig();
if (TextUtils.isEmpty(k8sConfig.getKubeConfig())) {
kubernetesClient = new DefaultKubernetesClient();
} else {
kubernetesClient = DefaultKubernetesClient.fromConfig(k8sConfig.getKubeConfig());
}
}
@Override
public TestResult test() {
kubernetsConfiguration = config.getKubernetesConfig().getConfiguration();
flinkConfig = config.getFlinkConfig();
config.getFlinkConfig().setJobName("test");
kubernetesClient = new DefaultKubernetesClient();
initKubeClient();
initBase();
initMetadata();
initSpec();
......@@ -97,8 +105,7 @@ public abstract class KubernetsOperatorGateway extends AbstractGateway {
public boolean onJobFinishCallback(String status) {
kubernetsConfiguration = config.getKubernetesConfig().getConfiguration();
kubernetesClient = new DefaultKubernetesClient();
initKubeClient();
String jobName = config.getFlinkConfig().getJobName();
if (status.equals(JobStatus.FINISHED.getValue())) {
logger.info(
......
......@@ -34,6 +34,7 @@ import org.dinky.gateway.result.TestResult;
import org.apache.flink.client.deployment.ClusterRetrieveException;
import org.apache.flink.client.program.ClusterClient;
import org.apache.flink.configuration.CoreOptions;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.configuration.SecurityOptions;
......@@ -89,6 +90,7 @@ public abstract class YarnGateway extends AbstractGateway {
private void initConfig() {
final ClusterConfig clusterConfig = config.getClusterConfig();
configuration = GlobalConfiguration.loadConfiguration(clusterConfig.getFlinkConfigPath());
configuration.set(CoreOptions.CLASSLOADER_RESOLVE_ORDER, "parent-first");
final FlinkConfig flinkConfig = config.getFlinkConfig();
if (Asserts.isNotNull(flinkConfig.getConfiguration())) {
......
......@@ -656,7 +656,7 @@ export default {
'rc.cc.flinkConfig': 'Flink Config',
'rc.cc.flinkConfigPath': 'Flink Config File Path',
'rc.cc.flinkConfigPathHelp':
'Please enter flink-conf.yaml path! Values such as /opt/module/flink/conf ',
'Only specify to the folder, dinky will auto read the configuration, this parameter is optional in K8S mode',
'rc.cc.flinkConfigPathPlaceholder':
'Please enter the flink-conf.yaml path! Values such as /opt/module/flink/conf ',
'rc.cc.hadoop.defineConfig': 'Hadoop Custom Config (high priority)',
......@@ -685,6 +685,8 @@ export default {
'rc.cc.k8s.tmCpu': 'TaskManager CPU Config',
'rc.cc.k8s.tmCpuHelp': 'TaskManager CPU configuration! eg',
'rc.cc.k8sConfig': 'Kubernetes Config',
'rc.cc.k8s.defaultKubeConfigHelp':
'If you do not fill in this field, the `~/.kube/config` file will be used by default',
'rc.cc.k8sOp.version': 'Flink Version',
'rc.cc.k8sOp.versionHelp': 'Please select the Flink version!',
'rc.cc.key': 'Config Key',
......@@ -714,6 +716,8 @@ export default {
'rc.cc.type': 'Type',
'rc.cc.typePlaceholder': 'Please select the cluster configuration type!',
'rc.cc.value': 'Config Value',
'rc.cc.loadFromLocal': 'Load from a local file',
'rc.ci.alias': 'Alias',
'rc.ci.aliasPlaceholder': 'Please enter an alias!',
'rc.ci.ar': 'Auto Registration',
......
......@@ -611,7 +611,7 @@ export default {
'rc.cc.addConfig': '添加配置项',
'rc.cc.baseConfig': '基本配置',
'rc.cc.ckpDir': '检查点路径',
'rc.cc.ckpDirHelp': '请输入检查点路径! 此参数配置项为',
'rc.cc.ckpDirHelp': '对应SavePoint路径',
'rc.cc.create': '创建集群配置',
'rc.cc.defineConfig': 'Flink 预设配置(高优先级)',
'rc.cc.delete': '删除集群配置',
......@@ -634,7 +634,8 @@ export default {
'rc.cc.flink.defineConfig': 'Flink 自定义配置(高优先级)',
'rc.cc.flinkConfig': 'Flink 配置',
'rc.cc.flinkConfigPath': 'Flink 配置文件路径',
'rc.cc.flinkConfigPathHelp': '请输入 flink-conf.yaml 路径!值如 /opt/module/flink/conf ',
'rc.cc.flinkConfigPathHelp':
'仅指定到文件夹,dinky会自行读取文件夹下的配置文件,k8s模式下此参数可选填',
'rc.cc.flinkConfigPathPlaceholder': '请输入 flink-conf.yaml 路径!值如 /opt/module/flink/conf ',
'rc.cc.hadoop.defineConfig': 'Hadoop 自定义配置(高优先级)',
'rc.cc.hadoopConfig': 'Hadoop 配置',
......@@ -659,6 +660,7 @@ export default {
'rc.cc.k8s.tmCpu': 'TaskManager CPU 配置',
'rc.cc.k8s.tmCpuHelp': 'TaskManager CPU 配置! eg',
'rc.cc.k8sConfig': 'Kubernetes 配置',
'rc.cc.k8s.defaultKubeConfigHelp': '如果不填写,则默认使用`~/.kube/config`文件',
'rc.cc.k8sOp.version': 'Flink 版本',
'rc.cc.k8sOp.versionHelp': '请选择Flink 版本!',
'rc.cc.key': '配置项',
......@@ -671,7 +673,7 @@ export default {
'rc.cc.search': '搜索 名称',
'rc.cc.namePlaceholder': '请输入集群配置名称!',
'rc.cc.spDir': '保存点路径',
'rc.cc.spDirHelp': '请输入保存点路径! 此参数配置项为',
'rc.cc.spDirHelp': '对应CheckPoint目录,如 hdfs://xxx/flink',
'rc.cc.sqlSubmitJarPath': 'Jar 文件路径',
'rc.cc.sqlSubmitJarPathHelp': '请输入 Jar 文件路径! eg',
'rc.cc.start': '启动 Session 集群',
......@@ -685,8 +687,11 @@ export default {
'rc.cc.type': '类型',
'rc.cc.typePlaceholder': '请选择集群配置类型!',
'rc.cc.value': '配置值',
'rc.cc.loadFromLocal': '从本地文件加载',
'rc.ci.alias': '实例别名',
'rc.ci.aliasPlaceholder': '请输入别名!',
'rc.ci.ar': '自动注册',
'rc.ci.mr': '手动注册',
'rc.ci.create': '创建集群',
......
......@@ -23,7 +23,6 @@ export const ExposedTypeOptions = [
];
export const versionOptions = [
{ label: '1.13', value: 'v1_13' },
{ label: '1.14', value: 'v1_14' },
{ label: '1.15', value: 'v1_15' },
{ label: '1.16', value: 'v1_16' },
......
......@@ -28,6 +28,7 @@ import { KUBERNETES_CONFIG_LIST } from '@/pages/RegCenter/Cluster/Configuration/
import { ClusterType } from '@/pages/RegCenter/Cluster/constants';
import { l } from '@/utils/intl';
import { connect } from '@@/exports';
import { UploadOutlined } from '@ant-design/icons';
import {
ProCard,
ProFormGroup,
......@@ -36,17 +37,29 @@ import {
ProFormSelect,
ProFormText
} from '@ant-design/pro-components';
import { Col, Divider, Row, Space } from 'antd';
import { Button, Col, Divider, Row, Space, Typography, Upload, UploadProps } from 'antd';
import { FormInstance } from 'antd/es/form/hooks/useForm';
import { RcFile } from 'antd/es/upload/interface';
import { Values } from 'async-validator';
import { useState } from 'react';
const { Text } = Typography;
const CodeEditProps = {
height: '40vh',
height: '30vh',
width: '90vh',
lineNumbers: 'on',
language: 'yaml'
};
const FlinkK8s = (props: { type: string; value: any } & connect) => {
const { type, value, flinkConfigOptions } = props;
const FlinkK8s = (props: { type: string; value: any; form: FormInstance<Values> } & connect) => {
const { type, value, form, flinkConfigOptions } = props;
const k8sConfig = value.config?.kubernetesConfig;
const [kubeConfig, setKubeConfig] = useState<string>(k8sConfig?.kubeConfig);
const [podTemplate, setPodTemplate] = useState<string>(k8sConfig?.podTemplate);
const [jmPodTemplate, setJmPodTemplate] = useState<string>(k8sConfig?.jmPodTemplate);
const [tmPodTemplate, setTmPodTemplate] = useState<string>(k8sConfig?.tmPodTemplate);
const renderK8sConfig = () => {
return (
......@@ -58,6 +71,7 @@ const FlinkK8s = (props: { type: string; value: any } & connect) => {
name={['config', 'kubernetesConfig', 'configuration', item.name]}
label={item.label}
width={260}
rules={item.rules ?? []}
placeholder={item.placeholder}
/>
))}
......@@ -65,32 +79,81 @@ const FlinkK8s = (props: { type: string; value: any } & connect) => {
);
};
const renderEdit = (
name: string[],
onChange: (value: string) => void,
key: string,
value?: string,
tips?: string
) => {
const uploadProp: UploadProps = {
beforeUpload: (file: RcFile) => {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
form.setFieldValue(name, reader.result as string);
onChange(reader.result as string);
};
},
showUploadList: false
};
return (
<Space direction={'vertical'}>
<Upload {...uploadProp}>
<Button icon={<UploadOutlined />}>{l('rc.cc.loadFromLocal')}</Button>
<Text type={'secondary'}> {tips}</Text>
</Upload>
<ProFormItem key={key} name={name}>
<CodeEdit {...CodeEditProps} code={value ?? ''} />
</ProFormItem>
</Space>
);
};
const configTags = [
{
key: 'kubeConfig',
forceRender: true,
label: <TagAlignCenter>K8s KubeConfig</TagAlignCenter>,
children: renderEdit(
['config', 'kubernetesConfig', 'kubeConfig'],
(value) => setKubeConfig(value),
'k8s-kubeconfig-item',
kubeConfig,
l('rc.cc.k8s.defaultKubeConfigHelp')
)
},
{
key: 'defaultPodTemplate',
forceRender: true,
label: <TagAlignCenter>Default Pod Template</TagAlignCenter>,
children: (
<ProFormItem key='dpe' name={['config', 'kubernetesConfig', 'podTemplate']}>
<CodeEdit {...CodeEditProps} code={value.config?.kubernetesConfig?.podTemplate ?? ''} />
</ProFormItem>
children: renderEdit(
['config', 'kubernetesConfig', 'podTemplate'],
(value) => setPodTemplate(value),
'k8s-podTemplate-item',
podTemplate
)
},
{
key: 'JMPodTemplate',
forceRender: true,
label: <TagAlignCenter>JM Pod Template</TagAlignCenter>,
children: (
<ProFormItem key='jmdpe' name={['config', 'kubernetesConfig', 'jmPodTemplate']}>
<CodeEdit {...CodeEditProps} code={value.config?.kubernetesConfig?.jmPodTemplate ?? ''} />
</ProFormItem>
children: renderEdit(
['config', 'kubernetesConfig', 'jmPodTemplate'],
(value) => setJmPodTemplate(value),
'k8s-jmPodTemplate-item',
jmPodTemplate
)
},
{
key: 'TMPodTemplate',
forceRender: true,
label: <TagAlignCenter>TM Pod Template</TagAlignCenter>,
children: (
<ProFormItem key='tmdpe' name={['config', 'kubernetesConfig', 'tmPodTemplate']}>
<CodeEdit {...CodeEditProps} code={value.config?.kubernetesConfig?.tmPodTemplate ?? ''} />
</ProFormItem>
children: renderEdit(
['config', 'kubernetesConfig', 'tmPodTemplate'],
(value) => setTmPodTemplate(value),
'k8s-tmPodTemplate-item',
tmPodTemplate
)
}
];
......@@ -113,6 +176,7 @@ const FlinkK8s = (props: { type: string; value: any } & connect) => {
tooltip={l('rc.cc.k8s.exposedHelp')}
placeholder={l('rc.cc.k8s.exposedHelp')}
options={ExposedTypeOptions}
rules={[{ required: true }]}
width={250}
/>
)}
......@@ -123,9 +187,16 @@ const FlinkK8s = (props: { type: string; value: any } & connect) => {
width={250}
placeholder={l('rc.cc.k8sOp.versionHelp')}
options={versionOptions}
rules={[{ required: true }]}
/>
)}
{renderK8sConfig()}
<ProFormText
name={['config', 'clusterConfig', 'flinkConfigPath']}
label={l('rc.cc.flinkConfigPath')}
placeholder={l('rc.cc.flinkConfigPathPlaceholder')}
tooltip={l('rc.cc.flinkConfigPathHelp')}
/>
</ProFormGroup>
<ProFormList
name={['config', 'flinkConfig', 'flinkConfigList']}
......
......@@ -42,11 +42,6 @@ const HighPriorityConfig = () => {
<Divider>{l('rc.cc.defineConfig')}</Divider>
<ProFormGroup direction={'horizontal'} align={'baseline'}>
{renderHighPriorityConfig()}
<ProFormText
name={['config', 'clusterConfig', 'flinkConfigPath']}
label={'Flink Conf Yaml Path'}
width={400}
/>
</ProFormGroup>
</>
);
......
......@@ -44,7 +44,7 @@ const ConfigurationForm: React.FC<ConfigurationFormProps> = (props) => {
{type && type === ClusterType.YARN ? (
<YarnConfig />
) : (
<FlinkK8s type={type} value={value} />
<FlinkK8s type={type} value={value} form={form} />
)}
<HighPriorityConfig />
<ApplicationConfig />
......
......@@ -133,19 +133,22 @@ export const KUBERNETES_CONFIG_LIST: FormConfig[] = [
name: 'kubernetes.namespace',
label: l('rc.cc.k8s.namespace'),
placeholder: l('rc.cc.k8s.namespaceHelp'),
tooltip: l('rc.cc.k8s.namespaceHelp')
tooltip: l('rc.cc.k8s.namespaceHelp'),
rules: [{ required: true }]
},
{
name: 'kubernetes.service.account',
label: l('rc.cc.k8s.account'),
placeholder: l('rc.cc.k8s.accountHelp'),
tooltip: l('rc.cc.k8s.accountHelp')
tooltip: l('rc.cc.k8s.accountHelp'),
rules: [{ required: true }]
},
{
name: 'kubernetes.container.image',
label: l('rc.cc.k8s.image'),
placeholder: l('rc.cc.k8s.imageHelp'),
tooltip: l('rc.cc.k8s.imageHelp')
tooltip: l('rc.cc.k8s.imageHelp'),
rules: [{ required: true }]
},
{
name: 'kubernetes.jobmanager.cpu',
......@@ -158,11 +161,5 @@ export const KUBERNETES_CONFIG_LIST: FormConfig[] = [
label: l('rc.cc.k8s.tmCpu'),
placeholder: l('rc.cc.k8s.tmCpuHelp'),
tooltip: l('rc.cc.k8s.tmCpuHelp')
},
{
name: 'kubernetes.config.file',
label: l('rc.cc.k8s.configFile'),
placeholder: l('rc.cc.k8s.configFileHelp'),
tooltip: l('rc.cc.k8s.configFileHelp')
}
];
......@@ -26,4 +26,5 @@ export type FormConfig = {
help?: string;
showOnSubmitType?: string;
showType?: string;
rules?: any[];
};
......@@ -124,6 +124,7 @@ export type AppConfig = {
export type K8sConfig = {
configuration: Map<string, string>;
dockerConfig: Map<string, string>;
kubeConfig: string;
podTemplate: string;
jmPodTemplate: string;
tmPodTemplate: string;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment