Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrent map writes error #724

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 43 additions & 33 deletions pkg/dashboard/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,48 @@ func envs(w http.ResponseWriter, r *http.Request) {

envs := []*api.GitopsEnv{}
for _, env := range envsFromDB {
repo, err := gitRepoCache.InstanceForRead(env.InfraRepo)
if err != nil {
if strings.Contains(err.Error(), "repository not found") ||
strings.Contains(err.Error(), "repo name is mandatory") {
envs = append(envs, &api.GitopsEnv{
Name: env.Name,
})
continue
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}

var stackConfig *dx.StackConfig
if env.RepoPerEnv {
stackConfig, err = stackYaml(repo, "stack.yaml")
if err != nil && !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
gitRepoCache.PerformAction(env.InfraRepo, func(repo *git.Repository) {
stackConfig, err = stackYaml(repo, "stack.yaml")
})
if err != nil {
if strings.Contains(err.Error(), "repository not found") ||
strings.Contains(err.Error(), "repo name is mandatory") {
envs = append(envs, &api.GitopsEnv{
Name: env.Name,
})
continue
} else if !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
} else {
stackConfig, err = stackYaml(repo, filepath.Join(env.Name, "stack.yaml"))
if err != nil && !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from %s repo for env %s: %s", env.InfraRepo, env.Name, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
gitRepoCache.PerformAction(env.InfraRepo, func(repo *git.Repository) {
stackConfig, err = stackYaml(repo, filepath.Join(env.Name, "stack.yaml"))
})
if err != nil {
if strings.Contains(err.Error(), "repository not found") ||
strings.Contains(err.Error(), "repo name is mandatory") {
envs = append(envs, &api.GitopsEnv{
Name: env.Name,
})
continue
} else if !strings.Contains(err.Error(), "file not found") {
logrus.Errorf("cannot get stack yaml from %s repo for env %s: %s", env.InfraRepo, env.Name, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
} else {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
}

Expand Down Expand Up @@ -356,14 +369,11 @@ func deploymentTemplateForApp(w http.ResponseWriter, r *http.Request) {
installationToken, _, _ := tokenManager.Token()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)

repo, err := gitRepoCache.InstanceForRead(fmt.Sprintf("%s/%s", owner, repoName))
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

appChart, err := getChartForApp(repo, env, configName)
var appChart *dx.Chart
var err error
gitRepoCache.PerformAction(fmt.Sprintf("%s/%s", owner, repoName), func(repo *git.Repository) {
appChart, err = getChartForApp(repo, env, configName)
})
if err != nil {
logrus.Errorf("cannot get manifest chart: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
28 changes: 16 additions & 12 deletions pkg/dashboard/server/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,32 @@ func commits(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)

repo, err := gitRepoCache.InstanceForRead(repoName)
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

var err error
if branch == "" {
branch, err = helper.HeadBranch(repo)
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
branch, err = helper.HeadBranch(repo)
})
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
logrus.Errorf("cannot get head branch: %s", err)
return
}
}

hash := helper.BranchHeadHash(repo, branch)
var hash plumbing.Hash
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
hash = helper.BranchHeadHash(repo, branch)
})

if hashString != "head" {
hash = plumbing.NewHash(hashString)
}

commitWalker, err := repo.Log(&git.LogOptions{
From: hash,
var commitWalker object.CommitIter
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
commitWalker, err = repo.Log(&git.LogOptions{
From: hash,
})
})
if err != nil {
logrus.Errorf("cannot walk commits: %s", err)
Expand Down Expand Up @@ -98,7 +100,9 @@ func commits(w http.ResponseWriter, r *http.Request) {
return
}

commits, err = decorateCommitsWithGimletArtifacts(commits, dao, repo, owner, repoName)
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
commits, err = decorateCommitsWithGimletArtifacts(commits, dao, repo, owner, repoName)
})
if err != nil {
logrus.Warnf("cannot get deplyotargets: %s", err)
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/dashboard/server/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,12 @@ func updateGitopsCommitStatuses(
return err
}

repo, err := gitopsRepoCache.InstanceForRead(repoName)
if err != nil {
return err
}

var commitWalker object.CommitIter
hash := plumbing.NewHash(eventHash)
commitWalker, err := repo.Log(&git.LogOptions{
From: hash,
gitopsRepoCache.PerformAction(repoName, func(repo *git.Repository) {
commitWalker, err = repo.Log(&git.LogOptions{
From: hash,
})
})
if err != nil {
return err
Expand Down
71 changes: 29 additions & 42 deletions pkg/dashboard/server/gitContents.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/go-chi/chi"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
Expand All @@ -38,15 +39,11 @@ func branches(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)

repo, err := gitRepoCache.InstanceForRead(repoName)
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

var refIter storer.ReferenceIter
branches := []string{}
refIter, _ := repo.References()
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
refIter, _ = repo.References()
})
refIter.ForEach(func(r *plumbing.Reference) error {
if r.Name().IsRemote() {
branch := r.Name().Short()
Expand All @@ -68,44 +65,43 @@ func branches(w http.ResponseWriter, r *http.Request) {

func getMetas(w http.ResponseWriter, r *http.Request) {
owner := chi.URLParam(r, "owner")
repoName := chi.URLParam(r, "name")
repo := chi.URLParam(r, "name")

ctx := r.Context()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)
repoName := fmt.Sprintf("%s/%s", owner, repo)

repo, err := gitRepoCache.InstanceForRead(fmt.Sprintf("%s/%s", owner, repoName))
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

var hasGithubActionsConfig bool
var githubActionsShipper *string
var err error
githubActionsConfigPath := filepath.Join(".github", "workflows")
githubActionsShipperCommand := "gimlet-io/gimlet-artifact-shipper-action"
hasGithubActionsConfig, githubActionsShipper, err := hasCiConfigAndShipper(repo, githubActionsConfigPath, githubActionsShipperCommand)
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
hasGithubActionsConfig, githubActionsShipper, err = hasCiConfigAndShipper(repo, githubActionsConfigPath, githubActionsShipperCommand)
})
if err != nil {
logrus.Errorf("cannot determine ci status: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

var hasCircleCiConfig bool
var circleCiShipper *string
circleCiConfigPath := ".circleci"
circleCiShipperCommand := "gimlet/gimlet-artifact-create"
hasCircleCiConfig, circleCiShipper, err := hasCiConfigAndShipper(repo, circleCiConfigPath, circleCiShipperCommand)
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
hasCircleCiConfig, circleCiShipper, err = hasCiConfigAndShipper(repo, circleCiConfigPath, circleCiShipperCommand)
})
if err != nil {
logrus.Errorf("cannot determine ci status: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

branch, err := helper.HeadBranch(repo)
if err != nil {
logrus.Errorf("cannot get head branch: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

files, err := helper.RemoteFolderOnBranchWithoutCheckout(repo, branch, ".gimlet")
var files map[string]string
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
files, err = helper.RemoteFolderOnBranchWithoutCheckout(repo, "", ".gimlet")
})
if err != nil {
if !strings.Contains(err.Error(), "directory not found") {
logrus.Errorf("cannot list files in .gimlet/: %s", err)
Expand Down Expand Up @@ -319,26 +315,17 @@ type gitRepoMetas struct {
// envConfig fetches all environment configs from source control for a repo
func envConfigs(w http.ResponseWriter, r *http.Request) {
owner := chi.URLParam(r, "owner")
repoName := chi.URLParam(r, "name")
repo := chi.URLParam(r, "name")

ctx := r.Context()
gitRepoCache, _ := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)
repoName := fmt.Sprintf("%s/%s", owner, repo)

repo, err := gitRepoCache.InstanceForRead(fmt.Sprintf("%s/%s", owner, repoName))
if err != nil {
logrus.Errorf("cannot get repo: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

branch, err := helper.HeadBranch(repo)
if err != nil {
logrus.Errorf("cannot get head branch: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

files, err := helper.RemoteFolderOnBranchWithoutCheckout(repo, branch, ".gimlet")
var files map[string]string
var err error
gitRepoCache.PerformAction(repoName, func(repo *git.Repository) {
files, err = helper.RemoteFolderOnBranchWithoutCheckout(repo, "", ".gimlet")
})
if err != nil {
if strings.Contains(err.Error(), "directory not found") {
w.WriteHeader(http.StatusOK)
Expand Down
12 changes: 6 additions & 6 deletions pkg/dashboard/server/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/gimlet-io/gimlet-cli/pkg/git/genericScm"
"github.com/gimlet-io/gimlet-cli/pkg/git/nativeGit"
"github.com/gimlet-io/go-scm/scm"
"github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -142,12 +143,11 @@ func processStatusHook(
statusOnCommits[sha] = &c.Status
}

r, err := repoCache.InstanceForRead(repo)
if err != nil {
logrus.Errorf("Could get repo, %v", err)
return
}
decoratedCommits, err := decorateCommitsWithGimletArtifacts([]*Commit{{SHA: sha}}, dao, r, owner, name)
var decoratedCommits []*Commit
repoCache.PerformAction(repo, func(repo *git.Repository) {
decoratedCommits, err = decorateCommitsWithGimletArtifacts([]*Commit{{SHA: sha}}, dao, repo, owner, name)

})
if err != nil {
logrus.Warnf("cannot get deplyotargets: %s", err)
}
Expand Down
11 changes: 4 additions & 7 deletions pkg/dashboard/server/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,10 @@ func getStatus(w http.ResponseWriter, r *http.Request) {
return
}

repo, err := gitopsRepoCache.InstanceForReadWithHistory(repoName)
if err != nil {
logrus.Errorf("cannot get repocache: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
appReleases, err := gitops.Status(repo, app, env, repoPerEnv, perf)
var appReleases map[string]*dx.Release
gitopsRepoCache.PerformActionWithHistory(repoName, func(repo *git.Repository) {
appReleases, err = gitops.Status(repo, app, env, repoPerEnv, perf)
})
if err != nil {
logrus.Errorf("cannot get status: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
20 changes: 8 additions & 12 deletions pkg/dashboard/worker/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,10 @@ func processReleaseEvent(
}
artifact.Environments = append(artifact.Environments, manifests...)

repo, err := gitopsRepoCache.InstanceForRead(artifact.Version.RepositoryName)
if err != nil {
return deployResults, fmt.Errorf("cannot load repo %s", err.Error())
}

repoVars, err := loadVars(repo, ".gimlet/vars")
var repoVars map[string]string
gitopsRepoCache.PerformAction(artifact.Version.RepositoryName, func(repo *git.Repository) {
repoVars, err = loadVars(repo, ".gimlet/vars")
})
if err != nil {
return deployResults, fmt.Errorf("cannot load vars %s", err.Error())
}
Expand Down Expand Up @@ -549,12 +547,10 @@ func processArtifactEvent(
}
artifact.Environments = append(artifact.Environments, manifests...)

repo, err := gitopsRepoCache.InstanceForRead(artifact.Version.RepositoryName)
if err != nil {
return deployResults, fmt.Errorf("cannot load repo %s", err.Error())
}

repoVars, err := loadVars(repo, ".gimlet/vars")
var repoVars map[string]string
gitopsRepoCache.PerformAction(artifact.Version.RepositoryName, func(repo *git.Repository) {
repoVars, err = loadVars(repo, ".gimlet/vars")
})
if err != nil {
return deployResults, fmt.Errorf("cannot load vars %s", err.Error())
}
Expand Down
Loading