diff --git a/.github/workflows/bootstrap.yaml b/.github/workflows/bootstrap.yaml index 60c86385..5b2f9666 100644 --- a/.github/workflows/bootstrap.yaml +++ b/.github/workflows/bootstrap.yaml @@ -91,16 +91,24 @@ jobs: run: | /tmp/flux uninstall -s --keep-namespace kubectl delete ns flux-system --timeout=10m --wait=true - - name: bootstrap reinstall + - name: test image automation run: | + make setup-image-automation /tmp/flux bootstrap github --manifests ./manifests/install/ \ --owner=fluxcd-testing \ --repository=${{ steps.vars.outputs.test_repo_name }} \ --branch=main \ --path=test-cluster \ - --team=team-z + --read-write-key + /tmp/flux reconcile image repository podinfo + /tmp/flux reconcile image update flux-system + /tmp/flux get images all + /tmp/flux get images policy podinfo | grep "5.2.1" + /tmp/flux get image update flux-system | grep commit env: GITHUB_TOKEN: ${{ secrets.GITPROVIDER_BOT_TOKEN }} + GITHUB_REPO_NAME: ${{ steps.vars.outputs.test_repo_name }} + GITHUB_ORG_NAME: fluxcd-testing - name: delete repository run: | curl \ diff --git a/Makefile b/Makefile index 631d2767..3d375d42 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,9 @@ install-envtest: setup-envtest setup-bootstrap-patch: go run ./tests/bootstrap/main.go +setup-image-automation: + cd tests/image-automation && go run main.go + # Find or download setup-envtest setup-envtest: ifeq (, $(shell which setup-envtest)) diff --git a/tests/image-automation/auto.yaml b/tests/image-automation/auto.yaml new file mode 100644 index 00000000..321012c0 --- /dev/null +++ b/tests/image-automation/auto.yaml @@ -0,0 +1,45 @@ +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImageRepository +metadata: + name: podinfo + namespace: flux-system +spec: + image: ghcr.io/stefanprodan/podinfo + interval: 1m0s +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImagePolicy +metadata: + name: podinfo + namespace: flux-system +spec: + imageRepositoryRef: + name: podinfo + policy: + semver: + range: 5.2.x +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImageUpdateAutomation +metadata: + name: flux-system + namespace: flux-system +spec: + interval: 5m0s + sourceRef: + kind: GitRepository + name: flux-system + git: + checkout: + ref: + branch: main + commit: + author: + email: fluxcdbot@users.noreply.github.com + name: fluxcdbot + messageTemplate: '{{range .Updated.Images}}{{println .}}{{end}}' + push: + branch: main + update: + path: ./test-cluster/podinfo-auto + strategy: Setters diff --git a/tests/image-automation/kustomization.yaml b/tests/image-automation/kustomization.yaml new file mode 100644 index 00000000..64cb51fc --- /dev/null +++ b/tests/image-automation/kustomization.yaml @@ -0,0 +1,10 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: flux-system +resources: + - https://raw.githubusercontent.com/stefanprodan/podinfo/5.2.0/kustomize/deployment.yaml + - auto.yaml +images: + - name: ghcr.io/stefanprodan/podinfo + newName: ghcr.io/stefanprodan/podinfo + newTag: 5.2.0 # {"$imagepolicy": "flux-system:podinfo:tag"} diff --git a/tests/image-automation/main.go b/tests/image-automation/main.go new file mode 100644 index 00000000..026f19cb --- /dev/null +++ b/tests/image-automation/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "context" + "log" + "os" + + "github.com/fluxcd/go-git-providers/github" + "github.com/fluxcd/go-git-providers/gitprovider" + "k8s.io/client-go/util/retry" +) + +func main() { + ksPath := "test-cluster/podinfo-auto/kustomization.yaml" + autoPath := "test-cluster/podinfo-auto/auto.yaml" + + ksContent, err := os.ReadFile("kustomization.yaml") + if err != nil { + log.Fatal(err) + } + ks := string(ksContent) + + autoContent, err := os.ReadFile("auto.yaml") + if err != nil { + log.Fatal(err) + } + auto := string(autoContent) + + commitFiles := []gitprovider.CommitFile{ + { + Path: &ksPath, + Content: &ks, + }, + { + Path: &autoPath, + Content: &auto, + }, + } + + orgName := os.Getenv("GITHUB_ORG_NAME") + repoName := os.Getenv("GITHUB_REPO_NAME") + githubToken := os.Getenv(github.TokenVariable) + client, err := github.NewClient(gitprovider.WithOAuth2Token(githubToken)) + if err != nil { + log.Fatalf("error initializing github client: %s", err) + } + + repoRef := gitprovider.OrgRepositoryRef{ + OrganizationRef: gitprovider.OrganizationRef{ + Organization: orgName, + Domain: github.DefaultDomain, + }, + RepositoryName: repoName, + } + + var repo gitprovider.OrgRepository + err = retry.OnError(retry.DefaultRetry, func(err error) bool { + return err != nil + }, func() error { + repo, err = client.OrgRepositories().Get(context.Background(), repoRef) + return err + }) + if err != nil { + log.Fatalf("error getting %s repository in org %s: %s", repoRef.RepositoryName, repoRef.Organization, err) + } + + _, err = repo.Commits().Create(context.Background(), "main", "automation test", commitFiles) + if err != nil { + log.Fatalf("error making commit: %s", err) + } +}