open-consul/build-support/scripts/envoy-library-references.sh
Nitya Dhanushkodi 77f6b20db0
refactor: remove troubleshoot module dependency on consul top level module (#16162)
Ensure nothing in the troubleshoot go module depends on consul's top level module. This is so we can import troubleshoot into consul-k8s and not import all of consul.

* turns troubleshoot into a go module [authored by @curtbushko]
* gets the envoy protos into the troubleshoot module [authored by @curtbushko]
* adds a new go module `envoyextensions` which has xdscommon and extensioncommon folders that both the xds package and the troubleshoot package can import
* adds testing and linting for the new go modules
* moves the unit tests in `troubleshoot/validateupstream` that depend on proxycfg/xds into the xds package, with a comment describing why those tests cannot be in the troubleshoot package
* fixes all the imports everywhere as a result of these changes 

Co-authored-by: Curt Bushko <cbushko@gmail.com>
2023-02-06 09:14:35 -08:00

93 lines
2.3 KiB
Bash

#!/bin/bash
####
# envoy-library-references.sh
#
# This script ensures that all of the protobuf packages present in the
# github.com/envoyproxy/go-control-plane library are referenced in the consul
# codebase somewhere so that the ultimate binary doesn't eliminate those
# packages. When the packages are linked in they use proto.RegisterFile() to
# globally register the types in the protobuf machinery so that they are
# available for decoding.
#
# We primarily need this for the Escape Hatch feature where users can provide
# arbitrary xDS JSON for Consul to decode. If extension points use *any.Any and
# use an extention package that Consul itself doesn't use then it won't decode
# unless the package is linked into the binary.
#
####
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")" # build-support/scripts
cd ../.. # <ROOT>
if [[ ! -f GNUmakefile ]] || [[ ! -f go.mod ]]; then
echo "not in root consul checkout: ${PWD}" >&2
exit 1
fi
readonly LIBRARY_VERSION="$(grep github.com/envoyproxy/go-control-plane go.mod | awk '{print $2}')"
readonly OUTFILE=z_xds_packages.go
echo "Fetching envoyproxy/go-control-plane @ ${LIBRARY_VERSION}..."
rm -rf _envoy_tmp
mkdir _envoy_tmp
trap "rm -rf _envoy_tmp" EXIT
(
cd _envoy_tmp
git clone git@github.com:envoyproxy/go-control-plane
cd go-control-plane
git checkout -b consul-temp "${LIBRARY_VERSION}"
IFS=$'\n' candidates=($(find . -name *.pb.go -a -type f | sed 's@/[^/]*\.pb\.go$@@' | sort -u))
echo "Generating a fresh agent xds ${OUTFILE} file..."
cat <<-EOF > "${OUTFILE}"
// Code generated by envoy-library-references.sh. DO NOT EDIT.
package xds
import (
EOF
for cand in "${candidates[@]}"; do
import_name="${cand#./}"
echo " _ \"github.com/envoyproxy/go-control-plane/${import_name}\"" >> "${OUTFILE}"
done
echo ")" >> "${OUTFILE}"
goimports -w "${OUTFILE}"
mv -f "${OUTFILE}" ../../agent/xds
)
echo "Generating a fresh troubleshoot ${OUTFILE} file..."
cat <<-EOF > "${OUTFILE}"
// Code generated by envoy-library-references.sh. DO NOT EDIT.
package troubleshoot
import (
EOF
for cand in "${candidates[@]}"; do
import_name="${cand#./}"
echo " _ \"github.com/envoyproxy/go-control-plane/${import_name}\"" >> "${OUTFILE}"
done
echo ")" >> "${OUTFILE}"
goimports -w "${OUTFILE}"
mv -f "${OUTFILE}" ../../troubleshoot/connect
)
echo "tidying dependencies..."
make go-mod-tidy