Skip to content

Commit

Permalink
Use checkout location during content init
Browse files Browse the repository at this point in the history
  • Loading branch information
Furisto committed Mar 31, 2022
1 parent cbc136e commit e84d21d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 60 deletions.
19 changes: 19 additions & 0 deletions components/content-service/pkg/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,22 @@ func PlaceWorkspaceReadyFile(ctx context.Context, wspath string, initsrc csapi.W

return nil
}

func GetCheckoutLocationFromInitializer(init *csapi.WorkspaceInitializer) string {
switch {
case init.GetGit() != nil:
return init.GetGit().CheckoutLocation
case init.GetPrebuild() != nil && len(init.GetPrebuild().Git) > 0:
return init.GetPrebuild().Git[0].CheckoutLocation
case init.GetBackup() != nil:
return init.GetBackup().CheckoutLocation
case init.GetComposite() != nil:
for _, c := range init.GetComposite().Initializer {
loc := GetCheckoutLocationFromInitializer(c)
if loc != "" {
return loc
}
}
}
return ""
}
20 changes: 1 addition & 19 deletions components/ws-daemon/pkg/content/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (s *WorkspaceService) creator(req *api.InitWorkspaceRequest) session.Worksp
return func(ctx context.Context, location string) (res *session.Workspace, err error) {
return &session.Workspace{
Location: location,
CheckoutLocation: getCheckoutLocation(req),
CheckoutLocation: wsinit.GetCheckoutLocationFromInitializer(req.Initializer),
CreatedAt: time.Now(),
Owner: req.Metadata.Owner,
WorkspaceID: req.Metadata.MetaId,
Expand All @@ -273,24 +273,6 @@ func ServiceDirName(instanceID string) string {
return instanceID + "-daemon"
}

// getCheckoutLocation returns the first checkout location found of any Git initializer configured by this request
func getCheckoutLocation(req *api.InitWorkspaceRequest) string {
spec := req.Initializer.Spec
if ir, ok := spec.(*csapi.WorkspaceInitializer_Git); ok {
if ir.Git != nil {
return ir.Git.CheckoutLocation
}
}
if ir, ok := spec.(*csapi.WorkspaceInitializer_Prebuild); ok {
if ir.Prebuild != nil {
if len(ir.Prebuild.Git) > 0 {
return ir.Prebuild.Git[0].CheckoutLocation
}
}
}
return ""
}

// DisposeWorkspace cleans up a workspace, possibly after taking a final backup
func (s *WorkspaceService) DisposeWorkspace(ctx context.Context, req *api.DisposeWorkspaceRequest) (resp *api.DisposeWorkspaceResponse, err error) {
//nolint:ineffassign
Expand Down
2 changes: 1 addition & 1 deletion components/ws-daemon/pkg/internal/session/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (s *Workspace) UpdateGitStatus(ctx context.Context) (res *csapi.GitStatus,

loc = filepath.Join(loc, s.CheckoutLocation)
if !git.IsWorkingCopy(loc) {
log.WithField("loc", loc).WithFields(s.OWI()).Debug("did not find a Git working copy - not updating Git status")
log.WithField("loc", loc).WithField("checkout location", s.CheckoutLocation).WithFields(s.OWI()).Debug("did not find a Git working copy - not updating Git status")
return nil, nil
}

Expand Down
42 changes: 12 additions & 30 deletions components/ws-manager/pkg/manager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/gitpod-io/gitpod/common-go/kubernetes"
wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
"github.com/gitpod-io/gitpod/common-go/tracing"
csapi "github.com/gitpod-io/gitpod/content-service/api"
content "github.com/gitpod-io/gitpod/content-service/pkg/initializer"
regapi "github.com/gitpod-io/gitpod/registry-facade/api"
"github.com/gitpod-io/gitpod/ws-manager/api"
config "github.com/gitpod-io/gitpod/ws-manager/api/config"
Expand Down Expand Up @@ -575,9 +575,11 @@ func (m *Manager) createWorkspaceEnvironment(startContext *startWorkspaceContext
return filepath.Join("/workspace", strings.TrimPrefix(segment, "/workspace"))
}

repoRoot := content.GetCheckoutLocationFromInitializer(spec.Initializer)

// Envs that start with GITPOD_ are appended to the Terminal environments
result := []corev1.EnvVar{}
result = append(result, corev1.EnvVar{Name: "GITPOD_REPO_ROOT", Value: getWorkspaceRelativePath(startContext.CheckoutLocation)})
result = append(result, corev1.EnvVar{Name: "GITPOD_REPO_ROOT", Value: getWorkspaceRelativePath(repoRoot)})
result = append(result, corev1.EnvVar{Name: "GITPOD_CLI_APITOKEN", Value: startContext.CLIAPIKey})
result = append(result, corev1.EnvVar{Name: "GITPOD_WORKSPACE_ID", Value: startContext.Request.Metadata.MetaId})
result = append(result, corev1.EnvVar{Name: "GITPOD_INSTANCE_ID", Value: startContext.Request.Id})
Expand Down Expand Up @@ -710,25 +712,6 @@ func (m *Manager) createDefaultSecurityContext() (*corev1.SecurityContext, error
return res, nil
}

func getCheckoutLocationFromInitializer(init *csapi.WorkspaceInitializer) string {
switch {
case init.GetGit() != nil:
return init.GetGit().CheckoutLocation
case init.GetPrebuild() != nil && len(init.GetPrebuild().Git) > 0:
return init.GetPrebuild().Git[0].CheckoutLocation
case init.GetBackup() != nil:
return init.GetBackup().CheckoutLocation
case init.GetComposite() != nil:
for _, c := range init.GetComposite().Initializer {
loc := getCheckoutLocationFromInitializer(c)
if loc != "" {
return loc
}
}
}
return ""
}

func (m *Manager) newStartWorkspaceContext(ctx context.Context, req *api.StartWorkspaceRequest) (res *startWorkspaceContext, err error) {
// we deliberately do not shadow ctx here as we need the original context later to extract the TraceID
span, ctx := tracing.FromContext(ctx, "newStartWorkspaceContext")
Expand Down Expand Up @@ -769,15 +752,14 @@ func (m *Manager) newStartWorkspaceContext(ctx context.Context, req *api.StartWo
headlessLabel: fmt.Sprintf("%v", headless),
markerLabel: "true",
},
CLIAPIKey: cliAPIKey,
OwnerToken: ownerToken,
Request: req,
IDEPort: 23000,
SupervisorPort: 22999,
WorkspaceURL: workspaceURL,
TraceID: traceID,
Headless: headless,
CheckoutLocation: getCheckoutLocationFromInitializer(req.Spec.Initializer),
CLIAPIKey: cliAPIKey,
OwnerToken: ownerToken,
Request: req,
IDEPort: 23000,
SupervisorPort: 22999,
WorkspaceURL: workspaceURL,
TraceID: traceID,
Headless: headless,
}, nil
}

Expand Down
19 changes: 9 additions & 10 deletions components/ws-manager/pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ type Manager struct {
}

type startWorkspaceContext struct {
Request *api.StartWorkspaceRequest `json:"request"`
Labels map[string]string `json:"labels"`
CLIAPIKey string `json:"cliApiKey"`
OwnerToken string `json:"ownerToken"`
IDEPort int32 `json:"idePort"`
SupervisorPort int32 `json:"supervisorPort"`
WorkspaceURL string `json:"workspaceURL"`
TraceID string `json:"traceID"`
Headless bool `json:"headless"`
CheckoutLocation string `json:"checkoutLocation"`
Request *api.StartWorkspaceRequest `json:"request"`
Labels map[string]string `json:"labels"`
CLIAPIKey string `json:"cliApiKey"`
OwnerToken string `json:"ownerToken"`
IDEPort int32 `json:"idePort"`
SupervisorPort int32 `json:"supervisorPort"`
WorkspaceURL string `json:"workspaceURL"`
TraceID string `json:"traceID"`
Headless bool `json:"headless"`
}

const (
Expand Down

0 comments on commit e84d21d

Please sign in to comment.