diff --git a/cmd/gotk/bootstrap.go b/cmd/gotk/bootstrap.go index fb9616f2352fcd2e52ad59e300bd945ec993031c..9a0f7b78744cb37a31fd320594626d2ecd8f53b2 100644 --- a/cmd/gotk/bootstrap.go +++ b/cmd/gotk/bootstrap.go @@ -58,7 +58,7 @@ var ( ) const ( - bootstrapDefaultBranch = "master" + bootstrapDefaultBranch = "main" bootstrapInstallManifest = "toolkit-components.yaml" bootstrapSourceManifest = "toolkit-source.yaml" bootstrapKustomizationManifest = "toolkit-kustomization.yaml" diff --git a/cmd/gotk/reconcile_helmrelease.go b/cmd/gotk/reconcile_helmrelease.go index d5f9ecda1c6fc0041308dc0b2488a2118b959d61..398bb40dcffff5e072951cde4655dc677a15653a 100644 --- a/cmd/gotk/reconcile_helmrelease.go +++ b/cmd/gotk/reconcile_helmrelease.go @@ -21,14 +21,15 @@ import ( "fmt" "time" - "github.com/fluxcd/pkg/apis/meta" - corev1 "k8s.io/api/core/v1" - "github.com/spf13/cobra" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/fluxcd/pkg/apis/meta" + helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" ) @@ -98,14 +99,7 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error { } logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace) - if helmRelease.Annotations == nil { - helmRelease.Annotations = map[string]string{ - meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), - } - } else { - helmRelease.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) - } - if err := kubeClient.Update(ctx, &helmRelease); err != nil { + if err := requestHelmReleaseReconciliation(ctx, kubeClient, namespacedName); err != nil { return err } logger.Successf("HelmRelease annotated") @@ -151,3 +145,23 @@ func helmReleaseReconciliationHandled(ctx context.Context, kubeClient client.Cli return helmRelease.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil } } + +func requestHelmReleaseReconciliation(ctx context.Context, kubeClient client.Client, namespacedName types.NamespacedName) error { + var release helmv2.HelmRelease + return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { + if err := kubeClient.Get(ctx, namespacedName, &release); err != nil { + return err + } + + if release.Annotations == nil { + release.Annotations = map[string]string{ + meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), + } + } else { + release.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) + } + + err = kubeClient.Update(ctx, &release) + return + }) +} diff --git a/cmd/gotk/reconcile_kustomization.go b/cmd/gotk/reconcile_kustomization.go index dee828095e0e954f19bf154468ffca16438cecb0..b31bc7062df3589b82c1d2ca4d5d2d1831cc006f 100644 --- a/cmd/gotk/reconcile_kustomization.go +++ b/cmd/gotk/reconcile_kustomization.go @@ -21,10 +21,11 @@ import ( "fmt" "time" - "github.com/fluxcd/pkg/apis/meta" corev1 "k8s.io/api/core/v1" + "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/fluxcd/pkg/apis/meta" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" @@ -96,14 +97,7 @@ func reconcileKsCmdRun(cmd *cobra.Command, args []string) error { } logger.Actionf("annotating kustomization %s in %s namespace", name, namespace) - if kustomization.Annotations == nil { - kustomization.Annotations = map[string]string{ - meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), - } - } else { - kustomization.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) - } - if err := kubeClient.Update(ctx, &kustomization); err != nil { + if err := requestKustomizeReconciliation(ctx, kubeClient, namespacedName); err != nil { return err } logger.Successf("kustomization annotated") @@ -150,3 +144,23 @@ func kustomizeReconciliationHandled(ctx context.Context, kubeClient client.Clien return kustomize.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil } } + +func requestKustomizeReconciliation(ctx context.Context, kubeClient client.Client, namespacedName types.NamespacedName) error { + var kustomization kustomizev1.Kustomization + return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { + if err := kubeClient.Get(ctx, namespacedName, &kustomization); err != nil { + return err + } + + if kustomization.Annotations == nil { + kustomization.Annotations = map[string]string{ + meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), + } + } else { + kustomization.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) + } + + err = kubeClient.Update(ctx, &kustomization) + return + }) +} diff --git a/docs/cmd/gotk_bootstrap.md b/docs/cmd/gotk_bootstrap.md index 79a34f8c45d912077466465cf54832c879d726e5..4bc26be38222540daaccd044ea21a137b477d758 100644 --- a/docs/cmd/gotk_bootstrap.md +++ b/docs/cmd/gotk_bootstrap.md @@ -10,7 +10,7 @@ The bootstrap sub-commands bootstrap the toolkit components on the targeted Git ``` --arch string arch can be amd64 or arm64 (default "amd64") - --branch string default branch (for GitHub this must match the default branch setting for the organization) (default "master") + --branch string default branch (for GitHub this must match the default branch setting for the organization) (default "main") --components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller]) -h, --help help for bootstrap --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry diff --git a/docs/cmd/gotk_bootstrap_github.md b/docs/cmd/gotk_bootstrap_github.md index 7e06aae08aef39ef48dbb4544072e7087e0d3ccf..71e2b1bba2ebe6feb5b4cada6f683c3a618c4559 100644 --- a/docs/cmd/gotk_bootstrap_github.md +++ b/docs/cmd/gotk_bootstrap_github.md @@ -58,7 +58,7 @@ gotk bootstrap github [flags] ``` --arch string arch can be amd64 or arm64 (default "amd64") - --branch string default branch (for GitHub this must match the default branch setting for the organization) (default "master") + --branch string default branch (for GitHub this must match the default branch setting for the organization) (default "main") --components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller]) --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --kubeconfig string path to the kubeconfig file (default "~/.kube/config") diff --git a/docs/cmd/gotk_bootstrap_gitlab.md b/docs/cmd/gotk_bootstrap_gitlab.md index 24ebd006a4e0151269429688e11678a7585ee2d5..b3671fa39a8ee3f720b54eda1367216060d57583 100644 --- a/docs/cmd/gotk_bootstrap_gitlab.md +++ b/docs/cmd/gotk_bootstrap_gitlab.md @@ -55,7 +55,7 @@ gotk bootstrap gitlab [flags] ``` --arch string arch can be amd64 or arm64 (default "amd64") - --branch string default branch (for GitHub this must match the default branch setting for the organization) (default "master") + --branch string default branch (for GitHub this must match the default branch setting for the organization) (default "main") --components strings list of components, accepts comma-separated values (default [source-controller,kustomize-controller,helm-controller,notification-controller]) --image-pull-secret string Kubernetes secret name used for pulling the toolkit images from a private registry --kubeconfig string path to the kubeconfig file (default "~/.kube/config") diff --git a/docs/get-started/index.md b/docs/get-started/index.md index 4b6e0d1afd0eb250232187c0ac6e331853c59c39..3f5c5c14ce82c603e2e9d034a734186c1d7d1cc8 100644 --- a/docs/get-started/index.md +++ b/docs/get-started/index.md @@ -83,12 +83,13 @@ Run the bootstrap command: gotk bootstrap github \ --owner=$GITHUB_USER \ --repository=fleet-infra \ + --branch=main \ --path=staging-cluster \ --personal ``` The bootstrap command creates a repository if one doesn't exist and -commits the toolkit components manifests to the master branch at the specified path. +commits the toolkit components manifests to the default branch at the specified path. Then it configures the target cluster to synchronize with the specified path inside the repository. If you wish to create the repository under a GitHub organization: @@ -97,6 +98,7 @@ If you wish to create the repository under a GitHub organization: gotk bootstrap github \ --owner=<organization> \ --repository=<repo-name> \ + --branch=<organization default branch> \ --team=<team1-slug> \ --team=<team2-slug> \ --path=staging-cluster diff --git a/docs/guides/installation.md b/docs/guides/installation.md index 341556fab66b8e5773c710cb92dbafbb5ac1f9ff..b03d817308bf172a5185bd6798a1318e9b0c5ffe 100644 --- a/docs/guides/installation.md +++ b/docs/guides/installation.md @@ -44,7 +44,7 @@ Using the `gotk bootstrap` command you can install the toolkit on a Kubernetes c and configure it to manage itself from a Git repository. The bootstrap creates a Git repository if one doesn't exist and -commits the toolkit components manifests to the master branch. +commits the toolkit components manifests to the main branch. Then it configures the target cluster to synchronize with that repository by setting up SSH deploy keys. @@ -84,7 +84,7 @@ cluster e.g. `staging-cluster` and `production-cluster`: ``` !!! hint "Change the default branch" - If you wish to change the branch to something else than master, create the repository manually, + If you wish to change the branch to something else than main, create the repository manually, push a branch to origin and then use `gotk bootstrap <GIT-PROVIDER> --branch=your-branch`. ### GitHub and GitHub Enterprise @@ -128,6 +128,7 @@ gotk bootstrap github \ --hostname=my-github-enterprise.com \ --owner=my-github-organization \ --repository=my-repository \ + --branch=main \ --path=my-cluster ``` @@ -148,6 +149,7 @@ Run the bootstrap for a repository on your personal GitLab account: gotk bootstrap gitlab \ --owner=my-gitlab-username \ --repository=my-repository \ + --branch=master \ --path=my-cluster \ --personal ``` @@ -158,6 +160,7 @@ Run the bootstrap for a repository owned by a GitLab group: gotk bootstrap gitlab \ --owner=my-gitlab-group \ --repository=my-repository \ + --branch=master \ --path=my-cluster ``` @@ -168,6 +171,7 @@ gotk bootstrap gitlab \ --hostname=my-gitlab.com \ --owner=my-gitlab-group \ --repository=my-repository \ + --branch=master \ --path=my-cluster ``` @@ -359,7 +363,7 @@ The GitOps Toolkit comes with an optional monitoring stack. You can install the stack in the `gotk-system` namespace with: ```yaml -kustomize build github.com/fluxcd/toolkit/manifests/monitoring?ref=master | kubectl apply -f- +kustomize build github.com/fluxcd/toolkit/manifests/monitoring | kubectl apply -f- ``` The monitoring stack is composed of: