Skip to content

Commit

Permalink
Add runtime checking of golang version
Browse files Browse the repository at this point in the history
Forces other groups packaging k3s to intentionally choose to build k3s with an unvalidated golang version

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
brandond committed Jan 5, 2024
1 parent 5fe074b commit b297996
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.test
Expand Up @@ -14,7 +14,7 @@ ENTRYPOINT ["/bin/test-mods"]

FROM test-base as test-k3s

RUN apk -U --no-cache add git gcc musl-dev docker curl coreutils python3 openssl py3-pip procps findutils
RUN apk -U --no-cache add git gcc musl-dev docker curl coreutils python3 openssl py3-pip procps findutils yq

RUN python3 -m pip install awscli

Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/agent/agent.go
Expand Up @@ -20,6 +20,9 @@ import (
)

func Run(ctx *cli.Context) error {
// Validate build env
cmds.MustValidateGolang()

// hide process arguments from ps output, since they may contain
// database credentials or other secrets.
gspt.SetProcTitle(os.Args[0] + " agent")
Expand Down
27 changes: 27 additions & 0 deletions pkg/cli/cmds/golang.go
@@ -0,0 +1,27 @@
package cmds

import (
"fmt"
"runtime"
"strings"

"github.com/k3s-io/k3s/pkg/version"
"github.com/sirupsen/logrus"
)

func ValidateGolang() error {
k8sVersion, _, _ := strings.Cut(version.Version, "+")
if version.UpstreamGolang == "" {
return fmt.Errorf("kubernetes golang build version not set - see 'golang: upstream version' in https://github.com/kubernetes/kubernetes/blob/%s/build/dependencies.yaml", k8sVersion)
}
if v, _, _ := strings.Cut(runtime.Version(), " "); version.UpstreamGolang != v {
return fmt.Errorf("incorrect golang build version - kubernetes %s should be built with %s, runtime version is %s", k8sVersion, version.UpstreamGolang, v)
}
return nil
}

func MustValidateGolang() {
if err := ValidateGolang(); err != nil {
logrus.Fatalf("Failed to validate golang version: %v", err)
}
}
2 changes: 2 additions & 0 deletions pkg/cli/server/server.go
Expand Up @@ -49,6 +49,8 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
var (
err error
)
// Validate build env
cmds.MustValidateGolang()

// hide process arguments from ps output, since they may contain
// database credentials or other secrets.
Expand Down
2 changes: 2 additions & 0 deletions pkg/version/version.go
Expand Up @@ -7,4 +7,6 @@ var (
ProgramUpper = strings.ToUpper(Program)
Version = "dev"
GitCommit = "HEAD"

UpstreamGolang = ""
)
1 change: 1 addition & 0 deletions scripts/build
Expand Up @@ -22,6 +22,7 @@ buildDate=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
VERSIONFLAGS="
-X ${PKG}/pkg/version.Version=${VERSION}
-X ${PKG}/pkg/version.GitCommit=${COMMIT:0:8}
-X ${PKG}/pkg/version.UpstreamGolang=${VERSION_GOLANG}
-X ${PKG_K8S_CLIENT}/version.gitVersion=${VERSION}
-X ${PKG_K8S_CLIENT}/version.gitCommit=${COMMIT}
Expand Down
6 changes: 2 additions & 4 deletions scripts/validate
Expand Up @@ -29,10 +29,8 @@ if [ -n "$DIRTY" ]; then
fi

echo Running: go version
DEPENDENCIES_URL="https://raw.githubusercontent.com/kubernetes/kubernetes/${VERSION_K8S}/build/dependencies.yaml"
GOLANG_VERSION=$(curl -sL "${DEPENDENCIES_URL}" | yq e '.dependencies[] | select(.name == "golang: upstream version").version' -)
if ! go version | grep -s "go version go${GOLANG_VERSION} "; then
echo "Unexpected $(go version) - Kubernetes ${VERSION_K8S} should be built with go version go${GOLANG_VERSION}"
if ! go version | grep -s "go version ${VERSION_GOLANG} "; then
echo "Unexpected $(go version) - Kubernetes ${VERSION_K8S} should be built with go version ${VERSION_GOLANG}"
exit 1
fi

Expand Down
5 changes: 4 additions & 1 deletion scripts/version.sh
Expand Up @@ -77,6 +77,9 @@ fi

VERSION_ROOT="v0.12.2"

DEPENDENCIES_URL="https://raw.githubusercontent.com/kubernetes/kubernetes/${VERSION_K8S}/build/dependencies.yaml"
VERSION_GOLANG="go"$(curl -sL "${DEPENDENCIES_URL}" | yq e '.dependencies[] | select(.name == "golang: upstream version").version' -)

if [[ -n "$GIT_TAG" ]]; then
if [[ ! "$GIT_TAG" =~ ^"$VERSION_K8S"[+-] ]]; then
echo "Tagged version '$GIT_TAG' does not match expected version '$VERSION_K8S[+-]*'" >&2
Expand All @@ -91,4 +94,4 @@ VERSION_TAG="$(sed -e 's/+/-/g' <<< "$VERSION")"
BINARY_POSTFIX=
if [ ${OS} = windows ]; then
BINARY_POSTFIX=.exe
fi
fi

0 comments on commit b297996

Please sign in to comment.