From abab40c6f550c07ea070686a3bb16bae71de356b Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Wed, 3 Jul 2019 15:19:52 +0200 Subject: [PATCH] gateway/cmd: generate CacheGroup for user direct runs Since the user direct runs all belong to the same run group (the user id) all the user direct runs will share the same caches. To distinguish between the different caches we need to use something in addition to the user id. In this case we are usin the local repo uuid generated by the direct run start command. --- cmd/agola/cmd/directrunstart.go | 1 + internal/services/gateway/action/run.go | 9 ++++++ internal/services/gateway/action/user.go | 37 ++++++++++++++++-------- internal/services/gateway/api/user.go | 10 ++++++- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/cmd/agola/cmd/directrunstart.go b/cmd/agola/cmd/directrunstart.go index ae97a60..dc9e7c8 100644 --- a/cmd/agola/cmd/directrunstart.go +++ b/cmd/agola/cmd/directrunstart.go @@ -103,6 +103,7 @@ func directRunStart(cmd *cobra.Command, args []string) error { log.Infof("starting direct run") req := &api.UserCreateRunRequest{ + RepoUUID: repoUUID, RepoPath: repoPath, Branch: branch, CommitSHA: commitSHA, diff --git a/internal/services/gateway/action/run.go b/internal/services/gateway/action/run.go index 427957d..698fa75 100644 --- a/internal/services/gateway/action/run.go +++ b/internal/services/gateway/action/run.go @@ -318,6 +318,8 @@ type CreateRunRequest struct { // CompareLink is provided only when triggered by a webhook and contains the // commit compare link CompareLink string + + UserRunRepoUUID string } func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) error { @@ -428,6 +430,12 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e annotations[AnnotationPullRequestLink] = req.PullRequestLink } + // Since user belong to the same group (the user uuid) we needed another way to differentiate the cache. We'll use the user uuid + the user run repo uuid + var cacheGroup string + if req.RunType == types.RunTypeUser { + cacheGroup = req.User.ID + "-" + req.UserRunRepoUUID + } + data, filename, err := h.fetchConfigFiles(req.GitSource, req.RepoPath, req.CommitSHA) if err != nil { return util.NewErrInternal(errors.Errorf("failed to fetch config file: %w", err)) @@ -477,6 +485,7 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e Name: run.Name, StaticEnvironment: env, Annotations: annotations, + CacheGroup: cacheGroup, } if _, _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil { diff --git a/internal/services/gateway/action/user.go b/internal/services/gateway/action/user.go index ca40ecc..0a43498 100644 --- a/internal/services/gateway/action/user.go +++ b/internal/services/gateway/action/user.go @@ -806,7 +806,15 @@ func (h *ActionHandler) DeleteUserToken(ctx context.Context, userRef, tokenName return nil } -func (h *ActionHandler) UserCreateRun(ctx context.Context, repoPath, branch, commitSHA, message string) error { +type UserCreateRunRequest struct { + RepoUUID string + RepoPath string + Branch string + CommitSHA string + Message string +} + +func (h *ActionHandler) UserCreateRun(ctx context.Context, req *UserCreateRunRequest) error { curUserID := h.CurrentUserID(ctx) user, resp, err := h.configstoreClient.GetUser(ctx, curUserID) @@ -815,39 +823,44 @@ func (h *ActionHandler) UserCreateRun(ctx context.Context, repoPath, branch, com } // Verify that the repo is owned by the user - repoParts := strings.Split(repoPath, "/") + repoParts := strings.Split(req.RepoPath, "/") + if req.RepoUUID == "" { + return util.NewErrBadRequest(errors.Errorf("empty repo uuid")) + } if len(repoParts) != 2 { - return util.NewErrBadRequest(errors.Errorf("wrong repo path: %q", repoPath)) + return util.NewErrBadRequest(errors.Errorf("wrong repo path: %q", req.RepoPath)) } if repoParts[0] != user.ID { - return util.NewErrUnauthorized(errors.Errorf("repo %q not owned", repoPath)) + return util.NewErrUnauthorized(errors.Errorf("repo %q not owned", req.RepoPath)) } gitSource := agolagit.New(h.apiExposedURL + "/repos") - cloneURL := fmt.Sprintf("%s/%s.git", h.apiExposedURL+"/repos", repoPath) + cloneURL := fmt.Sprintf("%s/%s.git", h.apiExposedURL+"/repos", req.RepoPath) - req := &CreateRunRequest{ + creq := &CreateRunRequest{ RunType: types.RunTypeUser, RefType: types.RunRefTypeBranch, RunCreationTrigger: types.RunCreationTriggerTypeManual, Project: nil, User: user, - RepoPath: repoPath, + RepoPath: req.RepoPath, GitSource: gitSource, - CommitSHA: commitSHA, - Message: message, - Branch: branch, + CommitSHA: req.CommitSHA, + Message: req.Message, + Branch: req.Branch, Tag: "", PullRequestID: "", - Ref: gitSource.BranchRef(branch), + Ref: gitSource.BranchRef(req.Branch), CloneURL: cloneURL, CommitLink: "", BranchLink: "", TagLink: "", PullRequestLink: "", + + UserRunRepoUUID: req.RepoUUID, } - return h.CreateRuns(ctx, req) + return h.CreateRuns(ctx, creq) } diff --git a/internal/services/gateway/api/user.go b/internal/services/gateway/api/user.go index 5ae51f5..bbedb6b 100644 --- a/internal/services/gateway/api/user.go +++ b/internal/services/gateway/api/user.go @@ -605,6 +605,7 @@ func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest) } type UserCreateRunRequest struct { + RepoUUID string `json:"repo_uuid,omitempty"` RepoPath string `json:"repo_path,omitempty"` Branch string `json:"branch,omitempty"` CommitSHA string `json:"commit_sha,omitempty"` @@ -630,7 +631,14 @@ func (h *UserCreateRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) return } - err := h.ah.UserCreateRun(ctx, req.RepoPath, req.Branch, req.CommitSHA, req.Message) + creq := &action.UserCreateRunRequest{ + RepoUUID: req.RepoUUID, + RepoPath: req.RepoPath, + Branch: req.Branch, + CommitSHA: req.CommitSHA, + Message: req.Message, + } + err := h.ah.UserCreateRun(ctx, creq) if httpError(w, err) { h.log.Errorf("err: %+v", err) return