From ac7d0788f42eda5f9f2e6d0f716a2db1dbc4531e Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Mon, 8 May 2023 09:38:02 -0700 Subject: [PATCH] CI: Automate updating cni-plugins --- .../workflows/update-cni-plugins-version.yml | 70 ++++++++++++ Makefile | 5 + .../update_cni_plugins_version.go | 104 ++++++++++++++++++ hack/update/get_version/get_version.go | 1 + .../golang_version/update_golang_version.go | 5 + 5 files changed, 185 insertions(+) create mode 100644 .github/workflows/update-cni-plugins-version.yml create mode 100644 hack/update/cni_plugins_version/update_cni_plugins_version.go diff --git a/.github/workflows/update-cni-plugins-version.yml b/.github/workflows/update-cni-plugins-version.yml new file mode 100644 index 0000000000..31c14058a3 --- /dev/null +++ b/.github/workflows/update-cni-plugins-version.yml @@ -0,0 +1,70 @@ +name: "update-cni-plugins-version" +on: + workflow_dispatch: + schedule: + # every Monday at around 3 am pacific/10 am UTC + - cron: "0 10 * * 1" +env: + GOPROXY: https://proxy.golang.org + GO_VERSION: '1.20.3' +permissions: + contents: read + +jobs: + bump-cni-plugins-version: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab + - uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 + with: + go-version: ${{env.GO_VERSION}} + cache-dependency-path: ./go.sum + - name: Bump cni-plugins Version + id: bumpCNIPlugins + run: | + echo "OLD_VERSION=$(DEP=cni-plugins make get-dependency-version)" >> $GITHUB_OUTPUT + make update-cni-plugins-version + echo "NEW_VERSION=$(DEP=cni-plugins make get-dependency-version)" >> $GITHUB_OUTPUT + # The following is to support multiline with GITHUB_OUTPUT, see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings + echo "changes<> $GITHUB_OUTPUT + echo "$(git status --porcelain)" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + - name: Create PR + id: createPR + if: ${{ steps.bumpCNIPlugins.outputs.changes != '' }} + uses: peter-evans/create-pull-request@284f54f989303d2699d373481a0cfa13ad5a6666 + with: + token: ${{ secrets.MINIKUBE_BOT_PAT }} + commit-message: 'Kicbase/ISO: Update cni-plugins from ${{ steps.bumpCNIPlugins.outputs.OLD_VERSION }} to ${{ steps.bumpCNIPlugins.outputs.NEW_VERSION }}' + committer: minikube-bot + author: minikube-bot + branch: auto_bump_cni_plugins_version + branch-suffix: short-commit-hash + push-to-fork: minikube-bot/minikube + base: master + delete-branch: true + title: 'Kicbase/ISO: Update cni-plugins from ${{ steps.bumpCNIPlugins.outputs.OLD_VERSION }} to ${{ steps.bumpCNIPlugins.outputs.NEW_VERSION }}' + body: | + The cni-plugins project released a [new version](https://github.com/containernetworking/plugins/releases) + + This PR was auto-generated by `make update-cni-plugins-version` using [update-cni-plugins-version.yml](https://github.com/kubernetes/minikube/tree/master/.github/workflows/update-cni-plugins-version.yml) CI Workflow. + - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 + with: + github-token: ${{ secrets.MINIKUBE_BOT_PAT }} + script: | + github.rest.issues.createComment({ + issue_number: ${{ steps.createPR.outputs.pull-request-number }}, + owner: context.repo.owner, + repo: context.repo.repo, + body: 'ok-to-build-image' + }) + - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 + with: + github-token: ${{ secrets.MINIKUBE_BOT_PAT }} + script: | + github.rest.issues.createComment({ + issue_number: ${{ steps.createPR.outputs.pull-request-number }}, + owner: context.repo.owner, + repo: context.repo.repo, + body: 'ok-to-build-iso' + }) diff --git a/Makefile b/Makefile index 7ffff9dc46..18dcea5d16 100644 --- a/Makefile +++ b/Makefile @@ -1112,6 +1112,11 @@ update-ubuntu-version: (cd hack/update/ubuntu_version && \ go run update_ubuntu_version.go) +.PHONY: update-cni-plugins-version +update-cni-plugins-version: + (cd hack/update/cni_plugins_version && \ + go run update_cni_plugins_version.go) + .PHONY: get-dependency-verison get-dependency-version: @(cd hack/update/get_version && \ diff --git a/hack/update/cni_plugins_version/update_cni_plugins_version.go b/hack/update/cni_plugins_version/update_cni_plugins_version.go new file mode 100644 index 0000000000..08b4f6cd6d --- /dev/null +++ b/hack/update/cni_plugins_version/update_cni_plugins_version.go @@ -0,0 +1,104 @@ +/* +Copyright 2023 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "context" + "crypto/sha256" + "fmt" + "io" + "net/http" + "os" + "strings" + "time" + + "k8s.io/klog/v2" + "k8s.io/minikube/hack/update" +) + +const cxTimeout = 5 * time.Minute + +var ( + schema = map[string]update.Item{ + "deploy/iso/minikube-iso/arch/aarch64/package/cni-plugins-aarch64/cni-plugins.mk": { + Replace: map[string]string{ + `CNI_PLUGINS_AARCH64_VERSION = .*`: `CNI_PLUGINS_AARCH64_VERSION = {{.Version}}`, + }, + }, + "deploy/iso/minikube-iso/arch/x86_64/package/cni-plugins/cni-plugins.mk": { + Replace: map[string]string{ + `CNI_PLUGINS_VERSION = .*`: `CNI_PLUGINS_VERSION = {{.Version}}`, + }, + }, + } +) + +type Data struct { + Version string +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), cxTimeout) + defer cancel() + + stable, _, _, err := update.GHReleases(ctx, "containernetworking", "plugins") + if err != nil { + klog.Fatalf("Unable to get stable version: %v", err) + } + + data := Data{Version: stable.Tag} + + update.Apply(schema, data) + + if err := updateHashFile(data.Version, "arm64", "aarch64/package/cni-plugins-aarch64"); err != nil { + klog.Fatalf("failed to update hash files: %v", err) + } + if err := updateHashFile(data.Version, "amd64", "x86_64/package/cni-plugins"); err != nil { + klog.Fatalf("failed to update hash files: %v", err) + } +} + +func updateHashFile(version, arch, packagePath string) error { + r, err := http.Get(fmt.Sprintf("https://github.com/containernetworking/plugins/releases/download/%s/cni-plugins-linux-%s-%s.tgz", version, arch, version)) + if err != nil { + return fmt.Errorf("failed to download source code: %v", err) + } + defer r.Body.Close() + b, err := io.ReadAll(r.Body) + if err != nil { + return fmt.Errorf("failed to read response body: %v", err) + } + sum := sha256.Sum256(b) + filePath := fmt.Sprintf("../../../deploy/iso/minikube-iso/arch/%s/cni-plugins.hash", packagePath) + b, err = os.ReadFile(filePath) + if err != nil { + return fmt.Errorf("failed to read hash file: %v", err) + } + if strings.Contains(string(b), version) { + klog.Infof("hash file already contains %q", version) + return nil + } + f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("failed to open hash file: %v", err) + } + defer f.Close() + if _, err := f.WriteString(fmt.Sprintf("sha256 %x cni-plugins-linux-%s-%s.tgz\n", sum, arch, version)); err != nil { + return fmt.Errorf("failed to write to hash file: %v", err) + } + return nil +} diff --git a/hack/update/get_version/get_version.go b/hack/update/get_version/get_version.go index d1c704311f..6132ddd422 100644 --- a/hack/update/get_version/get_version.go +++ b/hack/update/get_version/get_version.go @@ -30,6 +30,7 @@ type dependency struct { var dependencies = map[string]dependency{ "buildkit": {"deploy/iso/minikube-iso/arch/x86_64/package/buildkit-bin/buildkit-bin.mk", `BUILDKIT_BIN_VERSION = (.*)`}, "cloud-spanner": {"pkg/minikube/assets/addons.go", `cloud-spanner-emulator/emulator:(.*)@`}, + "cni-plugins": {"deploy/iso/minikube-iso/arch/x86_64/package/cni-plugins/cni-plugins.mk", `CNI_PLUGINS_VERSION = (.*)`}, "containerd": {"deploy/iso/minikube-iso/arch/x86_64/package/containerd-bin/containerd-bin.mk", `CONTAINERD_BIN_VERSION = (.*)`}, "cri-o": {"deploy/iso/minikube-iso/package/crio-bin/crio-bin.mk", `CRIO_BIN_VERSION = (.*)`}, "docker": {"deploy/iso/minikube-iso/arch/x86_64/package/docker-bin/docker-bin.mk", `DOCKER_BIN_VERSION = (.*)`}, diff --git a/hack/update/golang_version/update_golang_version.go b/hack/update/golang_version/update_golang_version.go index 5e6f12a030..aa0198208e 100644 --- a/hack/update/golang_version/update_golang_version.go +++ b/hack/update/golang_version/update_golang_version.go @@ -165,6 +165,11 @@ var ( `GO_VERSION: .*`: `GO_VERSION: '{{.StableVersion}}'`, }, }, + ".github/workflows/update-cni-plugins-version.yml": { + Replace: map[string]string{ + `GO_VERSION: .*`: `GO_VERSION: '{{.StableVersion}}'`, + }, + }, ".github/workflows/sync-minikube.yml": { Replace: map[string]string{ `GO_VERSION: .*`: `GO_VERSION: '{{.StableVersion}}'`,