-
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
Signed-off-by: Alexandre Mahdhaoui <alexandre.mahdhaoui@gmail.com>
- Loading branch information
1 parent
45e166d
commit 714596f
Showing
3 changed files
with
137 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" | ||
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) | ||
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 | ||
} |