Merge pull request #124195 from bart0sh/PR139-use-downloaded-go-to-place-binaries
hacks: build main target without go in the PATH
This commit is contained in:
		@@ -635,6 +635,7 @@ kube::golang::place_bins() {
 | 
				
			|||||||
      ln -s "${KUBE_OUTPUT_BIN}/${platform}" "${THIS_PLATFORM_BIN}"
 | 
					      ln -s "${KUBE_OUTPUT_BIN}/${platform}" "${THIS_PLATFORM_BIN}"
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    V=3 kube::log::status "Placing binaries for ${platform} in ${KUBE_OUTPUT_BIN}/${platform}"
 | 
				
			||||||
    local full_binpath_src="${KUBE_GOPATH}/bin${platform_src}"
 | 
					    local full_binpath_src="${KUBE_GOPATH}/bin${platform_src}"
 | 
				
			||||||
    if [[ -d "${full_binpath_src}" ]]; then
 | 
					    if [[ -d "${full_binpath_src}" ]]; then
 | 
				
			||||||
      mkdir -p "${KUBE_OUTPUT_BIN}/${platform}"
 | 
					      mkdir -p "${KUBE_OUTPUT_BIN}/${platform}"
 | 
				
			||||||
@@ -795,7 +796,7 @@ kube::golang::build_binaries_for_platform() {
 | 
				
			|||||||
    fi
 | 
					    fi
 | 
				
			||||||
   done
 | 
					   done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  V=2 kube::log::info "Env for ${platform}: GOOS=${GOOS-} GOARCH=${GOARCH-} GOROOT=${GOROOT-} CGO_ENABLED=${CGO_ENABLED-} CC=${CC-}"
 | 
					  V=2 kube::log::info "Env for ${platform}: GOPATH=${GOPATH-} GOOS=${GOOS-} GOARCH=${GOARCH-} GOROOT=${GOROOT-} CGO_ENABLED=${CGO_ENABLED-} CC=${CC-}"
 | 
				
			||||||
  V=3 kube::log::info "Building binaries with GCFLAGS=${gogcflags} LDFLAGS=${goldflags}"
 | 
					  V=3 kube::log::info "Building binaries with GCFLAGS=${gogcflags} LDFLAGS=${goldflags}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  local -a build_args
 | 
					  local -a build_args
 | 
				
			||||||
@@ -873,106 +874,101 @@ kube::golang::get_physmem() {
 | 
				
			|||||||
#   KUBE_BUILD_PLATFORMS - Incoming variable of targets to build for.  If unset
 | 
					#   KUBE_BUILD_PLATFORMS - Incoming variable of targets to build for.  If unset
 | 
				
			||||||
#     then just the host architecture is built.
 | 
					#     then just the host architecture is built.
 | 
				
			||||||
kube::golang::build_binaries() {
 | 
					kube::golang::build_binaries() {
 | 
				
			||||||
  # Create a sub-shell so that we don't pollute the outer environment
 | 
					  V=2 kube::log::info "Go version: $(GOFLAGS='' go version)"
 | 
				
			||||||
  (
 | 
					 | 
				
			||||||
    # Check for `go` binary and set ${GOPATH}.
 | 
					 | 
				
			||||||
    kube::golang::setup_env
 | 
					 | 
				
			||||||
    V=2 kube::log::info "Go version: $(GOFLAGS='' go version)"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    local host_platform
 | 
					  local host_platform
 | 
				
			||||||
    host_platform=$(kube::golang::host_platform)
 | 
					  host_platform=$(kube::golang::host_platform)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # These are "local" but are visible to and relied on by functions this
 | 
					  # These are "local" but are visible to and relied on by functions this
 | 
				
			||||||
    # function calls.  They are effectively part of the calling API to
 | 
					  # function calls.  They are effectively part of the calling API to
 | 
				
			||||||
    # build_binaries_for_platform.
 | 
					  # build_binaries_for_platform.
 | 
				
			||||||
    local goflags goldflags gogcflags gotags
 | 
					  local goflags goldflags gogcflags gotags
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    goflags=()
 | 
					  goflags=()
 | 
				
			||||||
    gogcflags="${GOGCFLAGS:-}"
 | 
					  gogcflags="${GOGCFLAGS:-}"
 | 
				
			||||||
    goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}"
 | 
					  goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if [[ "${DBG:-}" == 1 ]]; then
 | 
					  if [[ "${DBG:-}" == 1 ]]; then
 | 
				
			||||||
        # Debugging - disable optimizations and inlining and trimPath
 | 
					      # Debugging - disable optimizations and inlining and trimPath
 | 
				
			||||||
        gogcflags="${gogcflags} all=-N -l"
 | 
					      gogcflags="${gogcflags} all=-N -l"
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					      # Not debugging - disable symbols and DWARF, trim embedded paths
 | 
				
			||||||
 | 
					      goldflags="${goldflags} -s -w"
 | 
				
			||||||
 | 
					      goflags+=("-trimpath")
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Extract tags if any specified in GOFLAGS
 | 
				
			||||||
 | 
					  gotags="selinux,notest,$(echo "${GOFLAGS:-}" | sed -ne 's|.*-tags=\([^-]*\).*|\1|p')"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  local -a targets=()
 | 
				
			||||||
 | 
					  local arg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  for arg; do
 | 
				
			||||||
 | 
					    if [[ "${arg}" == -* ]]; then
 | 
				
			||||||
 | 
					      # Assume arguments starting with a dash are flags to pass to go.
 | 
				
			||||||
 | 
					      goflags+=("${arg}")
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        # Not debugging - disable symbols and DWARF, trim embedded paths
 | 
					      targets+=("${arg}")
 | 
				
			||||||
        goldflags="${goldflags} -s -w"
 | 
					 | 
				
			||||||
        goflags+=("-trimpath")
 | 
					 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					  done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Extract tags if any specified in GOFLAGS
 | 
					  local -a platforms
 | 
				
			||||||
    gotags="selinux,notest,$(echo "${GOFLAGS:-}" | sed -ne 's|.*-tags=\([^-]*\).*|\1|p')"
 | 
					  IFS=" " read -ra platforms <<< "${KUBE_BUILD_PLATFORMS:-}"
 | 
				
			||||||
 | 
					  if [[ ${#platforms[@]} -eq 0 ]]; then
 | 
				
			||||||
 | 
					    platforms=("${host_platform}")
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    local -a targets=()
 | 
					  if [[ ${#targets[@]} -eq 0 ]]; then
 | 
				
			||||||
    local arg
 | 
					    targets=("${KUBE_ALL_TARGETS[@]}")
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					  kube::util::read-array targets < <(kube::golang::dedup "${targets[@]}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for arg; do
 | 
					  local -a binaries
 | 
				
			||||||
      if [[ "${arg}" == -* ]]; then
 | 
					  kube::util::read-array binaries < <(kube::golang::normalize_go_targets "${targets[@]}")
 | 
				
			||||||
        # Assume arguments starting with a dash are flags to pass to go.
 | 
					  kube::util::read-array binaries < <(kube::golang::dedup "${binaries[@]}")
 | 
				
			||||||
        goflags+=("${arg}")
 | 
					
 | 
				
			||||||
      else
 | 
					  local parallel=false
 | 
				
			||||||
        targets+=("${arg}")
 | 
					  if [[ ${#platforms[@]} -gt 1 ]]; then
 | 
				
			||||||
      fi
 | 
					    local gigs
 | 
				
			||||||
 | 
					    gigs=$(kube::golang::get_physmem)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then
 | 
				
			||||||
 | 
					      kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"
 | 
				
			||||||
 | 
					      parallel=true
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"
 | 
				
			||||||
 | 
					      parallel=false
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if [[ "${parallel}" == "true" ]]; then
 | 
				
			||||||
 | 
					    kube::log::status "Building go targets for {${platforms[*]}} in parallel (output will appear in a burst when complete):" "${targets[@]}"
 | 
				
			||||||
 | 
					    local platform
 | 
				
			||||||
 | 
					    for platform in "${platforms[@]}"; do (
 | 
				
			||||||
 | 
					        kube::golang::set_platform_envs "${platform}"
 | 
				
			||||||
 | 
					        kube::log::status "${platform}: build started"
 | 
				
			||||||
 | 
					        kube::golang::build_binaries_for_platform "${platform}"
 | 
				
			||||||
 | 
					        kube::log::status "${platform}: build finished"
 | 
				
			||||||
 | 
					      ) &> "/tmp//${platform//\//_}.build" &
 | 
				
			||||||
    done
 | 
					    done
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    local -a platforms
 | 
					    local fails=0
 | 
				
			||||||
    IFS=" " read -ra platforms <<< "${KUBE_BUILD_PLATFORMS:-}"
 | 
					    for job in $(jobs -p); do
 | 
				
			||||||
    if [[ ${#platforms[@]} -eq 0 ]]; then
 | 
					      wait "${job}" || (( fails+=1 ))
 | 
				
			||||||
      platforms=("${host_platform}")
 | 
					    done
 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if [[ ${#targets[@]} -eq 0 ]]; then
 | 
					    for platform in "${platforms[@]}"; do
 | 
				
			||||||
      targets=("${KUBE_ALL_TARGETS[@]}")
 | 
					      cat "/tmp//${platform//\//_}.build"
 | 
				
			||||||
    fi
 | 
					    done
 | 
				
			||||||
    kube::util::read-array targets < <(kube::golang::dedup "${targets[@]}")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    local -a binaries
 | 
					    return "${fails}"
 | 
				
			||||||
    kube::util::read-array binaries < <(kube::golang::normalize_go_targets "${targets[@]}")
 | 
					  else
 | 
				
			||||||
    kube::util::read-array binaries < <(kube::golang::dedup "${binaries[@]}")
 | 
					    for platform in "${platforms[@]}"; do
 | 
				
			||||||
 | 
					      kube::log::status "Building go targets for ${platform}"
 | 
				
			||||||
    local parallel=false
 | 
					      (
 | 
				
			||||||
    if [[ ${#platforms[@]} -gt 1 ]]; then
 | 
					        kube::golang::set_platform_envs "${platform}"
 | 
				
			||||||
      local gigs
 | 
					        kube::golang::build_binaries_for_platform "${platform}"
 | 
				
			||||||
      gigs=$(kube::golang::get_physmem)
 | 
					      )
 | 
				
			||||||
 | 
					    done
 | 
				
			||||||
      if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then
 | 
					  fi
 | 
				
			||||||
        kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"
 | 
					 | 
				
			||||||
        parallel=true
 | 
					 | 
				
			||||||
      else
 | 
					 | 
				
			||||||
        kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"
 | 
					 | 
				
			||||||
        parallel=false
 | 
					 | 
				
			||||||
      fi
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if [[ "${parallel}" == "true" ]]; then
 | 
					 | 
				
			||||||
      kube::log::status "Building go targets for {${platforms[*]}} in parallel (output will appear in a burst when complete):" "${targets[@]}"
 | 
					 | 
				
			||||||
      local platform
 | 
					 | 
				
			||||||
      for platform in "${platforms[@]}"; do (
 | 
					 | 
				
			||||||
          kube::golang::set_platform_envs "${platform}"
 | 
					 | 
				
			||||||
          kube::log::status "${platform}: build started"
 | 
					 | 
				
			||||||
          kube::golang::build_binaries_for_platform "${platform}"
 | 
					 | 
				
			||||||
          kube::log::status "${platform}: build finished"
 | 
					 | 
				
			||||||
        ) &> "/tmp//${platform//\//_}.build" &
 | 
					 | 
				
			||||||
      done
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      local fails=0
 | 
					 | 
				
			||||||
      for job in $(jobs -p); do
 | 
					 | 
				
			||||||
        wait "${job}" || (( fails+=1 ))
 | 
					 | 
				
			||||||
      done
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      for platform in "${platforms[@]}"; do
 | 
					 | 
				
			||||||
        cat "/tmp//${platform//\//_}.build"
 | 
					 | 
				
			||||||
      done
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      exit "${fails}"
 | 
					 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
      for platform in "${platforms[@]}"; do
 | 
					 | 
				
			||||||
        kube::log::status "Building go targets for ${platform}"
 | 
					 | 
				
			||||||
        (
 | 
					 | 
				
			||||||
          kube::golang::set_platform_envs "${platform}"
 | 
					 | 
				
			||||||
          kube::golang::build_binaries_for_platform "${platform}"
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
      done
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
  )
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,5 +24,6 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
 | 
				
			|||||||
KUBE_VERBOSE="${KUBE_VERBOSE:-1}"
 | 
					KUBE_VERBOSE="${KUBE_VERBOSE:-1}"
 | 
				
			||||||
source "${KUBE_ROOT}/hack/lib/init.sh"
 | 
					source "${KUBE_ROOT}/hack/lib/init.sh"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					kube::golang::setup_env
 | 
				
			||||||
kube::golang::build_binaries "$@"
 | 
					kube::golang::build_binaries "$@"
 | 
				
			||||||
kube::golang::place_bins
 | 
					kube::golang::place_bins
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user