Skip to content

Commit

Permalink
Merge pull request #248 from projectsyn/fix/do-not-adpot
Browse files Browse the repository at this point in the history
Stop adopting existing git repositories by default
  • Loading branch information
glrf authored Sep 28, 2022
2 parents b90a90a + a161eac commit f208f57
Show file tree
Hide file tree
Showing 3 changed files with 365 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/v1alpha1/gitrepo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
// GitPhase enum values
const (
Created GitPhase = "created"
Creating GitPhase = "creating"
Failed GitPhase = "failed"
PhaseUnknown GitPhase = ""
ArchivePolicy DeletionPolicy = "Archive"
Expand Down
34 changes: 28 additions & 6 deletions controllers/gitrepo/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/go-logr/logr"
synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1"

// Register Gitrepo implementation - DONOT REMOVE
Expand All @@ -14,6 +15,12 @@ import (
)

func Steps(obj pipeline.Object, data *pipeline.Context) pipeline.Result {
return steps(obj, data, manager.GetGitClient)
}

type gitClientFactory func(ctx context.Context, instance *synv1alpha1.GitRepoTemplate, namespace string, reqLogger logr.Logger, client client.Client) (manager.Repo, string, error)

func steps(obj pipeline.Object, data *pipeline.Context, getGitClient gitClientFactory) pipeline.Result {
instance, ok := obj.(*synv1alpha1.GitRepo)
if !ok {
return pipeline.Result{Err: fmt.Errorf("object '%s/%s' is not of kind GitRepository", obj.GetNamespace(), obj.GetName())}
Expand All @@ -29,7 +36,7 @@ func Steps(obj pipeline.Object, data *pipeline.Context) pipeline.Result {
return pipeline.Result{}
}

repo, hostKeys, err := manager.GetGitClient(data.Context, &instance.Spec.GitRepoTemplate, instance.GetNamespace(), data.Log, data.Client)
repo, hostKeys, err := getGitClient(data.Context, &instance.Spec.GitRepoTemplate, instance.GetNamespace(), data.Log, data.Client)
if err != nil {
return pipeline.Result{Err: fmt.Errorf("get Git client: %w", err)}
}
Expand All @@ -38,14 +45,31 @@ func Steps(obj pipeline.Object, data *pipeline.Context) pipeline.Result {

if !repoExists(repo) {
data.Log.Info("creating git repo", manager.SecretEndpointName, repo.FullURL())
instance.Status.URL = repo.FullURL().String()
phase := synv1alpha1.Creating
instance.Status.Phase = &phase
if err := data.Client.Status().Update(data.Context, instance); err != nil {
return pipeline.Result{Err: fmt.Errorf("could not set status while creating repository: %w", err)}
}
err := repo.Create()
if err != nil {
instance.Status.URL = "" // Revert status to reduce race condition likelihood
return pipeline.Result{Err: handleRepoError(data.Context, err, instance, data.Client)}

}
data.Log.Info("successfully created the repository")
}

if instance.Status.URL != repo.FullURL().String() {
var err error
if !data.Deleted {
phase := synv1alpha1.Failed
instance.Status.Phase = &phase
err = handleRepoError(data.Context, fmt.Errorf("Failed to adopt repository. Repository %q already exists and is not managed by %s ", repo.FullURL().String(), instance.Name), instance, data.Client)
}
return pipeline.Result{Err: err}
}

if data.Deleted {
err := repo.Remove()
if err != nil {
Expand All @@ -61,7 +85,7 @@ func Steps(obj pipeline.Object, data *pipeline.Context) pipeline.Result {

changed, err := repo.Update()
if err != nil {
return pipeline.Result{Err: fmt.Errorf("update repo: %w", err)}
return pipeline.Result{Err: handleRepoError(data.Context, fmt.Errorf("update repo: %w", err), instance, data.Client)}
}

if changed {
Expand All @@ -77,11 +101,9 @@ func Steps(obj pipeline.Object, data *pipeline.Context) pipeline.Result {
}

func repoExists(repo manager.Repo) bool {
if err := repo.Read(); err == nil {
return true
}
err := repo.Read()
return err == nil

return false
}

func handleRepoError(ctx context.Context, err error, instance *synv1alpha1.GitRepo, client client.Client) error {
Expand Down
Loading

0 comments on commit f208f57

Please sign in to comment.