You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by do...@apache.org on 2022/09/28 09:24:56 UTC

[inlong] branch release-1.3.0 updated: [INLONG-6047][Agent] Fix file could not be matched in the k8s (#6048)

This is an automated email from the ASF dual-hosted git repository.

dockerzhang pushed a commit to branch release-1.3.0
in repository https://gitbox.apache.org/repos/asf/inlong.git


The following commit(s) were added to refs/heads/release-1.3.0 by this push:
     new 407f81f0f [INLONG-6047][Agent] Fix file could not be matched in the k8s (#6048)
407f81f0f is described below

commit 407f81f0fc329ce9492d64cf1b606e2a8593231b
Author: ganfengtan <Ga...@users.noreply.github.com>
AuthorDate: Wed Sep 28 17:23:31 2022 +0800

    [INLONG-6047][Agent] Fix file could not be matched in the k8s (#6048)
---
 .../sources/reader/file/KubernetesFileReader.java  |  2 +-
 .../inlong/agent/plugin/utils/MetaDataUtils.java   |  9 ++++--
 .../agent/plugin/utils/MetaDataUtilsTest.java      | 37 ++++++++++++++++++++++
 3 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/file/KubernetesFileReader.java b/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/file/KubernetesFileReader.java
index 026bdc805..8ea052bc1 100644
--- a/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/file/KubernetesFileReader.java
+++ b/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/sources/reader/file/KubernetesFileReader.java
@@ -115,7 +115,7 @@ public final class KubernetesFileReader extends AbstractFileReader {
             return null;
         }
         Map<String, String> k8sInfo = MetaDataUtils.getLogInfo(fileReaderOperator.file.getName());
-        log.info("k8s information size:{}", k8sInfo.size());
+        log.info("file name is: {}, k8s information size: {}", fileReaderOperator.file.getName(), k8sInfo.size());
         Map<String, String> metadata = new HashMap<>();
         if (k8sInfo.isEmpty()) {
             return metadata;
diff --git a/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/utils/MetaDataUtils.java b/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/utils/MetaDataUtils.java
index e3a00ee9d..379e2dc32 100644
--- a/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/utils/MetaDataUtils.java
+++ b/inlong-agent/agent-plugins/src/main/java/org/apache/inlong/agent/plugin/utils/MetaDataUtils.java
@@ -28,6 +28,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 import static org.apache.inlong.agent.constant.JobConstants.JOB_FILE_META_FILTER_BY_LABELS;
@@ -47,7 +49,9 @@ public class MetaDataUtils {
     private static final String LOG_MARK = ".log";
 
     // standard log path for k8s
-    private static final String STANDARD_OUT = "/var/log/containers";
+    private static final String FILE_NAME_PATTERN = "(^[-a-zA-Z0-9]+)_([a-zA-Z0-9-]+)_([a-zA-Z0-9-]+)(.log)";
+
+    private static final Pattern PATTERN = Pattern.compile(FILE_NAME_PATTERN);
 
     /**
      * standard log for k8s
@@ -55,8 +59,9 @@ public class MetaDataUtils {
      * get pod_name,namespace,container_name,container_id
      */
     public static Map<String, String> getLogInfo(String fileName) {
+        Matcher matcher = PATTERN.matcher(fileName);
         Map<String, String> podInf = new HashMap<>();
-        if (StringUtils.isBlank(fileName) || !fileName.contains(STANDARD_OUT)) {
+        if (StringUtils.isBlank(fileName) || !matcher.matches()) {
             return podInf;
         }
         // file name example: /var/log/containers/<pod_name>_<namespace>_<container_name>-<continer_id>.log
diff --git a/inlong-agent/agent-plugins/src/test/java/org/apache/inlong/agent/plugin/utils/MetaDataUtilsTest.java b/inlong-agent/agent-plugins/src/test/java/org/apache/inlong/agent/plugin/utils/MetaDataUtilsTest.java
new file mode 100644
index 000000000..5dfc655e5
--- /dev/null
+++ b/inlong-agent/agent-plugins/src/test/java/org/apache/inlong/agent/plugin/utils/MetaDataUtilsTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.inlong.agent.plugin.utils;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Map;
+
+/**
+ * metadata of k8s utils test
+ */
+public class MetaDataUtilsTest {
+
+    @Test
+    public void getLogInfo() {
+        String fileName = "testcase-0_xb-test240_testcase2"
+                + "-8050825882878a0aef05cd597abb09917a1e090d09f4d1ed288488311ca0309c.log";
+        Map<String, String> metaMap = MetaDataUtils.getLogInfo(fileName);
+        Assert.assertEquals(4, metaMap.size());
+    }
+}
\ No newline at end of file