Skip to content

Commit

Permalink
Merge pull request #1262 from fluxcd/reconcile-opt
Browse files Browse the repository at this point in the history
Put potentially destructive reconcile behind flag
  • Loading branch information
hiddeco authored Apr 13, 2021
2 parents 40ce3d5 + b6d349d commit 7dcf884
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
5 changes: 5 additions & 0 deletions cmd/flux/bootstrap_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type githubFlags struct {
path flags.SafeRelativePath
teams []string
readWriteKey bool
reconcile bool
}

const (
Expand All @@ -100,6 +101,7 @@ func init() {
bootstrapGitHubCmd.Flags().StringVar(&githubArgs.hostname, "hostname", ghDefaultDomain, "GitHub hostname")
bootstrapGitHubCmd.Flags().Var(&githubArgs.path, "path", "path relative to the repository root, when specified the cluster sync will be scoped to this path")
bootstrapGitHubCmd.Flags().BoolVar(&githubArgs.readWriteKey, "read-write-key", false, "if true, the deploy key is configured with read/write permissions")
bootstrapGitHubCmd.Flags().BoolVar(&githubArgs.reconcile, "reconcile", false, "if true, the configured options are also reconciled if the repository already exists")

bootstrapCmd.AddCommand(bootstrapGitHubCmd)
}
Expand Down Expand Up @@ -235,6 +237,9 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if !githubArgs.private {
bootstrapOpts = append(bootstrapOpts, bootstrap.WithProviderRepositoryConfig("", "", "public"))
}
if githubArgs.reconcile {
bootstrapOpts = append(bootstrapOpts, bootstrap.WithReconcile())
}

// Setup bootstrapper with constructed configs
b, err := bootstrap.NewGitProviderBootstrapper(gitClient, providerClient, kubeClient, bootstrapOpts...)
Expand Down
5 changes: 5 additions & 0 deletions cmd/flux/bootstrap_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type gitlabFlags struct {
path flags.SafeRelativePath
teams []string
readWriteKey bool
reconcile bool
}

var gitlabArgs gitlabFlags
Expand All @@ -100,6 +101,7 @@ func init() {
bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.hostname, "hostname", glDefaultDomain, "GitLab hostname")
bootstrapGitLabCmd.Flags().Var(&gitlabArgs.path, "path", "path relative to the repository root, when specified the cluster sync will be scoped to this path")
bootstrapGitLabCmd.Flags().BoolVar(&gitlabArgs.readWriteKey, "read-write-key", false, "if true, the deploy key is configured with read/write permissions")
bootstrapGitLabCmd.Flags().BoolVar(&gitlabArgs.reconcile, "reconcile", false, "if true, the configured options are also reconciled if the repository already exists")

bootstrapCmd.AddCommand(bootstrapGitLabCmd)
}
Expand Down Expand Up @@ -251,6 +253,9 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
if !gitlabArgs.private {
bootstrapOpts = append(bootstrapOpts, bootstrap.WithProviderRepositoryConfig("", "", "public"))
}
if gitlabArgs.reconcile {
bootstrapOpts = append(bootstrapOpts, bootstrap.WithReconcile())
}

// Setup bootstrapper with constructed configs
b, err := bootstrap.NewGitProviderBootstrapper(gitClient, providerClient, kubeClient, bootstrapOpts...)
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/flux_bootstrap_github.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ flux bootstrap github [flags]
--personal if true, the owner is assumed to be a GitHub user; otherwise an org
--private if true, the repository is setup or configured as private (default true)
--read-write-key if true, the deploy key is configured with read/write permissions
--reconcile if true, the configured options are also reconciled if the repository already exists
--repository string GitHub repository name
--team stringArray GitHub team to be given maintainer access
```
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/flux_bootstrap_gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ flux bootstrap gitlab [flags]
--personal if true, the owner is assumed to be a GitLab user; otherwise a group
--private if true, the repository is setup or configured as private (default true)
--read-write-key if true, the deploy key is configured with read/write permissions
--reconcile if true, the configured options are also reconciled if the repository already exists
--repository string GitLab repository name
--team stringArray GitLab teams to be given maintainer access
```
Expand Down
66 changes: 40 additions & 26 deletions internal/bootstrap/bootstrap_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type GitProviderBootstrapper struct {
defaultBranch string
visibility string

reconcile bool

teams map[string]string

readWriteKey bool
Expand Down Expand Up @@ -168,6 +170,16 @@ func (o sshHostnameOption) applyGitProvider(b *GitProviderBootstrapper) {
b.sshHostname = string(o)
}

func WithReconcile() GitProviderOption {
return reconcileOption(true)
}

type reconcileOption bool

func (o reconcileOption) applyGitProvider(b *GitProviderBootstrapper) {
b.reconcile = true
}

func (b *GitProviderBootstrapper) ReconcileSyncConfig(ctx context.Context, options sync.Options) error {
repo, err := b.getRepository(ctx)
if err != nil {
Expand Down Expand Up @@ -279,18 +291,20 @@ func (b *GitProviderBootstrapper) reconcileOrgRepository(ctx context.Context) (g
b.logger.Successf("repository %q created", repoRef.String())
}

// Set default branch before calling Reconcile due to bug described
// above.
repoInfo.DefaultBranch = repo.Get().DefaultBranch
var changed bool
if err = retry(1, 2*time.Second, func() (err error) {
repo, changed, err = b.provider.OrgRepositories().Reconcile(ctx, repoRef, repoInfo)
return
}); err != nil {
return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err)
}
if changed {
b.logger.Successf("repository %q reconciled", repoRef.String())
if b.reconcile {
// Set default branch before calling Reconcile due to bug described
// above.
repoInfo.DefaultBranch = repo.Get().DefaultBranch
if err = retry(1, 2*time.Second, func() (err error) {
repo, changed, err = b.provider.OrgRepositories().Reconcile(ctx, repoRef, repoInfo)
return
}); err != nil {
return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err)
}
if changed {
b.logger.Successf("repository %q reconciled", repoRef.String())
}
}

// Build the team access config
Expand Down Expand Up @@ -343,27 +357,27 @@ func (b *GitProviderBootstrapper) reconcileUserRepository(ctx context.Context) (
// of the available Reconcile methods, and setting e.g. the default
// branch correctly. Resort to Create with AutoInit until this has
// been resolved.
repo, err = b.provider.UserRepositories().Create(ctx, repoRef, repoInfo, &gitprovider.RepositoryCreateOptions{
AutoInit: gitprovider.BoolVar(true),
})
repo, err = b.provider.UserRepositories().Create(ctx, repoRef, repoInfo, &gitprovider.RepositoryCreateOptions{})
if err != nil {
return nil, fmt.Errorf("failed to create new Git repository %q: %w", repoRef.String(), err)
}
b.logger.Successf("repository %q created", repoRef.String())
}

// Set default branch before calling Reconcile due to bug described
// above.
repoInfo.DefaultBranch = repo.Get().DefaultBranch
var changed bool
if err = retry(1, 2*time.Second, func() (err error) {
repo, changed, err = b.provider.UserRepositories().Reconcile(ctx, repoRef, repoInfo)
return
}); err != nil {
return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err)
}
if changed {
b.logger.Successf("repository %q reconciled", repoRef.String())
if b.reconcile {
// Set default branch before calling Reconcile due to bug described
// above.
repoInfo.DefaultBranch = repo.Get().DefaultBranch
var changed bool
if err = retry(1, 2*time.Second, func() (err error) {
repo, changed, err = b.provider.UserRepositories().Reconcile(ctx, repoRef, repoInfo)
return
}); err != nil {
return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err)
}
if changed {
b.logger.Successf("repository %q reconciled", repoRef.String())
}
}

return repo, nil
Expand Down

0 comments on commit 7dcf884

Please sign in to comment.