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.
This commit is contained in:
Simone Gotti 2019-07-03 15:19:52 +02:00
parent 87a472aaaf
commit abab40c6f5
4 changed files with 44 additions and 13 deletions

View File

@ -103,6 +103,7 @@ func directRunStart(cmd *cobra.Command, args []string) error {
log.Infof("starting direct run") log.Infof("starting direct run")
req := &api.UserCreateRunRequest{ req := &api.UserCreateRunRequest{
RepoUUID: repoUUID,
RepoPath: repoPath, RepoPath: repoPath,
Branch: branch, Branch: branch,
CommitSHA: commitSHA, CommitSHA: commitSHA,

View File

@ -318,6 +318,8 @@ type CreateRunRequest struct {
// CompareLink is provided only when triggered by a webhook and contains the // CompareLink is provided only when triggered by a webhook and contains the
// commit compare link // commit compare link
CompareLink string CompareLink string
UserRunRepoUUID string
} }
func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) error { 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 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) data, filename, err := h.fetchConfigFiles(req.GitSource, req.RepoPath, req.CommitSHA)
if err != nil { if err != nil {
return util.NewErrInternal(errors.Errorf("failed to fetch config file: %w", err)) 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, Name: run.Name,
StaticEnvironment: env, StaticEnvironment: env,
Annotations: annotations, Annotations: annotations,
CacheGroup: cacheGroup,
} }
if _, _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil { if _, _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil {

View File

@ -806,7 +806,15 @@ func (h *ActionHandler) DeleteUserToken(ctx context.Context, userRef, tokenName
return nil 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) curUserID := h.CurrentUserID(ctx)
user, resp, err := h.configstoreClient.GetUser(ctx, curUserID) 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 // 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 { 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 { 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") 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, RunType: types.RunTypeUser,
RefType: types.RunRefTypeBranch, RefType: types.RunRefTypeBranch,
RunCreationTrigger: types.RunCreationTriggerTypeManual, RunCreationTrigger: types.RunCreationTriggerTypeManual,
Project: nil, Project: nil,
User: user, User: user,
RepoPath: repoPath, RepoPath: req.RepoPath,
GitSource: gitSource, GitSource: gitSource,
CommitSHA: commitSHA, CommitSHA: req.CommitSHA,
Message: message, Message: req.Message,
Branch: branch, Branch: req.Branch,
Tag: "", Tag: "",
PullRequestID: "", PullRequestID: "",
Ref: gitSource.BranchRef(branch), Ref: gitSource.BranchRef(req.Branch),
CloneURL: cloneURL, CloneURL: cloneURL,
CommitLink: "", CommitLink: "",
BranchLink: "", BranchLink: "",
TagLink: "", TagLink: "",
PullRequestLink: "", PullRequestLink: "",
UserRunRepoUUID: req.RepoUUID,
} }
return h.CreateRuns(ctx, req) return h.CreateRuns(ctx, creq)
} }

View File

@ -605,6 +605,7 @@ func (h *LoginUserHandler) loginUser(ctx context.Context, req *LoginUserRequest)
} }
type UserCreateRunRequest struct { type UserCreateRunRequest struct {
RepoUUID string `json:"repo_uuid,omitempty"`
RepoPath string `json:"repo_path,omitempty"` RepoPath string `json:"repo_path,omitempty"`
Branch string `json:"branch,omitempty"` Branch string `json:"branch,omitempty"`
CommitSHA string `json:"commit_sha,omitempty"` CommitSHA string `json:"commit_sha,omitempty"`
@ -630,7 +631,14 @@ func (h *UserCreateRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
return 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) { if httpError(w, err) {
h.log.Errorf("err: %+v", err) h.log.Errorf("err: %+v", err)
return return