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

ulyssesyou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new c357e3f  [KYUUBI #1396][K8S] Add docker image tools and replace 
Dockerfile
c357e3f is described below

commit c357e3f54f3c4c56f41b206d55d907a01ea2777d
Author: zwangsheng <[email protected]>
AuthorDate: Fri Nov 26 17:48:50 2021 +0800

    [KYUUBI #1396][K8S] Add docker image tools and replace Dockerfile
    
    <!--
    Thanks for sending a pull request!
    
    Here are some tips for you:
      1. If this is your first time, please read our contributor guidelines: 
https://kyuubi.readthedocs.io/en/latest/community/contributions.html
      2. If the PR is related to an issue in 
https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your 
PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
      3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][KYUUBI #XXXX] Your PR title ...'.
    -->
    
    ### _Why are the changes needed?_
    <!--
    Please clarify why the changes are needed. For instance,
      1. If you add a feature, you can talk about the use case of it.
      2. If you fix a bug, you can clarify why it is a bug.
    -->
    At the beginning, you just want to add a script to help the user build the 
image.In the process, the Dockerfile of the existing master branch was 
discovered.
    
    Although the multi-layer build method can help to build quickly and easily, 
it is only limited to source code.
    
    In other words, when the user uses the binary package officially released, 
the Dockerfile is deactivated.
    
    So, In this PR, we move it(the multi-layer Dockerfile) to dev. And use the 
previous Dockerfile.
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including 
negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run 
test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests)
 locally before make a pull request
    
    Closes #1435 from zwangsheng/add-docker-image-tools.
    
    Closes #1396
    
    83d593af [zwangsheng] fix note
    30772c5e [zwangsheng] fix
    499b3969 [zwangsheng] fix
    e64ffb7d [zwangsheng] fix
    27b54113 [zwangsheng] fix
    fcdfc55e [zwangsheng] dix
    3aea0a0b [zwangsheng] fix SPARK_HOME print
    67a97083 [zwangsheng] remove help code
    74efb245 [zwangsheng] move and modify workflow
    f7649e86 [zwangsheng] move and modify workflow
    4672a09e [zwangsheng] move and modify workflow
    7447ce28 [zwangsheng] move
    567478d3 [zwangsheng] move
    a224ac0b [zwangsheng] move
    cadeef29 [zwangsheng] workflow use dev/Dockerfile
    354bc14c [zwangsheng] fix Dockerfile
    83561d8b [zwangsheng] build spark into docker image
    7d4323c3 [zwangsheng] add kyuubi verison help build tag
    4748d09b [zwangsheng] use offical image
    4f107d8a [zwangsheng] use offical iamhge
    f8c8ba9d [zwangsheng] for test
    cecd0485 [zwangsheng] modify
    09917320 [zwangsheng] add docker-image
    
    Authored-by: zwangsheng <[email protected]>
    Signed-off-by: ulysses-you <[email protected]>
---
 .github/workflows/docker-image.yml |   2 +-
 .github/workflows/master.yml       |   2 +-
 bin/docker-image-tool.sh           | 259 +++++++++++++++++++++++++++++++++++++
 bin/load-kyuubi-env.sh             |   6 +
 {docker => build}/Dockerfile       |   2 +-
 docker/Dockerfile                  |  55 +++-----
 6 files changed, 284 insertions(+), 42 deletions(-)

diff --git a/.github/workflows/docker-image.yml 
b/.github/workflows/docker-image.yml
index 85c585f..51e939d 100644
--- a/.github/workflows/docker-image.yml
+++ b/.github/workflows/docker-image.yml
@@ -22,7 +22,7 @@ jobs:
           username: ${{ secrets.DOCKERHUB_USER }}
           password: ${{ secrets.DOCKERHUB_TOKEN }}
       - name: Build Kyuubi Docker Image
-        run: docker build --tag apache/kyuubi:master-snapshot --file 
docker/Dockerfile .
+        run: docker build --tag apache/kyuubi:master-snapshot --file 
build/Dockerfile .
       - name: Docker image
         run: docker images
       - name: Push Docker image
diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml
index 86d1395..0b271ba 100644
--- a/.github/workflows/master.yml
+++ b/.github/workflows/master.yml
@@ -168,7 +168,7 @@ jobs:
           # passthrough CI into build container
           build-args: CI=${CI}
           context: .
-          file: docker/Dockerfile
+          file: build/Dockerfile
           load: true
           tags: apache/kyuubi:latest
       # from 
https://github.com/marketplace/actions/setup-minikube-kubernetes-cluster
diff --git a/bin/docker-image-tool.sh b/bin/docker-image-tool.sh
new file mode 100755
index 0000000..8359c93
--- /dev/null
+++ b/bin/docker-image-tool.sh
@@ -0,0 +1,259 @@
+#!/usr/bin/env bash
+
+# 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.
+#
+
+# This script builds and pushes docker images when run from a release of Kyuubi
+# with Kubernetes support.
+
+function error {
+  echo "$@" 1>&2
+  exit 1
+}
+
+if [ -z "${KYUUBI_HOME}" ]; then
+  KYUUBI_HOME="$(cd "`dirname "$0"`"/..; pwd)"
+fi
+
+CTX_DIR="$KYUUBI_HOME/target/tmp/docker"
+
+function is_dev_build {
+  [ ! -f "$KYUUBI_HOME/RELEASE" ]
+}
+
+function cleanup_ctx_dir {
+  if is_dev_build; then
+    rm -rf "$CTX_DIR"
+  fi
+}
+trap cleanup_ctx_dir EXIT
+
+function image_ref {
+  local image="$1"
+  local add_repo="${2:-1}"
+  if [ $add_repo = 1 ] && [ -n "$REPO" ]; then
+    image="$REPO/$image"
+  fi
+  if [ -n "$TAG" ]; then
+    image="$image:$TAG"
+  fi
+  echo "$image:$KYUUBI_VERSION"
+}
+
+function docker_push {
+  local image_name="$1"
+  if [ ! -z $(docker images -q "$(image_ref ${image_name})") ]; then
+    docker push "$(image_ref ${image_name})"
+    if [ $? -ne 0 ]; then
+      error "Failed to push $image_name Docker image."
+    fi
+  else
+    echo "$(image_ref ${image_name}) image not found. Skipping push for this 
image."
+  fi
+}
+
+function resolve_file {
+  local FILE=$1
+  if [ -n "$FILE" ]; then
+    local DIR=$(dirname $FILE)
+    DIR=$(cd $DIR && pwd)
+    FILE="${DIR}/$(basename $FILE)"
+  fi
+  echo $FILE
+}
+
+# Create a smaller build context for docker in dev builds to make the build 
faster. Docker
+# uploads all of the current directory to the daemon, and it can get pretty 
big with dev
+# builds that contain test log files and other artifacts.
+#
+# Note: docker does not support symlinks in the build context.
+function create_dev_build_context {(
+  set -e
+  local BASE_CTX="$CTX_DIR/base"
+  mkdir -r "$BASE_CTX/docker"
+  cp -r "docker/" "$BASE_CTX/docker"
+
+  cp -r "kyuubi-assembly/target/scala-${KYUUBI_SCALA_VERSION}/jars" 
"$BASE_CTX/jars"
+
+  mkdir -p "$BASE_CTX/externals/engines/spark"
+  cp 
"$KYUUBI_HOME/externals/kyuubi-spark-sql-engine/target/kyuubi-spark-sql-engine_${KYUUBI_SCALA_VERSION}-${KYUUBI_VERSION}.jar"
 "$BASE_CTX/externals/engines/spark"
+
+  for other in bin conf; do
+    cp -r "$other" "$BASE_CTX/$other"
+  done
+)}
+
+function img_ctx_dir {
+  if is_dev_build; then
+    echo "$CTX_DIR/$1"
+  else
+    echo "$KYUUBI_HOME"
+  fi
+}
+
+function build {
+  local BUILD_ARGS
+  local KYUUBI_ROOT="$KYUUBI_HOME"
+
+  if is_dev_build; then
+    create_dev_build_context || error "Failed to create docker build context."
+    KYUUBI_ROOT="$CTX_DIR/base"
+  fi
+
+  # cp spark for kyuubi as submit client
+  # if user set -s(spark-provider), use if
+  # else use builtin spark
+  if [[ ! -d "$KYUUBI_ROOT/spark-binary" ]]; then
+    mkdir "$KYUUBI_ROOT/spark-binary"
+  fi
+  cp -r "$SPARK_HOME/" "$KYUUBI_ROOT/spark-binary/"
+
+  # Verify that the Docker image content directory is present
+  if [ ! -d "$KYUUBI_ROOT/docker" ]; then
+    error "Cannot find docker image. This script must be run from a runnable 
distribution of Apache Kyuubi."
+  fi
+
+  # Verify that Kyuubi has actually been built/is a runnable distribution
+  # i.e. the Kyuubi JARs that the Docker files will place into the image are 
present
+  local TOTAL_JARS=$(ls $KYUUBI_ROOT/jars/kyuubi-* | wc -l)
+  TOTAL_JARS=$(( $TOTAL_JARS ))
+  if [ "${TOTAL_JARS}" -eq 0 ]; then
+    error "Cannot find Kyuubi JARs. This script assumes that Apache Kyuubi has 
first been built locally or this is a runnable distribution."
+  fi
+
+  local BUILD_ARGS=(${BUILD_PARAMS})
+
+  # If a custom Kyuubi_UID was set add it to build arguments
+  if [ -n "$KYUUBI_UID" ]; then
+    BUILD_ARGS+=(--build-arg kyuubi_uid=$KYUUBI_UID)
+  fi
+
+  local BINDING_BUILD_ARGS=(
+    ${BUILD_ARGS[@]}
+    --build-arg
+    base_img=$(image_ref kyuubi)
+  )
+
+  local BASEDOCKERFILE=${BASEDOCKERFILE:-"docker/Dockerfile"}
+  local ARCHS=${ARCHS:-"--platform linux/amd64,linux/arm64"}
+
+  (cd $(img_ctx_dir base) && docker build $NOCACHEARG "${BUILD_ARGS[@]}" \
+    -t $(image_ref kyuubi) \
+    -f "$BASEDOCKERFILE" .)
+  if [ $? -ne 0 ]; then
+    error "Failed to build Kyuubi JVM Docker image, please refer to Docker 
build output for details."
+  fi
+  if [ "${CROSS_BUILD}" != "false" ]; then
+  (cd $(img_ctx_dir base) && docker buildx build $ARCHS $NOCACHEARG 
"${BUILD_ARGS[@]}" --push \
+    -t $(image_ref kyuubi) \
+    -f "$BASEDOCKERFILE" .)
+  fi
+}
+
+function push {
+  docker_push "kyuubi"
+}
+
+function usage {
+  cat <<EOF
+Usage: $0 [options] [command]
+Builds or pushes the built-in Kyuubi Docker image.
+
+Commands:
+  build       Build image. Requires a repository address to be provided if the 
image will be
+              pushed to a different registry.
+  push        Push a pre-built image to a registry. Requires a repository 
address to be provided.
+
+Options:
+  -f                    Dockerfile to build for JVM based Jobs. By default 
builds the Dockerfile shipped with Kyuubi.
+  -r                    Repository address.
+  -t                    Tag to apply to the built image, or to identify the 
image to be pushed.
+  -n                    Build docker image with --no-cache
+  -u                    UID to use in the USER directive to set the user the 
main Kyuubi process runs as inside the
+                        resulting container
+  -X                    Use docker buildx to cross build. Automatically pushes.
+                        See 
https://docs.docker.com/buildx/working-with-buildx/ for steps to setup buildx.
+  -b                    Build arg to build or push the image. For multiple 
build args, this option needs to
+                        be used separately for each build arg.
+  -s                    Put the specified Spark into the Kyuubi image to be 
used as the internal SPARK_HOME
+                        of the container.
+
+Examples:
+
+  - Build and push image with tag "v1.4.0" to docker.io/myrepo
+    $0 -r docker.io/myrepo -t v1.4.0 build
+    $0 -r docker.io/myrepo -t v1.4.0 push
+
+  - Build and push with tag "v3.0.0" and Spark-3.1.2 as base image to 
docker.io/myrepo
+    $0 -r docker.io/myrepo -t v3.0.0 -b BASE_IMAGE=repo/spark:3.1.2 build
+    $0 -r docker.io/myrepo -t v3.0.0 push
+
+  - Build and push for multiple archs to docker.io/myrepo
+    $0 -r docker.io/myrepo -t v3.0.0 -X build
+
+    # Note: buildx, which does cross building, needs to do the push during 
build
+    # So there is no separate push step with -X
+
+  - Build with Spark placed "/path/spark"
+    $0 -s /path/spark build
+
+EOF
+}
+
+if [[ "$@" = *--help ]] || [[ "$@" = *-h ]]; then
+  usage
+  exit 0
+fi
+
+REPO=
+TAG=
+BASEDOCKERFILE=
+NOCACHEARG=
+BUILD_PARAMS=
+KYUUBI_UID=
+CROSS_BUILD="false"
+while getopts f:r:t:Xnb:u:s: option
+do
+ case "${option}"
+ in
+ f) BASEDOCKERFILE=$(resolve_file ${OPTARG});;
+ r) REPO=${OPTARG};;
+ t) TAG=${OPTARG};;
+ n) NOCACHEARG="--no-cache";;
+ b) BUILD_PARAMS=${BUILD_PARAMS}" --build-arg "${OPTARG};;
+ X) CROSS_BUILD=1;;
+ u) KYUUBI_UID=${OPTARG};;
+ s) SPARK_HOME=${OPTARG};;
+ esac
+done
+
+. "${KYUUBI_HOME}/bin/load-kyuubi-env.sh"
+case "${@: -1}" in
+  build)
+    build
+    ;;
+  push)
+    if [ -z "$REPO" ]; then
+      usage
+      exit 1
+    fi
+    push
+    ;;
+  *)
+    usage
+    exit 1
+    ;;
+esac
diff --git a/bin/load-kyuubi-env.sh b/bin/load-kyuubi-env.sh
index 872ca46..b05aca5 100755
--- a/bin/load-kyuubi-env.sh
+++ b/bin/load-kyuubi-env.sh
@@ -71,11 +71,16 @@ fi
 export KYUUBI_SCALA_VERSION="${KYUUBI_SCALA_VERSION:-"2.12"}"
 
 if [[ -f ${KYUUBI_HOME}/RELEASE ]]; then
+  KYUUBI_VERSION="$(grep "Kyuubi " "$KYUUBI_HOME/RELEASE" | awk -F ' ' '{print 
$2}')"
   SPARK_VERSION_BUILD="$(grep "Spark " "$KYUUBI_HOME/RELEASE" | awk -F ' ' 
'{print $2}')"
   HADOOP_VERSION_BUILD="$(grep "Hadoop " "$KYUUBI_HOME/RELEASE" | awk -F ' ' 
'{print $2}')"
   
SPARK_BUILTIN="${KYUUBI_HOME}/externals/spark-$SPARK_VERSION_BUILD-bin-hadoop${HADOOP_VERSION_BUILD:0:3}"
 else
   MVN="${MVN:-"${KYUUBI_HOME}/build/mvn"}"
+  KYUUBI_VERSION=$("$MVN" help:evaluate -Dexpression=project.version 
2>/dev/null\
+    | grep -v "INFO"\
+    | grep -v "WARNING"\
+    | tail -n 1)
   SPARK_VERSION_BUILD=$("$MVN" help:evaluate -Dexpression=spark.version 
2>/dev/null\
     | grep -v "INFO"\
     | grep -v "WARNING"\
@@ -94,6 +99,7 @@ if [ $silent -eq 0 ]; then
   echo "JAVA_HOME: ${JAVA_HOME}"
 
   echo "KYUUBI_HOME: ${KYUUBI_HOME}"
+  echo "KYUUBI_VERSION: ${KYUUBI_VERSION}"
   echo "KYUUBI_CONF_DIR: ${KYUUBI_CONF_DIR}"
   echo "KYUUBI_LOG_DIR: ${KYUUBI_LOG_DIR}"
   echo "KYUUBI_PID_DIR: ${KYUUBI_PID_DIR}"
diff --git a/docker/Dockerfile b/build/Dockerfile
similarity index 98%
copy from docker/Dockerfile
copy to build/Dockerfile
index 6394dcd..49247e0 100644
--- a/docker/Dockerfile
+++ b/build/Dockerfile
@@ -19,7 +19,7 @@
 #   Run the docker command below
 #      docker build \
 #        --build-arg MVN_ARG="-Pspark-3.1,spark-hadoop-3.2" \
-#        --file docker/Dockerfile \
+#        --file build/Dockerfile \
 #        --tag apache/kyuubi:tagname \
 #        .
 #   Options:
diff --git a/docker/Dockerfile b/docker/Dockerfile
index 6394dcd..2469f20 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -16,65 +16,42 @@
 #
 
 # Usage:
-#   Run the docker command below
-#      docker build \
-#        --build-arg MVN_ARG="-Pspark-3.1,spark-hadoop-3.2" \
-#        --file docker/Dockerfile \
-#        --tag apache/kyuubi:tagname \
-#        .
+#   1. use ./build/dist to make binary distributions of Kyuubi or download a 
release
+#   2. Untar it and run the docker command below
+#      docker build -f docker/Dockerfile -t repository/kyuubi:tagname .
 #   Options:
-#     -f, --file  this docker file
-#     -t, --tag   the target repo and tag name
-#     more options can be found with -h, --help
+#     -f this docker file
+#     -t the target repo and tag name
+#     more options can be found with -h
 
-# Declare the BASE_IMAGE argument in the first line, for more detail
-# see: https://github.com/moby/moby/issues/38379
-ARG BASE_IMAGE=openjdk:8-jdk
-
-FROM maven:3.6-jdk-8 as builder
-
-ARG MVN_ARG
-
-# Pass the environment variable `CI` into container, for internal use only.
-#
-# Continuous integration(aka. CI) services like GitHub Actions, Travis always 
provide
-# an environment variable `CI` in runners, and we detect this variable to run 
some
-# specific actions, e.g. run `mvn` in batch mode to suppress noisy logs.
-ARG CI
-ENV CI ${CI}
-
-ADD . /workspace/kyuubi
-WORKDIR /workspace/kyuubi
-
-RUN ./build/dist ${MVN_ARG} && \
-    mv /workspace/kyuubi/dist /opt/kyuubi && \
-    # Removing stuff saves time because docker creates a temporary layer
-    rm -rf ~/.m2 && \
-    rm -rf /workspace/kyuubi
-
-FROM ${BASE_IMAGE}
+ARG BASE_IMAGE=8-jre-slim
+FROM openjdk:${BASE_IMAGE}
 
 ARG kyuubi_uid=10009
 
 USER root
 
 ENV KYUUBI_HOME /opt/kyuubi
+ENV SPARK_HOME /opt/spark
 ENV KYUUBI_LOG_DIR ${KYUUBI_HOME}/logs
 ENV KYUUBI_PID_DIR ${KYUUBI_HOME}/pid
 ENV KYUUBI_WORK_DIR_ROOT ${KYUUBI_HOME}/work
 
-COPY --from=builder /opt/kyuubi ${KYUUBI_HOME}
-
 RUN set -ex && \
+    sed -i 's/http:\/\/deb.\(.*\)/https:\/\/deb.\1/g' /etc/apt/sources.list && 
\
     apt-get update && \
-    DEBIAN_FRONTEND=noninteractive \
     apt install -y bash tini libc6 libpam-modules krb5-user libnss3 procps && \
     useradd -u ${kyuubi_uid} -g root kyuubi && \
-    mkdir -p ${KYUUBI_HOME} ${KYUUBI_LOG_DIR} ${KYUUBI_PID_DIR} 
${KYUUBI_WORK_DIR_ROOT} && \
+    mkdir -p ${KYUUBI_HOME} ${KYUUBI_LOG_DIR} ${KYUUBI_PID_DIR} 
${KYUUBI_WORK_DIR_ROOT} ${SPARK_HOME} && \
     chmod ug+rw -R ${KYUUBI_HOME} && \
     chmod a+rwx -R ${KYUUBI_WORK_DIR_ROOT} && \
     rm -rf /var/cache/apt/*
 
+COPY spark-binary ${SPARK_HOME}
+COPY bin ${KYUUBI_HOME}/bin
+COPY jars ${KYUUBI_HOME}/jars
+COPY externals/engines/spark ${KYUUBI_HOME}/externals/engines/spark
+
 WORKDIR ${KYUUBI_HOME}
 
 CMD [ "./bin/kyuubi", "run" ]

Reply via email to