treewide: update to UwUbernetes (Kubernetes 1.30)

Co-authored-by: Serge Bazanski <serge@monogon.tech>
Co-authored-by: Lorenz Brun <lorenz@monogon.tech>
Change-Id: Id923f503938314ef8fb4243f36604752edbb4605
Reviewed-on: https://review.monogon.dev/c/monogon/+/3047
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/build/gotoolwrap/main.go b/metropolis/build/gotoolwrap/main.go
index d2645ce..343b7ff 100644
--- a/metropolis/build/gotoolwrap/main.go
+++ b/metropolis/build/gotoolwrap/main.go
@@ -27,6 +27,13 @@
 //  				     go_path target.
 //  - GOTOOLWRAP_GOROOT: A Go SDK's GOROOT, eg. one from rules_go's GoSDK
 //                       provider.
+//  - GOTOOLWRAP_COPYOUT: Pairs of source:destination elements separated by ;.
+//                        Files given in this list will be copied from source
+//                        (relative to the GOPATH sources) to the destination
+//                        (absolute path). This is primarily useful when dealing
+//                        with code generators which insist on putting their
+//                        generated files within the GOPATH, and allows copying
+//                        of said files to a declared output file.
 //
 // gotoolwrap will set PATH to contain GOROOT/bin, and set GOPATH and GOROOT as
 // resolved, absolute paths. Absolute paths are expected by tools like 'gofmt'.
@@ -121,6 +128,9 @@
 		if strings.HasPrefix(v, "GOTOOLWRAP_GOPATH=") {
 			continue
 		}
+		if strings.HasPrefix(v, "GOTOOLWRAP_COPYOUT=") {
+			continue
+		}
 		if strings.HasPrefix(v, "PATH=") {
 			continue
 		}
@@ -136,11 +146,11 @@
 	defer os.RemoveAll(tempDir)
 
 	cmd.Env = append(cmd.Env,
-		"GOROOT=" + gorootAbs,
-		"GOPATH=" + gopathAbs,
-		"PATH=" + path,
+		"GOROOT="+gorootAbs,
+		"GOPATH="+gopathAbs,
+		"PATH="+path,
 		"GO111MODULE=off",
-		"GOCACHE=" + tempDir,
+		"GOCACHE="+tempDir,
 	)
 
 	// Run the command interactively.
@@ -155,4 +165,29 @@
 			log.Fatalf("Could not run %q: %v", os.Args[1], err)
 		}
 	}
+
+	copyout := os.Getenv("GOTOOLWRAP_COPYOUT")
+	if len(copyout) != 0 {
+		for _, pair := range strings.Split(copyout, ";") {
+			parts := strings.Split(pair, ":")
+			if len(parts) != 2 {
+				log.Fatalf("GOTOOL_COPYOUT invalid pair: %q", pair)
+			}
+			from := filepath.Join(gopathAbs, "src", parts[0])
+			to := parts[1]
+			log.Printf("gotoolwrap: Copying %s to %s...", from, to)
+			data, err := os.ReadFile(from)
+			if err != nil {
+				log.Fatalf("gotoolwrap: read failed: %v", err)
+			}
+			err = os.MkdirAll(filepath.Dir(to), 0755)
+			if err != nil {
+				log.Fatalf("gotoolwrap: mkdir failed: %v", err)
+			}
+			err = os.WriteFile(to, data, 0644)
+			if err != nil {
+				log.Fatalf("gotoolwrap: write failed: %v", err)
+			}
+		}
+	}
 }
diff --git a/metropolis/build/kube-code-generator/defs.bzl b/metropolis/build/kube-code-generator/defs.bzl
index 77ee1b6..7d431ef 100644
--- a/metropolis/build/kube-code-generator/defs.bzl
+++ b/metropolis/build/kube-code-generator/defs.bzl
@@ -171,7 +171,14 @@
 # //metropolis/build/gotoolwrap, effectively setting up everything required to
 # use standard Go tooling on the monogon workspace (ie. GOPATH/GOROOT). This is
 # required by generators to run 'go fmt'.
-def _gotool_run(ctx, executable, arguments, **kwargs):
+#
+# The optional `copyout` argument configures which files to copy out from the
+# GOPATH into declared files. The contents of the list should be strings that
+# refer to a file within GOPATH/src, eg. source.monogon.dev/foo/bar/baz.go. The
+# files is then going to be copied into the path corresponding to a declared
+# file of the same basename.
+def _gotool_run(cg, executable, arguments, copyout=[], **kwargs):
+    ctx = cg.ctx
     go = go_context(ctx)
     gopath = ctx.attr.gopath[0][GoPath]
 
@@ -182,11 +189,17 @@
     tools = [
         executable,
         go.sdk.go,
-    ] + go.sdk.tools + kwargs.get("tools", [])
+    ] + go.sdk.srcs + go.sdk.tools + kwargs.get("tools", [])
+
+    copyout_env = []
+    for path in copyout:
+        to = cg.output_root + "/" + path
+        copyout_env.append("{}:{}".format(path, to))
 
     env = {
         "GOTOOLWRAP_GOPATH": gopath.gopath_file.path,
         "GOTOOLWRAP_GOROOT": go.sdk.root_file.dirname,
+        "GOTOOLWRAP_COPYOUT": ';'.join(copyout_env),
     }
     env.update(kwargs.get("env", {}))
 
@@ -241,6 +254,11 @@
         output_root = output_root,
         # The list of outputs that have to be generated by the codegen.
         outputs = [],
+        # List of files compatible with the `copyout` argument of _gotool_run.
+        # Populated by calls to _declare_library. Should be used when the tool
+        # generates files within the GOPATH instead of directly at declared
+        # output file locations.
+        copyout = [],
         # A map of importpath to list of outputs (from the above list) that
         # make up a generated Go package/library.
         libraries = {},
@@ -260,6 +278,7 @@
         ))
         cg.outputs.append(output)
         cg.libraries[importpath].append(output)
+        cg.copyout.append("{}/{}".format(importpath, f))
 
 # _declare_libraries declares multiple Go package/libraries to the codegen
 # context. The key of the dictionary is the importpath of the library, and the
@@ -296,7 +315,7 @@
         })
 
     _gotool_run(
-        ctx,
+        cg,
         mnemonic = "ClientsetGen",
         executable = ctx.executable._client_gen,
         arguments = [
@@ -306,10 +325,10 @@
             ctx.attr.apipath,
             "--input",
             ",".join(ctx.attr.apis),
-            "--output-package",
+            "--output-pkg",
             cg.importpath + "/clientset",
-            "--output-base",
-            cg.output_root,
+            "--output-dir",
+            cg.output_root + "/" + cg.importpath + "/clientset",
             "--go-header-file",
             ctx.file.boilerplate.path,
         ],
@@ -332,22 +351,18 @@
         })
 
     _gotool_run(
-        ctx,
+        cg,
+        copyout = cg.copyout,
         mnemonic = "DeepcopyGen",
         executable = ctx.executable._deepcopy_gen,
         arguments = [
-            "--input-dirs",
-            ",".join(["{}/{}".format(ctx.attr.apipath, api) for api in ctx.attr.apis]),
             "--go-header-file",
             ctx.file.boilerplate.path,
             "--stderrthreshold",
             "0",
-            "-O",
-            "zz_generated.deepcopy",
-            "--output-base",
-            cg.output_root,
-            ctx.attr.apipath,
-        ],
+            "--output-file",
+            "zz_generated.deepcopy.go",
+        ] + ["{}/{}".format(ctx.attr.apipath, api) for api in ctx.attr.apis],
         inputs = [
             ctx.file.boilerplate,
         ],
@@ -377,23 +392,21 @@
         })
 
     _gotool_run(
-        ctx,
+        cg,
         mnemonic = "InformerGen",
         executable = ctx.executable._informer_gen,
         arguments = [
-            "--input-dirs",
-            ",".join(["{}/{}".format(ctx.attr.apipath, api) for api in ctx.attr.apis]),
             "--versioned-clientset-package",
             "{}/clientset/versioned".format(ctx.attr.importpath),
             "--listers-package",
             "{}/listers".format(ctx.attr.importpath),
-            "--output-package",
+            "--output-pkg",
             "{}/informers".format(ctx.attr.importpath),
-            "--output-base",
-            cg.output_root,
+            "--output-dir",
+            cg.output_root + "/" + cg.importpath + "/informers",
             "--go-header-file",
             ctx.file.boilerplate.path,
-        ],
+        ] + ["{}/{}".format(ctx.attr.apipath, api) for api in ctx.attr.apis],
         inputs = [
             ctx.file.boilerplate,
         ],
@@ -418,21 +431,19 @@
         })
 
     _gotool_run(
-        ctx,
+        cg,
         mnemonic = "ListerGen",
         executable = ctx.executable._lister_gen,
         arguments = [
-            "--input-dirs",
-            ",".join(["{}/{}".format(ctx.attr.apipath, api) for api in ctx.attr.apis]),
-            "--output-package",
+            "--output-pkg",
             "{}/listers".format(ctx.attr.importpath),
-            "--output-base",
-            cg.output_root,
+            "--output-dir",
+            cg.output_root + "/" + cg.importpath + "/listers",
             "--go-header-file",
             ctx.file.boilerplate.path,
             "-v",
             "10",
-        ],
+        ] + ["{}/{}".format(ctx.attr.apipath, api) for api in ctx.attr.apis],
         inputs = [
             ctx.file.boilerplate,
         ],