Skip to content

Commit

Permalink
feat: [CDE-142]: provider interface pass pointers. (harness#2232)
Browse files Browse the repository at this point in the history
* feat: [CDE-142]: provider interface pass pointers.
* feat: [CDE-142]: provider interface pass pointers.
* feat: [CDE-142]: provider interface pass pointers.
* feat: [CDE-142]: provider interface pass pointers.
* feat: [CDE-142]: provider interface pass pointers.
* feat: [CDE-142]: provider interface pass pointers.
  • Loading branch information
n00bitax authored and Harness committed Jul 16, 2024
1 parent b83348c commit 767bb71
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 153 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ ENV GITNESS_DATABASE_DATASOURCE /data/database.sqlite
ENV GITNESS_METRIC_ENABLED=true
ENV GITNESS_METRIC_ENDPOINT=https://stats.drone.ci/api/v1/gitness
ENV GITNESS_TOKEN_COOKIE_NAME=token
ENV GITNESS_GITSPACE_ROOT /data
ENV GITNESS_DOCKER_HOST unix:///var/run/docker.sock

COPY --from=builder /app/gitness /app/gitness
Expand Down
22 changes: 10 additions & 12 deletions app/gitspace/infrastructure/provisioner_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (i infraProvisioner) Provision(
// TODO: Update the infraProvisioned record
}

return &provisionedInfra, nil
return provisionedInfra, nil
}

func (i infraProvisioner) Stop(
Expand Down Expand Up @@ -133,11 +133,11 @@ func (i infraProvisioner) Stop(
return nil, fmt.Errorf("invalid provisioning params %+v: %w", infraProviderResource.Metadata, err)
}

var provisionedInfra infraprovider.Infrastructure
var provisionedInfra *infraprovider.Infrastructure
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew { //nolint:revive
// TODO: Fetch and check existing infraProvisioned record
} else {
provisionedInfra = infraprovider.Infrastructure{
provisionedInfra = &infraprovider.Infrastructure{
ResourceKey: gitspaceConfig.Identifier,
ProviderType: infraProviderEntity.Type,
Parameters: allParams,
Expand All @@ -153,7 +153,7 @@ func (i infraProvisioner) Stop(
// TODO: Update existing infraProvisioned record
}

return &stoppedInfra, err
return stoppedInfra, err
}

func (i infraProvisioner) Deprovision(
Expand Down Expand Up @@ -193,18 +193,16 @@ func (i infraProvisioner) Deprovision(
return nil, fmt.Errorf("invalid provisioning params %+v: %w", infraProviderResource.Metadata, err)
}

var provisionedInfra infraprovider.Infrastructure
var provisionedInfra *infraprovider.Infrastructure
if infraProvider.ProvisioningType() == enum.InfraProvisioningTypeNew { //nolint:revive
// TODO: Fetch and check existing infraProvisioned record
} else {
provisionedInfra = infraprovider.Infrastructure{
ResourceKey: gitspaceConfig.Identifier,
SpacePath: gitspaceConfig.SpacePath,
ProviderType: infraProviderEntity.Type,
Parameters: allParams,
provisionedInfra, err = infraProvider.Find(ctx, gitspaceConfig.SpacePath, gitspaceConfig.Identifier, allParams)
if err != nil {
return nil, fmt.Errorf("unable to find provisioned infra for gitspace %s: %w",
gitspaceConfig.Identifier, err)
}
}

destroyedInfra, err := infraProvider.Deprovision(ctx, provisionedInfra)
if err != nil {
return nil, fmt.Errorf("unable to stop provisioned infra %+v: %w", provisionedInfra, err)
Expand All @@ -214,7 +212,7 @@ func (i infraProvisioner) Deprovision(
// TODO: Update existing infraProvisioned record
}

return &destroyedInfra, err
return destroyedInfra, err
}

func (i infraProvisioner) Find(
Expand Down
28 changes: 14 additions & 14 deletions app/gitspace/orchestrator/container/embedded_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
"path/filepath"
"strings"

"github.com/harness/gitness/app/gitspace/logutil"
Expand Down Expand Up @@ -46,7 +45,6 @@ const (
containerStateRemoved = "removed"
templateCloneGit = "clone_git.sh"
templateSetupSSHServer = "setup_ssh_server.sh"
gitspacesDir = "gitspaces"
)

type Config struct {
Expand Down Expand Up @@ -159,6 +157,7 @@ func (e *EmbeddedDockerOrchestrator) StartGitspace(
dockerClient,
ideService,
logStreamInstance,
infra.Storage,
)
if startErr != nil {
return nil, fmt.Errorf("failed to start gitspace %s: %w", containerName, startErr)
Expand Down Expand Up @@ -194,6 +193,7 @@ func (e *EmbeddedDockerOrchestrator) startGitspace(
dockerClient *client.Client,
ideService IDE,
logStreamInstance *logutil.LogStreamInstance,
volumeName string,
) error {
var imageName = devcontainerConfig.Image
if imageName == "" {
Expand All @@ -205,7 +205,15 @@ func (e *EmbeddedDockerOrchestrator) startGitspace(
return err
}

err = e.createContainer(ctx, gitspaceConfig, dockerClient, imageName, containerName, ideService, logStreamInstance)
err = e.createContainer(
ctx,
dockerClient,
imageName,
containerName,
ideService,
logStreamInstance,
volumeName,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -411,12 +419,12 @@ func (e *EmbeddedDockerOrchestrator) executePostCreateCommand(

func (e *EmbeddedDockerOrchestrator) createContainer(
ctx context.Context,
gitspaceConfig *types.GitspaceConfig,
dockerClient *client.Client,
imageName string,
containerName string,
ideService IDE,
logStreamInstance *logutil.LogStreamInstance,
volumeName string,
) error {
portUsedByIDE := ideService.PortAndProtocol()

Expand All @@ -442,14 +450,6 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
commands := make(strslice.StrSlice, 0)
commands = append(commands, "infinity")

mountSource :=
filepath.Join(
e.config.RootSource,
gitspacesDir,
gitspaceConfig.SpacePath,
gitspaceConfig.Identifier,
)

loggingErr := logStreamInstance.Write("Creating container: " + containerName)
if loggingErr != nil {
return fmt.Errorf("logging error: %w", loggingErr)
Expand All @@ -464,8 +464,8 @@ func (e *EmbeddedDockerOrchestrator) createContainer(
PortBindings: portBindings,
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: mountSource,
Type: mount.TypeVolume,
Source: volumeName,
Target: e.config.WorkingDirectory,
},
},
Expand Down
28 changes: 1 addition & 27 deletions cli/operations/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,18 +405,10 @@ func ProvideIDEVSCodeWebConfig(config *types.Config) *container.VSCodeWebConfig
}

// ProvideGitspaceContainerOrchestratorConfig loads the Gitspace container orchestrator config from the main config.
func ProvideGitspaceContainerOrchestratorConfig(
config *types.Config,
dockerProviderConfig *infraprovider.DockerProviderConfig,
) *container.Config {
if config.Gitspace.RootSource == "" {
config.Gitspace.RootSource = dockerProviderConfig.MountSourceBasePath
}

func ProvideGitspaceContainerOrchestratorConfig(config *types.Config) *container.Config {
return &container.Config{
DefaultBaseImage: config.Gitspace.DefaultBaseImage,
WorkingDirectory: config.Gitspace.WorkingDirectory,
RootSource: config.Gitspace.RootSource,
}
}

Expand All @@ -428,21 +420,3 @@ func ProvideGitspaceEventConfig(config *types.Config) gitspaceevent.Config {
MaxRetries: config.Gitspace.Events.MaxRetries,
}
}

// ProvideDockerProviderConfig loads the Docker provider config from the main config.
func ProvideDockerProviderConfig(config *types.Config) (*infraprovider.DockerProviderConfig, error) {
if config.Gitspace.Root == "" {
var homedir string

homedir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("unable to determine home directory: %w", err)
}

config.Gitspace.Root = filepath.Join(homedir, gitnessHomeDir)
}

return &infraprovider.DockerProviderConfig{
MountSourceBasePath: config.Gitspace.Root,
}, nil
}
1 change: 0 additions & 1 deletion cmd/gitness/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ func initSystem(ctx context.Context, config *types.Config) (*cliserver.System, e
cliserver.ProvideGitspaceContainerOrchestratorConfig,
cliserver.ProvideGitspaceEventConfig,
logutil.WireSet,
cliserver.ProvideDockerProviderConfig,
)
return &cliserver.System{}, nil
}
8 changes: 2 additions & 6 deletions cmd/gitness/wire_gen.go

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

Loading

0 comments on commit 767bb71

Please sign in to comment.