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

wangyang0918 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-kubernetes-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new 06dd786  [FLINK-27124] Flink Kubernetes operator prints starting logs 
with corrent version
06dd786 is described below

commit 06dd78676e4ece0bbbcbd75de3d69eb37e3a6331
Author: SteNicholas <[email protected]>
AuthorDate: Thu Apr 7 23:02:13 2022 +0800

    [FLINK-27124] Flink Kubernetes operator prints starting logs with corrent 
version
    
    This closes #160.
---
 Dockerfile                                         |   1 +
 flink-kubernetes-operator/pom.xml                  |  40 ++++++
 .../flink/kubernetes/operator/FlinkOperator.java   |   4 +-
 .../flink/kubernetes/operator/utils/EnvUtils.java  | 135 +++++++++++++++++++++
 .../.flink-kubernetes-operator.version.properties  |  18 +++
 .../operator/admission/FlinkOperatorWebhook.java   |   3 +-
 pom.xml                                            |   2 +
 7 files changed, 199 insertions(+), 4 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 16a19b3..b45eb23 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -34,6 +34,7 @@ COPY $OPERATOR_DIR/pom.xml ./$OPERATOR_DIR/
 COPY $OPERATOR_DIR/src ./$OPERATOR_DIR/src
 COPY $WEBHOOK_DIR/src ./$WEBHOOK_DIR/src
 
+COPY .git ./.git
 COPY tools ./tools
 
 RUN --mount=type=cache,target=/root/.m2 mvn -ntp clean install 
-DskipTests=$SKIP_TESTS
diff --git a/flink-kubernetes-operator/pom.xml 
b/flink-kubernetes-operator/pom.xml
index d0181e7..c7ccfba 100644
--- a/flink-kubernetes-operator/pom.xml
+++ b/flink-kubernetes-operator/pom.xml
@@ -138,6 +138,17 @@ under the License.
     </dependencies>
 
     <build>
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <filtering>false</filtering>
+            </resource>
+            <resource>
+                <directory>src/main/resources-filtered</directory>
+                <filtering>true</filtering>
+            </resource>
+        </resources>
+
         <plugins>
             <plugin>
                 <artifactId>maven-resources-plugin</artifactId>
@@ -282,6 +293,35 @@ under the License.
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <!-- Description: 
https://github.com/git-commit-id/git-commit-id-maven-plugin
+                    Used to show the git ref when starting the Flink 
Kubernetes operator. -->
+                <groupId>io.github.git-commit-id</groupId>
+                <artifactId>git-commit-id-maven-plugin</artifactId>
+                <version>${git-commit-id-maven-plugin.version}</version>
+                <executions>
+                    <execution>
+                        <id>get-the-git-infos</id>
+                        <phase>validate</phase>
+                        <goals>
+                            <goal>revision</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <skipPoms>false</skipPoms>
+                    <failOnNoGitDirectory>false</failOnNoGitDirectory>
+                    
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
+                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
+                    
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
+                    </generateGitPropertiesFilename>
+                    <includeOnlyProperties>
+                        
<includeOnlyProperty>^git.commit.id.abbrev$</includeOnlyProperty>
+                        
<includeOnlyProperty>^git.commit.time$</includeOnlyProperty>
+                    </includeOnlyProperties>
+                    <commitIdGenerationMode>full</commitIdGenerationMode>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
     <profiles>
diff --git 
a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/FlinkOperator.java
 
b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/FlinkOperator.java
index b2ca303..aab5d96 100644
--- 
a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/FlinkOperator.java
+++ 
b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/FlinkOperator.java
@@ -33,10 +33,10 @@ import 
org.apache.flink.kubernetes.operator.reconciler.Reconciler;
 import 
org.apache.flink.kubernetes.operator.reconciler.deployment.ReconcilerFactory;
 import 
org.apache.flink.kubernetes.operator.reconciler.sessionjob.FlinkSessionJobReconciler;
 import org.apache.flink.kubernetes.operator.service.FlinkService;
+import org.apache.flink.kubernetes.operator.utils.EnvUtils;
 import org.apache.flink.kubernetes.operator.utils.FlinkUtils;
 import org.apache.flink.kubernetes.operator.utils.ValidatorUtils;
 import org.apache.flink.kubernetes.operator.validation.FlinkResourceValidator;
-import org.apache.flink.runtime.util.EnvironmentInformation;
 
 import io.fabric8.kubernetes.client.DefaultKubernetesClient;
 import io.fabric8.kubernetes.client.KubernetesClient;
@@ -159,7 +159,7 @@ public class FlinkOperator {
     }
 
     public static void main(String... args) {
-        EnvironmentInformation.logEnvironmentInfo(LOG, "Flink Kubernetes 
Operator", args);
+        EnvUtils.logEnvironmentInfo(LOG, "Flink Kubernetes Operator", args);
         new FlinkOperator().run();
     }
 }
diff --git 
a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/EnvUtils.java
 
b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/EnvUtils.java
index 4fc9e12..a000582 100644
--- 
a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/EnvUtils.java
+++ 
b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/utils/EnvUtils.java
@@ -18,11 +18,32 @@
 
 package org.apache.flink.kubernetes.operator.utils;
 
+import org.apache.flink.configuration.GlobalConfiguration;
+
 import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.Properties;
+
+import static org.apache.flink.runtime.util.EnvironmentInformation.UNKNOWN;
+import static 
org.apache.flink.runtime.util.EnvironmentInformation.UNKNOWN_COMMIT_ID_ABBREV;
+import static 
org.apache.flink.runtime.util.EnvironmentInformation.getJvmStartupOptionsArray;
+import static 
org.apache.flink.runtime.util.EnvironmentInformation.getJvmVersion;
+import static 
org.apache.flink.runtime.util.EnvironmentInformation.getMaxJvmHeapMemory;
+import static org.apache.flink.runtime.util.EnvironmentInformation.getVersion;
 
 /** Util to get value from environments. */
 public class EnvUtils {
 
+    private static final Logger LOG = LoggerFactory.getLogger(EnvUtils.class);
+
     public static final String ENV_FLINK_OPERATOR_CONF_DIR = 
"FLINK_OPERATOR_CONF_DIR";
     public static final String ENV_FLINK_CONF_DIR = "FLINK_CONF_DIR";
     public static final String ENV_WEBHOOK_KEYSTORE_FILE = 
"WEBHOOK_KEYSTORE_FILE";
@@ -34,6 +55,13 @@ public class EnvUtils {
     public static final String ENV_OPERATOR_NAME = "OPERATOR_NAME";
     public static final String ENV_OPERATOR_NAMESPACE = "OPERATOR_NAMESPACE";
 
+    private static final String PROP_FILE = 
".flink-kubernetes-operator.version.properties";
+    private static final String FAIL_MESSAGE =
+            "The file "
+                    + PROP_FILE
+                    + " has not been generated correctly. You MUST run 'mvn 
generate-sources' in the flink-kubernetes-operator module.";
+    private static final String DEFAULT_TIME_STRING = 
"1970-01-01T00:00:00+0000";
+
     /**
      * Get the value provided by environments.
      *
@@ -68,4 +96,111 @@ public class EnvUtils {
         }
         return value;
     }
+
+    /**
+     * Logs information about the environment, like code revision, current 
user, Java version, and
+     * JVM parameters.
+     *
+     * @param log The logger to log the information to.
+     * @param componentName The component name to mention in the log.
+     * @param commandLineArgs The arguments accompanying the starting the 
component.
+     */
+    public static void logEnvironmentInfo(
+            Logger log, String componentName, String[] commandLineArgs) {
+        if (log.isInfoEnabled()) {
+            Properties properties = new Properties();
+            try (InputStream propFile =
+                    
EnvUtils.class.getClassLoader().getResourceAsStream(PROP_FILE)) {
+                if (propFile != null) {
+                    properties.load(propFile);
+                }
+            } catch (IOException e) {
+                LOG.info(
+                        "Cannot determine code revision: Unable to read 
version property file.: {}",
+                        e.getMessage());
+            }
+            String javaHome = System.getenv("JAVA_HOME");
+            String arch = System.getProperty("os.arch");
+            long maxHeapMegabytes = getMaxJvmHeapMemory() >>> 20;
+            log.info(
+                    
"--------------------------------------------------------------------------------");
+            log.info(
+                    " Starting "
+                            + componentName
+                            + " (Version: "
+                            + getProperty(properties, "project.version", 
UNKNOWN)
+                            + ", Flink Version: "
+                            + getVersion()
+                            + ", "
+                            + "Rev:"
+                            + getProperty(
+                                    properties, "git.commit.id.abbrev", 
UNKNOWN_COMMIT_ID_ABBREV)
+                            + ", "
+                            + "Date:"
+                            + getGitCommitTimeString(properties)
+                            + ")");
+            log.info(" OS current user: " + System.getProperty("user.name"));
+            log.info(" JVM: " + getJvmVersion());
+            log.info(" Arch: " + arch);
+            log.info(" Maximum heap size: " + maxHeapMegabytes + " MiBytes");
+            log.info(" JAVA_HOME: " + (javaHome == null ? "(not set)" : 
javaHome));
+            String[] options = getJvmStartupOptionsArray();
+            if (options.length == 0) {
+                log.info(" JVM Options: (none)");
+            } else {
+                log.info(" JVM Options:");
+                for (String s : options) {
+                    log.info("    " + s);
+                }
+            }
+            if (commandLineArgs == null || commandLineArgs.length == 0) {
+                log.info(" Program Arguments: (none)");
+            } else {
+                log.info(" Program Arguments:");
+                for (String s : commandLineArgs) {
+                    if (GlobalConfiguration.isSensitive(s)) {
+                        log.info(
+                                "    "
+                                        + GlobalConfiguration.HIDDEN_CONTENT
+                                        + " (sensitive information)");
+                    } else {
+                        log.info("    " + s);
+                    }
+                }
+            }
+            log.info(" Classpath: " + System.getProperty("java.class.path"));
+            log.info(
+                    
"--------------------------------------------------------------------------------");
+        }
+    }
+
+    /**
+     * @return The Instant of the last commit of this code as a String using 
the Europe/Berlin
+     *     timezone.
+     */
+    private static String getGitCommitTimeString(Properties properties) {
+        try {
+            return DateTimeFormatter.ISO_OFFSET_DATE_TIME
+                    .withZone(ZoneId.of("Europe/Berlin"))
+                    .format(
+                            
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")
+                                    .parse(
+                                            getProperty(
+                                                    properties,
+                                                    "git.commit.time",
+                                                    DEFAULT_TIME_STRING),
+                                            Instant::from));
+        } catch (DateTimeParseException e) {
+            LOG.error("{} : {}", FAIL_MESSAGE, e);
+            throw new IllegalStateException(FAIL_MESSAGE);
+        }
+    }
+
+    private static String getProperty(Properties properties, String key, 
String defaultValue) {
+        String value = properties.getProperty(key);
+        if (value == null || value.charAt(0) == '$') {
+            return defaultValue;
+        }
+        return value;
+    }
 }
diff --git 
a/flink-kubernetes-operator/src/main/resources-filtered/.flink-kubernetes-operator.version.properties
 
b/flink-kubernetes-operator/src/main/resources-filtered/.flink-kubernetes-operator.version.properties
new file mode 100644
index 0000000..52e64c3
--- /dev/null
+++ 
b/flink-kubernetes-operator/src/main/resources-filtered/.flink-kubernetes-operator.version.properties
@@ -0,0 +1,18 @@
+# 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.
+
+project.version=${project.version}
+git.commit.id.abbrev=${git.commit.id.abbrev}
+git.commit.time=${git.commit.time}
diff --git 
a/flink-kubernetes-webhook/src/main/java/org/apache/flink/kubernetes/operator/admission/FlinkOperatorWebhook.java
 
b/flink-kubernetes-webhook/src/main/java/org/apache/flink/kubernetes/operator/admission/FlinkOperatorWebhook.java
index cfcd991..48cb2af 100644
--- 
a/flink-kubernetes-webhook/src/main/java/org/apache/flink/kubernetes/operator/admission/FlinkOperatorWebhook.java
+++ 
b/flink-kubernetes-webhook/src/main/java/org/apache/flink/kubernetes/operator/admission/FlinkOperatorWebhook.java
@@ -19,7 +19,6 @@ package org.apache.flink.kubernetes.operator.admission;
 
 import org.apache.flink.kubernetes.operator.utils.EnvUtils;
 import org.apache.flink.kubernetes.operator.validation.DefaultValidator;
-import org.apache.flink.runtime.util.EnvironmentInformation;
 
 import org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap;
 import org.apache.flink.shaded.netty4.io.netty.channel.Channel;
@@ -55,7 +54,7 @@ public class FlinkOperatorWebhook {
     private static final int MAX_CONTEXT_LENGTH = 104_857_600;
 
     public static void main(String[] args) throws Exception {
-        EnvironmentInformation.logEnvironmentInfo(LOG, "Flink Kubernetes 
Webhook", args);
+        EnvUtils.logEnvironmentInfo(LOG, "Flink Kubernetes Webhook", args);
         AdmissionHandler endpoint =
                 new AdmissionHandler(new FlinkValidator(new 
DefaultValidator()));
         ChannelInitializer<SocketChannel> initializer = 
createChannelInitializer(endpoint);
diff --git a/pom.xml b/pom.xml
index f14d0a2..55c0808 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,7 @@ under the License.
         <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
         <maven-failsafe-plugin.version>3.0.0-M5</maven-failsafe-plugin.version>
         <maven-resources-plugin.version>3.2.0</maven-resources-plugin.version>
+        
<git-commit-id-maven-plugin.version>5.0.0</git-commit-id-maven-plugin.version>
 
         <operator.sdk.version>2.1.2</operator.sdk.version>
         <fabric8.version>5.12.1</fabric8.version>
@@ -256,6 +257,7 @@ under the License.
                             <exclude>**/dependency-reduced-pom.xml</exclude>
                             <!-- Administrative files in the main trunk. -->
                             <exclude>**/README.md</exclude>
+                            <exclude>.git/**</exclude>
                             <exclude>.github/**</exclude>
                             <!-- Build files -->
                             <exclude>**/*.iml</exclude>

Reply via email to