Skip to content

Commit

Permalink
feat: [CDE-127]: updated gitspace delete logic, fixed listing (harnes…
Browse files Browse the repository at this point in the history
…s#2183)

* fixed events list API query
* feat: [CDE-127]: fixed events list API
* fixed events list API query
* feat: [CDE-127]: updated gitspace delete logic, fixed listing
  • Loading branch information
n00bitax authored and Harness committed Jul 9, 2024
1 parent f784395 commit bf0890b
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 51 deletions.
25 changes: 9 additions & 16 deletions app/api/controller/gitspace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ func (c *Controller) Delete(
log.Err(err).Msg(gitspaceConfigNotFound + identifier)
return err
}
instance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
instance, _ := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
gitspaceConfig.GitspaceInstance = instance
if err != nil {
return fmt.Errorf("failed to find gitspace with config : %q %w", gitspaceConfig.Identifier, err)
}
stopErr := c.stopRunningGitspace(ctx, instance, gitspaceConfig)
if stopErr != nil {
return stopErr
if instance != nil {
if stopErr := c.stopRunningGitspace(ctx, gitspaceConfig); stopErr != nil {
return stopErr
}
}
gitspaceConfig.IsDeleted = true
if err = c.gitspaceConfigStore.Update(ctx, gitspaceConfig); err != nil {
Expand All @@ -68,16 +66,11 @@ func (c *Controller) Delete(

func (c *Controller) stopRunningGitspace(
ctx context.Context,
instance *types.GitspaceInstance,
config *types.GitspaceConfig) error {
if instance != nil &&
(instance.State == enum.GitspaceInstanceStateRunning ||
instance.State == enum.GitspaceInstanceStateUnknown) {
if instanceUpdated, err := c.orchestrator.DeleteGitspace(ctx, config); err != nil {
return err
} else if err = c.gitspaceInstanceStore.Update(ctx, instanceUpdated); err != nil {
return err
}
if instanceUpdated, err := c.orchestrator.DeleteGitspace(ctx, config); err != nil {
return err
} else if err = c.gitspaceInstanceStore.Update(ctx, instanceUpdated); err != nil {
return err
}
return nil
}
5 changes: 1 addition & 4 deletions app/api/controller/gitspace/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func (c *Controller) Find(
}
gitspaceConfig.SpacePath = space.Path
gitspaceConfig.InfraProviderResourceIdentifier = infraProviderResource.Identifier
instance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
if err != nil {
return nil, err
}
instance, _ := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
if instance != nil {
gitspaceConfig.GitspaceInstance = instance
gitspaceStateType, err := enum.GetGitspaceStateFromInstance(instance.State)
Expand Down
5 changes: 5 additions & 0 deletions app/api/controller/infraprovider/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@ func (c *Controller) sanitizeCreateInput(in *CreateInput) error {
if err := check.Identifier(in.Identifier); err != nil {
return err
}
for _, resource := range in.Resources {
if err := check.Identifier(resource.Identifier); err != nil {
return err
}
}
return nil
}
1 change: 1 addition & 0 deletions app/api/controller/infraprovider/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (c *Controller) Find(
if err != nil {
return nil, fmt.Errorf("failed to find infraprovider resources: %w", err)
}
infraProviderConfig.SpacePath = space.Path
infraProviderConfig.Resources = resources
return infraProviderConfig, nil
}
3 changes: 2 additions & 1 deletion app/api/openapi/gitspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type gitspacesListRequest struct {
}

type gitspaceEventsListRequest struct {
Ref string `path:"gitspace_identifier"`
paginationRequest
}

Expand Down Expand Up @@ -118,7 +119,7 @@ func gitspaceOperations(reflector *openapi3.Reflector) {
opEventList.WithTags("gitspaces")
opEventList.WithSummary("List gitspace events")
opEventList.WithMapOfAnything(map[string]interface{}{"operationId": "listGitspaceEvents"})
_ = reflector.SetRequest(&opList, new(gitspaceEventsListRequest), http.MethodGet)
_ = reflector.SetRequest(&opEventList, new(gitspaceEventsListRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opEventList, new([]*types.GitspaceEventResponse), http.StatusOK)
_ = reflector.SetJSONResponse(&opEventList, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opEventList, new(usererror.Error), http.StatusInternalServerError)
Expand Down
7 changes: 5 additions & 2 deletions app/gitspace/orchestrator/container/embedded_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package container
import (
"context"
"fmt"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -371,7 +372,7 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
gitspaceConfig.SpacePath,
gitspaceConfig.Identifier,
)
err := os.MkdirAll(bindMountSourcePath, 0600)
err := os.MkdirAll(bindMountSourcePath, os.ModePerm)
if err != nil {
return fmt.Errorf(
"could not create bind mount source path %s: %w", bindMountSourcePath, err)
Expand Down Expand Up @@ -419,7 +420,9 @@ func (e *EmbeddedDockerOrchestrator) pullImage(
if err != nil {
return fmt.Errorf("could not pull image %s: %w", imageName, err)
}

// TODO: This is required to ensure the execution waits till the image is downloaded.
// Will be removed once logs PR is merged.
io.Copy(io.Discard, resp) // nolint:errcheck
return nil
}

Expand Down
32 changes: 12 additions & 20 deletions app/gitspace/orchestrator/orchestrator_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,14 @@ func (o orchestrator) DeleteGitspace(
ctx context.Context,
gitspaceConfig *types.GitspaceConfig,
) (*types.GitspaceInstance, error) {
var updatedGitspaceInstance *types.GitspaceInstance

gitspaceInstance := gitspaceConfig.GitspaceInstance

infraProviderResource, err := o.infraProviderResourceStore.Find(ctx, gitspaceConfig.InfraProviderResourceID)
if err != nil {
return nil, fmt.Errorf(
"cannot get the infraProviderResource with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
if gitspaceInstance.State == enum.GitspaceInstanceStateRunning ||
gitspaceInstance.State == enum.GitspaceInstanceStateUnknown {
infraProviderResource, err := o.infraProviderResourceStore.Find(ctx, gitspaceConfig.InfraProviderResourceID)
if err != nil {
return nil, fmt.Errorf(
"cannot get the infraProviderResource with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}

infra, err := o.infraProvisioner.Find(ctx, infraProviderResource, gitspaceConfig)
if err != nil {
return nil, fmt.Errorf("cannot find the provisioned infra: %w", err)
Expand All @@ -180,16 +176,12 @@ func (o orchestrator) DeleteGitspace(
if err != nil {
return nil, fmt.Errorf("error stopping the Gitspace container: %w", err)
}

_, err = o.infraProvisioner.Unprovision(ctx, infraProviderResource, gitspaceConfig)
if err != nil {
return nil, fmt.Errorf(
"cannot stop provisioned infrastructure with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}

gitspaceInstance.State = enum.GitspaceInstanceStateDeleted
updatedGitspaceInstance = gitspaceInstance
}

return updatedGitspaceInstance, nil
_, err = o.infraProvisioner.Unprovision(ctx, infraProviderResource, gitspaceConfig)
if err != nil {
return nil, fmt.Errorf(
"cannot stop provisioned infrastructure with ID %d: %w", gitspaceConfig.InfraProviderResourceID, err)
}
gitspaceInstance.State = enum.GitspaceInstanceStateDeleted
return gitspaceInstance, nil
}
2 changes: 1 addition & 1 deletion app/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type APIHandler interface {
var (
// terminatedPathPrefixesAPI is the list of prefixes that will require resolving terminated paths.
terminatedPathPrefixesAPI = []string{"/v1/spaces/", "/v1/repos/",
"/v1/secrets/", "/v1/connectors", "/v1/templates/step", "/v1/templates/stage", "/v1/gitspaces"}
"/v1/secrets/", "/v1/connectors", "/v1/templates/step", "/v1/templates/stage", "/v1/gitspaces", "/v1/infraproviders"}
)

// NewAPIHandler returns a new APIHandler.
Expand Down
4 changes: 4 additions & 0 deletions app/services/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package services

import (
"github.com/harness/gitness/app/services/cleanup"
"github.com/harness/gitness/app/services/gitspaceevent"
"github.com/harness/gitness/app/services/keywordsearch"
"github.com/harness/gitness/app/services/metric"
"github.com/harness/gitness/app/services/notification"
Expand Down Expand Up @@ -43,6 +44,7 @@ type Services struct {
Cleanup *cleanup.Service
Notification *notification.Service
Keywordsearch *keywordsearch.Service
GitspaceEvent *gitspaceevent.Service
}

func ProvideServices(
Expand All @@ -56,6 +58,7 @@ func ProvideServices(
cleanupSvc *cleanup.Service,
notificationSvc *notification.Service,
keywordsearchSvc *keywordsearch.Service,
gitspaceEventSvc *gitspaceevent.Service,
) Services {
return Services{
Webhook: webhooksSvc,
Expand All @@ -68,5 +71,6 @@ func ProvideServices(
Cleanup: cleanupSvc,
Notification: notificationSvc,
Keywordsearch: keywordsearchSvc,
GitspaceEvent: gitspaceEventSvc,
}
}
8 changes: 4 additions & 4 deletions app/store/database/gitspace_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (g gitspaceEventStore) FindLatestByTypeAndGitspaceConfigID(
db := dbtx.GetAccessor(ctx, g.db)
gitspaceEventEntity := new(gitspaceEvent)
if err = db.GetContext(ctx, gitspaceEventEntity, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspaceEvent")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace event")
}
return g.mapGitspaceEvent(gitspaceEventEntity), nil
}
Expand Down Expand Up @@ -128,9 +128,9 @@ func (g gitspaceEventStore) List(

db := dbtx.GetAccessor(ctx, g.db)

gitspaceEventEntities := make([]*gitspaceEvent, 0)
if err = db.SelectContext(ctx, gitspaceEventEntities, sql, args...); err != nil {
return nil, 0, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspaceEvent")
var gitspaceEventEntities []*gitspaceEvent
if err = db.SelectContext(ctx, &gitspaceEventEntities, sql, args...); err != nil {
return nil, 0, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace event")
}

countStmt := database.Builder.
Expand Down
2 changes: 1 addition & 1 deletion app/store/database/gitspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (g gitspaceInstanceStore) FindAllLatestByGitspaceConfigID(
Where(whereClause)

// Use the base select query in a common table expression (CTE)
stmt := squirrel.Select(gitspaceConfigSelectColumns).
stmt := squirrel.Select(gitspaceInstanceSelectColumns).
FromSelect(baseSelect, "RankedRows").
Where("rn = 1")

Expand Down
3 changes: 3 additions & 0 deletions cmd/gitness/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package main

import (
"context"
"github.com/harness/gitness/app/services/gitspaceevent"

checkcontroller "github.com/harness/gitness/app/api/controller/check"
"github.com/harness/gitness/app/api/controller/connector"
Expand Down Expand Up @@ -197,6 +198,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
reposervice.WireSet,
cliserver.ProvideCodeOwnerConfig,
codeowners.WireSet,
gitspaceevent.WireSet,
cliserver.ProvideKeywordSearchConfig,
keywordsearch.WireSet,
controllerkeywordsearch.WireSet,
Expand All @@ -218,6 +220,7 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
cliserver.ProvideIDEVSCodeWebConfig,
cliserver.ProvideDockerConfig,
cliserver.ProvideGitspaceContainerOrchestratorConfig,
cliserver.ProvideGitspaceEventConfig,
)
return &cliserver.System{}, nil
}
12 changes: 11 additions & 1 deletion cmd/gitness/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion types/enum/gitspace_state_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ func (GitspaceStateType) Enum() []interface{} {
}

var gitspaceStateTypes = []GitspaceStateType{
GitspaceStateRunning, GitspaceStateStopped, GitspaceStateError, GitspaceStateUninitialized,
GitspaceStateRunning,
GitspaceStateStopped,
GitspaceStateError,
GitspaceStateUninitialized,
GitspaceStateStarting,
GitspaceStateStopping,
}

const (
GitspaceStateRunning GitspaceStateType = "running"
GitspaceStateStopped GitspaceStateType = "stopped"
GitspaceStateStarting GitspaceStateType = "starting"
GitspaceStateStopping GitspaceStateType = "stopping"
GitspaceStateError GitspaceStateType = "error"
GitspaceStateUninitialized GitspaceStateType = "uninitialized"
)
Expand Down

0 comments on commit bf0890b

Please sign in to comment.