Skip to content

Commit 2991d5d

Browse files
committed
Changes to allow calling as make target
Signed-off-by: Brett Tofel <[email protected]>
1 parent 88753e3 commit 2991d5d

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ custom-linter-build: #HELP Build custom linter
107107
lint-custom: custom-linter-build #HELP Call custom linter for the project
108108
go vet -tags=$(GO_BUILD_TAGS) -vettool=./bin/custom-linter ./...
109109

110+
.PHONY: k8s-maintainer #EXHELP this tool also calls `go mod tidy` but also allows us maintain k8s.io/kubernetes` changes bumping related staging modules (e.g., `k8s.io/api`, `k8s.io/apimachinery) as needed
111+
k8s-maintainer:
112+
go run hack/tools/k8sMaintainer.go
113+
110114
.PHONY: tidy
111-
tidy: #HELP Update dependencies.
112-
# Force tidy to use the version already in go.mod
113-
$(Q)go mod tidy -go=$(GOLANG_VERSION)
115+
tidy: k8s-maintainer #HELP Update dependencies.
116+
# k8s-maintainer calls go mod tidy
114117

115118
.PHONY: manifests
116119
KUSTOMIZE_CATD_CRDS_DIR := config/base/catalogd/crd/bases

hack/tools/k8sMaintainer.go

+30-39
Original file line numberDiff line numberDiff line change
@@ -40,81 +40,72 @@ func main() {
4040
// Remove old k8s.io/* replace lines, rewrite + tidy so they’re really gone.
4141
// Parse again, unify staging modules in require + replace to the new patch version, rewrite + tidy.
4242
func fixGoMod(goModPath string) error {
43-
// parse & remove old lines, write, go mod tidy
44-
mf1, err := parseMod(goModPath)
43+
mf, err := parseMod(goModPath)
4544
if err != nil {
4645
return err
4746
}
48-
pruneK8sReplaces(mf1)
49-
mf1.SortBlocks()
50-
mf1.Cleanup()
47+
pruneK8sReplaces(mf)
48+
mf.SortBlocks()
49+
mf.Cleanup()
5150

52-
if err := writeModFile(mf1, goModPath); err != nil {
51+
if err := writeModFile(mf, goModPath); err != nil {
5352
return err
5453
}
55-
if err := runCmd("go", "mod", "tidy"); err != nil {
56-
return fmt.Errorf("go mod tidy failed: %w", err)
57-
}
58-
//parse again, unify everything to derived patch version in both require + replace blocks, write, go mod tidy
59-
mf2, err := parseMod(goModPath)
54+
55+
mf, err = parseMod(goModPath)
6056
if err != nil {
6157
return err
6258
}
6359

64-
k8sVer := findKubernetesVersion(mf2)
60+
k8sVer := findKubernetesVersion(mf)
6561
if k8sVer == "" {
6662
return fmt.Errorf("did not find k8s.io/kubernetes in require block")
6763
}
68-
if debug {
69-
fmt.Printf("Found k8s.io/kubernetes version: %s\n", k8sVer)
70-
}
7164

7265
published := toPublishedVersion(k8sVer)
7366
if published == "" {
7467
return fmt.Errorf("cannot derive staging version from %s", k8sVer)
7568
}
76-
if debug {
77-
fmt.Printf("Unifying staging modules to: %s (from %s)\n", published, k8sVer)
78-
}
7969

80-
// forcibly unify the REQUIRE items for all staging modules
81-
forceRequireStaging(mf2, published)
70+
forceRequireStaging(mf, published)
8271

83-
// discover all k8s.io/* modules in the graph and unify them with new replace lines
8472
listOut, errOut, err := runGoList()
8573
if err != nil {
8674
return fmt.Errorf("go list: %v\nStderr:\n%s", err, errOut)
8775
}
8876
stagingPins := discoverPinsAlways(listOut, published)
89-
applyReplacements(mf2, stagingPins)
77+
applyReplacements(mf, stagingPins)
9078

91-
// also ensure we have a replace for k8s.io/kubernetes => same version
92-
ensureKubernetesReplace(mf2, k8sVer)
79+
ensureKubernetesReplace(mf, k8sVer)
9380

94-
mf2.SortBlocks()
95-
mf2.Cleanup()
81+
mf.SortBlocks()
82+
mf.Cleanup()
9683

97-
if err := writeModFile(mf2, goModPath); err != nil {
84+
if err := writeModFile(mf, goModPath); err != nil {
9885
return err
9986
}
100-
if err := runCmd("go", "mod", "tidy"); err != nil {
101-
return fmt.Errorf("final tidy failed: %w", err)
87+
88+
goVersion, err := getGoVersion(goModPath)
89+
if err != nil {
90+
return fmt.Errorf("failed to determine Go version: %w", err)
10291
}
103-
if err := runCmd("go", "mod", "download", "k8s.io/kubernetes"); err != nil {
104-
return fmt.Errorf("final: go mod download k8s.io/kubernetes failed: %w", err)
92+
93+
if err := runCmd("go", "mod", "tidy", "-go="+goVersion); err != nil {
94+
return fmt.Errorf("final tidy failed: %w", err)
10595
}
10696

107-
// final check
108-
finalOut, err := exec.Command("go", "list", "-m", "all").Output()
97+
return nil
98+
}
99+
100+
func getGoVersion(goModPath string) (string, error) {
101+
mf, err := parseMod(goModPath)
109102
if err != nil {
110-
return fmt.Errorf("running final go list: %w", err)
103+
return "", err
111104
}
112-
if bytes.Contains(finalOut, []byte("v0.0.0")) {
113-
fmt.Println("WARNING: Some modules remain at v0.0.0, possibly no valid tags.")
114-
} else {
115-
fmt.Println("Success: staging modules pinned to", published)
105+
if mf.Go == nil {
106+
return "", fmt.Errorf("go version not found in go.mod")
116107
}
117-
return nil
108+
return mf.Go.Version, nil
118109
}
119110

120111
// parseMod reads go.mod into memory as a modfile.File

0 commit comments

Comments
 (0)