treewide: replace stampsrcs with embed
A rules_go maintainer suggested using embed instead of stampsrcs:
https://github.com/bazel-contrib/rules_go/issues/3507
For Kubernetes, this means we need to patch the version libraries.
Instead of creating a separate file for each variable, I put them all in
one file, which is parsed in an init function. This init function needs
to run before all other init functions, which access the variables.
Another benefit of this change is that versions are stamped in all
binaries which include Kubernetes client libraries, not just hyperkube.
Change-Id: Ib1157d3686fc35e0c4191d2fc8e165862a1973c7
Reviewed-on: https://review.monogon.dev/c/monogon/+/4208
Tested-by: Jenkins CI
Reviewed-by: Tim Windelschmidt <tim@monogon.tech>
diff --git a/third_party/com_k8s_io_client_go/version-stamp.patch b/third_party/com_k8s_io_client_go/version-stamp.patch
new file mode 100644
index 0000000..d1dc52d
--- /dev/null
+++ b/third_party/com_k8s_io_client_go/version-stamp.patch
@@ -0,0 +1,56 @@
+diff --git a/pkg/version/BUILD.bazel b/pkg/version/BUILD.bazel
+index a0ab203..1514297 100644
+--- a/pkg/version/BUILD.bazel
++++ b/pkg/version/BUILD.bazel
+@@ -7,6 +7,7 @@ go_library(
+ "doc.go",
+ "version.go",
+ ],
++ embedsrcs = ["@@//build:kubernetes_stamp"],
+ importpath = "k8s.io/client-go/pkg/version",
+ visibility = ["//visibility:public"],
+ deps = ["@io_k8s_apimachinery//pkg/version"],
+diff --git a/pkg/version/base.go b/pkg/version/base.go
+index 676d51d..c76789a 100644
+--- a/pkg/version/base.go
++++ b/pkg/version/base.go
+@@ -16,6 +16,8 @@ limitations under the License.
+
+ package version
+
++import _ "embed"
++
+ // Base version information.
+ //
+ // This is the fallback data used when version information from git is not
+@@ -39,8 +41,10 @@ var (
+ // them irrelevant. (Next we'll take it out, which may muck with
+ // scripts consuming the kubectl version output - but most of
+ // these should be looking at gitVersion already anyways.)
+- gitMajor string = "" // major version, always numeric
+- gitMinor string = "" // minor version, numeric possibly followed by "+"
++ //go:embed build/kubernetes_gitMajor.txt
++ gitMajor string // major version, always numeric
++ //go:embed build/kubernetes_gitMinor.txt
++ gitMinor string // minor version, numeric possibly followed by "+"
+
+ // semantic version, derived by build scripts (see
+ // https://github.com/kubernetes/sig-release/blob/master/release-engineering/versioning.md#kubernetes-release-versioning
+@@ -56,9 +60,13 @@ var (
+ // NOTE: The $Format strings are replaced during 'git archive' thanks to the
+ // companion .gitattributes file containing 'export-subst' in this same
+ // directory. See also https://git-scm.com/docs/gitattributes
+- gitVersion string = "v0.0.0-master+$Format:%H$"
+- gitCommit string = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
+- gitTreeState string = "" // state of git tree, either "clean" or "dirty"
++ //go:embed build/kubernetes_gitVersion.txt
++ gitVersion string
++ //go:embed build/kubernetes_gitCommit.txt
++ gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
++ //go:embed build/kubernetes_gitTreeState.txt
++ gitTreeState string // state of git tree, either "clean" or "dirty"
+
+- buildDate string = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
++ //go:embed build/kubernetes_buildDate.txt
++ buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
+ )