From 49d0fa4e1ba00aade317b3ea4885fe1a893472f2 Mon Sep 17 00:00:00 2001 From: Medya Ghazizadeh Date: Wed, 30 Jul 2025 11:40:08 -0700 Subject: [PATCH] ci: use authenticated github when possible (#21189) * hack update to use github token if available * automate go mod tidy for both * add ghc client for k8s vers list --- hack/preload-images/kubernetes.go | 5 +++-- hack/update/github.go | 12 +++++++++++- .../go_github_version/update_go_github_version.go | 8 ++++++++ .../update/ingress_version/update_ingress_version.go | 2 +- .../kubeadm_constants/update_kubeadm_constants.go | 2 +- .../update_kubernetes_versions_list.go | 2 +- .../site_node_version/update_site_node_version.go | 2 +- 7 files changed, 26 insertions(+), 7 deletions(-) diff --git a/hack/preload-images/kubernetes.go b/hack/preload-images/kubernetes.go index ef5e29186e..4ae146d13c 100644 --- a/hack/preload-images/kubernetes.go +++ b/hack/preload-images/kubernetes.go @@ -23,13 +23,14 @@ import ( "github.com/google/go-github/v73/github" "k8s.io/klog/v2" + "k8s.io/minikube/hack/update" ) // recentK8sVersions returns the most recent k8s version, usually around 100. func recentK8sVersions() ([]string, error) { const k8s = "kubernetes" - client := github.NewClient(nil) - list, _, err := client.Repositories.ListReleases(context.Background(), k8s, k8s, &github.ListOptions{PerPage: 100}) + ghc := update.GHClient() + list, _, err := ghc.Repositories.ListReleases(context.Background(), k8s, k8s, &github.ListOptions{PerPage: 100}) if err != nil { return nil, err } diff --git a/hack/update/github.go b/hack/update/github.go index cac43c9b9b..cd886374b5 100644 --- a/hack/update/github.go +++ b/hack/update/github.go @@ -19,6 +19,7 @@ package update import ( "context" "fmt" + "os" "strings" "golang.org/x/mod/semver" @@ -43,7 +44,7 @@ type Release struct { // GHReleases returns greatest current stable release and greatest latest rc or beta pre-release from GitHub owner/repo repository, and any error occurred. // If latest pre-release version is lower than the current stable release, then it will return current stable release for both. func GHReleases(ctx context.Context, owner, repo string) (stable, latest, edge Release, err error) { - ghc := github.NewClient(nil) + ghc := GHClient() // walk through the paginated list of up to ghSearchLimit newest releases opts := &github.ListOptions{PerPage: ghListPerPage} @@ -107,3 +108,12 @@ func StableVersion(ctx context.Context, owner, repo string) (string, error) { } return stable.Tag, nil } + +// GHClient returns a GitHub client regardless of whether the GITHUB_TOKEN is set or not. +func GHClient() *github.Client { + if os.Getenv("GITHUB_TOKEN") == "" { + return github.NewClient(nil) + } + return github.NewClient(nil).WithAuthToken(os.Getenv("GITHUB_TOKEN")) + +} diff --git a/hack/update/go_github_version/update_go_github_version.go b/hack/update/go_github_version/update_go_github_version.go index 6c1371e985..2ae3eebcc9 100644 --- a/hack/update/go_github_version/update_go_github_version.go +++ b/hack/update/go_github_version/update_go_github_version.go @@ -57,6 +57,14 @@ func main() { if err := exec.Command("go", "mod", "tidy").Run(); err != nil { klog.Fatalf("failed to run go mod tidy: %v", err) } + + // we need to run go mod tidy in the root folder too (since both minikube and hack use go-github) + cmd := exec.Command("go", "mod", "tidy") + cmd.Dir = ".." + if err := cmd.Run(); err != nil { + klog.Fatalf("failed to run go mod tidy in parent folder: %v", err) + } + } func generateSchema() map[string]update.Item { diff --git a/hack/update/ingress_version/update_ingress_version.go b/hack/update/ingress_version/update_ingress_version.go index 5fd321e0cc..57be3fc4f2 100644 --- a/hack/update/ingress_version/update_ingress_version.go +++ b/hack/update/ingress_version/update_ingress_version.go @@ -86,7 +86,7 @@ func main() { func LatestControllerTag(ctx context.Context) (string, error) { latest := "v0.0.0" - ghc := github.NewClient(nil) + ghc := update.GHClient() re := regexp.MustCompile(`controller-(.*)`) // walk through the paginated list of up to ghSearchLimit newest releases diff --git a/hack/update/kubeadm_constants/update_kubeadm_constants.go b/hack/update/kubeadm_constants/update_kubeadm_constants.go index 0f7a781f75..e132325014 100644 --- a/hack/update/kubeadm_constants/update_kubeadm_constants.go +++ b/hack/update/kubeadm_constants/update_kubeadm_constants.go @@ -62,7 +62,7 @@ func main() { releases := []string{} - ghc := github.NewClient(nil) + ghc := update.GHClient() opts := &github.ListOptions{PerPage: 100} for { diff --git a/hack/update/kubernetes_versions_list/update_kubernetes_versions_list.go b/hack/update/kubernetes_versions_list/update_kubernetes_versions_list.go index f4bbce3b59..0630e3d2ba 100644 --- a/hack/update/kubernetes_versions_list/update_kubernetes_versions_list.go +++ b/hack/update/kubernetes_versions_list/update_kubernetes_versions_list.go @@ -52,7 +52,7 @@ type Data struct { func main() { releases := []string{} - ghc := github.NewClient(nil) + ghc := update.GHClient() opts := &github.ListOptions{PerPage: 100} for { diff --git a/hack/update/site_node_version/update_site_node_version.go b/hack/update/site_node_version/update_site_node_version.go index bc9c1f6d98..6002046864 100644 --- a/hack/update/site_node_version/update_site_node_version.go +++ b/hack/update/site_node_version/update_site_node_version.go @@ -66,7 +66,7 @@ func main() { } func latestNodeVersionByMajor(ctx context.Context, major string) (string, error) { - ghc := github.NewClient(nil) + ghc := update.GHClient() // walk through the paginated list of up to ghSearchLimit newest releases opts := &github.ListOptions{PerPage: ghListPerPage}