github.com/kubewharf/katalyst-core@v0.5.3/build/lib/build.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2022 The Katalyst Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  build_targets() {
    18      local goflags goldflags gcflags
    19      goldflags="${GOLDFLAGS:-}"
    20      gcflags="${GOGCFLAGS:-}"
    21      goflags=${GOFLAGS:-}
    22  
    23      local arg
    24      local archs=()
    25  
    26      for arg; do
    27          if [[ "$arg" == -* ]]; then
    28              # args starting with a dash are flags for go.
    29              goflags+=("$arg")
    30          else
    31              targets+=("$arg")
    32          fi
    33      done
    34  
    35      # by default build for linux/amd64
    36      # does not support multi-arch for now
    37      if [[ -z ${TARGET_ARCHS} ]]; then
    38          archs=(amd64)
    39      else
    40          IFS=',' read -r -a archs <<< "${TARGET_ARCHS}"
    41      fi
    42  
    43      # target_bin_dir contains the GOOS and GOARCH
    44      # eg: ./output/bin/linux/amd64/
    45      for arch in ${archs[@]}; do
    46          export GOARCH=${arch}
    47          export GOOS=linux
    48          local target_bin_dir=$BIN_DIR/${GOOS}/${GOARCH}
    49  
    50          rm -rf $target_bin_dir
    51          mkdir -p $target_bin_dir
    52  
    53          if [[ ${#targets[*]} == 0 ]]; then
    54              targets=(katalyst-agent katalyst-controller katalyst-metric katalyst-scheduler katalyst-webhook)
    55          fi
    56  
    57          for target in "${targets[@]}"; do
    58              echo "Building $target for linux/${arch}"
    59              go build -o $target_bin_dir/$target \
    60                  -ldflags "${goldflags:-}" \
    61                  -gcflags "${gcflags:-}" \
    62                  $goflags $ROOT_DIR/cmd/${target}
    63          done
    64      done
    65  
    66      if [[ "${BUILD_IMAGES}" =~ [yY] ]]; then
    67          local platform_list=("${archs[@]/#/linux/}")
    68          local build_platforms=$(IFS=,; echo "${platform_list[*]}")
    69          local builder=$(docker buildx create --use)
    70          for target in "${targets[@]}"; do
    71              echo "Building image for $target"
    72              docker buildx build --no-cache --load --platform ${build_platforms} --build-arg BINARY=${target} -f ${DOCKERFILE} -t ${REGISTRY}/${REGISTRY_NAMESPACE}/${target}:${IMAGE_TAG} ${ROOT_DIR}
    73          done
    74          docker buildx rm ${builder}
    75      fi
    76  }