|
| 1 | +import React, {useEffect, useRef} from "react"; |
| 2 | +import {Form, Input, InputNumber, Modal, Select} from "antd"; |
| 3 | +import * as ServiceBackend from "./backend/ServiceBackend"; |
| 4 | +import * as Setting from "./Setting"; |
| 5 | + |
| 6 | +function DeploymentExposeModal({deploy, open, onClose}) { |
| 7 | + const [form] = Form.useForm(); |
| 8 | + const [submitting, setSubmitting] = React.useState(false); |
| 9 | + const prevOpen = useRef(false); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + if (open && !prevOpen.current && deploy) { |
| 13 | + form.setFieldsValue({ |
| 14 | + name: deploy.name, |
| 15 | + port: 80, |
| 16 | + targetPort: 80, |
| 17 | + type: "ClusterIP", |
| 18 | + }); |
| 19 | + } |
| 20 | + prevOpen.current = open; |
| 21 | + }, [open, deploy, form]); |
| 22 | + |
| 23 | + function handleOk() { |
| 24 | + form.validateFields().then(values => { |
| 25 | + const payload = { |
| 26 | + namespace: deploy.namespace, |
| 27 | + name: values.name, |
| 28 | + type: values.type, |
| 29 | + selector: {"app": deploy.name}, |
| 30 | + ports: [{ |
| 31 | + name: "http", |
| 32 | + protocol: "TCP", |
| 33 | + port: values.port, |
| 34 | + targetPort: String(values.targetPort), |
| 35 | + }], |
| 36 | + }; |
| 37 | + setSubmitting(true); |
| 38 | + ServiceBackend.addService(payload).then(res => { |
| 39 | + if (res.status === "ok") { |
| 40 | + Setting.showMessage("success", `Service "${values.name}" created`); |
| 41 | + onClose(); |
| 42 | + } else { |
| 43 | + Setting.showMessage("error", res.msg); |
| 44 | + } |
| 45 | + }).catch(e => Setting.showMessage("error", e.message)) |
| 46 | + .finally(() => setSubmitting(false)); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <Modal |
| 52 | + title={`Expose Deployment: ${deploy?.name ?? ""}`} |
| 53 | + open={open} |
| 54 | + onOk={handleOk} |
| 55 | + onCancel={onClose} |
| 56 | + confirmLoading={submitting} |
| 57 | + okText="Create Service" |
| 58 | + width={480} |
| 59 | + destroyOnHidden |
| 60 | + > |
| 61 | + <Form form={form} layout="vertical"> |
| 62 | + <Form.Item |
| 63 | + label="Service Name" |
| 64 | + name="name" |
| 65 | + rules={[{required: true, message: "Service name is required"}]} |
| 66 | + > |
| 67 | + <Input placeholder="my-deployment" /> |
| 68 | + </Form.Item> |
| 69 | + <Form.Item label="Type" name="type" rules={[{required: true}]}> |
| 70 | + <Select options={[ |
| 71 | + {label: "ClusterIP — 集群内访问", value: "ClusterIP"}, |
| 72 | + {label: "NodePort — 节点端口对外暴露", value: "NodePort"}, |
| 73 | + {label: "LoadBalancer — 云负载均衡器", value: "LoadBalancer"}, |
| 74 | + ]} /> |
| 75 | + </Form.Item> |
| 76 | + <Form.Item label="Port (Service 端口)" name="port" rules={[{required: true}]}> |
| 77 | + <InputNumber min={1} max={65535} style={{width: "100%"}} /> |
| 78 | + </Form.Item> |
| 79 | + <Form.Item label="Target Port (Pod 容器端口)" name="targetPort" rules={[{required: true}]}> |
| 80 | + <InputNumber min={1} max={65535} style={{width: "100%"}} /> |
| 81 | + </Form.Item> |
| 82 | + </Form> |
| 83 | + </Modal> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +export default DeploymentExposeModal; |
0 commit comments