-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 verify go modules are in sync with upstream k/k
This commit addresses issues were go modules aren't in sync with upstream k/k by adding these changes: - add `tools/cmd/gomodcheck/main.go` to: - Parse and compares k/k direct dependencies to controller-runtime's direct deps. - If any version diffs is found, returns a payload describing the diffs and exit 1. - The user may exclude packages by passing them as arguments. - extend the `verify-modules` make target with `gomodcheck`. Signed-off-by: Alexandre Mahdhaoui <alexandre.mahdhaoui@gmail.com>
- Loading branch information
1 parent
45e166d
commit 86ad2dc
Showing
3 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
github.com/onsi/gomega | ||
github.com/onsi/ginkgo/v2 | ||
|
||
k8s.io/api | ||
k8s.io/apimachinery | ||
k8s.io/apiextensions-apiserver | ||
k8s.io/apiserver | ||
k8s.io/client-go | ||
k8s.io/component-base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
const ( | ||
kkModUrl = "https://raw.githubusercontent.com/kubernetes/kubernetes/master/go.mod" | ||
Check failure on line 16 in tools/cmd/gomodcheck/main.go GitHub Actions / lint
|
||
modFile = "./go.mod" | ||
) | ||
|
||
var ( | ||
cleanMods = regexp.MustCompile(`\t| *//.*`) | ||
modDelimStart = regexp.MustCompile(`^require.*`) | ||
modDelimEnd = ")" | ||
) | ||
|
||
type oosMod struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
UpstreamVersion string `json:"upstreamVersion"` | ||
} | ||
|
||
func main() { | ||
logger := zap.New() | ||
excludedMods := getExcludedMods() | ||
|
||
// 1. kk | ||
resp, err := http.Get(kkModUrl) | ||
Check failure on line 37 in tools/cmd/gomodcheck/main.go GitHub Actions / lint
|
||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
kkMods := parseMod(b) | ||
|
||
// 2. go.mod | ||
b, err = os.ReadFile(modFile) | ||
if err != nil { | ||
return | ||
} | ||
|
||
oosMods := make([]oosMod, 0) | ||
|
||
mods := parseMod(b) | ||
for k, v := range mods { | ||
if _, ok := excludedMods[k]; ok { | ||
logger.Info(fmt.Sprintf("skipped module: %s", k)) | ||
continue | ||
} | ||
|
||
if upstreamVersion, ok := kkMods[k]; ok && v != upstreamVersion { | ||
oosMods = append(oosMods, oosMod{ | ||
Name: k, | ||
Version: v, | ||
UpstreamVersion: upstreamVersion, | ||
}) | ||
} | ||
} | ||
|
||
if len(oosMods) == 0 { | ||
fmt.Println("Success! 🎉") | ||
os.Exit(0) | ||
} | ||
|
||
b, err = json.MarshalIndent(map[string]any{"outOfSyncModules": oosMods}, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(string(b)) | ||
os.Exit(1) | ||
} | ||
|
||
func parseMod(b []byte) map[string]string { | ||
in := string(cleanMods.ReplaceAll(b, []byte(""))) | ||
out := make(map[string]string) | ||
|
||
start := false | ||
for _, s := range strings.Split(in, "\n") { | ||
switch { | ||
case modDelimStart.MatchString(s) && !start: | ||
start = true | ||
case s == modDelimEnd: | ||
return out | ||
case start: | ||
kv := strings.SplitN(s, " ", 2) | ||
if len(kv) < 2 { | ||
panic(fmt.Sprintf("unexpected format for module: %q", s)) | ||
} | ||
|
||
out[kv[0]] = kv[1] | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func getExcludedMods() map[string]any { | ||
out := make(map[string]any) | ||
|
||
for _, mod := range os.Args[1:] { | ||
out[mod] = nil | ||
} | ||
|
||
return out | ||
} |