summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshizhendong <[email protected]>2021-08-13 17:40:37 +0800
committershizhendong <[email protected]>2021-08-13 17:40:37 +0800
commit4cc53cf92ae4edc8f3a6683c2ca33bb2d4cbcb1b (patch)
tree9b0b829c681002174152be360c2ce1a7633d9520
parenta67159e9702a98a375f98c5be393225db4c1cd17 (diff)
feat: 新增 Talon promtail 批量代理接口
1. 新增 Talon promtail 批量代理接口 2. 调整 agent/config 接口
-rw-r--r--src/main/java/net/geedge/confagent/controller/ConfigController.java32
-rw-r--r--src/main/java/net/geedge/confagent/controller/TalonProxyController.java119
-rw-r--r--src/main/java/net/geedge/confagent/entity/LokiPushUrlEntity.java1
3 files changed, 150 insertions, 2 deletions
diff --git a/src/main/java/net/geedge/confagent/controller/ConfigController.java b/src/main/java/net/geedge/confagent/controller/ConfigController.java
index b56f826..3cea8a8 100644
--- a/src/main/java/net/geedge/confagent/controller/ConfigController.java
+++ b/src/main/java/net/geedge/confagent/controller/ConfigController.java
@@ -4,11 +4,13 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import net.geedge.confagent.entity.LokiPushUrlEntity;
import net.geedge.confagent.entity.LokiPushUrlsEntity;
+import net.geedge.confagent.util.ConfagentUtil;
import net.geedge.confagent.util.R;
import net.geedge.confagent.util.RCode;
import net.geedge.confagent.util.Tool;
import net.geedge.confagent.util.Tool.YamlUtil;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@@ -25,6 +27,16 @@ public class ConfigController extends BaseController{
@Value("${confagent.lokiPushUrlsFile:lokiPushUrls.yml}")
private String lokiFile;
+
+ @Value("${server.port:10090}")
+ private Integer agentPort;
+
+ @Value("${confagent.loki.config}")
+ private String lokiConfPath;
+
+ @Autowired
+ private ConfagentUtil confagentUtil;
+
private static String rootPath = Tool.WebPathUtil.getRootPath();
@@ -38,11 +50,23 @@ public class ConfigController extends BaseController{
//进行验证:target和token均非空
if(lokiPushUrls!=null && lokiPushUrls.getLokiPushUrls()!=null) {
+ int lokiPort = 13100;
+ Map<String, Object> lokiMap = Tool.YamlUtil.readAsMap(lokiConfPath);
+ Map<String, Object> serverObj = (Map<String, Object>) lokiMap.get("server");
+ if (serverObj != null) {
+ lokiPort = (Integer) serverObj.get("http_listen_port");
+ }
+
List<LokiPushUrlEntity> lokiPushUrlList = lokiPushUrls.getLokiPushUrls();
for(LokiPushUrlEntity urlEntity:lokiPushUrlList) {
if(!validLokiPushUrl(urlEntity)) {
return R.error(RCode.LOKI_PUSH_URL_ISNULL);
}
+ // mode = local , target 为 本地 loki地址
+ if (StrUtil.equals("local", urlEntity.getMode())) {
+ urlEntity.setTarget(String.format("127.0.0.1:%s/loki/api/v1/push", lokiPort));
+ urlEntity.setToken(confagentUtil.readToken());
+ }
}
}
@@ -53,10 +77,14 @@ public class ConfigController extends BaseController{
}
/**
- * @Description 验证:target和token均非空
+ * @Description mode = remote 验证:target和token均非空
*/
public Boolean validLokiPushUrl(LokiPushUrlEntity lokiPushUrlsEntity){
- return StrUtil.isNotBlank(lokiPushUrlsEntity.getTarget())&&StrUtil.isNotBlank(lokiPushUrlsEntity.getToken());
+ // return StrUtil.isNotBlank(lokiPushUrlsEntity.getTarget())&&StrUtil.isNotBlank(lokiPushUrlsEntity.getToken());
+ if (StrUtil.equals("remote", lokiPushUrlsEntity.getMode())) {
+ return StrUtil.isNotBlank(lokiPushUrlsEntity.getTarget()) && StrUtil.isNotBlank(lokiPushUrlsEntity.getToken());
+ }
+ return true;
}
}
diff --git a/src/main/java/net/geedge/confagent/controller/TalonProxyController.java b/src/main/java/net/geedge/confagent/controller/TalonProxyController.java
new file mode 100644
index 0000000..ab954be
--- /dev/null
+++ b/src/main/java/net/geedge/confagent/controller/TalonProxyController.java
@@ -0,0 +1,119 @@
+package net.geedge.confagent.controller;
+
+import cn.hutool.core.net.url.UrlBuilder;
+import cn.hutool.log.Log;
+import com.alibaba.fastjson.JSONObject;
+import net.geedge.confagent.util.ConfagentUtil;
+import net.geedge.confagent.util.R;
+import net.geedge.confagent.util.RCode;
+import net.geedge.confagent.util.Tool;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+@RestController
+@RequestMapping
+@SuppressWarnings("all")
+public class TalonProxyController extends BaseController {
+
+ private final static Log log = Log.get();
+
+ @Autowired
+ private ConfagentUtil confagentUtil;
+
+ /**
+ * talon 批量代理接口
+ *
+ * @param mapList
+ * @return
+ */
+ @PostMapping(value = "/talon/proxy/**")
+ public void overwriteConfig(@RequestBody List<Map<String, Object>> talonConfigList, HttpServletRequest request, HttpServletResponse response) {
+ String proxyPath = request.getServletPath().replace("/talon/proxy", "");
+ String token = request.getHeader("Authorization");
+
+ // 校验 token
+ R checkToken = confagentUtil.checkToken(token);
+ if (!Tool.ObjectUtil.equal(RCode.SUCCESS.getCode(), checkToken.getCode())) {
+ ConfagentUtil.writeResponse(response, checkToken);
+ return;
+ }
+
+ // promtail config 接口代理
+ if ("/promtail/config".equals(proxyPath)) {
+ List<Map> resultMapList = new ArrayList<>();
+ int successNum = 0;
+ for (Map<String, Object> talonConfig : talonConfigList) {
+ // ip:port
+ String target = (String) talonConfig.get("target");
+ String talonToken = (String) talonConfig.get("token");
+ Map requestParams = (Map) talonConfig.get("params");
+ Callable<String> task = new Callable<String>() {
+ @Override
+ public String call() throws Exception {
+ UrlBuilder urlBuilder = UrlBuilder.create().setScheme("http").setHost(target.split(":")[0]).setPort(Integer.valueOf(target.split(":")[1])).appendPath("nz-talon").appendPath("promtail").appendPath("config");
+ String result = null;
+ try {
+ result = Tool.HttpUtil.createPost(urlBuilder.build()).auth(talonToken).body(JSONObject.toJSONString(requestParams), "application/json;charset=UTF-8").execute().body();
+ } catch (RuntimeException e) {
+ log.error(String.format("请求 %s 接口失败", urlBuilder.toString()), e);
+ result = JSONObject.toJSONString(R.error(500, e.getMessage()));
+ }
+ return result;
+ }
+ };
+ Future<String> f = Tool.ThreadUtil.execAsync(task);
+ Map resultMap = new LinkedHashMap(4);
+ resultMap.put("assetId", talonConfig.get("assetId"));
+ try {
+ String resultStr = f.get(5, TimeUnit.SECONDS);
+ if (Tool.StrUtil.isNotEmpty(resultStr)) {
+ R parseObject = JSONObject.parseObject(resultStr, R.class);
+ if (RCode.SUCCESS.getCode().equals((Integer) parseObject.get("code"))) {
+ successNum++;
+ }
+ }
+ resultMap.put("result", resultStr);
+ } catch (Exception e) {
+ log.error(e);
+ resultMap.put("result", JSONObject.toJSONString(R.error(500, e.getMessage())));
+ }
+ resultMapList.add(resultMap);
+ }
+
+ OutputStream outputStream = null;
+ try {
+ log.info("talon 返回内容:" + resultMapList.toString());
+ R r = R.ok();
+ Map dataMap = new LinkedHashMap();
+ dataMap.put("total", resultMapList.size());
+ dataMap.put("success", successNum);
+ dataMap.put("fail", resultMapList.size() - successNum);
+ dataMap.put("list", resultMapList);
+ r.put("data", dataMap);
+ outputStream = response.getOutputStream();
+ Tool.IoUtil.writeUtf8(outputStream, true, JSONObject.toJSONString(r));
+ outputStream.flush();
+ } catch (Exception e) {
+ log.error(e);
+ } finally {
+ Tool.IoUtil.close(outputStream);
+ }
+ } else {
+ // TODO
+ }
+ }
+}
diff --git a/src/main/java/net/geedge/confagent/entity/LokiPushUrlEntity.java b/src/main/java/net/geedge/confagent/entity/LokiPushUrlEntity.java
index 9dd3192..83c6be7 100644
--- a/src/main/java/net/geedge/confagent/entity/LokiPushUrlEntity.java
+++ b/src/main/java/net/geedge/confagent/entity/LokiPushUrlEntity.java
@@ -4,6 +4,7 @@ import lombok.Data;
@Data
public class LokiPushUrlEntity {
+ private String mode;
private String token;
private String target;
}